KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > change > TCChangeBufferTest


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.change;
5
6 import com.tc.io.TCByteBufferInputStream;
7 import com.tc.io.TCByteBufferOutputStream;
8 import com.tc.object.MockTCObject;
9 import com.tc.object.ObjectID;
10 import com.tc.object.SerializationUtil;
11 import com.tc.object.bytecode.MockClassProvider;
12 import com.tc.object.dna.api.DNACursor;
13 import com.tc.object.dna.api.LogicalAction;
14 import com.tc.object.dna.api.PhysicalAction;
15 import com.tc.object.dna.impl.DNAEncoding;
16 import com.tc.object.dna.impl.DNAImpl;
17 import com.tc.object.dna.impl.ObjectStringSerializer;
18 import com.tc.object.loaders.ClassProvider;
19
20 import junit.framework.TestCase;
21
22 public class TCChangeBufferTest extends TestCase {
23
24   public void testLogicalClassIgnoresPhysicalChanges() throws Exception JavaDoc {
25     ObjectStringSerializer serializer = new ObjectStringSerializer();
26     ClassProvider classProvider = new MockClassProvider();
27     DNAEncoding encoding = new DNAEncoding(classProvider);
28     TCChangeBuffer buffer = new TCChangeBufferImpl(new MockTCObject(new ObjectID(1), this, false, true));
29
30     // physical updates should be ignored
31
buffer.fieldChanged("classname", "fieldname", new ObjectID(12), -1);
32     buffer.fieldChanged("classname", "fieldname", new Long JavaDoc(3), -1);
33
34     buffer.logicalInvoke(SerializationUtil.PUT, new Object JavaDoc[] { new ObjectID(1), new ObjectID(2) });
35
36     TCByteBufferOutputStream output = new TCByteBufferOutputStream();
37     buffer.writeTo(output, serializer, encoding);
38     output.close();
39
40     DNAImpl dna = new DNAImpl(serializer, true);
41     dna.deserializeFrom(new TCByteBufferInputStream(output.toArray()));
42
43     int count = 0;
44     DNACursor cursor = dna.getCursor();
45     while (cursor.next(encoding)) {
46       count++;
47       LogicalAction action = dna.getLogicalAction();
48
49       if (action.getMethod() == SerializationUtil.PUT) {
50         assertEquals(new ObjectID(1), action.getParameters()[0]);
51         assertEquals(new ObjectID(2), action.getParameters()[1]);
52       } else {
53         fail("method was " + action.getMethod());
54       }
55     }
56
57     assertEquals(1, count);
58   }
59
60   public void testLastPhysicalChangeWins() throws Exception JavaDoc {
61     ObjectStringSerializer serializer = new ObjectStringSerializer();
62     ClassProvider classProvider = new MockClassProvider();
63     DNAEncoding encoding = new DNAEncoding(classProvider);
64     TCChangeBuffer buffer = new TCChangeBufferImpl(new MockTCObject(new ObjectID(1), this));
65
66     for (int i = 0; i < 100; i++) {
67       buffer.fieldChanged("class", "class.field", new ObjectID(i), -1);
68     }
69
70     TCByteBufferOutputStream output = new TCByteBufferOutputStream();
71     buffer.writeTo(output, serializer, encoding);
72     output.close();
73
74     DNAImpl dna = new DNAImpl(serializer, true);
75     dna.deserializeFrom(new TCByteBufferInputStream(output.toArray()));
76
77     int count = 0;
78     DNACursor cursor = dna.getCursor();
79     while (cursor.next(encoding)) {
80       count++;
81       PhysicalAction action = dna.getPhysicalAction();
82
83       if (action.isTruePhysical() && action.getFieldName().equals("class.field")) {
84         assertEquals(new ObjectID(99), action.getObject());
85       } else {
86         fail();
87       }
88     }
89
90     assertEquals(1, count);
91   }
92
93   public void testLastArrayChangeWins() throws Exception JavaDoc {
94     ObjectStringSerializer serializer = new ObjectStringSerializer();
95     ClassProvider classProvider = new MockClassProvider();
96     DNAEncoding encoding = new DNAEncoding(classProvider);
97     TCChangeBuffer buffer = new TCChangeBufferImpl(new MockTCObject(new ObjectID(1), this, true, false));
98
99     for (int i = 0; i < 100; i++) {
100       buffer.fieldChanged("class", "class.arrayField", new ObjectID(1000 + i), 1);
101     }
102
103     TCByteBufferOutputStream output = new TCByteBufferOutputStream();
104     buffer.writeTo(output, serializer, encoding);
105     output.close();
106
107     DNAImpl dna = new DNAImpl(serializer, true);
108     dna.deserializeFrom(new TCByteBufferInputStream(output.toArray()));
109
110     int count = 0;
111     DNACursor cursor = dna.getCursor();
112     while (cursor.next(encoding)) {
113       count++;
114       PhysicalAction action = dna.getPhysicalAction();
115
116       if (action.isArrayElement() && action.getArrayIndex() == 1) {
117         assertEquals(new ObjectID(1099), action.getObject());
118       } else {
119         fail();
120       }
121     }
122
123     assertEquals(1, count);
124   }
125
126 }
127
Popular Tags