001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.dna.impl;
005:
006: import com.tc.io.TCByteBufferInputStream;
007: import com.tc.io.TCByteBufferOutputStream;
008: import com.tc.object.ObjectID;
009: import com.tc.object.bytecode.MockClassProvider;
010: import com.tc.object.dna.api.DNACursor;
011: import com.tc.object.dna.api.DNAWriter;
012: import com.tc.object.dna.api.DNAEncoding;
013: import com.tc.object.dna.api.LogicalAction;
014: import com.tc.object.dna.api.PhysicalAction;
015: import com.tc.object.loaders.ClassProvider;
016:
017: import java.util.Arrays;
018:
019: import junit.framework.TestCase;
020:
021: public class DNAImplTest extends TestCase {
022:
023: protected DNAImpl dna;
024:
025: public void testSerializeDeserialize() throws Exception {
026: TCByteBufferOutputStream out = new TCByteBufferOutputStream();
027:
028: final ObjectID id = new ObjectID(1);
029: final ObjectID pid = new ObjectID(2);
030: final String type = getClass().getName();
031: final int arrayLen = 42;
032: ObjectStringSerializer serializer = new ObjectStringSerializer();
033: ClassProvider classProvider = new MockClassProvider();
034: DNAEncoding encoding = new DNAEncodingImpl(classProvider);
035: DNAWriter dnaWriter = createDNAWriter(out, id, type,
036: serializer, encoding, "loader description");
037: PhysicalAction action1 = new PhysicalAction("class.field1",
038: new Integer(1), false);
039: LogicalAction action2 = new LogicalAction(12, new Object[] {
040: "key", "value" });
041: PhysicalAction action3 = new PhysicalAction("class.field2",
042: new ObjectID(3), true);
043: dnaWriter.addPhysicalAction(action1.getFieldName(), action1
044: .getObject());
045: dnaWriter.addLogicalAction(action2.getMethod(), action2
046: .getParameters());
047: dnaWriter.addPhysicalAction(action3.getFieldName(), action3
048: .getObject());
049: dnaWriter.setParentObjectID(pid);
050: dnaWriter.setArrayLength(arrayLen);
051: dnaWriter.finalizeDNA(getIsDelta());
052:
053: TCByteBufferInputStream in = new TCByteBufferInputStream(out
054: .toArray());
055: dna = createDNAImpl(serializer, true);
056: assertSame(dna, dna.deserializeFrom(in));
057: assertEquals(0, in.available());
058: DNACursor cursor = dna.getCursor();
059: int count = 1;
060: while (cursor.next(encoding)) {
061: switch (count) {
062: case 1:
063: compareAction(action1, cursor.getPhysicalAction());
064: break;
065: case 2:
066: compareAction(action2, cursor.getLogicalAction());
067: break;
068: case 3:
069: compareAction(action3, cursor.getPhysicalAction());
070: break;
071: default:
072: fail("count got to " + count);
073: }
074: count++;
075: }
076:
077: assertEquals(id, dna.getObjectID());
078: assertEquals(pid, dna.getParentObjectID());
079: assertEquals(type, dna.getTypeName());
080: assertEquals(arrayLen, dna.getArraySize());
081: assertOverridable();
082: }
083:
084: protected boolean getIsDelta() {
085: return true;
086: }
087:
088: protected void assertOverridable() {
089: assertTrue(dna.isDelta());
090: }
091:
092: protected DNAImpl createDNAImpl(ObjectStringSerializer serializer,
093: boolean b) {
094: return new DNAImpl(serializer, b);
095: }
096:
097: protected DNAWriter createDNAWriter(TCByteBufferOutputStream out,
098: ObjectID id, String type,
099: ObjectStringSerializer serializer, DNAEncoding encoding,
100: String string) {
101: return new DNAWriterImpl(out, id, type, serializer, encoding,
102: "loader description");
103: }
104:
105: private void compareAction(LogicalAction expect,
106: LogicalAction actual) {
107: assertEquals(expect.getMethod(), actual.getMethod());
108: assertTrue(Arrays.equals(expect.getParameters(), actual
109: .getParameters()));
110: }
111:
112: private void compareAction(PhysicalAction expect,
113: PhysicalAction actual) {
114: assertEquals(expect.getFieldName(), actual.getFieldName());
115: assertEquals(expect.getObject(), actual.getObject());
116: assertEquals(expect.isReference(), actual.isReference());
117: }
118:
119: }
|