1 4 package com.tctest; 5 6 import com.tc.exception.ImplementMe; 7 import com.tc.io.serializer.TCObjectInputStream; 8 import com.tc.io.serializer.TCObjectOutputStream; 9 import com.tc.object.ObjectID; 10 import com.tc.object.dna.api.DNACursor; 11 import com.tc.object.dna.api.DNAWriter; 12 import com.tc.object.dna.api.LiteralAction; 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.objectserver.core.api.ManagedObjectState; 17 import com.tc.objectserver.managedobject.BackReferences; 18 import com.tc.objectserver.managedobject.ManagedObjectChangeListenerProvider; 19 import com.tc.objectserver.managedobject.ManagedObjectStateFactory; 20 import com.tc.objectserver.managedobject.NullManagedObjectChangeListenerProvider; 21 import com.tc.objectserver.persistence.impl.InMemoryPersistor; 22 import com.tc.util.Assert; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import junit.framework.TestCase; 32 33 public class ManagedObjectStateSerialization15Test extends TestCase { 34 private static final String loaderDesc = "System.loader"; 35 36 private ObjectID objectID; 37 private ManagedObjectChangeListenerProvider listenerProvider; 38 39 public void setUp() throws Exception { 40 super.setUp(); 41 listenerProvider = new NullManagedObjectChangeListenerProvider(); 42 ManagedObjectStateFactory.disableSingleton(true); 43 ManagedObjectStateFactory.createInstance(listenerProvider, new InMemoryPersistor()); 44 objectID = new ObjectID(2000); 45 } 46 47 protected void tearDown() throws Exception { 48 super.tearDown(); 49 ManagedObjectStateFactory.disableSingleton(false); 50 objectID = null; 51 listenerProvider = null; 52 } 53 54 public void testEnum() throws Exception { 55 String className = "java.lang.Enum"; 56 State state = State.RUN; 57 TestDNACursor cursor = new TestDNACursor(); 58 59 cursor.addLiteralAction(state); 60 ManagedObjectState managedObjectState = applyValidation(className, cursor); 61 serializationValidation(managedObjectState, cursor, ManagedObjectState.LITERAL_TYPE); 62 } 63 64 private ManagedObjectState applyValidation(String className, DNACursor dnaCursor) throws Exception { 65 ManagedObjectState state = apply(className, dnaCursor); 66 TestDNAWriter dnaWriter = dehydrate(state); 67 validate(dnaCursor, dnaWriter); 68 69 return state; 70 } 71 72 private void serializationValidation(ManagedObjectState state, DNACursor dnaCursor, byte type) throws Exception { 73 byte[] buffer = writeTo(state); 74 TestDNAWriter dnaWriter = readFrom(type, buffer); 75 validate(dnaCursor, dnaWriter); 76 } 77 78 private byte[] writeTo(ManagedObjectState state) throws Exception { 79 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 80 TCObjectOutputStream out = new TCObjectOutputStream(bout); 81 state.writeTo(out); 82 83 return bout.toByteArray(); 84 } 85 86 private TestDNAWriter readFrom(byte type, byte[] buffer) throws Exception { 87 ByteArrayInputStream bin = new ByteArrayInputStream (buffer); 88 TCObjectInputStream in = new TCObjectInputStream(bin); 89 90 ManagedObjectState state = ManagedObjectStateFactory.getInstance().readManagedObjectStateFrom(in, type); 91 return dehydrate(state); 92 } 93 94 private ManagedObjectState apply(String className, DNACursor dnaCursor) throws Exception { 95 ManagedObjectState state = ManagedObjectStateFactory.getInstance().createState(new ObjectID(1), ObjectID.NULL_ID, 96 className, loaderDesc, dnaCursor); 97 state.apply(objectID, dnaCursor, new BackReferences()); 98 return state; 99 } 100 101 private TestDNAWriter dehydrate(ManagedObjectState state) throws Exception { 102 TestDNAWriter dnaWriter = new TestDNAWriter(); 103 state.dehydrate(objectID, dnaWriter); 104 return dnaWriter; 105 } 106 107 private void validate(DNACursor dnaCursor, TestDNAWriter writer) throws Exception { 108 Assert.assertEquals(dnaCursor.getActionCount(), writer.getActionCount()); 109 dnaCursor.reset(); 110 while (dnaCursor.next()) { 111 Object action = dnaCursor.getAction(); 112 Assert.assertTrue(writer.containsAction(action)); 113 } 114 } 115 116 public class TestDNAWriter implements DNAWriter { 117 private List physicalActions = new ArrayList (); 118 private List logicalActions = new ArrayList (); 119 private List literalActions = new ArrayList (); 120 121 public TestDNAWriter() { 122 } 124 125 public void addLogicalAction(int method, Object [] parameters) { 126 logicalActions.add(new LogicalAction(method, parameters)); 127 } 128 129 public void addPhysicalAction(String field, Object value) { 130 addPhysicalAction(field, value, value instanceof ObjectID); 131 } 132 133 public void finalizeDNA() { 134 } 136 137 public void addArrayElementAction(int index, Object value) { 138 } 140 141 public void addEntireArray(Object value) { 142 physicalActions.add(new PhysicalAction(value)); 143 } 144 145 public void addLiteralValue(Object value) { 146 literalActions.add(new LiteralAction(value)); 147 } 148 149 public void setParentObjectID(ObjectID id) { 150 } 152 153 public void setArrayLength(int length) { 154 } 156 157 public void addPhysicalAction(String fieldName, Object value, boolean canBeReference) { 158 physicalActions.add(new PhysicalAction(fieldName, value, canBeReference)); 159 } 160 161 public int getActionCount() { 162 return logicalActions.size() + physicalActions.size() + literalActions.size(); 163 } 164 165 private boolean containsAction(Object targetAction) { 166 if (targetAction instanceof LogicalAction) { 167 return containsLogicalAction((LogicalAction) targetAction); 168 } else if (targetAction instanceof PhysicalAction) { 169 return containsPhysicalAction((PhysicalAction) targetAction); 170 } else if (targetAction instanceof LiteralAction) { return containsLiteralAction((LiteralAction) targetAction); } 171 172 return false; 173 } 174 175 private boolean containsLogicalAction(LogicalAction targetAction) { 176 for (Iterator i = logicalActions.iterator(); i.hasNext();) { 177 LogicalAction action = (LogicalAction) i.next(); 178 if (identicalLogicalAction(targetAction, action)) { return true; } 179 } 180 return false; 181 } 182 183 private boolean containsPhysicalAction(PhysicalAction targetAction) { 184 for (Iterator i = physicalActions.iterator(); i.hasNext();) { 185 PhysicalAction action = (PhysicalAction) i.next(); 186 if (identicalPhysicalAction(targetAction, action)) { return true; } 187 } 188 return false; 189 } 190 191 private boolean containsLiteralAction(LiteralAction targetAction) { 192 for (Iterator i = literalActions.iterator(); i.hasNext();) { 193 LiteralAction action = (LiteralAction) i.next(); 194 if (identicalLiteralAction(targetAction, action)) { return true; } 195 } 196 return false; 197 } 198 199 private boolean identicalLiteralAction(LiteralAction a1, LiteralAction a2) { 200 if (a1 == null || a2 == null) { return false; } 201 if (a1.getObject() == null || a2.getObject() == null) { return false; } 202 203 return a1.getObject().equals(a2.getObject()); 204 } 205 206 private boolean identicalPhysicalAction(PhysicalAction a1, PhysicalAction a2) { 207 if (a1 == null || a2 == null) { return false; } 208 209 if (!a1.isEntireArray() && !a2.isEntireArray()) { 210 if (a1.getFieldName() == null || a2.getFieldName() == null) { return false; } 211 } 212 213 if (a1.isEntireArray() != a2.isEntireArray()) { return false; } 214 215 if (a1.getObject() == null && a2.getObject() == null) { return true; } 216 if (a1.getObject() == null && a2.getObject() != null) { return false; } 217 if (a1.getObject() != null && a2.getObject() == null) { return false; } 218 219 if (a1.isEntireArray()) { 220 return Arrays.equals((Object []) a1.getObject(), (Object []) a2.getObject()); 221 } else if (a1.getObject() instanceof Object [] && a2.getObject() instanceof Object []) { 222 return Arrays.equals((Object []) a1.getObject(), (Object []) a2.getObject()); 223 } else { 224 if (a1.getFieldName().equals(a2.getFieldName())) { return (a1.getObject().equals(a2.getObject())); } 225 } 226 return false; 227 } 228 229 private boolean identicalLogicalAction(LogicalAction a1, LogicalAction a2) { 230 if (a1 == null || a2 == null) { return false; } 231 if (a1.getParameters() == null || a2.getParameters() == null) { return false; } 232 233 if (a1.getMethod() == a2.getMethod()) { 234 if (a1.getParameters().length == a2.getParameters().length) { 235 for (int i = 0; i < a1.getParameters().length; i++) { 236 if (!a1.getParameters()[i].equals(a2.getParameters()[i])) { return false; } 237 } 238 return true; 239 } 240 } 241 return false; 242 } 243 244 public void addClassLoaderAction(String classLoaderFieldName, Object value) { 245 } 247 248 public void addSubArrayAction(int start, Object array, int length) { 249 } 251 } 252 253 public class TestDNACursor implements DNACursor { 254 private List actions = new ArrayList (); 255 private int current = -1; 256 257 public void addPhysicalAction(String addFieldName, Object addObj, boolean isref) { 258 actions.add(new PhysicalAction(addFieldName, addObj, isref)); 259 } 260 261 public void addLogicalAction(int method, Object params[]) { 262 actions.add(new LogicalAction(method, params)); 263 } 264 265 public void addArrayAction(Object [] objects) { 266 actions.add(new PhysicalAction(objects)); 267 } 268 269 public void addLiteralAction(Object value) { 270 actions.add(new LiteralAction(value)); 271 } 272 273 public boolean next() { 274 return actions.size() > ++current; 275 } 276 277 public LogicalAction getLogicalAction() { 278 return (LogicalAction) actions.get(current); 279 } 280 281 public Object getAction() { 282 return actions.get(current); 283 } 284 285 public PhysicalAction getPhysicalAction() { 286 return (PhysicalAction) actions.get(current); 287 } 288 289 public boolean next(DNAEncoding encoding) { 290 throw new ImplementMe(); 291 } 292 293 public int getActionCount() { 294 return actions.size(); 295 } 296 297 public void reset() throws UnsupportedOperationException { 298 current = -1; 299 } 300 } 301 302 public interface EnumIntf { 303 public int getStateNum(); 304 305 public void setStateNum(int stateNum); 306 } 307 308 public enum State implements EnumIntf { 309 START(0), RUN(1), STOP(2); 310 311 private int stateNum; 312 313 State(int stateNum) { 314 this.stateNum = stateNum; 315 } 316 317 public int getStateNum() { 318 return this.stateNum; 319 } 320 321 public void setStateNum(int stateNum) { 322 this.stateNum = stateNum; 323 } 324 } 325 326 } 327
| Popular Tags
|