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.change;
005:
006: import com.tc.io.TCByteBufferInputStream;
007: import com.tc.io.TCByteBufferOutputStream;
008: import com.tc.object.MockTCObject;
009: import com.tc.object.ObjectID;
010: import com.tc.object.SerializationUtil;
011: import com.tc.object.bytecode.MockClassProvider;
012: import com.tc.object.dna.api.DNACursor;
013: import com.tc.object.dna.api.DNAEncoding;
014: import com.tc.object.dna.api.LogicalAction;
015: import com.tc.object.dna.api.PhysicalAction;
016: import com.tc.object.dna.impl.DNAEncodingImpl;
017: import com.tc.object.dna.impl.DNAImpl;
018: import com.tc.object.dna.impl.ObjectStringSerializer;
019: import com.tc.object.loaders.ClassProvider;
020:
021: import junit.framework.TestCase;
022:
023: public class TCChangeBufferTest extends TestCase {
024:
025: public void testLogicalClassIgnoresPhysicalChanges()
026: throws Exception {
027: ObjectStringSerializer serializer = new ObjectStringSerializer();
028: ClassProvider classProvider = new MockClassProvider();
029: DNAEncoding encoding = new DNAEncodingImpl(classProvider);
030: MockTCObject mockTCObject = new MockTCObject(new ObjectID(1),
031: this , false, true);
032: mockTCObject.setDehydrateReturnValue(false);
033: TCChangeBuffer buffer = new TCChangeBufferImpl(mockTCObject);
034:
035: // physical updates should be ignored
036: buffer.fieldChanged("classname", "fieldname", new ObjectID(12),
037: -1);
038: buffer.fieldChanged("classname", "fieldname", new Long(3), -1);
039:
040: buffer.logicalInvoke(SerializationUtil.PUT, new Object[] {
041: new ObjectID(1), new ObjectID(2) });
042:
043: TCByteBufferOutputStream output = new TCByteBufferOutputStream();
044: buffer.writeTo(output, serializer, encoding);
045: output.close();
046:
047: DNAImpl dna = new DNAImpl(serializer, true);
048: dna.deserializeFrom(new TCByteBufferInputStream(output
049: .toArray()));
050:
051: int count = 0;
052: DNACursor cursor = dna.getCursor();
053: while (cursor.next(encoding)) {
054: count++;
055: LogicalAction action = dna.getLogicalAction();
056:
057: if (action.getMethod() == SerializationUtil.PUT) {
058: assertEquals(new ObjectID(1), action.getParameters()[0]);
059: assertEquals(new ObjectID(2), action.getParameters()[1]);
060: } else {
061: fail("method was " + action.getMethod());
062: }
063: }
064:
065: assertEquals(1, count);
066: }
067:
068: public void testLastPhysicalChangeWins() throws Exception {
069: ObjectStringSerializer serializer = new ObjectStringSerializer();
070: ClassProvider classProvider = new MockClassProvider();
071: DNAEncoding encoding = new DNAEncodingImpl(classProvider);
072: MockTCObject mockTCObject = new MockTCObject(new ObjectID(1),
073: this );
074: mockTCObject.setDehydrateReturnValue(false);
075: TCChangeBuffer buffer = new TCChangeBufferImpl(mockTCObject);
076:
077: for (int i = 0; i < 100; i++) {
078: buffer.fieldChanged("class", "class.field",
079: new ObjectID(i), -1);
080: }
081:
082: TCByteBufferOutputStream output = new TCByteBufferOutputStream();
083: buffer.writeTo(output, serializer, encoding);
084: output.close();
085:
086: DNAImpl dna = new DNAImpl(serializer, true);
087: dna.deserializeFrom(new TCByteBufferInputStream(output
088: .toArray()));
089:
090: int count = 0;
091: DNACursor cursor = dna.getCursor();
092: while (cursor.next(encoding)) {
093: count++;
094: PhysicalAction action = dna.getPhysicalAction();
095:
096: if (action.isTruePhysical()
097: && action.getFieldName().equals("class.field")) {
098: assertEquals(new ObjectID(99), action.getObject());
099: } else {
100: fail();
101: }
102: }
103:
104: assertEquals(1, count);
105: }
106:
107: public void testLastArrayChangeWins() throws Exception {
108: ObjectStringSerializer serializer = new ObjectStringSerializer();
109: ClassProvider classProvider = new MockClassProvider();
110: DNAEncoding encoding = new DNAEncodingImpl(classProvider);
111: MockTCObject mockTCObject = new MockTCObject(new ObjectID(1),
112: this , true, false);
113: mockTCObject.setDehydrateReturnValue(false);
114: TCChangeBuffer buffer = new TCChangeBufferImpl(mockTCObject);
115:
116: for (int i = 0; i < 100; i++) {
117: buffer.fieldChanged("class", "class.arrayField",
118: new ObjectID(1000 + i), 1);
119: }
120:
121: TCByteBufferOutputStream output = new TCByteBufferOutputStream();
122: buffer.writeTo(output, serializer, encoding);
123: output.close();
124:
125: DNAImpl dna = new DNAImpl(serializer, true);
126: dna.deserializeFrom(new TCByteBufferInputStream(output
127: .toArray()));
128:
129: int count = 0;
130: DNACursor cursor = dna.getCursor();
131: while (cursor.next(encoding)) {
132: count++;
133: PhysicalAction action = dna.getPhysicalAction();
134:
135: if (action.isArrayElement() && action.getArrayIndex() == 1) {
136: assertEquals(new ObjectID(1099), action.getObject());
137: } else {
138: fail();
139: }
140: }
141:
142: assertEquals(1, count);
143: }
144:
145: }
|