Author: aadamchik Date: Sun Jan 30 01:46:13 2011 New Revision: 1065164 URL: http://svn.apache.org/viewvc?rev=1065164&view=rev Log: CAY-1536 Expand deserialization context to provide Injector object instead of just a DataChannel
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/CayenneRuntimeTest.java - copied, changed from r1065163, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneDataObject.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSerializationTest.java cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/MultiContextCase.java Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original) +++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sun Jan 30 01:46:13 2011 @@ -19,6 +19,7 @@ CAY-1509 Lifecycle event annotations sup CAY-1532 Import cayenne-lifecycle from sandbox CAY-1533 Deprecate org.apache.cayenne.xml CAY-1534 cayenne-lifecycle: implement AuditableFilter +CAY-1536 Expand deserialization context to provide Injector object instead of just a DataChannel. Bug Fixes Since 3.1M1: Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml Sun Jan 30 01:46:13 2011 @@ -32,6 +32,10 @@ <artifactId>junit</artifactId> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + </dependency> + <dependency> <groupId>org.apache.cayenne.build-tools</groupId> <artifactId>cayenne-test-utilities</artifactId> <version>${version}</version> Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java Sun Jan 30 01:46:13 2011 @@ -26,7 +26,6 @@ import java.util.Map; import org.apache.cayenne.cache.MapQueryCache; import org.apache.cayenne.cache.QueryCache; -import org.apache.cayenne.configuration.web.CayenneFilter; import org.apache.cayenne.event.EventManager; import org.apache.cayenne.exp.ValueInjector; import org.apache.cayenne.graph.CompoundDiff; @@ -62,14 +61,6 @@ public abstract class BaseContext implem protected static final ThreadLocal<ObjectContext> threadObjectContext = new ThreadLocal<ObjectContext>(); /** - * A holder of a DataChannel bound to the current thread. Used mainly for proper - * contexts and objects deserialization. - * - * @since 3.1 - */ - protected static final ThreadLocal<DataChannel> threadDeserializationChannel = new ThreadLocal<DataChannel>(); - - /** * Returns the ObjectContext bound to the current thread. * * @since 3.0 @@ -97,33 +88,6 @@ public abstract class BaseContext implem threadObjectContext.set(context); } - /** - * Binds a DataChannel to the current thread that should be used for deserializing of - * ObjectContexts. An ObjectContext implementation may call - * {@link #getThreadDeserializationChannel()} from its deserialization method to - * attach to the currently active channel. - * <p> - * {@link CayenneFilter} will automatically bind the right channel to each request - * thread. If you are not using CayenneFilter, your application is responsible for - * calling this method at appropriate points of the lifecycle. - * - * @since 3.1 - */ - public static void bindThreadDeserializationChannel(DataChannel dataChannel) { - threadDeserializationChannel.set(dataChannel); - } - - /** - * Returns the DataChannel bound to the current thread. May return null if none is - * bound (unlike {@link #getThreadObjectContext()} that throws if a context is not - * bound). - * - * @since 3.1 - */ - public static DataChannel getThreadDeserializationChannel() { - return threadDeserializationChannel.get(); - } - // if we are to pass the context around, channel should be left alone and // reinjected later if needed protected transient DataChannel channel; Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneDataObject.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneDataObject.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneDataObject.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneDataObject.java Sun Jan 30 01:46:13 2011 @@ -29,6 +29,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.di.Injector; import org.apache.cayenne.map.DbAttribute; import org.apache.cayenne.map.DbJoin; import org.apache.cayenne.map.DbRelationship; @@ -706,13 +708,15 @@ public class CayenneDataObject extends P */ public void decodeFromXML(XMLDecoder decoder) { - DataChannel channel = BaseContext.getThreadDeserializationChannel(); - if (channel == null) { - throw new IllegalStateException( - "Can't perform deserialization - no DataChannel bound to the current thread."); + Injector injector = CayenneRuntime.getThreadInjector(); + if (injector == null) { + throw new IllegalStateException("Can't perform deserialization - " + + "no Injector bound to the current thread."); } - EntityResolver resolver = channel.getEntityResolver(); + EntityResolver resolver = injector + .getInstance(DataChannel.class) + .getEntityResolver(); ObjEntity objectEntity = resolver.lookupObjEntity(getClass()); for (final ObjAttribute att : objectEntity.getDeclaredAttributes()) { Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java Sun Jan 30 01:46:13 2011 @@ -44,6 +44,8 @@ import org.apache.cayenne.QueryResponse; import org.apache.cayenne.access.util.IteratedSelectObserver; import org.apache.cayenne.cache.QueryCache; import org.apache.cayenne.cache.QueryCacheFactory; +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.di.Injector; import org.apache.cayenne.event.EventManager; import org.apache.cayenne.graph.ChildDiffLoader; import org.apache.cayenne.graph.CompoundDiff; @@ -95,7 +97,7 @@ public class DataContext extends BaseCon /** * Creates a new DataContext with parent DataChannel and ObjectStore. - * + * * @since 1.2 */ public DataContext(DataChannel channel, ObjectStore objectStore) { @@ -117,7 +119,7 @@ public class DataContext extends BaseCon * Returns {@link QueryCache} used by this DataContext, creating it on the fly if * needed. Uses parent DataDomain {@link QueryCacheFactory} to initialize the cache * for the first time, passing parent DataDomain's properties. - * + * * @since 3.0 */ @Override @@ -138,7 +140,7 @@ public class DataContext extends BaseCon /** * Creates and returns a new child ObjectContext. - * + * * @since 3.0 */ public ObjectContext createChildContext() { @@ -197,23 +199,16 @@ public class DataContext extends BaseCon } } - @Override - public DataChannel getChannel() { - awakeFromDeserialization(); - return super.getChannel(); - } - /** * Returns a DataDomain used by this DataContext. DataDomain is looked up in the * DataChannel hierarchy. If a channel is not a DataDomain or a DataContext, null is * returned. - * + * * @return DataDomain that is a direct or indirect parent of this DataContext in the * DataChannel hierarchy. * @since 1.1 */ public DataDomain getParentDataDomain() { - awakeFromDeserialization(); if (channel == null) { return null; @@ -237,7 +232,7 @@ public class DataContext extends BaseCon /** * Sets a DataContextDelegate for this context. Delegate is notified of certain events * in the DataContext lifecycle and can customize DataContext behavior. - * + * * @since 1.1 */ public void setDelegate(DataContextDelegate delegate) { @@ -246,7 +241,7 @@ public class DataContext extends BaseCon /** * Returns a delegate currently associated with this DataContext. - * + * * @since 1.1 */ public DataContextDelegate getDelegate() { @@ -307,7 +302,7 @@ public class DataContext extends BaseCon /** * Returns a collection of all uncommitted registered objects. - * + * * @since 1.2 */ @Override @@ -344,7 +339,7 @@ public class DataContext extends BaseCon * temporary ids. DO NOT USE this method if you expect a DataRow to represent a * complete object state. * </p> - * + * * @since 1.1 */ public DataRow currentSnapshot(final Persistent object) { @@ -452,7 +447,7 @@ public class DataContext extends BaseCon /** * Converts a list of DataRows to a List of DataObject registered with this * DataContext. - * + * * @since 3.0 */ public List objectsFromDataRows( @@ -479,7 +474,7 @@ public class DataContext extends BaseCon /** * Creates a DataObject from DataRow. - * + * * @deprecated Use objectFromDataRow(Class<T> objectClass, DataRow dataRow) instead. * @see DataRow */ @@ -493,7 +488,7 @@ public class DataContext extends BaseCon /** * Creates a DataObject from DataRow. - * + * * @see DataRow * @since 3.1 */ @@ -515,7 +510,7 @@ public class DataContext extends BaseCon /** * Creates a DataObject from DataRow. This variety of the 'objectFromDataRow' method * is normally used for generic classes. - * + * * @deprecated Use objectFromDataRow(String entityName, DataRow dataRow) instead. * @see DataRow * @since 3.0 @@ -531,13 +526,11 @@ public class DataContext extends BaseCon /** * Creates a DataObject from DataRow. This variety of the 'objectFromDataRow' method * is normally used for generic classes. - * + * * @see DataRow * @since 3.1 */ - public DataObject objectFromDataRow( - String entityName, - DataRow dataRow) { + public DataObject objectFromDataRow(String entityName, DataRow dataRow) { ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entityName); List<?> list = objectsFromDataRows(descriptor, Collections.singletonList(dataRow)); @@ -546,7 +539,7 @@ public class DataContext extends BaseCon /** * Creates and registers a new persistent object. - * + * * @since 1.2 */ @Override @@ -570,7 +563,7 @@ public class DataContext extends BaseCon * <p/> * <i>Note: in most cases {@link #newObject(Class)} method should be used, however * this method is helpful when generic persistent classes are used.</i> - * + * * @since 3.0 */ public Persistent newObject(String entityName) { @@ -607,7 +600,7 @@ public class DataContext extends BaseCon * <p/> * <i>Note that since 3.0 this method takes Object as an argument instead of a * {@link DataObject}.</i> - * + * * @param object new object that needs to be made persistent. */ @Override @@ -711,7 +704,7 @@ public class DataContext extends BaseCon * Unregisters a Collection of DataObjects from the DataContext and the underlying * ObjectStore. This operation also unsets DataContext and ObjectId for each object * and changes its state to TRANSIENT. - * + * * @see #invalidateObjects(Collection) */ public void unregisterObjects(Collection dataObjects) { @@ -721,7 +714,7 @@ public class DataContext extends BaseCon /** * If the parent channel is a DataContext, reverts local changes to make this context * look like the parent, if the parent channel is a DataDomain, reverts all changes. - * + * * @since 1.2 */ @Override @@ -771,7 +764,7 @@ public class DataContext extends BaseCon * update. If it is a DataDomain (the most common case), the changes are written to * the database. To cause cascading commit all the way to the database, one must use * {@link #commitChanges()}. - * + * * @since 1.2 * @see #commitChanges() */ @@ -815,7 +808,7 @@ public class DataContext extends BaseCon /** * Synchronizes with the parent channel, performing a flush or a commit. - * + * * @since 1.2 */ GraphDiff flushToParent(boolean cascade) { @@ -949,7 +942,7 @@ public class DataContext extends BaseCon /** * Runs an iterated query in transactional context provided by the caller. - * + * * @since 1.2 */ ResultIterator internalPerformIteratedQuery(Query query) throws CayenneException { @@ -962,7 +955,7 @@ public class DataContext extends BaseCon /** * Executes a query returning a generic response. - * + * * @since 1.2 */ @Override @@ -996,7 +989,7 @@ public class DataContext extends BaseCon * <p> * <i>Since 1.2 takes any Query parameter, not just GenericSelectQuery</i> * </p> - * + * * @return A list of DataObjects or a DataRows, depending on the value returned by * {@link QueryMetadata#isFetchingDataRows()}. */ @@ -1015,7 +1008,7 @@ public class DataContext extends BaseCon /** * An implementation of a {@link DataChannel} method that is used by child contexts to * execute queries. Not intended for direct use. - * + * * @since 1.2 */ public QueryResponse onQuery(ObjectContext context, Query query) { @@ -1025,7 +1018,7 @@ public class DataContext extends BaseCon /** * Performs a single database query that does not select rows. Returns an array of * update counts. - * + * * @since 1.1 */ public int[] performNonSelectingQuery(Query query) { @@ -1036,7 +1029,7 @@ public class DataContext extends BaseCon /** * Performs a named mapped query that does not select rows. Returns an array of update * counts. - * + * * @since 1.1 */ public int[] performNonSelectingQuery(String queryName) { @@ -1046,7 +1039,7 @@ public class DataContext extends BaseCon /** * Performs a named mapped non-selecting query using a map of parameters. Returns an * array of update counts. - * + * * @since 1.1 */ public int[] performNonSelectingQuery(String queryName, Map<String, ?> parameters) { @@ -1057,7 +1050,7 @@ public class DataContext extends BaseCon * Returns a list of objects or DataRows for a named query stored in one of the * DataMaps. Internally Cayenne uses a caching policy defined in the named query. If * refresh flag is true, a refresh is forced no matter what the caching policy is. - * + * * @param queryName a name of a GenericSelectQuery defined in one of the DataMaps. If * no such query is defined, this method will throw a * CayenneRuntimeException. @@ -1073,7 +1066,7 @@ public class DataContext extends BaseCon * Returns a list of objects or DataRows for a named query stored in one of the * DataMaps. Internally Cayenne uses a caching policy defined in the named query. If * refresh flag is true, a refresh is forced no matter what the caching policy is. - * + * * @param queryName a name of a GenericSelectQuery defined in one of the DataMaps. If * no such query is defined, this method will throw a * CayenneRuntimeException. @@ -1097,14 +1090,13 @@ public class DataContext extends BaseCon */ @Override public EntityResolver getEntityResolver() { - awakeFromDeserialization(); return entityResolver; } /** * Returns <code>true</code> if the ObjectStore uses shared cache of a parent * DataDomain. - * + * * @since 1.1 */ public boolean isUsingSharedSnapshotCache() { @@ -1121,7 +1113,7 @@ public class DataContext extends BaseCon /** * Returns whether this DataContext performs object validation before commit is * executed. - * + * * @since 1.1 */ public boolean isValidatingObjectsOnCommit() { @@ -1131,7 +1123,7 @@ public class DataContext extends BaseCon /** * Sets the property defining whether this DataContext should perform object * validation before commit is executed. - * + * * @since 1.1 */ public void setValidatingObjectsOnCommit(boolean flag) { @@ -1164,6 +1156,18 @@ public class DataContext extends BaseCon objectStore.setDataRowCache(cache); } + // 3. set parent channel + // call a channel setter to ensure EntityResolver is extracted from channel + // call it after DataRowCache is deserialized to avoid incorrect DataRowCache lazy + // creation + Injector injector = CayenneRuntime.getThreadInjector(); + if (injector != null) { + setChannel(injector.getInstance(DataChannel.class)); + } + else { + // throw? + } + // CayenneDataObjects have a transient datacontext // because at deserialize time the datacontext may need to be different // than the one at serialize time (for programmer defined reasons). @@ -1181,19 +1185,8 @@ public class DataContext extends BaseCon } /** - * Re-attaches itself to a DataChannel attached to the current thread. The DataChannel - * is taken from {@link BaseContext#getThreadDeserializationChannel()}. - */ - private final void awakeFromDeserialization() { - if (channel == null) { - // call a channel setter to ensure EntityResolver is extracted from channel - setChannel(BaseContext.getThreadDeserializationChannel()); - } - } - - /** * Returns this context's ObjectStore. - * + * * @since 1.2 */ @Override @@ -1209,7 +1202,7 @@ public class DataContext extends BaseCon * In case you pass a non-null second parameter, you are responsible for setting * correct persistence state of the returned local object, as generally there is no * way for Cayenne to determine the resulting local object state. - * + * * @since 1.2 */ @Override Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java Sun Jan 30 01:46:13 2011 @@ -22,6 +22,7 @@ import java.util.Collection; import org.apache.cayenne.DataChannel; import org.apache.cayenne.ObjectContext; +import org.apache.cayenne.configuration.web.CayenneFilter; import org.apache.cayenne.di.BeforeScopeEnd; import org.apache.cayenne.di.DIBootstrap; import org.apache.cayenne.di.Injector; @@ -38,6 +39,38 @@ import org.apache.cayenne.di.Module; */ public abstract class CayenneRuntime { + /** + * A holder of an Injector bound to the current thread. Used mainly to allow + * serializable contexts to attach to correct Cayenne stack on deserialization. + * + * @since 3.1 + */ + protected static final ThreadLocal<Injector> threadInjector = new ThreadLocal<Injector>(); + + /** + * Binds a DI {@link Injector} bound to the current thread. It is primarily intended + * for deserialization of ObjectContexts. + * <p> + * {@link CayenneFilter} will automatically bind the right injector to each request + * thread. If you are not using CayenneFilter, your application is responsible for + * calling this method at appropriate points of the lifecycle. + * + * @since 3.1 + */ + public static void bindThreadInjector(Injector injector) { + threadInjector.set(injector); + } + + /** + * Returns the {@link Injector} bound to the current thread. Will return null if none + * is bound. + * + * @since 3.1 + */ + public static Injector getThreadInjector() { + return threadInjector.get(); + } + protected Injector injector; protected Module[] modules; Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java Sun Jan 30 01:46:13 2011 @@ -24,8 +24,8 @@ import javax.servlet.http.HttpServletReq import javax.servlet.http.HttpSession; import org.apache.cayenne.BaseContext; -import org.apache.cayenne.DataChannel; import org.apache.cayenne.ObjectContext; +import org.apache.cayenne.configuration.CayenneRuntime; import org.apache.cayenne.configuration.ObjectContextFactory; import org.apache.cayenne.di.Inject; import org.apache.cayenne.di.Injector; @@ -35,7 +35,7 @@ import org.apache.cayenne.di.Injector; * {@link ObjectContext} in a web session and binds it to request thread. Note that using * this handler would force {@link HttpSession} creation, that may not be desirable in * many cases. Also session-bound context may result in a race condition with two user - * requests updating the same periststent objects in parallel. + * requests updating the same persistent objects in parallel. * <p> * User applications in most cases should provide a custom RequestHandler that implements * a smarter app-specific strategy for providing ObjectContext. @@ -55,8 +55,7 @@ public class SessionContextRequestHandle public void requestStart(ServletRequest request, ServletResponse response) { - BaseContext.bindThreadDeserializationChannel(injector - .getInstance(DataChannel.class)); + CayenneRuntime.bindThreadInjector(injector); if (request instanceof HttpServletRequest) { @@ -80,7 +79,7 @@ public class SessionContextRequestHandle } public void requestEnd(ServletRequest request, ServletResponse response) { - BaseContext.bindThreadDeserializationChannel(null); + CayenneRuntime.bindThreadInjector(null); BaseContext.bindThreadObjectContext(null); } Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java Sun Jan 30 01:46:13 2011 @@ -32,20 +32,4 @@ public class BaseContextTest extends Tes assertNotNull(properties); assertSame(properties, context.getUserProperties()); } - - public void testBindThreadDeserializationChannel() { - - assertNull(BaseContext.getThreadDeserializationChannel()); - - try { - DataChannel channel = new MockDataChannel(); - BaseContext.bindThreadDeserializationChannel(channel); - assertSame(channel, BaseContext.getThreadDeserializationChannel()); - } - finally { - BaseContext.bindThreadDeserializationChannel(null); - } - - assertNull(BaseContext.getThreadDeserializationChannel()); - } } Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSerializationTest.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSerializationTest.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSerializationTest.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSerializationTest.java Sun Jan 30 01:46:13 2011 @@ -19,12 +19,17 @@ package org.apache.cayenne.access; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import java.util.List; -import org.apache.cayenne.BaseContext; import org.apache.cayenne.Cayenne; +import org.apache.cayenne.DataChannel; import org.apache.cayenne.ObjectContext; import org.apache.cayenne.PersistenceState; +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.di.Injector; import org.apache.cayenne.testdo.testmap.Artist; import org.apache.cayenne.unit.CayenneCase; import org.apache.cayenne.util.Util; @@ -33,13 +38,15 @@ public class DataContextSerializationTes @Override protected void setUp() throws Exception { - BaseContext.bindThreadDeserializationChannel(getDomain()); + Injector injector = mock(Injector.class); + when(injector.getInstance(DataChannel.class)).thenReturn(getDomain()); + CayenneRuntime.bindThreadInjector(injector); deleteTestData(); } @Override protected void tearDown() throws Exception { - BaseContext.bindThreadDeserializationChannel(null); + CayenneRuntime.bindThreadInjector(null); super.tearDown(); } Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/CayenneRuntimeTest.java (from r1065163, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java) URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/CayenneRuntimeTest.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/CayenneRuntimeTest.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java&r1=1065163&r2=1065164&rev=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/BaseContextTest.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/CayenneRuntimeTest.java Sun Jan 30 01:46:13 2011 @@ -1,51 +1,45 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne; - -import java.util.Map; - -import junit.framework.TestCase; - -public class BaseContextTest extends TestCase { - - public void testUserPropertiesLazyInit() { - BaseContext context = new MockBaseContext(); - assertNull(context.userProperties); - - Map<String, Object> properties = context.getUserProperties(); - assertNotNull(properties); - assertSame(properties, context.getUserProperties()); - } - - public void testBindThreadDeserializationChannel() { - - assertNull(BaseContext.getThreadDeserializationChannel()); - - try { - DataChannel channel = new MockDataChannel(); - BaseContext.bindThreadDeserializationChannel(channel); - assertSame(channel, BaseContext.getThreadDeserializationChannel()); - } - finally { - BaseContext.bindThreadDeserializationChannel(null); - } - - assertNull(BaseContext.getThreadDeserializationChannel()); - } -} +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.configuration; + +import static org.mockito.Mockito.mock; + +import org.apache.cayenne.di.Injector; + +import junit.framework.TestCase; + +public class CayenneRuntimeTest extends TestCase { + + public void testBindThreadInjector() { + + Injector injector = mock(Injector.class); + + assertNull(CayenneRuntime.getThreadInjector()); + + try { + CayenneRuntime.bindThreadInjector(injector); + assertSame(injector, CayenneRuntime.getThreadInjector()); + } + finally { + CayenneRuntime.bindThreadInjector(null); + } + + assertNull(CayenneRuntime.getThreadInjector()); + } +} Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/MultiContextCase.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/MultiContextCase.java?rev=1065164&r1=1065163&r2=1065164&view=diff ============================================================================== --- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/MultiContextCase.java (original) +++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/MultiContextCase.java Sun Jan 30 01:46:13 2011 @@ -19,8 +19,13 @@ package org.apache.cayenne.unit; -import org.apache.cayenne.BaseContext; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.cayenne.DataChannel; import org.apache.cayenne.access.DataContext; +import org.apache.cayenne.configuration.CayenneRuntime; +import org.apache.cayenne.di.Injector; import org.apache.cayenne.util.Util; /** @@ -47,7 +52,11 @@ public abstract class MultiContextCase e protected DataContext mirrorDataContext(DataContext context) throws Exception { DataContext mirror; - BaseContext.bindThreadDeserializationChannel(getDomain()); + + Injector injector = mock(Injector.class); + when(injector.getInstance(DataChannel.class)).thenReturn(getDomain()); + CayenneRuntime.bindThreadInjector(injector); + try { mirror = (DataContext) Util.cloneViaSerialization(context); @@ -55,7 +64,7 @@ public abstract class MultiContextCase e mirror.getChannel(); } finally { - BaseContext.bindThreadDeserializationChannel(null); + CayenneRuntime.bindThreadInjector(null); } assertNotSame(context, mirror);