001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.exception.ImplementMe;
008: import com.tc.io.serializer.TCObjectInputStream;
009: import com.tc.io.serializer.TCObjectOutputStream;
010: import com.tc.object.ObjectID;
011: import com.tc.object.dna.api.DNACursor;
012: import com.tc.object.dna.api.DNAWriter;
013: import com.tc.object.dna.api.DNAEncoding;
014: import com.tc.object.dna.api.LiteralAction;
015: import com.tc.object.dna.api.LogicalAction;
016: import com.tc.object.dna.api.PhysicalAction;
017: import com.tc.objectserver.core.api.ManagedObjectState;
018: import com.tc.objectserver.managedobject.BackReferences;
019: import com.tc.objectserver.managedobject.ManagedObjectChangeListenerProvider;
020: import com.tc.objectserver.managedobject.ManagedObjectStateFactory;
021: import com.tc.objectserver.managedobject.NullManagedObjectChangeListenerProvider;
022: import com.tc.objectserver.persistence.impl.InMemoryPersistor;
023: import com.tc.util.Assert;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import junit.framework.TestCase;
033:
034: public class ManagedObjectStateSerialization15Test extends TestCase {
035: private static final String loaderDesc = "System.loader";
036:
037: private ObjectID objectID;
038: private ManagedObjectChangeListenerProvider listenerProvider;
039:
040: public void setUp() throws Exception {
041: super .setUp();
042: listenerProvider = new NullManagedObjectChangeListenerProvider();
043: ManagedObjectStateFactory.disableSingleton(true);
044: ManagedObjectStateFactory.createInstance(listenerProvider,
045: new InMemoryPersistor());
046: objectID = new ObjectID(2000);
047: }
048:
049: protected void tearDown() throws Exception {
050: super .tearDown();
051: ManagedObjectStateFactory.disableSingleton(false);
052: objectID = null;
053: listenerProvider = null;
054: }
055:
056: public void testEnum() throws Exception {
057: String className = "java.lang.Enum";
058: State state = State.RUN;
059: TestDNACursor cursor = new TestDNACursor();
060:
061: cursor.addLiteralAction(state);
062: ManagedObjectState managedObjectState = applyValidation(
063: className, cursor);
064: serializationValidation(managedObjectState, cursor,
065: ManagedObjectState.LITERAL_TYPE);
066: }
067:
068: private ManagedObjectState applyValidation(String className,
069: DNACursor dnaCursor) throws Exception {
070: ManagedObjectState state = apply(className, dnaCursor);
071: TestDNAWriter dnaWriter = dehydrate(state);
072: validate(dnaCursor, dnaWriter);
073:
074: return state;
075: }
076:
077: private void serializationValidation(ManagedObjectState state,
078: DNACursor dnaCursor, byte type) throws Exception {
079: byte[] buffer = writeTo(state);
080: TestDNAWriter dnaWriter = readFrom(type, buffer);
081: validate(dnaCursor, dnaWriter);
082: }
083:
084: private byte[] writeTo(ManagedObjectState state) throws Exception {
085: ByteArrayOutputStream bout = new ByteArrayOutputStream();
086: TCObjectOutputStream out = new TCObjectOutputStream(bout);
087: state.writeTo(out);
088:
089: return bout.toByteArray();
090: }
091:
092: private TestDNAWriter readFrom(byte type, byte[] buffer)
093: throws Exception {
094: ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
095: TCObjectInputStream in = new TCObjectInputStream(bin);
096:
097: ManagedObjectState state = ManagedObjectStateFactory
098: .getInstance().readManagedObjectStateFrom(in, type);
099: return dehydrate(state);
100: }
101:
102: private ManagedObjectState apply(String className,
103: DNACursor dnaCursor) throws Exception {
104: ManagedObjectState state = ManagedObjectStateFactory
105: .getInstance().createState(new ObjectID(1),
106: ObjectID.NULL_ID, className, loaderDesc,
107: dnaCursor);
108: state.apply(objectID, dnaCursor, new BackReferences());
109: return state;
110: }
111:
112: private TestDNAWriter dehydrate(ManagedObjectState state)
113: throws Exception {
114: TestDNAWriter dnaWriter = new TestDNAWriter();
115: state.dehydrate(objectID, dnaWriter);
116: return dnaWriter;
117: }
118:
119: private void validate(DNACursor dnaCursor, TestDNAWriter writer)
120: throws Exception {
121: Assert.assertEquals(dnaCursor.getActionCount(), writer
122: .getActionCount());
123: dnaCursor.reset();
124: while (dnaCursor.next()) {
125: Object action = dnaCursor.getAction();
126: Assert.assertTrue(writer.containsAction(action));
127: }
128: }
129:
130: public class TestDNAWriter implements DNAWriter {
131: private List physicalActions = new ArrayList();
132: private List logicalActions = new ArrayList();
133: private List literalActions = new ArrayList();
134:
135: public TestDNAWriter() {
136: //
137: }
138:
139: public void addLogicalAction(int method, Object[] parameters) {
140: logicalActions.add(new LogicalAction(method, parameters));
141: }
142:
143: public void addPhysicalAction(String field, Object value) {
144: addPhysicalAction(field, value, value instanceof ObjectID);
145: }
146:
147: public void finalizeDNA(boolean isDeltaDNA) {
148: //
149: }
150:
151: public void addArrayElementAction(int index, Object value) {
152: //
153: }
154:
155: public void addEntireArray(Object value) {
156: physicalActions.add(new PhysicalAction(value));
157: }
158:
159: public void addLiteralValue(Object value) {
160: literalActions.add(new LiteralAction(value));
161: }
162:
163: public void setParentObjectID(ObjectID id) {
164: //
165: }
166:
167: public void setArrayLength(int length) {
168: //
169: }
170:
171: public void addPhysicalAction(String fieldName, Object value,
172: boolean canBeReference) {
173: physicalActions.add(new PhysicalAction(fieldName, value,
174: canBeReference));
175: }
176:
177: public int getActionCount() {
178: return logicalActions.size() + physicalActions.size()
179: + literalActions.size();
180: }
181:
182: private boolean containsAction(Object targetAction) {
183: if (targetAction instanceof LogicalAction) {
184: return containsLogicalAction((LogicalAction) targetAction);
185: } else if (targetAction instanceof PhysicalAction) {
186: return containsPhysicalAction((PhysicalAction) targetAction);
187: } else if (targetAction instanceof LiteralAction) {
188: return containsLiteralAction((LiteralAction) targetAction);
189: }
190:
191: return false;
192: }
193:
194: private boolean containsLogicalAction(LogicalAction targetAction) {
195: for (Iterator i = logicalActions.iterator(); i.hasNext();) {
196: LogicalAction action = (LogicalAction) i.next();
197: if (identicalLogicalAction(targetAction, action)) {
198: return true;
199: }
200: }
201: return false;
202: }
203:
204: private boolean containsPhysicalAction(
205: PhysicalAction targetAction) {
206: for (Iterator i = physicalActions.iterator(); i.hasNext();) {
207: PhysicalAction action = (PhysicalAction) i.next();
208: if (identicalPhysicalAction(targetAction, action)) {
209: return true;
210: }
211: }
212: return false;
213: }
214:
215: private boolean containsLiteralAction(LiteralAction targetAction) {
216: for (Iterator i = literalActions.iterator(); i.hasNext();) {
217: LiteralAction action = (LiteralAction) i.next();
218: if (identicalLiteralAction(targetAction, action)) {
219: return true;
220: }
221: }
222: return false;
223: }
224:
225: private boolean identicalLiteralAction(LiteralAction a1,
226: LiteralAction a2) {
227: if (a1 == null || a2 == null) {
228: return false;
229: }
230: if (a1.getObject() == null || a2.getObject() == null) {
231: return false;
232: }
233:
234: return a1.getObject().equals(a2.getObject());
235: }
236:
237: private boolean identicalPhysicalAction(PhysicalAction a1,
238: PhysicalAction a2) {
239: if (a1 == null || a2 == null) {
240: return false;
241: }
242:
243: if (!a1.isEntireArray() && !a2.isEntireArray()) {
244: if (a1.getFieldName() == null
245: || a2.getFieldName() == null) {
246: return false;
247: }
248: }
249:
250: if (a1.isEntireArray() != a2.isEntireArray()) {
251: return false;
252: }
253:
254: if (a1.getObject() == null && a2.getObject() == null) {
255: return true;
256: }
257: if (a1.getObject() == null && a2.getObject() != null) {
258: return false;
259: }
260: if (a1.getObject() != null && a2.getObject() == null) {
261: return false;
262: }
263:
264: if (a1.isEntireArray()) {
265: return Arrays.equals((Object[]) a1.getObject(),
266: (Object[]) a2.getObject());
267: } else if (a1.getObject() instanceof Object[]
268: && a2.getObject() instanceof Object[]) {
269: return Arrays.equals((Object[]) a1.getObject(),
270: (Object[]) a2.getObject());
271: } else {
272: if (a1.getFieldName().equals(a2.getFieldName())) {
273: return (a1.getObject().equals(a2.getObject()));
274: }
275: }
276: return false;
277: }
278:
279: private boolean identicalLogicalAction(LogicalAction a1,
280: LogicalAction a2) {
281: if (a1 == null || a2 == null) {
282: return false;
283: }
284: if (a1.getParameters() == null
285: || a2.getParameters() == null) {
286: return false;
287: }
288:
289: if (a1.getMethod() == a2.getMethod()) {
290: if (a1.getParameters().length == a2.getParameters().length) {
291: for (int i = 0; i < a1.getParameters().length; i++) {
292: if (!a1.getParameters()[i].equals(a2
293: .getParameters()[i])) {
294: return false;
295: }
296: }
297: return true;
298: }
299: }
300: return false;
301: }
302:
303: public void addClassLoaderAction(String classLoaderFieldName,
304: Object value) {
305: //
306: }
307:
308: public void addSubArrayAction(int start, Object array,
309: int length) {
310: //
311: }
312: }
313:
314: public class TestDNACursor implements DNACursor {
315: private List actions = new ArrayList();
316: private int current = -1;
317:
318: public void addPhysicalAction(String addFieldName,
319: Object addObj, boolean isref) {
320: actions
321: .add(new PhysicalAction(addFieldName, addObj, isref));
322: }
323:
324: public void addLogicalAction(int method, Object params[]) {
325: actions.add(new LogicalAction(method, params));
326: }
327:
328: public void addArrayAction(Object[] objects) {
329: actions.add(new PhysicalAction(objects));
330: }
331:
332: public void addLiteralAction(Object value) {
333: actions.add(new LiteralAction(value));
334: }
335:
336: public boolean next() {
337: return actions.size() > ++current;
338: }
339:
340: public LogicalAction getLogicalAction() {
341: return (LogicalAction) actions.get(current);
342: }
343:
344: public Object getAction() {
345: return actions.get(current);
346: }
347:
348: public PhysicalAction getPhysicalAction() {
349: return (PhysicalAction) actions.get(current);
350: }
351:
352: public boolean next(DNAEncoding encoding) {
353: throw new ImplementMe();
354: }
355:
356: public int getActionCount() {
357: return actions.size();
358: }
359:
360: public void reset() throws UnsupportedOperationException {
361: current = -1;
362: }
363: }
364:
365: public interface EnumIntf {
366: public int getStateNum();
367:
368: public void setStateNum(int stateNum);
369: }
370:
371: public enum State implements EnumIntf {
372: START(0), RUN(1), STOP(2);
373:
374: private int stateNum;
375:
376: State(int stateNum) {
377: this .stateNum = stateNum;
378: }
379:
380: public int getStateNum() {
381: return this .stateNum;
382: }
383:
384: public void setStateNum(int stateNum) {
385: this.stateNum = stateNum;
386: }
387: }
388:
389: }
|