001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import com.tc.exception.ImplementMe;
008: import com.tc.net.protocol.tcm.ChannelIDProvider;
009: import com.tc.net.protocol.tcm.TestChannelIDProvider;
010: import com.tc.object.BaseDSOTestCase;
011: import com.tc.object.ClientObjectManagerImpl;
012: import com.tc.object.MockTCObject;
013: import com.tc.object.ObjectID;
014: import com.tc.object.Portability;
015: import com.tc.object.RemoteObjectManager;
016: import com.tc.object.TCClassFactory;
017: import com.tc.object.TCObject;
018: import com.tc.object.TCObjectFactory;
019: import com.tc.object.TestClassFactory;
020: import com.tc.object.TestObjectFactory;
021: import com.tc.object.TestRemoteObjectManager;
022: import com.tc.object.bytecode.MockClassProvider;
023: import com.tc.object.cache.EvictionPolicy;
024: import com.tc.object.cache.NullCache;
025: import com.tc.object.config.DSOClientConfigHelper;
026: import com.tc.object.idprovider.api.ObjectIDProvider;
027: import com.tc.object.idprovider.impl.ObjectIDProviderImpl;
028: import com.tc.object.loaders.ClassProvider;
029: import com.tc.object.logging.NullRuntimeLogger;
030: import com.tc.object.logging.RuntimeLogger;
031: import com.tc.object.tx.MockTransactionManager;
032: import com.tc.util.sequence.SimpleSequence;
033:
034: /**
035: * Test to see if an adapted class has all of the adaptations we expect.
036: */
037: public class DsoFinalMethodTest extends BaseDSOTestCase {
038: private String rootName;
039: private Object rootObject;
040: private MockClientObjectManagerImpl objectManager;
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: rootName = "testSyncRoot";
046: rootObject = new Object();
047: ObjectID objectID = new ObjectID(1);
048: TCObject tcObject = new MockTCObject(objectID, rootObject);
049: TestObjectFactory objectFactory = new TestObjectFactory();
050: objectFactory.peerObject = rootObject;
051: objectFactory.tcObject = tcObject;
052: objectManager = new MockClientObjectManagerImpl(
053: new MockRemoteObjectManagerImpl(), configHelper(),
054: new ObjectIDProviderImpl(new SimpleSequence()),
055: new NullCache(), new NullRuntimeLogger(),
056: new TestChannelIDProvider(), new MockClassProvider(),
057: new TestClassFactory(), objectFactory);
058:
059: objectManager
060: .setTransactionManager(new MockTransactionManagerImpl());
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: this .objectManager = null;
066: this .rootName = null;
067: this .rootObject = null;
068: }
069:
070: /**
071: * This test makes sure that method retrieveRootID() of RemoteObjectManager will not be called when dsoFinal is set to
072: * false when a root is being created.
073: */
074: public void testRootCreateOrReplaceWithNonDsoFinal()
075: throws Exception {
076: // If retrieveRootID() of RemoteObjectManager is being called, this call will throw an
077: // AssertionError.
078: objectManager.lookupOrCreateRoot(rootName, rootObject, false);
079: }
080:
081: public void testRootCreateOrReplaceWithDsoFinal() throws Exception {
082: try {
083: objectManager
084: .lookupOrCreateRoot(rootName, rootObject, true);
085: throw new AssertionError(
086: "should have thrown an AssertionError");
087: } catch (AssertionError e) {
088: // expected.
089: }
090: }
091:
092: private static class MockClientObjectManagerImpl extends
093: ClientObjectManagerImpl {
094: public MockClientObjectManagerImpl(
095: RemoteObjectManager remoteObjectManager,
096: DSOClientConfigHelper clientConfiguration,
097: ObjectIDProvider idProvider, EvictionPolicy cache,
098: RuntimeLogger runtimeLogger,
099: ChannelIDProvider provider,
100: ClassProvider classProvider,
101: TCClassFactory classFactory,
102: TCObjectFactory objectFactory) {
103: super (remoteObjectManager, clientConfiguration, idProvider,
104: cache, runtimeLogger, provider, classProvider,
105: classFactory, objectFactory, new TestPortability(),
106: null);
107: }
108:
109: public boolean isPortable(Object obj) {
110: return true;
111: }
112:
113: public Object lookupObject(ObjectID objectID) {
114: return null;
115: }
116: }
117:
118: private static class MockRemoteObjectManagerImpl extends
119: TestRemoteObjectManager {
120:
121: public ObjectID retrieveRootID(String name) {
122: throw new AssertionError(
123: "retrieveRootID should not be called.");
124: }
125: }
126:
127: private static class MockTransactionManagerImpl extends
128: MockTransactionManager {
129: public void createRoot(String name, ObjectID id) {
130: return;
131: }
132:
133: public void createObject(TCObject source) {
134: return;
135: }
136: }
137:
138: private static class TestPortability implements Portability {
139:
140: public boolean allowSubclassOf(Class clazz) {
141: return true;
142: }
143:
144: public NonPortableReason getNonPortableReason(
145: Class topLevelClass) {
146: throw new ImplementMe();
147: }
148:
149: public boolean isInstrumentationNotNeeded(String name) {
150: return false;
151: }
152:
153: public boolean isPortableClass(Class clazz) {
154: return true;
155: }
156:
157: public boolean isClassPhysicallyInstrumented(Class clazz) {
158: throw new ImplementMe();
159: }
160:
161: public boolean isPortableInstance(Object obj) {
162: return true;
163: }
164:
165: }
166:
167: }
|