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 VersionizedDNAWrapperTest extends TestCase {
022:
023: public void testResettingDNACursor() throws Exception {
024: TCByteBufferOutputStream out = new TCByteBufferOutputStream();
025:
026: final ObjectID id = new ObjectID(1);
027: final ObjectID pid = new ObjectID(2);
028: final String type = getClass().getName();
029: final int arrayLen = 42;
030: ObjectStringSerializer serializer = new ObjectStringSerializer();
031: ClassProvider classProvider = new MockClassProvider();
032: DNAEncoding encoding = new DNAEncodingImpl(classProvider);
033: DNAWriter dnaWriter = createDNAWriter(out, id, type,
034: serializer, encoding, "loader description");
035: PhysicalAction action1 = new PhysicalAction("class.field1",
036: new Integer(1), false);
037: LogicalAction action2 = new LogicalAction(12, new Object[] {
038: "key", "value" });
039: PhysicalAction action3 = new PhysicalAction("class.field2",
040: new ObjectID(3), true);
041: dnaWriter.addPhysicalAction(action1.getFieldName(), action1
042: .getObject());
043: dnaWriter.addLogicalAction(action2.getMethod(), action2
044: .getParameters());
045: dnaWriter.addPhysicalAction(action3.getFieldName(), action3
046: .getObject());
047: dnaWriter.setParentObjectID(pid);
048: dnaWriter.setArrayLength(arrayLen);
049: dnaWriter.finalizeDNA(true);
050:
051: TCByteBufferInputStream in = new TCByteBufferInputStream(out
052: .toArray());
053: DNAImpl dna = createDNAImpl(serializer, true);
054: assertSame(dna, dna.deserializeFrom(in));
055: assertEquals(0, in.available());
056:
057: VersionizedDNAWrapper vdna = new VersionizedDNAWrapper(dna, 10);
058: assertEquals(10, vdna.getVersion());
059: try {
060: vdna.getCursor().reset();
061: assertTrue(false);
062: } catch (UnsupportedOperationException use) {
063: // this is expected
064: }
065:
066: vdna = new VersionizedDNAWrapper(dna, 10, true);
067: DNACursor cursor = vdna.getCursor();
068: cursor.reset();
069: assertTrue(cursor.next(encoding));
070: compareAction(action1, cursor.getPhysicalAction());
071: cursor.reset();
072: assertTrue(cursor.next(encoding));
073: compareAction(action1, cursor.getPhysicalAction());
074: assertTrue(cursor.next(encoding));
075: compareAction(action2, cursor.getLogicalAction());
076: cursor.reset();
077: assertTrue(cursor.next(encoding));
078: compareAction(action1, cursor.getPhysicalAction());
079: assertTrue(cursor.next(encoding));
080: compareAction(action2, cursor.getLogicalAction());
081: assertTrue(cursor.next(encoding));
082: compareAction(action3, cursor.getPhysicalAction());
083: assertFalse(cursor.next(encoding));
084: cursor.reset();
085: assertTrue(cursor.next(encoding));
086: compareAction(action1, cursor.getPhysicalAction());
087: assertTrue(cursor.next(encoding));
088: compareAction(action2, cursor.getLogicalAction());
089: assertTrue(cursor.next(encoding));
090: compareAction(action3, cursor.getPhysicalAction());
091: assertFalse(cursor.next(encoding));
092: }
093:
094: protected DNAImpl createDNAImpl(ObjectStringSerializer serializer,
095: boolean b) {
096: return new DNAImpl(serializer, b);
097: }
098:
099: protected DNAWriter createDNAWriter(TCByteBufferOutputStream out,
100: ObjectID id, String type,
101: ObjectStringSerializer serializer, DNAEncoding encoding,
102: String string) {
103: return new DNAWriterImpl(out, id, type, serializer, encoding,
104: "loader description");
105: }
106:
107: private void compareAction(LogicalAction expect,
108: LogicalAction actual) {
109: assertEquals(expect.getMethod(), actual.getMethod());
110: assertTrue(Arrays.equals(expect.getParameters(), actual
111: .getParameters()));
112: }
113:
114: private void compareAction(PhysicalAction expect,
115: PhysicalAction actual) {
116: assertEquals(expect.getFieldName(), actual.getFieldName());
117: assertEquals(expect.getObject(), actual.getObject());
118: assertEquals(expect.isReference(), actual.isReference());
119: }
120:
121: }
|