1 4 package com.tc.objectserver.managedobject; 5 6 import com.tc.object.ObjectID; 7 import com.tc.objectserver.core.api.TestDNACursor; 8 import com.tc.objectserver.managedobject.bytecode.ClassNotCompatableException; 9 import com.tc.objectserver.persistence.impl.InMemoryPersistor; 10 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import junit.framework.TestCase; 19 20 public class ManagedObjectStateTest extends TestCase { 21 22 private ObjectID objectID; 23 private ManagedObjectChangeListenerProvider listenerProvider; 24 25 public void setUp() throws Exception { 26 objectID = new ObjectID(2002); 27 listenerProvider = new NullManagedObjectChangeListenerProvider(); 28 } 29 30 public void testPhysicalManagedObjectClassId() throws Exception { 31 String loaderDesc = "System.loader"; 32 33 ManagedObjectStateFactory.disableSingleton(true); 34 ManagedObjectStateFactory.createInstance(listenerProvider, new InMemoryPersistor()); 35 36 HashMap types = new HashMap (); 37 types.put("field1", new ObjectID(1)); 38 types.put("field2", new Boolean (true)); 39 types.put("field3", new Character ('c')); 40 types.put("field4", new Long (5)); 41 types.put("field5", new Integer (15)); 42 types.put("field6", new String ("neoistheone")); 43 types.put("field7", new Short ((short) 606)); 44 types.put("field8", new Double (98.76d)); 45 types.put("field9", new Float (8.8f)); 46 49 TestDNACursor cursor = new TestDNACursor(); 50 for (Iterator i = types.entrySet().iterator(); i.hasNext();) { 51 Map.Entry element = (Map.Entry ) i.next(); 52 cursor.addPhysicalAction((String ) element.getKey(), element.getValue()); 53 } 54 55 int numOfGeneratedClasses = 10000; 56 Map classNameToClassId = new HashMap (); 57 Map classIdToClassName = new HashMap (); 58 59 for (int i = 0; i < numOfGeneratedClasses; i++) { 60 String className = "com.xxx.SomeClassName" + i; 61 PhysicalManagedObjectState state = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 62 .createState(new ObjectID(1), ObjectID.NULL_ID, className, loaderDesc, cursor); 63 state.apply(objectID, cursor, new BackReferences()); 64 65 int classId = state.getClassId(); 66 assertTrue(classId != 0); 67 assertEquals(state.getClassName(), className); 68 assertEquals(state.getLoaderDescription(), loaderDesc); 69 70 classNameToClassId.put(className, new Integer (classId)); 71 assertNull(classIdToClassName.get(new Integer (classId))); 72 classIdToClassName.put(new Integer (classId), className); 73 } 74 75 for (int i = 0; i < numOfGeneratedClasses; i++) { 76 String className = "com.xxx.SomeClassName" + i; 77 PhysicalManagedObjectState state = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 78 .createState(new ObjectID(1), ObjectID.NULL_ID, className, loaderDesc, cursor); 79 state.apply(objectID, cursor, new BackReferences()); 80 81 int classId = state.getClassId(); 82 83 Integer storedClassId = (Integer )classNameToClassId.get(className); 84 assertEquals(classId, storedClassId.intValue()); 85 86 String storedClassName = (String )classIdToClassName.get(new Integer (classId)); 87 assertEquals(className, storedClassName); 88 89 } 90 } 91 92 public void testPhysicalManagedObjectState() throws Exception { 93 String loaderDesc = "System.loader"; 94 95 ManagedObjectStateFactory.disableSingleton(true); 96 ManagedObjectStateFactory.createInstance(listenerProvider, new InMemoryPersistor()); 97 98 HashMap types = new HashMap (); 99 types.put("field1", new ObjectID(1)); 100 types.put("field2", new Boolean (true)); 101 types.put("field3", new Character ('c')); 102 types.put("field4", new Long (5)); 103 types.put("field5", new Integer (15)); 104 types.put("field6", new String ("neoistheone")); 105 types.put("field7", new Short ((short) 606)); 106 types.put("field8", new Double (98.76d)); 107 types.put("field9", new Float (8.8f)); 108 111 TestDNACursor cursor = new TestDNACursor(); 112 for (Iterator i = types.entrySet().iterator(); i.hasNext();) { 113 Map.Entry element = (Map.Entry ) i.next(); 114 cursor.addPhysicalAction((String ) element.getKey(), element.getValue()); 115 } 116 117 PhysicalManagedObjectState state = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 118 .createState(new ObjectID(1), ObjectID.NULL_ID, "com.xxx.SomeClassName", loaderDesc, cursor); 119 state.apply(objectID, cursor, new BackReferences()); 120 121 assertTrue(state.getClassId() != 0); 122 assertEquals(state.getClassName(), "com.xxx.SomeClassName"); 123 assertEquals(state.getLoaderDescription(), loaderDesc); 124 125 Collection references = state.getObjectReferences(); 126 assertEquals(1, references.size()); 127 assertEquals(new ObjectID(1), references.iterator().next()); 128 129 Map values = state.addValues(new HashMap ()); 130 assertEquals(types.size(), values.size()); 131 assertEquals(new ObjectID(1), values.get("field1")); 132 133 Boolean l2 = (Boolean ) values.get("field2"); 134 assertTrue(l2.booleanValue()); 135 Character f3 = (Character ) values.get("field3"); 136 assertEquals(f3.charValue(), 'c'); 137 138 System.err.println("values =" + values); 139 assertEquals(types, values); 140 141 cursor = new TestDNACursor(); 142 cursor.addPhysicalAction("field1", new ObjectID(2)); 143 cursor.addPhysicalAction("field2", new Boolean (false)); 144 cursor.addPhysicalAction("field3", new Character ('d')); 145 146 state.apply(objectID, cursor, new BackReferences()); 147 148 references = state.getObjectReferences(); 149 assertEquals(1, references.size()); 150 assertEquals(new ObjectID(2), references.iterator().next()); 151 152 values = state.addValues(new HashMap ()); 153 assertEquals(types.size(), values.size()); 154 assertEquals(new ObjectID(2), values.get("field1")); 155 156 l2 = (Boolean ) values.get("field2"); 157 assertFalse(l2.booleanValue()); 158 f3 = (Character ) values.get("field3"); 159 assertEquals(f3.charValue(), 'd'); 160 161 assertEquals(ObjectID.NULL_ID, state.getParentID()); 162 } 163 164 public void testPhysicalMOStateClassInCompatibility() throws Exception { 165 String loaderDesc = "System.loader"; 166 167 InMemoryPersistor persistor = new InMemoryPersistor(); 168 ManagedObjectStateFactory.disableSingleton(true); 169 ManagedObjectStateFactory.createInstance(listenerProvider, persistor); 170 171 TestDNACursor cursor = new TestDNACursor(); 172 cursor.addPhysicalAction("field1", new ObjectID(1)); 173 cursor.addPhysicalAction("field2", new Long (11)); 174 cursor.addPhysicalAction("field3", new String ("neoistheone")); 175 176 PhysicalManagedObjectState state = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 177 .createState(objectID, ObjectID.NULL_ID, "com.xxx.SomeClassName", loaderDesc, cursor); 178 state.apply(objectID, cursor, new BackReferences()); 179 180 assertTrue(state.getClassId() != 0); 181 assertEquals(state.getClassName(), "com.xxx.SomeClassName"); 182 assertEquals(state.getLoaderDescription(), loaderDesc); 183 184 Collection references = state.getObjectReferences(); 185 assertEquals(1, references.size()); 186 assertEquals(new ObjectID(1), references.iterator().next()); 187 188 Map values = state.addValues(new HashMap ()); 189 assertEquals(3, values.size()); 190 assertEquals(new ObjectID(1), values.get("field1")); 191 192 Long l2 = (Long ) values.get("field2"); 193 assertEquals(11, l2.longValue()); 194 String f3 = (String ) values.get("field3"); 195 assertEquals(f3, "neoistheone"); 196 197 cursor = new TestDNACursor(); 198 cursor.addPhysicalAction("field1", new ObjectID(2)); 199 cursor.addPhysicalAction("field4", new Boolean (false)); 201 cursor.addPhysicalAction("field5", new ObjectID(22)); 202 203 try { 204 state.apply(objectID, cursor, new BackReferences()); 205 assertTrue(false); 206 } catch (ClassNotCompatableException cfe) { 207 } 209 210 PhysicalManagedObjectState state1 = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 212 .recreateState(objectID, ObjectID.NULL_ID, "com.xxx.SomeClassName", loaderDesc, cursor, state); 213 state1.apply(objectID, cursor, new BackReferences()); 214 215 assertTrue(state1.getClassId() != state.getClassId()); 216 assertEquals(state1.getClassName(), "com.xxx.SomeClassName"); 217 assertEquals(state1.getLoaderDescription(), loaderDesc); 218 219 references = state1.getObjectReferences(); 220 assertEquals(2, references.size()); 221 Set expectedRefs = new HashSet (); 222 expectedRefs.add(new ObjectID(2)); 223 expectedRefs.add(new ObjectID(22)); 224 225 values = state1.addValues(new HashMap ()); 226 assertEquals(new ObjectID(2), values.get("field1")); 227 l2 = (Long ) values.get("field2"); 228 assertEquals(11, l2.longValue()); 229 f3 = (String ) values.get("field3"); 230 assertEquals(f3, "neoistheone"); 231 Boolean f4 = (Boolean ) values.get("field4"); 232 assertEquals(f4, new Boolean (false)); 233 assertEquals(new ObjectID(22), values.get("field5")); 234 235 TestDNACursor cursor2 = new TestDNACursor(); 237 cursor2.addPhysicalAction("field6", new Integer (2), false); 238 239 try { 240 state.apply(objectID, cursor2, new BackReferences()); 241 assertTrue(false); 242 } catch (ClassNotCompatableException cfe) { 243 } 245 246 PhysicalManagedObjectState state2 = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 248 .recreateState(objectID, ObjectID.NULL_ID, "com.xxx.SomeClassName", loaderDesc, cursor2, state); 249 state2.apply(objectID, cursor2, new BackReferences()); 250 cursor.reset(); 251 state2.apply(objectID, cursor, new BackReferences()); 252 253 assertTrue(state1.getClassId() != state2.getClassId()); 254 assertEquals(state2.getClassName(), "com.xxx.SomeClassName"); 255 assertEquals(state2.getLoaderDescription(), loaderDesc); 256 257 values = state2.addValues(new HashMap ()); 258 assertEquals(new ObjectID(2), values.get("field1")); 259 l2 = (Long ) values.get("field2"); 260 assertEquals(11, l2.longValue()); 261 f3 = (String ) values.get("field3"); 262 assertEquals(f3, "neoistheone"); 263 f4 = (Boolean ) values.get("field4"); 264 assertEquals(f4, new Boolean (false)); 265 assertEquals(new ObjectID(22), values.get("field5")); 266 Integer i6 = (Integer ) values.get("field6"); 267 assertEquals(2, i6.intValue()); 268 269 assertEquals(state1.getClass().getName(), state2.getClass().getSuperclass().getName()); 270 271 cursor2 = new TestDNACursor(); 273 cursor2.addPhysicalAction("field6", new Long (2)); 274 try { 275 state2.apply(objectID, cursor2, new BackReferences()); 277 System.err.println("The above exception is NORMAL."); 278 } catch (ClassCastException cfe) { 279 assertTrue(false); 280 } 281 282 values = state2.addValues(new HashMap ()); 283 i6 = (Integer ) values.get("field6"); 284 assertEquals(2, i6.intValue()); 285 286 cursor = new TestDNACursor(); 288 cursor.addPhysicalAction("field1", new ObjectID(1)); 289 cursor.addPhysicalAction("field2", new Long (11)); 290 cursor.addPhysicalAction("field3", new String ("neoistheone")); 291 292 PhysicalManagedObjectState state3 = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance() 293 .createState(objectID, ObjectID.NULL_ID, "com.xxx.SomeClassName", loaderDesc, cursor); 294 state3.apply(objectID, cursor, new BackReferences()); 295 assertEquals(state2.getClass().getName(), state3.getClass().getName()); 296 297 cursor.reset(); 299 ManagedObjectStateFactory.createInstance(listenerProvider, persistor); 300 state3 = (PhysicalManagedObjectState) ManagedObjectStateFactory.getInstance().createState(objectID, 301 ObjectID.NULL_ID, 302 "com.xxx.SomeClassName", 303 loaderDesc, cursor); 304 state3.apply(objectID, cursor, new BackReferences()); 305 assertEquals(state2.getClass().getName(), state3.getClass().getName()); 306 307 assertEquals(ObjectID.NULL_ID, state.getParentID()); 308 } 309 310 } 311 | Popular Tags |