1 package org.jboss.cache.marshall; 2 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 import org.jboss.cache.CacheImpl; 6 import org.jboss.cache.Fqn; 7 import org.jboss.cache.config.Configuration; 8 import org.jboss.cache.lock.IsolationLevel; 9 import org.jboss.cache.marshall.data.Debug; 10 11 import javax.transaction.Transaction ; 12 import java.io.File ; 13 import java.lang.reflect.Method ; 14 import java.net.URL ; 15 import java.net.URLClassLoader ; 16 17 23 public class LocalTest extends RegionBasedMarshallingTestBase 24 { 25 CacheImpl cache = null; 26 Transaction tx = null; 27 final Fqn FQN = Fqn.fromString("/myNode"); 28 final String KEY = "key"; 29 final String VALUE = "value"; 30 Exception ex; 31 32 33 protected void setUp() throws Exception 34 { 35 super.setUp(); 36 cache = new CacheImpl(); 37 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL); 38 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 39 cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ); 40 cache.create(); 41 cache.start(); 42 ex = null; 43 } 44 45 protected void tearDown() throws Exception 46 { 47 super.tearDown(); 48 if (cache != null) 49 { 50 cache.stop(); 51 cache.destroy(); 52 cache = null; 53 } 54 if (ex != null) 55 { 56 throw ex; 57 } 58 } 59 60 public void testClassloader() throws Exception 61 { 62 String jarDir = System.getProperty("test.jar.dir"); 63 File jar0 = new File (jarDir + "/testMarshall.jar"); 64 URL [] cp0 = {jar0.toURL()}; 65 URLClassLoader ucl0 = new URLClassLoader (cp0); 66 Thread.currentThread().setContextClassLoader(ucl0); 67 Class clasz1 = ucl0.loadClass(PERSON_CLASSNAME); 68 StringBuffer buffer = new StringBuffer ("Person Info"); 69 Debug.displayClassInfo(clasz1, buffer, false); 70 log(buffer.toString()); 71 Object ben = clasz1.newInstance(); 72 Object value; 73 try 74 { 75 { 76 Class [] types = {String .class}; 77 Method setValue = clasz1.getMethod("setName", types); 78 Object [] margs = {"Ben"}; 79 value = setValue.invoke(ben, margs); 80 } 81 Class [] types = {}; 82 Class [] margs = {}; 83 Method getValue = clasz1.getMethod("getLastElementAsString", types); 84 value = getValue.invoke(ben, margs); 85 buffer.setLength(0); 86 buffer.append("main.obj.CodeSource: "); 87 Debug.displayClassInfo(value.getClass(), buffer, false); 88 log(buffer.toString()); 89 } 90 catch (Exception e) 91 { 92 e.printStackTrace(); 93 log("Failed to invoke getLastElementAsString: " + e); 94 } 95 96 cache.put("/a/b/c", "ben", ben); 97 assertNotNull(cache.get("/a/b/c")); 98 assertEquals(cache.get("/a/b/c", "ben"), ben); 99 Object obj = cache.get("/a/b/c", "ben"); 100 101 Class claszAddr = ucl0.loadClass(ADDRESS_CLASSNAME); 102 buffer = new StringBuffer ("Address Info"); 103 Debug.displayClassInfo(claszAddr, buffer, false); 104 log(buffer.toString()); 105 Object addr = claszAddr.newInstance(); 106 try 107 { 108 { 109 Class [] types = {String .class}; 110 Method setValue = claszAddr.getMethod("setCity", types); 111 Object [] margs = {"SF"}; 112 value = setValue.invoke(addr, margs); 113 } 114 115 { 116 Class [] types = {claszAddr}; 117 Method setValue = clasz1.getMethod("setAddress", types); 118 Object [] margs = {addr}; 119 value = setValue.invoke(obj, margs); 120 } 121 122 Class [] types = {}; 123 Class [] margs = {}; 124 Method getValue = clasz1.getMethod("getAddress", types); 125 value = getValue.invoke(obj, margs); 126 buffer.setLength(0); 127 buffer.append("main.obj.CodeSource: "); 128 Debug.displayClassInfo(value.getClass(), buffer, false); 129 log(buffer.toString()); 130 } 131 catch (Exception e) 132 { 133 e.printStackTrace(); 134 log("Failed to invoke: " + e); 135 throw e; 136 } 137 138 } 139 140 void log(String msg) 141 { 142 System.out.println("-- " + msg); 143 } 144 145 public static Test suite() 146 { 147 return new TestSuite(LocalTest.class); 148 } 149 150 154 } 155 | Popular Tags |