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.tc.object.bytecode;
006:
007: import com.tc.object.MockTCObject;
008: import com.tc.object.SerializationUtil;
009: import com.tc.object.TestClientObjectManager;
010: import com.tc.object.bytecode.hook.DSOContext;
011: import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
012: import com.tc.object.bytecode.hook.impl.DSOContextImpl;
013: import com.tc.object.config.DSOClientConfigHelper;
014: import com.tc.object.loaders.ClassProvider;
015: import com.tc.object.tx.MockTransactionManager;
016: import com.tc.util.runtime.Vm;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Hashtable;
023: import java.util.IdentityHashMap;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Stack;
027: import java.util.Vector;
028:
029: public class LogicalClassAdapterTest extends ClassAdapterTestBase {
030:
031: private TestClientObjectManager objManager;
032: private Class clazz;
033: private Object instance;
034: private MockTCObject tcObject;
035: private List history;
036: private MockTCObject.MethodCall call;
037: private Object[] params;
038:
039: public LogicalClassAdapterTest() {
040: //
041: }
042:
043: public void setUp() throws Exception {
044: objManager = new TestClientObjectManager();
045: objManager.setIsManaged(true);
046:
047: DSOClientConfigHelper config = configHelper();
048:
049: ClassProvider classProvider = new MockClassProvider();
050: DSOContext context = DSOContextImpl.createContext(config,
051: classProvider, new ManagerImpl(false, objManager,
052: new MockTransactionManager(), config,
053: classProvider, null));
054:
055: ClassProcessorHelper.setContext(Thread.currentThread()
056: .getContextClassLoader(), context);
057: }
058:
059: protected void tearDown() throws Exception {
060: super .tearDown();
061: objManager.setIsManaged(false);
062: }
063:
064: public void testHashtable() throws Exception {
065: clazz = Hashtable.class;
066: instance = clazz.newInstance();
067:
068: objManager.lookupOrCreate(instance);
069: invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE,
070: new Class[] { Object.class, Object.class },
071: new Object[] { new Integer(1), new Integer(2) });
072: invokeMethod(clazz, instance,
073: SerializationUtil.REMOVE_KEY_SIGNATURE,
074: new Class[] { Object.class },
075: new Object[] { new Integer(1) });
076: invokeMethod(clazz, instance,
077: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
078: new Object[] {});
079: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
080: history = tcObject.getHistory();
081: assertEquals(3, history.size());
082:
083: call = (MockTCObject.MethodCall) history.get(0);
084: assertEquals(SerializationUtil.PUT, call.method);
085: params = call.parameters;
086: assertEquals(new Integer(1), params[0]);
087: assertEquals(new Integer(2), params[1]);
088: assertEquals(2, params.length);
089:
090: call = (MockTCObject.MethodCall) history.get(1);
091: assertEquals(SerializationUtil.REMOVE, call.method);
092: params = call.parameters;
093: assertEquals(new Integer(1), params[0]);
094: assertEquals(1, params.length);
095:
096: call = (MockTCObject.MethodCall) history.get(2);
097: assertEquals(SerializationUtil.CLEAR, call.method);
098: params = call.parameters;
099: assertEquals(0, params.length);
100: }
101:
102: public void testHashMap() throws Exception {
103: clazz = HashMap.class;
104: instance = clazz.newInstance();
105: objManager.lookupOrCreate(instance);
106: invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE,
107: new Class[] { Object.class, Object.class },
108: new Object[] { new Integer(1), new Integer(2) });
109: invokeMethod(clazz, instance,
110: SerializationUtil.REMOVE_KEY_SIGNATURE,
111: new Class[] { Object.class },
112: new Object[] { new Integer(1) });
113: invokeMethod(clazz, instance,
114: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
115: new Object[] {});
116: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
117: history = tcObject.getHistory();
118: assertEquals(3, history.size());
119:
120: call = (MockTCObject.MethodCall) history.get(0);
121: assertEquals(SerializationUtil.PUT, call.method);
122: params = call.parameters;
123: assertEquals(new Integer(1), params[0]);
124: assertEquals(new Integer(2), params[1]);
125: assertEquals(2, params.length);
126:
127: call = (MockTCObject.MethodCall) history.get(1);
128: assertEquals(SerializationUtil.REMOVE, call.method);
129: params = call.parameters;
130: assertEquals(new Integer(1), params[0]);
131: assertEquals(1, params.length);
132:
133: call = (MockTCObject.MethodCall) history.get(2);
134: assertEquals(SerializationUtil.CLEAR, call.method);
135: params = call.parameters;
136: assertEquals(0, params.length);
137: }
138:
139: public void testIdentityHashMap() throws Exception {
140: clazz = IdentityHashMap.class;
141: instance = clazz.newInstance();
142: objManager.lookupOrCreate(instance);
143: invokeMethod(clazz, instance, SerializationUtil.PUT_SIGNATURE,
144: new Class[] { Object.class, Object.class },
145: new Object[] { new Integer(1), new Integer(2) });
146: invokeMethod(clazz, instance,
147: SerializationUtil.REMOVE_KEY_SIGNATURE,
148: new Class[] { Object.class },
149: new Object[] { new Integer(1) });
150: invokeMethod(clazz, instance,
151: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
152: new Object[] {});
153: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
154: history = tcObject.getHistory();
155: assertEquals(3, history.size());
156:
157: call = (MockTCObject.MethodCall) history.get(0);
158: assertEquals(SerializationUtil.PUT, call.method);
159: params = call.parameters;
160: assertEquals(new Integer(1), params[0]);
161: assertEquals(new Integer(2), params[1]);
162: assertEquals(2, params.length);
163:
164: call = (MockTCObject.MethodCall) history.get(1);
165: assertEquals(SerializationUtil.REMOVE, call.method);
166: params = call.parameters;
167: assertEquals(1, params.length);
168: assertEquals(new Integer(1), params[0]);
169:
170: call = (MockTCObject.MethodCall) history.get(2);
171: assertEquals(SerializationUtil.CLEAR, call.method);
172: params = call.parameters;
173: assertEquals(0, params.length);
174: }
175:
176: public void testVector() throws Exception {
177: clazz = Vector.class;
178: instance = clazz.newInstance();
179: objManager.lookupOrCreate(instance);
180: invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE,
181: new Class[] { Object.class },
182: new Object[] { new Integer(1) });
183: invokeMethod(clazz, instance,
184: SerializationUtil.ADD_AT_SIGNATURE, new Class[] {
185: int.class, Object.class }, new Object[] {
186: new Integer(0), new Integer(1) });
187: LinkedList l = new LinkedList();
188: l.add("Hello");
189: l.add("world");
190: invokeMethod(clazz, instance,
191: SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class[] {
192: int.class, Collection.class }, new Object[] {
193: new Integer(0), l });
194: invokeMethod(clazz, instance,
195: SerializationUtil.ADD_ALL_SIGNATURE,
196: new Class[] { Collection.class }, new Object[] { l });
197: invokeMethod(clazz, instance,
198: SerializationUtil.REMOVE_AT_SIGNATURE,
199: new Class[] { int.class },
200: new Object[] { new Integer(1) });
201: invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE,
202: new Class[] { int.class, Object.class }, new Object[] {
203: new Integer(1), new Integer(2) });
204:
205: // This one is very important, this test that the parameter reversing logic works
206: invokeMethod(clazz, instance,
207: SerializationUtil.SET_ELEMENT_SIGNATURE, new Class[] {
208: Object.class, int.class }, new Object[] {
209: new Integer(69), new Integer(1) });
210:
211: invokeMethod(clazz, instance,
212: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
213: new Object[] {});
214:
215: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
216: history = tcObject.getHistory();
217: assertEquals(10, history.size());
218:
219: call = (MockTCObject.MethodCall) history.get(0);
220: assertEquals(SerializationUtil.ADD, call.method);
221: assertEquals(new Integer(1), call.parameters[0]);
222:
223: call = (MockTCObject.MethodCall) history.get(1);
224: // Vector implementation calls insertElementAt() internally, hopefully this is constant over all of time
225: assertEquals(SerializationUtil.INSERT_AT, call.method);
226: assertEquals(new Integer(0), call.parameters[0]);
227: assertEquals(new Integer(1), call.parameters[1]);
228:
229: call = (MockTCObject.MethodCall) history.get(2);
230: assertEquals(SerializationUtil.ADD_AT, call.method);
231: assertEquals(new Integer(0), call.parameters[0]);
232: assertEquals("Hello", call.parameters[1]);
233:
234: call = (MockTCObject.MethodCall) history.get(3);
235: assertEquals(SerializationUtil.ADD_AT, call.method);
236: assertEquals(new Integer(1), call.parameters[0]);
237: assertEquals("world", call.parameters[1]);
238:
239: call = (MockTCObject.MethodCall) history.get(4);
240: if (Vm.isIBM()) {
241: // the IBM JDK delegates all add() method calls internally to addAt()
242: assertEquals(SerializationUtil.ADD_AT, call.method);
243: assertEquals(new Integer(4), call.parameters[0]);
244: assertEquals("Hello", call.parameters[1]);
245: } else {
246: assertEquals(SerializationUtil.ADD, call.method);
247: assertEquals("Hello", call.parameters[0]);
248: }
249:
250: call = (MockTCObject.MethodCall) history.get(5);
251: if (Vm.isIBM()) {
252: // the IBM JDK delegates all add() method calls internally to addAt()
253: assertEquals(SerializationUtil.ADD_AT, call.method);
254: assertEquals(new Integer(5), call.parameters[0]);
255: assertEquals("world", call.parameters[1]);
256: } else {
257: assertEquals(SerializationUtil.ADD, call.method);
258: assertEquals("world", call.parameters[0]);
259: }
260:
261: call = (MockTCObject.MethodCall) history.get(6);
262: assertEquals(SerializationUtil.REMOVE_AT, call.method);
263: assertEquals(new Integer(1), call.parameters[0]);
264:
265: call = (MockTCObject.MethodCall) history.get(7);
266: assertEquals(SerializationUtil.SET, call.method);
267: assertEquals(new Integer(1), call.parameters[0]);
268: assertEquals(new Integer(2), call.parameters[1]);
269:
270: call = (MockTCObject.MethodCall) history.get(8);
271: assertEquals(SerializationUtil.SET_ELEMENT, call.method);
272: // These are supposed to be reversed
273: assertEquals(new Integer(1), call.parameters[0]);
274: assertEquals(new Integer(69), call.parameters[1]);
275:
276: call = (MockTCObject.MethodCall) history.get(9);
277: assertEquals(SerializationUtil.CLEAR, call.method);
278: params = call.parameters;
279: assertEquals(0, params.length);
280: }
281:
282: public void testStack() throws Exception {
283: clazz = Stack.class;
284: instance = clazz.newInstance();
285: objManager.lookupOrCreate(instance);
286:
287: invokeMethod(clazz, instance, SerializationUtil.PUSH_SIGNATURE,
288: new Class[] { Object.class },
289: new Object[] { new Integer(1) });
290: invokeMethod(clazz, instance, SerializationUtil.POP_SIGNATURE,
291: new Class[] {}, new Object[] {});
292:
293: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
294: history = tcObject.getHistory();
295: assertEquals(2, history.size());
296:
297: call = (MockTCObject.MethodCall) history.get(0);
298: assertEquals(SerializationUtil.ADD, call.method);
299: assertEquals(new Integer(1), call.parameters[0]);
300:
301: call = (MockTCObject.MethodCall) history.get(1);
302: assertEquals(SerializationUtil.REMOVE_AT, call.method);
303: assertEquals(new Integer(0), call.parameters[0]);
304: }
305:
306: public void testArrayList() throws Exception {
307: // ArrayList
308: clazz = ArrayList.class;
309: instance = clazz.newInstance();
310: objManager.lookupOrCreate(instance);
311: invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE,
312: new Class[] { Object.class },
313: new Object[] { new Integer(1) });
314: invokeMethod(clazz, instance,
315: SerializationUtil.ADD_AT_SIGNATURE, new Class[] {
316: int.class, Object.class }, new Object[] {
317: new Integer(0), new Integer(1) });
318: LinkedList l = new LinkedList();
319: l.add("Hello");
320: l.add("world");
321: invokeMethod(clazz, instance,
322: SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class[] {
323: int.class, Collection.class }, new Object[] {
324: new Integer(0), l });
325: invokeMethod(clazz, instance,
326: SerializationUtil.ADD_ALL_SIGNATURE,
327: new Class[] { Collection.class }, new Object[] { l });
328: invokeMethod(clazz, instance,
329: SerializationUtil.REMOVE_AT_SIGNATURE,
330: new Class[] { int.class },
331: new Object[] { new Integer(1) });
332: invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE,
333: new Class[] { int.class, Object.class }, new Object[] {
334: new Integer(1), new Integer(2) });
335: invokeMethod(clazz, instance,
336: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
337: new Object[] {});
338:
339: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
340: history = tcObject.getHistory();
341: assertEquals(9, history.size());
342:
343: call = (MockTCObject.MethodCall) history.get(0);
344: assertEquals(SerializationUtil.ADD, call.method);
345: assertEquals(new Integer(1), call.parameters[0]);
346:
347: call = (MockTCObject.MethodCall) history.get(1);
348: assertEquals(SerializationUtil.ADD_AT, call.method);
349: assertEquals(new Integer(0), call.parameters[0]);
350: assertEquals(new Integer(1), call.parameters[1]);
351:
352: call = (MockTCObject.MethodCall) history.get(2);
353: assertEquals(SerializationUtil.ADD_AT, call.method);
354: assertEquals(new Integer(0), call.parameters[0]);
355: assertEquals("Hello", call.parameters[1]);
356:
357: call = (MockTCObject.MethodCall) history.get(3);
358: assertEquals(SerializationUtil.ADD_AT, call.method);
359: assertEquals(new Integer(1), call.parameters[0]);
360: assertEquals("world", call.parameters[1]);
361:
362: call = (MockTCObject.MethodCall) history.get(4);
363: assertEquals(SerializationUtil.ADD, call.method);
364: assertEquals("Hello", call.parameters[0]);
365:
366: call = (MockTCObject.MethodCall) history.get(5);
367: assertEquals(SerializationUtil.ADD, call.method);
368: assertEquals("world", call.parameters[0]);
369:
370: call = (MockTCObject.MethodCall) history.get(6);
371: assertEquals(SerializationUtil.REMOVE_AT, call.method);
372: assertEquals(new Integer(1), call.parameters[0]);
373:
374: call = (MockTCObject.MethodCall) history.get(7);
375: assertEquals(SerializationUtil.SET, call.method);
376: assertEquals(new Integer(1), call.parameters[0]);
377: assertEquals(new Integer(2), call.parameters[1]);
378:
379: call = (MockTCObject.MethodCall) history.get(8);
380: assertEquals(SerializationUtil.CLEAR, call.method);
381: params = call.parameters;
382: assertEquals(0, params.length);
383: }
384:
385: public void testLinkedList() throws Exception {
386: // LinkedList
387: clazz = LinkedList.class;
388: instance = clazz.newInstance();
389: objManager.lookupOrCreate(instance);
390: invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE,
391: new Class[] { Object.class },
392: new Object[] { new Integer(1) });
393: invokeMethod(clazz, instance,
394: SerializationUtil.ADD_AT_SIGNATURE, new Class[] {
395: int.class, Object.class }, new Object[] {
396: new Integer(0), new Integer(1) });
397: LinkedList l = new LinkedList();
398: l.add("Hello");
399: l.add("world");
400: invokeMethod(clazz, instance,
401: SerializationUtil.ADD_ALL_AT_SIGNATURE, new Class[] {
402: int.class, Collection.class }, new Object[] {
403: new Integer(0), l });
404:
405: invokeMethod(clazz, instance,
406: SerializationUtil.ADD_ALL_SIGNATURE,
407: new Class[] { Collection.class }, new Object[] { l });
408: invokeMethod(clazz, instance,
409: SerializationUtil.ADD_FIRST_SIGNATURE,
410: new Class[] { Object.class },
411: new Object[] { new Integer(2) });
412: invokeMethod(clazz, instance,
413: SerializationUtil.ADD_LAST_SIGNATURE,
414: new Class[] { Object.class },
415: new Object[] { new Integer(3) });
416: invokeMethod(clazz, instance,
417: SerializationUtil.REMOVE_AT_SIGNATURE,
418: new Class[] { int.class },
419: new Object[] { new Integer(1) });
420: invokeMethod(clazz, instance,
421: SerializationUtil.REMOVE_SIGNATURE,
422: new Class[] { Object.class }, new Object[] { "Hello" });
423: invokeMethod(clazz, instance, SerializationUtil.SET_SIGNATURE,
424: new Class[] { int.class, Object.class }, new Object[] {
425: new Integer(1), new Integer(2) });
426: invokeMethod(clazz, instance,
427: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
428: new Object[] {});
429:
430: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
431: history = tcObject.getHistory();
432: assertEquals(12, history.size());
433:
434: call = (MockTCObject.MethodCall) history.get(0);
435: assertEquals(SerializationUtil.ADD, call.method);
436: assertEquals(new Integer(1), call.parameters[0]);
437:
438: call = (MockTCObject.MethodCall) history.get(1);
439: assertEquals(SerializationUtil.ADD_AT, call.method);
440: assertEquals(new Integer(0), call.parameters[0]);
441: assertEquals(new Integer(1), call.parameters[1]);
442:
443: call = (MockTCObject.MethodCall) history.get(2);
444: assertEquals(SerializationUtil.ADD_AT, call.method);
445: assertEquals(new Integer(0), call.parameters[0]);
446: assertEquals("Hello", call.parameters[1]);
447:
448: call = (MockTCObject.MethodCall) history.get(3);
449: assertEquals(SerializationUtil.ADD_AT, call.method);
450: assertEquals(new Integer(1), call.parameters[0]);
451: assertEquals("world", call.parameters[1]);
452:
453: call = (MockTCObject.MethodCall) history.get(4);
454: assertEquals(SerializationUtil.ADD_AT, call.method);
455: assertEquals("Hello", call.parameters[1]);
456:
457: call = (MockTCObject.MethodCall) history.get(5);
458: assertEquals(SerializationUtil.ADD_AT, call.method);
459: assertEquals("world", call.parameters[1]);
460:
461: call = (MockTCObject.MethodCall) history.get(6);
462: assertEquals(SerializationUtil.ADD_FIRST, call.method);
463: assertEquals(new Integer(2), call.parameters[0]);
464:
465: call = (MockTCObject.MethodCall) history.get(7);
466: assertEquals(SerializationUtil.ADD_LAST, call.method);
467: assertEquals(new Integer(3), call.parameters[0]);
468:
469: call = (MockTCObject.MethodCall) history.get(8);
470: assertEquals(SerializationUtil.REMOVE_AT, call.method);
471: assertEquals(new Integer(1), call.parameters[0]);
472:
473: call = (MockTCObject.MethodCall) history.get(9);
474: assertEquals(SerializationUtil.REMOVE, call.method);
475: assertEquals("Hello", call.parameters[0]);
476:
477: call = (MockTCObject.MethodCall) history.get(10);
478: assertEquals(SerializationUtil.SET, call.method);
479: assertEquals(new Integer(1), call.parameters[0]);
480: assertEquals(new Integer(2), call.parameters[1]);
481:
482: call = (MockTCObject.MethodCall) history.get(11);
483: assertEquals(SerializationUtil.CLEAR, call.method);
484: params = call.parameters;
485: assertEquals(0, params.length);
486:
487: }
488:
489: public void testHashSet() throws Exception {
490: clazz = HashSet.class;
491: instance = clazz.newInstance();
492: objManager.lookupOrCreate(instance);
493: int callCount = 0;
494: int checkCount = 0;
495: invokeMethod(clazz, instance, SerializationUtil.ADD_SIGNATURE,
496: new Class[] { Object.class },
497: new Object[] { new Integer(1) });
498: callCount++;
499:
500: invokeMethod(clazz, instance,
501: SerializationUtil.REMOVE_SIGNATURE,
502: new Class[] { Object.class },
503: new Object[] { new Integer(1) });
504:
505: callCount++;
506:
507: invokeMethod(clazz, instance,
508: SerializationUtil.CLEAR_SIGNATURE, new Class[] {},
509: new Object[] {});
510: callCount++;
511:
512: tcObject = (MockTCObject) objManager.lookupOrCreate(instance);
513: history = tcObject.getHistory();
514:
515: assertEquals(callCount, history.size());
516:
517: call = (MockTCObject.MethodCall) history.get(checkCount++);
518: assertEquals(SerializationUtil.ADD, call.method);
519: assertEquals(new Integer(1), call.parameters[0]);
520:
521: call = (MockTCObject.MethodCall) history.get(checkCount++);
522: assertEquals(SerializationUtil.REMOVE, call.method);
523: assertEquals(new Integer(1), call.parameters[0]);
524:
525: call = (MockTCObject.MethodCall) history.get(checkCount++);
526: assertEquals(SerializationUtil.CLEAR, call.method);
527: assertEquals(0, call.parameters.length);
528:
529: }
530:
531: }
|