Revision: 1204
Author: dhanji
Date: Sat Aug 14 22:25:02 2010
Log: Fixes many persist tests to use Junit assertXXX rather than JDK assert
statements. Also cleans up some of the persist javadoc.
http://code.google.com/p/google-guice/source/detail?r=1204
Modified:
/trunk/extensions/persist/src/com/google/inject/persist/PersistModule.java
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceFilter.java
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceService.java
/trunk/extensions/persist/src/com/google/inject/persist/UnitOfWork.java
/trunk/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java
/trunk/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java
/trunk/extensions/persist/src/com/google/inject/persist/finder/NumResults.java
/trunk/extensions/persist/test/com/google/inject/persist/EdslTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ClassLevelManagedLocalTransactionsTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerPerRequestProvisionTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerProvisionTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JoiningLocalTransactionsTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JpaWorkManagerTest.java
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ManualLocalTransactionsWithCustomMatcherTest.java
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/PersistModule.java
Sat Jul 3 08:51:31 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/PersistModule.java
Sat Aug 14 22:25:02 2010
@@ -4,14 +4,13 @@
import com.google.inject.BindingAnnotation;
import com.google.inject.internal.util.Preconditions;
import com.google.inject.persist.jpa.InternalJpaModule;
-import com.google.inject.servlet.ServletModule;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Properties;
/**
- * Install this module to add guice-persist library support for JPA
persistence providers. This
- * module *MUST* be installed before any filters are registered
+ * Install this module to add guice-persist library support for JPA
persistence
+ * providers.
*
* @author [email protected] (Dhanji R. Prasanna)
*/
@@ -31,16 +30,6 @@
"Must specify the name of a JPA unit in the PersistModule.");
install(new InternalJpaModule(unitOfWork, jpaUnit, properties));
-
- // Servlet functionality requested.
- if (UnitOfWork.REQUEST.equals(unitOfWork)) {
- install(new ServletModule() {
- @Override
- protected void configureServlets() {
- filter("/*").through(PersistenceFilter.class);
- }
- });
- }
}
protected void configurePersistence() {
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceFilter.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceFilter.java
Sat Aug 14 22:25:02 2010
@@ -26,40 +26,43 @@
import javax.servlet.ServletResponse;
/**
- * Apply this filter to enable the HTTP Request unit of work and to have
Guice Persist manage the
- * lifecycle of all the active (module installed) {...@code
PersistenceService} instances. The filter
- * automatically starts and stops all registered {...@link
PersistenceService} instances upon {...@link
+ * Apply this filter to enable the HTTP Request unit of work and to have
+ * guice-persist manage the lifecycle of all active (module-installed)
+ * {...@link PersistenceService} instances. The filter automatically starts
and
+ * stops all registered {...@link PersistenceService} instances upon {...@link
* javax.servlet.Filter#init(javax.servlet.FilterConfig)} and {...@link
- * javax.servlet.Filter#destroy()}. To disable the managing of
PersistenceService instances,
- * override {...@link javax.servlet.Filter#init(javax.servlet.FilterConfig)}
and {...@link
* javax.servlet.Filter#destroy()}.
+ *
* <p> To be able to use {...@link
com.google.inject.persist.UnitOfWork#REQUEST},
- * register this filter <b>once</b> in Guice's {...@code ServletModule}. It
is important that you
- * register this filter before any other framework filter.
+ * register this filter <b>once</b> in your Guice {...@code ServletModule}.
It is
+ * important that you register this filter before any other filter.
+ *
+ * <p>
* Example configuration:
- * <pre>
+ * <pre>{...@code
* public class MyModule extends ServletModule {
- * {...@literal @}Override
+ * @literal @Override
* public void configureServlets() {
- * <b>filter("/*").through(PersistenceFilter.class);</b>
+ * filter("/*").through(PersistenceFilter.class);
*
* serve("/index.html").with(MyHtmlServlet.class);
- * //...
+ * // Etc.
* }
* }
- *
- * </pre>
+ * }</pre>
* <p>
- * This filter will make sure to initialize and shutdown the underlying
persistence engine on
- * Filter init() and destroy() respectively
- * <p> Even though all mutable state is package local, this Filter is
thread safe. This
- * allows people to create injectors concurrently and deploy multiple
guice Persist applications
- * within the same VM / web container.
+ * This filter will make sure to initialize and shutdown the underlying
+ * persistence engine on filter {...@code init()} and {...@code destroy()}
+ * respectively.
+ * <p>
+ * Even though all mutable state is package local, this filter is thread
safe.
+ * This allows you to create injectors concurrently and deploy multiple
+ * guice-persist applications within the same JVM or servlet container.
*
* @author Dhanji R. Prasanna ([email protected])
*/
@Singleton
-class PersistenceFilter implements Filter {
+public final class PersistenceFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceService.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/PersistenceService.java
Sat Aug 14 22:25:02 2010
@@ -16,42 +16,33 @@
package com.google.inject.persist;
-import com.google.inject.persist.finder.Finder;
-import java.lang.reflect.Method;
-
/**
- * <p> This is the core guice-persist artifact. It providers factories for
generating guice modules
- * for your persistence configuration. It also must be injected into your
code later as a service
- * abstraction for starting the underlying persistence engine (Hibernate
or JPA). <p>
- * Implementations of this type should make sure {...@link #start()} and
{...@link #shutdown()} are thread
- * safe.
+ * This is the core guice-persist artifact. It providers factories for
+ * generating guice modules for your persistence configuration. It also
must
+ * be injected into your code later as a service abstraction for starting
the
+ * underlying persistence engine (Hibernate or JPA).
+ * <p>
+ * Implementations of this type should make sure {...@link #start()} and
+ * {...@link #shutdown()} are threadsafe.
*
* @author Dhanji R. Prasanna ([email protected])
*/
public abstract class PersistenceService {
/**
- * Starts the underlying persistence engine and makes guice-persist
ready for use. For instance,
- * with hibernate, it creates a SessionFactory and may open connection
pools. This method *must*
- * be called by your code prior to using any guice-persist or hibernate
artifacts. If already
- * started, calling this method does nothing, if already stopped, it
also does nothing.
+ * Starts the underlying persistence engine and makes guice-persist
ready for
+ * use. For instance, with hibernate, it creates a SessionFactory and may
+ * open connection pools. This method must be called by your code prior
to
+ * using any guice-persist or hibernate artifacts. If already started,
+ * calling this method does nothing, if already stopped, it also does
+ * nothing.
*/
public abstract void start();
/**
- * Stops the underlying persistence engine. For instance, with
Hibernate, it closes the
- * SessionFactory. If already stopped, calling this method does nothing.
If not yet started, it
- * also does nothing.
+ * Stops the underlying persistence engine. For instance, with JPA, it
+ * closes the {...@code EntityManagerFactory}. If already stopped, calling
this
+ * method does nothing. If not yet started, it also does nothing.
*/
public abstract void shutdown();
-
- /**
- * Returns true if {...@code method} is a dynamic finder.
- *
- * @param method A method you suspect is a Dynamic Finder.
- * @return Returns true if the method is annotated with {...@code @Finder}
- */
- public static boolean isDynamicFinder(Method method) {
- return method.isAnnotationPresent(Finder.class);
- }
-}
+}
=======================================
--- /trunk/extensions/persist/src/com/google/inject/persist/UnitOfWork.java
Tue May 25 15:48:47 2010
+++ /trunk/extensions/persist/src/com/google/inject/persist/UnitOfWork.java
Sat Aug 14 22:25:02 2010
@@ -17,25 +17,27 @@
package com.google.inject.persist;
/**
- * Enumerates all the supported units-of-work (i.e. atomic lifespan of a
persistence session).
+ * Enumerates all the supported units-of-work (i.e. atomic lifespan of a
+ * persistence session).
*
* @author Dhanji R. Prasanna ([email protected])
*/
public enum UnitOfWork {
/**
- * Logical arbitrary unit of work that is managed manually and generally
specific to a running
- * thread.
+ * Logical arbitrary unit of work that is managed manually and generally
+ * specific to a running thread.
*/
CUSTOM,
/**
- * Logical unit of work (ObjectContainer or JPA EntityManager) that
spans an HTTP request.
+ * Logical unit of work (ObjectContainer or JPA EntityManager) that spans
+ * an HTTP request.
*/
REQUEST,
/**
- * Logical unit of work (Session, ObjectContainer or JPA EntityManager)
that spans a transaction
- * demarcated with {...@code @Transactional}.
+ * Logical unit of work (Session, ObjectContainer or JPA EntityManager)
that
+ * spans a transaction demarcated with {...@code @Transactional}.
*/
TRANSACTION
}
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java
Sat Aug 14 22:25:02 2010
@@ -2,8 +2,22 @@
package com.google.inject.persist.finder;
+import java.lang.reflect.Method;
+
/**
+ * Utility that helps you discover metadata about dynamic finder methods.
+ *
* @author [email protected] (Dhanji R. Prasanna)
*/
public final class DynamicFinder {
-}
+
+ /**
+ * Tests if {...@code method} is a dynamic finder method.
+ *
+ * @param method a method you want to test as a dynamic finder
+ * @return {...@code true} if the method is annotated {...@code @Finder}
+ */
+ public static boolean isFinder(Method method) {
+ return method.isAnnotationPresent(Finder.class);
+ }
+}
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java
Sat Aug 14 22:25:02 2010
@@ -22,14 +22,11 @@
import java.lang.annotation.Target;
/**
- * <p>
- * Annotate any dynamic finder method's integer argument with this to pass
in the first-result
- * index. Used for paging result lists.
- * </p>
+ * Annotate any dynamic finder method's integer argument with this to pass
in
+ * the index of the first result in the result set you are interested in.
+ * Useful for paging result sets. Complemented by {...@link NumResults}.
*
* @author Dhanji R. Prasanna ([email protected])
- * @since 1.0
- * @see NumResults
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
=======================================
---
/trunk/extensions/persist/src/com/google/inject/persist/finder/NumResults.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/src/com/google/inject/persist/finder/NumResults.java
Sat Aug 14 22:25:02 2010
@@ -22,9 +22,9 @@
import java.lang.annotation.Target;
/**
- * Annotate any dynamic finder method's integer argument with this to pass
in the maximum
- * size of returned results. Used for paging result lists.
- * Complement of {...@linkplain com.google.inject.persist.finder.FirstResult}
+ * Annotate any dynamic finder method's integer argument with this to pass
in
+ * the maximum size of returned result window. Usefule for paging result
sets.
+ * Complement of {...@link FirstResult}.
*
* @author Dhanji R. Prasanna ([email protected])
*/
=======================================
--- /trunk/extensions/persist/test/com/google/inject/persist/EdslTest.java
Tue May 25 15:48:47 2010
+++ /trunk/extensions/persist/test/com/google/inject/persist/EdslTest.java
Sat Aug 14 22:25:02 2010
@@ -17,7 +17,7 @@
workAcross(UnitOfWork.REQUEST).usingJpa("myunit");
}
- }).stage(Stage.TOOL)
+ }).stage(Stage.PRODUCTION)
.requireExplicitBindings()
.build();
}
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ClassLevelManagedLocalTransactionsTest.java
Mon Jul 5 03:22:06 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ClassLevelManagedLocalTransactionsTest.java
Sat Aug 14 22:25:02 2010
@@ -68,8 +68,8 @@
injector.getInstance(TransactionalObject.class).runOperationInTxn();
EntityManager session = injector.getInstance(EntityManager.class);
- assert !session.getTransaction().isActive() :
- "EntityManager was not closed by transactional service";
+ assertFalse("EntityManager was not closed by transactional service",
+ session.getTransaction().isActive());
//test that the data has been stored
session.getTransaction().begin();
@@ -137,8 +137,8 @@
}
EntityManager session = injector.getInstance(EntityManager.class);
- assert !session.getTransaction().isActive() :
- "EntityManager was not closed by transactional service (rollback
didnt happen?)";
+ assertFalse("EntityManager was not closed by transactional service
(rollback didnt happen?)",
+ session.getTransaction().isActive());
//test that the data has been stored
session.getTransaction().begin();
@@ -147,7 +147,8 @@
session.getTransaction().commit();
- assert result.isEmpty() : "a result was returned! rollback sure didnt
happen!!!";
+ assertTrue("a result was returned! rollback sure didnt happen!!!",
+ result.isEmpty());
}
@Transactional
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerPerRequestProvisionTest.java
Tue May 25 15:48:47 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerPerRequestProvisionTest.java
Sat Aug 14 22:25:02 2010
@@ -109,11 +109,11 @@
@Transactional
public <T> void persist(T t) {
- assert em.isOpen() : "em is not open!";
- assert em.getTransaction().isActive() : "no active txn!";
+ assertTrue("em is not open!", em.isOpen());
+ assertTrue("no active txn!", em.getTransaction().isActive());
em.persist(t);
- assert em.contains(t) : "Persisting object failed";
+ assertTrue("Persisting object failed", em.contains(t));
}
@Transactional
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerProvisionTest.java
Tue May 25 15:48:47 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/EntityManagerProvisionTest.java
Sat Aug 14 22:25:02 2010
@@ -101,11 +101,11 @@
@Transactional
public <T> void persist(T t) {
- assert em.isOpen() : "em is not open!";
- assert em.getTransaction().isActive() : "no active txn!";
+ assertTrue("em is not open!", em.isOpen());
+ assertTrue("no active txn!", em.getTransaction().isActive());
em.persist(t);
- assert em.contains(t) : "Persisting object failed";
+ assertTrue("Persisting object failed", em.contains(t));
}
@Transactional
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JoiningLocalTransactionsTest.java
Tue May 25 15:48:47 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JoiningLocalTransactionsTest.java
Sat Aug 14 22:25:02 2010
@@ -66,17 +66,18 @@
.runOperationInTxn();
EntityManager em = injector.getInstance(EntityManager.class);
- assert !em.getTransaction().isActive() : "txn was not closed by
transactional service";
+ assertFalse("txn was not closed by transactional service",
+ em.getTransaction().isActive());
//test that the data has been stored
Object result = em.createQuery("from JpaTestEntity where text = :text")
.setParameter("text", UNIQUE_TEXT).getSingleResult();
injector.getInstance(WorkManager.class).end();
- assert result instanceof JpaTestEntity : "odd result returned fatal";
-
- assert UNIQUE_TEXT.equals(((JpaTestEntity) result).getText()) :
- "queried entity did not match--did automatic txn fail?";
+ assertTrue("odd result returned fatal", result instanceof
JpaTestEntity);
+
+ assertEquals("queried entity did not match--did automatic txn fail?",
UNIQUE_TEXT,
+ ((JpaTestEntity) result).getText());
}
public void testSimpleTransactionRollbackOnChecked() {
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JpaWorkManagerTest.java
Tue May 25 15:48:47 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/JpaWorkManagerTest.java
Sat Aug 14 22:25:02 2010
@@ -75,12 +75,12 @@
query.setParameter("text", UNIQUE_TEXT_3);
final Object o = query.getSingleResult();
- assert null != o : "no result!!";
- assert o instanceof JpaTestEntity : "Unknown type returned " +
o.getClass();
+ assertNotNull("no result!!", o);
+ assertTrue("Unknown type returned " + o.getClass(), o instanceof
JpaTestEntity);
JpaTestEntity ent = (JpaTestEntity) o;
- assert UNIQUE_TEXT_3.equals(ent.getText()) :
- "Incorrect result returned or not persisted properly" +
ent.getText();
+ assertEquals("Incorrect result returned or not persisted properly" +
ent.getText(),
+ UNIQUE_TEXT_3, ent.getText());
} finally {
injector.getInstance(EntityManager.class).getTransaction().commit();
=======================================
---
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ManualLocalTransactionsWithCustomMatcherTest.java
Tue May 25 15:48:47 2010
+++
/trunk/extensions/persist/test/com/google/inject/persist/jpa/ManualLocalTransactionsWithCustomMatcherTest.java
Sat Aug 14 22:25:02 2010
@@ -76,17 +76,17 @@
//persisted entity should remain in the same em (which should still be
open)
assertTrue("EntityManager appears to have been closed across txns!",
injector.getInstance(EntityManager.class).contains(entity));
- assert em.contains(entity) : "EntityManager appears to have been
closed across txns!";
- assert em.isOpen() : "EntityManager appears to have been closed across
txns!";
+ assertTrue("EntityManager appears to have been closed across txns!",
em.contains(entity));
+ assertTrue("EntityManager appears to have been closed across txns!",
em.isOpen());
injector.getInstance(WorkManager.class).end();
//try to query them back out
em = injector.getInstance(EntityManager.class);
- assert null != em.createQuery("from JpaTestEntity where text = :text")
- .setParameter("text", UNIQUE_TEXT).getSingleResult();
- assert null != em.createQuery("from JpaTestEntity where text = :text")
- .setParameter("text", UNIQUE_TEXT_2).getSingleResult();
+ assertNotNull(em.createQuery("from JpaTestEntity where text = :text")
+ .setParameter("text", UNIQUE_TEXT).getSingleResult());
+ assertNotNull(em.createQuery("from JpaTestEntity where text = :text")
+ .setParameter("text", UNIQUE_TEXT_2).getSingleResult());
em.close();
}
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.