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.object.PortabilityImpl;
009: import com.tc.object.TestClientObjectManager;
010: import com.tc.object.bytecode.Clearable;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.loaders.IsolationClassLoader;
013: import com.tc.object.tx.MockTransactionManager;
014: import com.tc.test.TCTestCase;
015: import com.tc.util.Assert;
016: import com.tc.util.runtime.Vm;
017:
018: import java.lang.reflect.Constructor;
019: import java.lang.reflect.InvocationHandler;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Proxy;
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Map;
026:
027: public class HashtableAutoLockTest extends TCTestCase {
028: private ClassLoader origThreadContextClassLoader;
029: private TestClientObjectManager testClientObjectManager;
030: private MockTransactionManager testTransactionManager;
031:
032: protected void setUp() throws Exception {
033: ClassLoader loader = getClass().getClassLoader();
034: InvocationHandler handler = new InvocationHandler() {
035: public Object invoke(Object proxy, Method method,
036: Object[] args) throws Throwable {
037: String name = method.getName();
038: if ("getNewCommonL1Config".equals(name)
039: || "getInstrumentationLoggingOptions"
040: .equals(name)
041: || "instrumentationLoggingOptions".equals(name)
042: || "getLogicalExtendingClassName".equals(name)
043: || "createDsoClassAdapterFor".equals(name)
044: || "getModulesForInitialization".equals(name)
045: || "verifyBootJarContents".equals(name)) {
046: return null;
047: } else if ("shouldBeAdapted".equals(name)) {
048: return Boolean.FALSE;
049: } else if ("isNeverAdaptable".equals(name)) {
050: return Boolean.TRUE;
051: } else if ("isLogical".equals(name)) {
052: return Boolean.TRUE;
053: } else if ("getAspectModules".equals(name)) {
054: return new HashMap();
055: } else if ("getPortability".equals(name)) {
056: return new PortabilityImpl(
057: (DSOClientConfigHelper) proxy);
058: } else if (Vm.isIBM()
059: && "isRoot".equals(name)
060: && ("java.lang.reflect.Method".equals(args[0]) || "java.lang.reflect.Constructor"
061: .equals(args[0]))) {
062: // the implementation of java.lang.Class in the IBM JDK is different and caches
063: // fields of the Method and Constructor classes, which it retrieves afterwards by
064: // calling the Field.get method. This gets into the AccessibleObject changes for
065: // DSO, which checks if the returned value is a root
066: return Boolean.FALSE;
067: }
068:
069: throw new ImplementMe();
070: }
071: };
072: Object proxy = Proxy.newProxyInstance(loader,
073: new Class[] { DSOClientConfigHelper.class }, handler);
074:
075: testClientObjectManager = new TestClientObjectManager();
076: testTransactionManager = new MockTransactionManager();
077: IsolationClassLoader classLoader = new IsolationClassLoader(
078: (DSOClientConfigHelper) proxy, testClientObjectManager,
079: testTransactionManager);
080: classLoader.init();
081:
082: this .origThreadContextClassLoader = Thread.currentThread()
083: .getContextClassLoader();
084: Thread.currentThread().setContextClassLoader(classLoader);
085: }
086:
087: protected void tearDown() throws Exception {
088: super .tearDown();
089:
090: Thread.currentThread().setContextClassLoader(
091: this .origThreadContextClassLoader);
092: }
093:
094: public void testClearReferences() throws Exception {
095: testClientObjectManager.setIsManaged(true);
096: Hashtable ht = (Hashtable) createMap("java.util.Hashtable");
097: testClientObjectManager.lookupOrCreate(ht);
098: ((Clearable) ht).__tc_clearReferences(100);
099: System.err.println("# of begins: "
100: + testTransactionManager.getBegins().size());
101: Assert.assertEquals(0, testTransactionManager.getBegins()
102: .size());
103: }
104:
105: private Map createMap(String className)
106: throws ClassNotFoundException, SecurityException,
107: NoSuchMethodException, IllegalArgumentException,
108: InstantiationException, IllegalAccessException,
109: InvocationTargetException {
110: Class c = Class.forName(className);
111: Constructor constructor = c.getConstructor(new Class[0]);
112: return (Map) constructor.newInstance(new Object[0]);
113: }
114:
115: }
|