KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > LocalTest


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 JavaDoc;
12 import java.io.File JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLClassLoader JavaDoc;
16
17 /**
18  * Simple functional tests for LegacyTreeCacheMarshaller
19  *
20  * @author Ben Wang
21  * @version $Id: LocalTest.java,v 1.12 2006/12/31 02:08:42 msurtani Exp $
22  */

23 public class LocalTest extends RegionBasedMarshallingTestBase
24 {
25    CacheImpl cache = null;
26    Transaction JavaDoc tx = null;
27    final Fqn FQN = Fqn.fromString("/myNode");
28    final String JavaDoc KEY = "key";
29    final String JavaDoc VALUE = "value";
30    Exception JavaDoc ex;
31
32
33    protected void setUp() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
61    {
62       String JavaDoc jarDir = System.getProperty("test.jar.dir");
63       File JavaDoc jar0 = new File JavaDoc(jarDir + "/testMarshall.jar");
64       URL JavaDoc[] cp0 = {jar0.toURL()};
65       URLClassLoader JavaDoc ucl0 = new URLClassLoader JavaDoc(cp0);
66       Thread.currentThread().setContextClassLoader(ucl0);
67       Class JavaDoc clasz1 = ucl0.loadClass(PERSON_CLASSNAME);
68       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Person Info");
69       Debug.displayClassInfo(clasz1, buffer, false);
70       log(buffer.toString());
71       Object JavaDoc ben = clasz1.newInstance();
72       Object JavaDoc value;
73       try
74       {
75          {
76             Class JavaDoc[] types = {String JavaDoc.class};
77             Method JavaDoc setValue = clasz1.getMethod("setName", types);
78             Object JavaDoc[] margs = {"Ben"};
79             value = setValue.invoke(ben, margs);
80          }
81          Class JavaDoc[] types = {};
82          Class JavaDoc[] margs = {};
83          Method JavaDoc 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 JavaDoc 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 JavaDoc obj = cache.get("/a/b/c", "ben");
100
101       Class JavaDoc claszAddr = ucl0.loadClass(ADDRESS_CLASSNAME);
102       buffer = new StringBuffer JavaDoc("Address Info");
103       Debug.displayClassInfo(claszAddr, buffer, false);
104       log(buffer.toString());
105       Object JavaDoc addr = claszAddr.newInstance();
106       try
107       {
108          {
109             Class JavaDoc[] types = {String JavaDoc.class};
110             Method JavaDoc setValue = claszAddr.getMethod("setCity", types);
111             Object JavaDoc[] margs = {"SF"};
112             value = setValue.invoke(addr, margs);
113          }
114
115          {
116             Class JavaDoc[] types = {claszAddr};
117             Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
118             Object JavaDoc[] margs = {addr};
119             value = setValue.invoke(obj, margs);
120          }
121
122          Class JavaDoc[] types = {};
123          Class JavaDoc[] margs = {};
124          Method JavaDoc 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 JavaDoc e)
132       {
133          e.printStackTrace();
134          log("Failed to invoke: " + e);
135          throw e;
136       }
137
138    }
139
140    void log(String JavaDoc msg)
141    {
142       System.out.println("-- " + msg);
143    }
144
145    public static Test suite()
146    {
147       return new TestSuite(LocalTest.class);
148    }
149
150    //public static void main(String[] args) {
151
// junit.textui.TestRunner.run(suite());
152
//}
153

154 }
155
Popular Tags