KickJava   Java API By Example, From Geeks To Geeks.

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


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 DNAImplTest extends TestCase {
21
22   protected DNAImpl dna;
23
24   public void testSerializeDeserialize() throws Exception JavaDoc {
25     TCByteBufferOutputStream out = new TCByteBufferOutputStream();
26
27     final ObjectID id = new ObjectID(1);
28     final ObjectID pid = new ObjectID(2);
29     final String JavaDoc type = getClass().getName();
30     final int arrayLen = 42;
31     ObjectStringSerializer serializer = new ObjectStringSerializer();
32     ClassProvider classProvider = new MockClassProvider();
33     DNAEncoding encoding = new DNAEncoding(classProvider);
34     DNAWriter dnaWriter = createDNAWriter(out, id, type, serializer, encoding, "loader description");
35     PhysicalAction action1 = new PhysicalAction("class.field1", new Integer JavaDoc(1), false);
36     LogicalAction action2 = new LogicalAction(12, new Object JavaDoc[] { "key", "value" });
37     PhysicalAction action3 = new PhysicalAction("class.field2", new ObjectID(3), true);
38     dnaWriter.addPhysicalAction(action1.getFieldName(), action1.getObject());
39     dnaWriter.addLogicalAction(action2.getMethod(), action2.getParameters());
40     dnaWriter.addPhysicalAction(action3.getFieldName(), action3.getObject());
41     dnaWriter.setParentObjectID(pid);
42     dnaWriter.setArrayLength(arrayLen);
43     dnaWriter.finalizeDNA();
44
45     TCByteBufferInputStream in = new TCByteBufferInputStream(out.toArray());
46     dna = createDNAImpl(serializer, true);
47     assertSame(dna, dna.deserializeFrom(in));
48     assertEquals(0, in.available());
49     DNACursor cursor = dna.getCursor();
50     int count = 1;
51     while (cursor.next(encoding)) {
52       switch (count) {
53         case 1:
54           compareAction(action1, cursor.getPhysicalAction());
55           break;
56         case 2:
57           compareAction(action2, cursor.getLogicalAction());
58           break;
59         case 3:
60           compareAction(action3, cursor.getPhysicalAction());
61           break;
62         default:
63           fail("count got to " + count);
64       }
65       count++;
66     }
67
68     assertEquals(id, dna.getObjectID());
69     assertEquals(pid, dna.getParentObjectID());
70     assertEquals(type, dna.getTypeName());
71     assertEquals(arrayLen, dna.getArraySize());
72     assertOverridable();
73   }
74
75   protected void assertOverridable() {
76     assertTrue(dna.isDelta());
77   }
78
79   protected DNAImpl createDNAImpl(ObjectStringSerializer serializer, boolean b) {
80     return new DNAImpl(serializer, b);
81   }
82
83   protected DNAWriter createDNAWriter(TCByteBufferOutputStream out, ObjectID id, String JavaDoc type,
84                                       ObjectStringSerializer serializer, DNAEncoding encoding, String JavaDoc string) {
85     return new DNAWriterImpl(out, id, type, serializer, encoding, "loader description", true);
86   }
87
88   private void compareAction(LogicalAction expect, LogicalAction actual) {
89     assertEquals(expect.getMethod(), actual.getMethod());
90     assertTrue(Arrays.equals(expect.getParameters(), actual.getParameters()));
91   }
92
93   private void compareAction(PhysicalAction expect, PhysicalAction actual) {
94     assertEquals(expect.getFieldName(), actual.getFieldName());
95     assertEquals(expect.getObject(), actual.getObject());
96     assertEquals(expect.isReference(), actual.isReference());
97   }
98
99 }
100
Popular Tags