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.objectserver.core.api;
006:
007: import com.tc.object.ObjectID;
008: import com.tc.object.tx.TransactionID;
009: import com.tc.objectserver.api.ObjectInstanceMonitor;
010: import com.tc.objectserver.impl.ObjectInstanceMonitorImpl;
011: import com.tc.objectserver.managedobject.BackReferences;
012: import com.tc.objectserver.managedobject.ManagedObjectImpl;
013: import com.tc.objectserver.managedobject.ManagedObjectStateFactory;
014: import com.tc.objectserver.managedobject.NullManagedObjectChangeListenerProvider;
015: import com.tc.objectserver.persistence.impl.InMemoryPersistor;
016: import com.tc.test.TCTestCase;
017:
018: import java.util.Map;
019:
020: public class ManagedObjectTest extends TCTestCase {
021:
022: public void testBasics() throws Exception {
023: ObjectInstanceMonitor instanceMonitor = new ObjectInstanceMonitorImpl();
024: ObjectID objectID = new ObjectID(1);
025: ManagedObjectStateFactory.disableSingleton(true);
026: ManagedObjectStateFactory.createInstance(
027: new NullManagedObjectChangeListenerProvider(),
028: new InMemoryPersistor());
029:
030: ManagedObjectImpl mo = new ManagedObjectImpl(objectID);
031:
032: assertTrue(mo.isDirty());
033: assertTrue(mo.isNew());
034:
035: TestDNACursor cursor = new TestDNACursor();
036: cursor.addPhysicalAction("field1", new ObjectID(1));
037: cursor.addPhysicalAction("field2", new Boolean(true));
038: cursor.addPhysicalAction("field3", new Character('c'));
039: TestDNA dna = new TestDNA(cursor);
040:
041: Map instances = instanceMonitor.getInstanceCounts();
042: assertEquals(0, instances.size());
043: mo.apply(dna, new TransactionID(1), new BackReferences(),
044: instanceMonitor, false);
045: assertFalse(mo.isNew());
046:
047: instances = instanceMonitor.getInstanceCounts();
048: assertEquals(1, instances.size());
049: assertEquals(new Integer(1), instances.get(dna.getTypeName()));
050:
051: assertEquals(1, mo.getObjectReferences().size());
052: assertEquals(dna.typeName, mo.getClassname());
053: assertEquals(dna.loaderDesc, mo.getLoaderDescription());
054: }
055:
056: public void testApplyDNASameOrLowerVersion() throws Exception {
057: ObjectInstanceMonitor instanceMonitor = new ObjectInstanceMonitorImpl();
058: ObjectID objectID = new ObjectID(1);
059: ManagedObjectStateFactory.disableSingleton(true);
060: ManagedObjectStateFactory.createInstance(
061: new NullManagedObjectChangeListenerProvider(),
062: new InMemoryPersistor());
063:
064: ManagedObjectImpl mo = new ManagedObjectImpl(objectID);
065:
066: TestDNACursor cursor = new TestDNACursor();
067: cursor.addPhysicalAction("field1", new ObjectID(1));
068: TestDNA dna = new TestDNA(cursor);
069: dna.version = 10;
070:
071: mo.apply(dna, new TransactionID(1), new BackReferences(),
072: instanceMonitor, false);
073:
074: // Now reapply and see if it fails.
075: boolean failed = false;
076: try {
077: mo.apply(dna, new TransactionID(1), new BackReferences(),
078: instanceMonitor, false);
079: failed = true;
080: } catch (AssertionError ae) {
081: // expected.
082: }
083: if (failed) {
084: throw new AssertionError("Failed to fail !!!");
085: }
086:
087: // Apply with lower version
088: try {
089: dna = new TestDNA(cursor);
090: dna.version = 5;
091: dna.isDelta = true;
092: mo.apply(dna, new TransactionID(1), new BackReferences(),
093: instanceMonitor, false);
094: failed = true;
095: } catch (AssertionError ae) {
096: // expected.
097: }
098: if (failed) {
099: throw new AssertionError("Failed to fail !!!");
100: }
101:
102: // Apply with higher
103: dna = new TestDNA(cursor);
104: dna.version = 15;
105: dna.isDelta = true;
106: mo.apply(dna, new TransactionID(1), new BackReferences(),
107: instanceMonitor, false);
108:
109: long version = mo.getVersion();
110:
111: // Apply in passive, ignore as true.
112: dna = new TestDNA(cursor);
113: dna.version = 5;
114: dna.isDelta = true;
115: mo.apply(dna, new TransactionID(1), new BackReferences(),
116: instanceMonitor, true);
117:
118: assertTrue(version == mo.getVersion());
119:
120: dna = new TestDNA(cursor);
121: dna.version = 15;
122: dna.isDelta = true;
123: mo.apply(dna, new TransactionID(1), new BackReferences(),
124: instanceMonitor, true);
125:
126: assertTrue(version == mo.getVersion());
127:
128: dna = new TestDNA(cursor);
129: dna.version = 17;
130: dna.isDelta = true;
131: mo.apply(dna, new TransactionID(1), new BackReferences(),
132: instanceMonitor, true);
133:
134: assertTrue(version < mo.getVersion());
135: }
136:
137: }
|