01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.managedobject;
05:
06: import com.tc.io.serializer.TCObjectInputStream;
07: import com.tc.io.serializer.TCObjectOutputStream;
08: import com.tc.object.ObjectID;
09: import com.tc.object.tx.TransactionID;
10: import com.tc.objectserver.api.ObjectInstanceMonitor;
11: import com.tc.objectserver.core.api.ManagedObject;
12: import com.tc.objectserver.core.api.TestDNA;
13: import com.tc.objectserver.core.api.TestDNACursor;
14: import com.tc.objectserver.impl.ObjectInstanceMonitorImpl;
15: import com.tc.objectserver.persistence.impl.InMemoryPersistor;
16:
17: import java.io.ByteArrayInputStream;
18: import java.io.ByteArrayOutputStream;
19:
20: import junit.framework.TestCase;
21:
22: public class ManagedObjectSerializerTest extends TestCase {
23:
24: private ObjectID id;
25: private ManagedObjectStateSerializer stateSerializer;
26:
27: public void test() throws Exception {
28: ManagedObjectStateFactory.disableSingleton(true);
29: ManagedObjectStateFactory.createInstance(
30: new NullManagedObjectChangeListenerProvider(),
31: new InMemoryPersistor());
32:
33: stateSerializer = new ManagedObjectStateSerializer();
34: id = new ObjectID(1);
35:
36: ManagedObjectSerializer mos = new ManagedObjectSerializer(
37: stateSerializer);
38: ManagedObjectImpl mo = new ManagedObjectImpl(id);
39: assertTrue(mo.isDirty());
40: assertTrue(mo.isNew());
41: TestDNA dna = newDNA(1);
42: ObjectInstanceMonitor imo = new ObjectInstanceMonitorImpl();
43: mo.apply(dna, new TransactionID(1), new BackReferences(), imo,
44: false);
45: assertFalse(mo.isNew());
46:
47: ByteArrayOutputStream baout = new ByteArrayOutputStream();
48: TCObjectOutputStream out = new TCObjectOutputStream(baout);
49: mos.serializeTo(mo, out);
50: out.flush();
51: ManagedObject mo2 = (ManagedObject) mos
52: .deserializeFrom(new TCObjectInputStream(
53: new ByteArrayInputStream(baout.toByteArray())));
54:
55: assertFalse(mo2.isDirty());
56: assertFalse(mo2.isNew());
57: mo.setIsDirty(false);
58: assertNotSame(mo, mo2);
59: assertTrue(mo.isEqual(mo2));
60: }
61:
62: private TestDNA newDNA(int fieldSetCount) {
63: TestDNACursor cursor = new TestDNACursor();
64: for (int i = 0; i < fieldSetCount; i++) {
65: cursor.addPhysicalAction("refField" + i, new ObjectID(1));
66: cursor.addPhysicalAction("booleanField" + i, new Boolean(
67: true));
68: cursor.addPhysicalAction("byteField" + i,
69: new Byte((byte) 1));
70: cursor.addPhysicalAction("characterField" + i,
71: new Character('c'));
72: cursor.addPhysicalAction("doubleField" + i, new Double(
73: 100.001d));
74: cursor.addPhysicalAction("floatField" + i, new Float(
75: 100.001f));
76: cursor.addPhysicalAction("integerField" + i, new Integer(
77: 100));
78: cursor.addPhysicalAction("longField" + i, new Long(100));
79: cursor.addPhysicalAction("stringField" + i,
80: "Some nice string field" + i);
81: cursor.addPhysicalAction("shortField" + i, new Short(
82: (short) 1));
83: }
84: TestDNA dna = new TestDNA(cursor);
85: return dna;
86: }
87: }
|