1 5 package com.tc.object.bytecode; 6 7 import com.tc.object.MockTCObject; 8 import com.tc.object.SerializationUtil; 9 import com.tc.object.TestClientObjectManager; 10 import com.tc.object.bytecode.hook.DSOContext; 11 import com.tc.object.bytecode.hook.impl.ClassProcessorHelper; 12 import com.tc.object.bytecode.hook.impl.DSOContextImpl; 13 import com.tc.object.config.DSOClientConfigHelper; 14 import com.tc.object.loaders.ClassProvider; 15 import com.tc.object.tx.MockTransactionManager; 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Hashtable ; 22 import java.util.IdentityHashMap ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Stack ; 26 import java.util.Vector ; 27 28 public class LogicalClassAdapterTest extends ClassAdapterTestBase { 29 30 private TestClientObjectManager objManager; 31 private Class clazz; 32 private Object instance; 33 private MockTCObject tcObject; 34 private List history; 35 private MockTCObject.MethodCall call; 36 private Object [] params; 37 38 public void setUp() throws Exception { 39 objManager = new TestClientObjectManager(); 40 objManager.setIsManaged(true); 41 42 DSOClientConfigHelper config = configHelper(); 43 44 ClassProvider classProvider = new MockClassProvider(); 45 DSOContext context = DSOContextImpl.createContext(config, classProvider, 46 new ManagerImpl(false, objManager, new MockTransactionManager(), 47 config, classProvider, null)); 48 49 ClassProcessorHelper.setContext(Thread.currentThread().getContextClassLoader(), context); 50 } 51 52 public void testHashtable() throws Exception { 53 clazz = Hashtable .class; 54 instance = clazz.newInstance(); 55 56 objManager.lookupOrCreate(instance); 57 invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE, new Class [] { Object .class, Object .class }, 58 new Object [] { new Integer (1), new Integer (2) }); 59 invokeMethod(clazz, instance, SerializationUtil.REMOVE_KEY_SIGNATURE, new Class [] { Object .class }, 60 new Object [] { new Integer (1) }); 61 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 62 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 63 history = tcObject.getHistory(); 64 assertEquals(3, history.size()); 65 66 call = (MockTCObject.MethodCall) history.get(0); 67 assertEquals(SerializationUtil.PUT, call.method); 68 params = call.parameters; 69 assertEquals(new Integer (1), params[0]); 70 assertEquals(new Integer (2), params[1]); 71 assertEquals(2, params.length); 72 73 call = (MockTCObject.MethodCall) history.get(1); 74 assertEquals(SerializationUtil.REMOVE, call.method); 75 params = call.parameters; 76 assertEquals(new Integer (1), params[0]); 77 assertEquals(1, params.length); 78 79 call = (MockTCObject.MethodCall) history.get(2); 80 assertEquals(SerializationUtil.CLEAR, call.method); 81 params = call.parameters; 82 assertEquals(0, params.length); 83 } 84 85 public void testHashMap() throws Exception { 86 clazz = HashMap .class; 87 instance = clazz.newInstance(); 88 objManager.lookupOrCreate(instance); 89 invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE, new Class [] { Object .class, Object .class }, 90 new Object [] { new Integer (1), new Integer (2) }); 91 invokeMethod(clazz, instance, SerializationUtil.REMOVE_KEY_SIGNATURE, new Class [] { Object .class }, 92 new Object [] { new Integer (1) }); 93 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 94 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 95 history = tcObject.getHistory(); 96 assertEquals(3, history.size()); 97 98 call = (MockTCObject.MethodCall) history.get(0); 99 assertEquals(SerializationUtil.PUT, call.method); 100 params = call.parameters; 101 assertEquals(new Integer (1), params[0]); 102 assertEquals(new Integer (2), params[1]); 103 assertEquals(2, params.length); 104 105 call = (MockTCObject.MethodCall) history.get(1); 106 assertEquals(SerializationUtil.REMOVE, call.method); 107 params = call.parameters; 108 assertEquals(new Integer (1), params[0]); 109 assertEquals(1, params.length); 110 111 call = (MockTCObject.MethodCall) history.get(2); 112 assertEquals(SerializationUtil.CLEAR, call.method); 113 params = call.parameters; 114 assertEquals(0, params.length); 115 } 116 117 public void testIdentityHashMap() throws Exception { 118 clazz = IdentityHashMap .class; 119 instance = clazz.newInstance(); 120 objManager.lookupOrCreate(instance); 121 invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE, new Class [] { Object .class, Object .class }, 122 new Object [] { new Integer (1), new Integer (2) }); 123 invokeMethod(clazz, instance, SerializationUtil.REMOVE_KEY_SIGNATURE, new Class [] { Object .class }, 124 new Object [] { new Integer (1) }); 125 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 126 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 127 history = tcObject.getHistory(); 128 assertEquals(3, history.size()); 129 130 call = (MockTCObject.MethodCall) history.get(0); 131 assertEquals(SerializationUtil.PUT, call.method); 132 params = call.parameters; 133 assertEquals(new Integer (1), params[0]); 134 assertEquals(new Integer (2), params[1]); 135 assertEquals(2, params.length); 136 137 call = (MockTCObject.MethodCall) history.get(1); 138 assertEquals(SerializationUtil.REMOVE, call.method); 139 params = call.parameters; 140 assertEquals(1, params.length); 141 assertEquals(new Integer (1), params[0]); 142 143 call = (MockTCObject.MethodCall) history.get(2); 144 assertEquals(SerializationUtil.CLEAR, call.method); 145 params = call.parameters; 146 assertEquals(0, params.length); 147 } 148 149 public void testVector() throws Exception { 150 clazz = Vector .class; 151 instance = clazz.newInstance(); 152 objManager.lookupOrCreate(instance); 153 invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE, new Class [] { Object .class }, 154 new Object [] { new Integer (1) }); 155 invokeMethod(clazz, instance, SerializationUtil.ADD_AT_SIGNATURE, new Class [] { int.class, Object .class }, 156 new Object [] { new Integer (0), new Integer (1) }); 157 LinkedList l = new LinkedList (); 158 l.add("Hello"); 159 l.add("world"); 160 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class [] { int.class, Collection .class }, 161 new Object [] { new Integer (0), l }); 162 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_SIGNATURE, new Class [] { Collection .class }, 163 new Object [] { l }); 164 invokeMethod(clazz, instance, SerializationUtil.REMOVE_AT_SIGNATURE, new Class [] { int.class }, 165 new Object [] { new Integer (1) }); 166 invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE, new Class [] { int.class, Object .class }, 167 new Object [] { new Integer (1), new Integer (2) }); 168 169 invokeMethod(clazz, instance, SerializationUtil.SET_ELEMENT_SIGNATURE, new Class [] { Object .class, int.class }, 171 new Object [] { new Integer (69), new Integer (1) }); 172 173 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 174 175 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 176 history = tcObject.getHistory(); 177 assertEquals(10, history.size()); 178 179 call = (MockTCObject.MethodCall) history.get(0); 180 assertEquals(SerializationUtil.ADD, call.method); 181 assertEquals(new Integer (1), call.parameters[0]); 182 183 call = (MockTCObject.MethodCall) history.get(1); 184 assertEquals(SerializationUtil.INSERT_AT, call.method); 186 assertEquals(new Integer (0), call.parameters[0]); 187 assertEquals(new Integer (1), call.parameters[1]); 188 189 call = (MockTCObject.MethodCall) history.get(2); 190 assertEquals(SerializationUtil.ADD_AT, call.method); 191 assertEquals(new Integer (0), call.parameters[0]); 192 assertEquals("Hello", call.parameters[1]); 193 194 call = (MockTCObject.MethodCall) history.get(3); 195 assertEquals(SerializationUtil.ADD_AT, call.method); 196 assertEquals(new Integer (1), call.parameters[0]); 197 assertEquals("world", call.parameters[1]); 198 199 call = (MockTCObject.MethodCall) history.get(4); 200 assertEquals(SerializationUtil.ADD, call.method); 201 assertEquals("Hello", call.parameters[0]); 202 203 call = (MockTCObject.MethodCall) history.get(5); 204 assertEquals(SerializationUtil.ADD, call.method); 205 assertEquals("world", call.parameters[0]); 206 207 call = (MockTCObject.MethodCall) history.get(6); 208 assertEquals(SerializationUtil.REMOVE_AT, call.method); 209 assertEquals(new Integer (1), call.parameters[0]); 210 211 call = (MockTCObject.MethodCall) history.get(7); 212 assertEquals(SerializationUtil.SET, call.method); 213 assertEquals(new Integer (1), call.parameters[0]); 214 assertEquals(new Integer (2), call.parameters[1]); 215 216 call = (MockTCObject.MethodCall) history.get(8); 217 assertEquals(SerializationUtil.SET_ELEMENT, call.method); 218 assertEquals(new Integer (1), call.parameters[0]); 220 assertEquals(new Integer (69), call.parameters[1]); 221 222 call = (MockTCObject.MethodCall) history.get(9); 223 assertEquals(SerializationUtil.CLEAR, call.method); 224 params = call.parameters; 225 assertEquals(0, params.length); 226 } 227 228 public void testStack() throws Exception { 229 clazz = Stack .class; 230 instance = clazz.newInstance(); 231 objManager.lookupOrCreate(instance); 232 233 invokeMethod(clazz, instance, SerializationUtil.PUSH_SIGNATURE, new Class [] { Object .class }, 234 new Object [] { new Integer (1) }); 235 invokeMethod(clazz, instance, SerializationUtil.POP_SIGNATURE, new Class [] {}, new Object [] {}); 236 237 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 238 history = tcObject.getHistory(); 239 assertEquals(2, history.size()); 240 241 call = (MockTCObject.MethodCall) history.get(0); 242 assertEquals(SerializationUtil.ADD, call.method); 243 assertEquals(new Integer (1), call.parameters[0]); 244 245 call = (MockTCObject.MethodCall) history.get(1); 246 assertEquals(SerializationUtil.REMOVE_AT, call.method); 247 assertEquals(new Integer (0), call.parameters[0]); 248 } 249 250 public void testArrayList() throws Exception { 251 clazz = ArrayList .class; 253 instance = clazz.newInstance(); 254 objManager.lookupOrCreate(instance); 255 invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE, new Class [] { Object .class }, 256 new Object [] { new Integer (1) }); 257 invokeMethod(clazz, instance, SerializationUtil.ADD_AT_SIGNATURE, new Class [] { int.class, Object .class }, 258 new Object [] { new Integer (0), new Integer (1) }); 259 LinkedList l = new LinkedList (); 260 l.add("Hello"); 261 l.add("world"); 262 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class [] { int.class, Collection .class }, 263 new Object [] { new Integer (0), l }); 264 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_SIGNATURE, new Class [] { Collection .class }, 265 new Object [] { l }); 266 invokeMethod(clazz, instance, SerializationUtil.REMOVE_AT_SIGNATURE, new Class [] { int.class }, 267 new Object [] { new Integer (1) }); 268 invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE, new Class [] { int.class, Object .class }, 269 new Object [] { new Integer (1), new Integer (2) }); 270 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 271 272 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 273 history = tcObject.getHistory(); 274 assertEquals(9, history.size()); 275 276 call = (MockTCObject.MethodCall) history.get(0); 277 assertEquals(SerializationUtil.ADD, call.method); 278 assertEquals(new Integer (1), call.parameters[0]); 279 280 call = (MockTCObject.MethodCall) history.get(1); 281 assertEquals(SerializationUtil.ADD_AT, call.method); 282 assertEquals(new Integer (0), call.parameters[0]); 283 assertEquals(new Integer (1), call.parameters[1]); 284 285 call = (MockTCObject.MethodCall) history.get(2); 286 assertEquals(SerializationUtil.ADD_AT, call.method); 287 assertEquals(new Integer (0), call.parameters[0]); 288 assertEquals("Hello", call.parameters[1]); 289 290 call = (MockTCObject.MethodCall) history.get(3); 291 assertEquals(SerializationUtil.ADD_AT, call.method); 292 assertEquals(new Integer (1), call.parameters[0]); 293 assertEquals("world", call.parameters[1]); 294 295 call = (MockTCObject.MethodCall) history.get(4); 296 assertEquals(SerializationUtil.ADD, call.method); 297 assertEquals("Hello", call.parameters[0]); 298 299 call = (MockTCObject.MethodCall) history.get(5); 300 assertEquals(SerializationUtil.ADD, call.method); 301 assertEquals("world", call.parameters[0]); 302 303 call = (MockTCObject.MethodCall) history.get(6); 304 assertEquals(SerializationUtil.REMOVE_AT, call.method); 305 assertEquals(new Integer (1), call.parameters[0]); 306 307 call = (MockTCObject.MethodCall) history.get(7); 308 assertEquals(SerializationUtil.SET, call.method); 309 assertEquals(new Integer (1), call.parameters[0]); 310 assertEquals(new Integer (2), call.parameters[1]); 311 312 call = (MockTCObject.MethodCall) history.get(8); 313 assertEquals(SerializationUtil.CLEAR, call.method); 314 params = call.parameters; 315 assertEquals(0, params.length); 316 } 317 318 public void testLinkedList() throws Exception { 319 clazz = LinkedList .class; 321 instance = clazz.newInstance(); 322 objManager.lookupOrCreate(instance); 323 invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE, new Class [] { Object .class }, 324 new Object [] { new Integer (1) }); 325 invokeMethod(clazz, instance, SerializationUtil.ADD_AT_SIGNATURE, new Class [] { int.class, Object .class }, 326 new Object [] { new Integer (0), new Integer (1) }); 327 LinkedList l = new LinkedList (); 328 l.add("Hello"); 329 l.add("world"); 330 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class [] { int.class, Collection .class }, 331 new Object [] { new Integer (0), l }); 332 333 invokeMethod(clazz, instance, SerializationUtil.ADD_ALL_SIGNATURE, new Class [] { Collection .class }, 334 new Object [] { l }); 335 invokeMethod(clazz, instance, SerializationUtil.ADD_FIRST_SIGNATURE, new Class [] { Object .class }, 336 new Object [] { new Integer (2) }); 337 invokeMethod(clazz, instance, SerializationUtil.ADD_LAST_SIGNATURE, new Class [] { Object .class }, 338 new Object [] { new Integer (3) }); 339 invokeMethod(clazz, instance, SerializationUtil.REMOVE_AT_SIGNATURE, new Class [] { int.class }, 340 new Object [] { new Integer (1) }); 341 invokeMethod(clazz, instance, SerializationUtil.REMOVE_SIGNATURE, new Class [] { Object .class }, 342 new Object [] { "Hello" }); 343 invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE, new Class [] { int.class, Object .class }, 344 new Object [] { new Integer (1), new Integer (2) }); 345 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 346 347 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 348 history = tcObject.getHistory(); 349 assertEquals(12, history.size()); 350 351 call = (MockTCObject.MethodCall) history.get(0); 352 assertEquals(SerializationUtil.ADD, call.method); 353 assertEquals(new Integer (1), call.parameters[0]); 354 355 call = (MockTCObject.MethodCall) history.get(1); 356 assertEquals(SerializationUtil.ADD_AT, call.method); 357 assertEquals(new Integer (0), call.parameters[0]); 358 assertEquals(new Integer (1), call.parameters[1]); 359 360 call = (MockTCObject.MethodCall) history.get(2); 361 assertEquals(SerializationUtil.ADD_AT, call.method); 362 assertEquals(new Integer (0), call.parameters[0]); 363 assertEquals("Hello", call.parameters[1]); 364 365 call = (MockTCObject.MethodCall) history.get(3); 366 assertEquals(SerializationUtil.ADD_AT, call.method); 367 assertEquals(new Integer (1), call.parameters[0]); 368 assertEquals("world", call.parameters[1]); 369 370 call = (MockTCObject.MethodCall) history.get(4); 371 assertEquals(SerializationUtil.ADD_AT, call.method); 372 assertEquals("Hello", call.parameters[1]); 373 374 call = (MockTCObject.MethodCall) history.get(5); 375 assertEquals(SerializationUtil.ADD_AT, call.method); 376 assertEquals("world", call.parameters[1]); 377 378 call = (MockTCObject.MethodCall) history.get(6); 379 assertEquals(SerializationUtil.ADD_FIRST, call.method); 380 assertEquals(new Integer (2), call.parameters[0]); 381 382 call = (MockTCObject.MethodCall) history.get(7); 383 assertEquals(SerializationUtil.ADD_LAST, call.method); 384 assertEquals(new Integer (3), call.parameters[0]); 385 386 call = (MockTCObject.MethodCall) history.get(8); 387 assertEquals(SerializationUtil.REMOVE_AT, call.method); 388 assertEquals(new Integer (1), call.parameters[0]); 389 390 call = (MockTCObject.MethodCall) history.get(9); 391 assertEquals(SerializationUtil.REMOVE, call.method); 392 assertEquals("Hello", call.parameters[0]); 393 394 call = (MockTCObject.MethodCall) history.get(10); 395 assertEquals(SerializationUtil.SET, call.method); 396 assertEquals(new Integer (1), call.parameters[0]); 397 assertEquals(new Integer (2), call.parameters[1]); 398 399 call = (MockTCObject.MethodCall) history.get(11); 400 assertEquals(SerializationUtil.CLEAR, call.method); 401 params = call.parameters; 402 assertEquals(0, params.length); 403 404 } 405 406 public void testHashSet() throws Exception { 407 clazz = HashSet .class; 408 instance = clazz.newInstance(); 409 objManager.lookupOrCreate(instance); 410 int callCount = 0; 411 int checkCount = 0; 412 invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE, new Class [] { Object .class }, 413 new Object [] { new Integer (1) }); 414 callCount++; 415 416 invokeMethod(clazz, instance, SerializationUtil.REMOVE_SIGNATURE, new Class [] { Object .class }, 417 new Object [] { new Integer (1) }); 418 419 callCount++; 420 421 invokeMethod(clazz, instance, SerializationUtil.CLEAR_SIGNATURE, new Class [] {}, new Object [] {}); 422 callCount++; 423 424 tcObject = (MockTCObject) objManager.lookupOrCreate(instance); 425 history = tcObject.getHistory(); 426 427 assertEquals(callCount, history.size()); 428 429 call = (MockTCObject.MethodCall) history.get(checkCount++); 430 assertEquals(SerializationUtil.ADD, call.method); 431 assertEquals(new Integer (1), call.parameters[0]); 432 433 call = (MockTCObject.MethodCall) history.get(checkCount++); 434 assertEquals(SerializationUtil.REMOVE, call.method); 435 assertEquals(new Integer (1), call.parameters[0]); 436 437 call = (MockTCObject.MethodCall) history.get(checkCount++); 438 assertEquals(SerializationUtil.CLEAR, call.method); 439 assertEquals(0, call.parameters.length); 440 441 } 442 443 } | Popular Tags |