KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > dna > impl > VersionizedDNAWrapperTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.dna.impl;
5
6 import com.tc.io.TCByteBufferInputStream;
7 import com.tc.io.TCByteBufferOutputStream;
8 import com.tc.object.ObjectID;
9 import com.tc.object.bytecode.MockClassProvider;
10 import com.tc.object.dna.api.DNACursor;
11 import com.tc.object.dna.api.DNAWriter;
12 import com.tc.object.dna.api.LogicalAction;
13 import com.tc.object.dna.api.PhysicalAction;
14 import com.tc.object.loaders.ClassProvider;
15
16 import java.util.Arrays JavaDoc;
17
18 import junit.framework.TestCase;
19
20 public class VersionizedDNAWrapperTest extends TestCase {
21
22   public void testResettingDNACursor() throws Exception JavaDoc {
23     TCByteBufferOutputStream out = new TCByteBufferOutputStream();
24
25     final ObjectID id = new ObjectID(1);
26     final ObjectID pid = new ObjectID(2);
27     final String JavaDoc type = getClass().getName();
28     final int arrayLen = 42;
29     ObjectStringSerializer serializer = new ObjectStringSerializer();
30     ClassProvider classProvider = new MockClassProvider();
31     DNAEncoding encoding = new DNAEncoding(classProvider);
32     DNAWriter dnaWriter = createDNAWriter(out, id, type, serializer, encoding, "loader description");
33     PhysicalAction action1 = new PhysicalAction("class.field1", new Integer JavaDoc(1), false);
34     LogicalAction action2 = new LogicalAction(12, new Object JavaDoc[] { "key", "value" });
35     PhysicalAction action3 = new PhysicalAction("class.field2", new ObjectID(3), true);
36     dnaWriter.addPhysicalAction(action1.getFieldName(), action1.getObject());
37     dnaWriter.addLogicalAction(action2.getMethod(), action2.getParameters());
38     dnaWriter.addPhysicalAction(action3.getFieldName(), action3.getObject());
39     dnaWriter.setParentObjectID(pid);
40     dnaWriter.setArrayLength(arrayLen);
41     dnaWriter.finalizeDNA();
42
43     TCByteBufferInputStream in = new TCByteBufferInputStream(out.toArray());
44     DNAImpl dna = createDNAImpl(serializer, true);
45     assertSame(dna, dna.deserializeFrom(in));
46     assertEquals(0, in.available());
47     
48     VersionizedDNAWrapper vdna = new VersionizedDNAWrapper(dna, 10);
49     assertEquals(10, vdna.getVersion());
50     try {
51       vdna.getCursor().reset();
52       assertTrue(false);
53     } catch(UnsupportedOperationException JavaDoc use) {
54       // this is expected
55
}
56     
57     vdna = new VersionizedDNAWrapper(dna, 10, true);
58     DNACursor cursor = vdna.getCursor();
59     cursor.reset();
60     assertTrue(cursor.next(encoding));
61     compareAction(action1, cursor.getPhysicalAction());
62     cursor.reset();
63     assertTrue(cursor.next(encoding));
64     compareAction(action1, cursor.getPhysicalAction());
65     assertTrue(cursor.next(encoding));
66     compareAction(action2, cursor.getLogicalAction());
67     cursor.reset();
68     assertTrue(cursor.next(encoding));
69     compareAction(action1, cursor.getPhysicalAction());
70     assertTrue(cursor.next(encoding));
71     compareAction(action2, cursor.getLogicalAction());
72     assertTrue(cursor.next(encoding));
73     compareAction(action3, cursor.getPhysicalAction());
74     assertFalse(cursor.next(encoding));
75     cursor.reset();
76     assertTrue(cursor.next(encoding));
77     compareAction(action1, cursor.getPhysicalAction());
78     assertTrue(cursor.next(encoding));
79     compareAction(action2, cursor.getLogicalAction());
80     assertTrue(cursor.next(encoding));
81     compareAction(action3, cursor.getPhysicalAction());
82     assertFalse(cursor.next(encoding));
83   }
84   
85   protected DNAImpl createDNAImpl(ObjectStringSerializer serializer, boolean b) {
86     return new DNAImpl(serializer, b);
87   }
88   
89   protected DNAWriter createDNAWriter(TCByteBufferOutputStream out, ObjectID id, String JavaDoc type,
90                                       ObjectStringSerializer serializer, DNAEncoding encoding, String JavaDoc string) {
91     return new DNAWriterImpl(out, id, type, serializer, encoding, "loader description", true);
92   }
93   
94   private void compareAction(LogicalAction expect, LogicalAction actual) {
95     assertEquals(expect.getMethod(), actual.getMethod());
96     assertTrue(Arrays.equals(expect.getParameters(), actual.getParameters()));
97   }
98
99   private void compareAction(PhysicalAction expect, PhysicalAction actual) {
100     assertEquals(expect.getFieldName(), actual.getFieldName());
101     assertEquals(expect.getObject(), actual.getObject());
102     assertEquals(expect.isReference(), actual.isReference());
103   }
104
105 }
106
Popular Tags