KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > naming > test > ImplUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.naming.test;
23
24
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.ObjectInputStream JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NameAlreadyBoundException JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.Name JavaDoc;
38 import javax.naming.spi.StateFactory JavaDoc;
39 import javax.naming.spi.ObjectFactory JavaDoc;
40
41 import junit.framework.TestCase;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44 import junit.textui.TestRunner;
45
46 import org.jboss.logging.Logger;
47
48 /** Simple unit tests for the jndi implementation.
49  *
50  * @author Scott.Stark@jboss.org
51  * @version $Revision: 58115 $
52  */

53 public class ImplUnitTestCase extends TestCase
54 {
55    static final Logger log = Logger.getLogger(ImplUnitTestCase.class);
56
57    
58    /**
59     * Constructor for the SimpleUnitTestCase object
60     *
61     * @param name Test name
62     */

63    public ImplUnitTestCase(String JavaDoc name)
64    {
65       super(name);
66    }
67
68    /**
69     * Tests that the second time you create a subcontext you get an exception.
70     *
71     * @exception Exception Description of Exception
72     */

73    public void testCreateSubcontext() throws Exception JavaDoc
74    {
75       log.debug("+++ testCreateSubcontext");
76       InitialContext JavaDoc ctx = getInitialContext();
77       ctx.createSubcontext("foo");
78       try
79       {
80          ctx.createSubcontext("foo");
81          fail("Second createSubcontext(foo) did NOT fail");
82       }
83       catch (NameAlreadyBoundException JavaDoc e)
84       {
85          log.debug("Second createSubcontext(foo) failed as expected");
86       }
87       ctx.createSubcontext("foo/bar");
88       ctx.unbind("foo/bar");
89       ctx.unbind("foo");
90    }
91
92    /** Lookup a name to test basic connectivity and lookup of a known name
93     *
94     * @throws Exception
95     */

96    public void testLookup() throws Exception JavaDoc
97    {
98       log.debug("+++ testLookup");
99       InitialContext JavaDoc ctx = getInitialContext();
100       Object JavaDoc obj = ctx.lookup("");
101       log.debug("lookup('') = "+obj);
102    }
103
104    public void testEncPerf() throws Exception JavaDoc
105    {
106       int count = Integer.getInteger("jbosstest.threadcount", 10).intValue();
107       int iterations = Integer.getInteger("jbosstest.iterationcount", 1000).intValue();
108       log.info("Creating "+count+"threads doing "+iterations+" iterations");
109       InitialContext JavaDoc ctx = getInitialContext();
110       URL JavaDoc[] empty = {};
111       Thread JavaDoc[] testThreads = new Thread JavaDoc[count];
112       for(int t = 0; t < count; t ++)
113       {
114          ClassLoader JavaDoc encLoader = URLClassLoader.newInstance(empty);
115          Thread.currentThread().setContextClassLoader(encLoader);
116          Runnable JavaDoc test = new ENCTester(ctx, iterations);
117          Thread JavaDoc thr = new Thread JavaDoc(test, "Tester#"+t);
118          thr.setContextClassLoader(encLoader);
119          thr.start();
120          testThreads[t] = thr;
121       }
122
123       for(int t = 0; t < count; t ++)
124       {
125          Thread JavaDoc thr = testThreads[t];
126          thr.join();
127       }
128    }
129
130    /**
131     *
132     * @throws NamingException
133     */

134    public void testFactorySupport() throws NamingException JavaDoc
135    {
136       log.info("+++ testFactorySupport");
137       NotSerializableObject nso = new NotSerializableObject( "nsc" );
138       Context JavaDoc ctx = getInitialContext();
139
140       try
141       {
142          ctx.bind("test", nso);
143          fail();
144       }
145       catch( NamingException JavaDoc ex )
146       {
147          log.debug("bind failed as expected", ex);
148       }
149
150       Properties JavaDoc env = new Properties JavaDoc();
151       env.setProperty(Context.STATE_FACTORIES, TestFactory.class.getName());
152       env.setProperty(Context.OBJECT_FACTORIES, TestFactory.class.getName());
153       ctx = new InitialContext JavaDoc(env);
154
155       log.debug("Retest with TestFactory enabled");
156       ctx.bind("test", nso);
157
158       Object JavaDoc boundObject = ctx.lookup( "test" );
159       assertNotNull( boundObject );
160       // make sure it's of type NotSerializableObject
161
NotSerializableObject nso2 = (NotSerializableObject) boundObject;
162       assertEquals( nso.getId(), nso2.getId() );
163    }
164
165    static InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc
166    {
167       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
168       return ctx;
169    }
170
171    private static class ENCTester implements Runnable JavaDoc
172    {
173       Context JavaDoc enc;
174       int iterations;
175
176       ENCTester(InitialContext JavaDoc ctx, int iterations) throws Exception JavaDoc
177       {
178          log.info("CL: "+Thread.currentThread().getContextClassLoader());
179          this.iterations = iterations;
180          enc = (Context JavaDoc) ctx.lookup("java:comp");
181          enc = enc.createSubcontext("env");
182          enc.bind("int", new Integer JavaDoc(1));
183          enc.bind("double", new Double JavaDoc(1.234));
184          enc.bind("string", "str");
185          enc.bind("url", new URL JavaDoc("http://www.jboss.org"));
186       }
187
188       public void run()
189       {
190          try
191          {
192             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
193             for(int i = 0; i < iterations; i ++)
194             {
195                Integer JavaDoc i1 = (Integer JavaDoc) enc.lookup("int");
196                log.debug("int: "+i1);
197                i1 = (Integer JavaDoc) ctx.lookup("java:comp/env/int");
198                log.debug("java:comp/env/int: "+i1);
199                Double JavaDoc d = (Double JavaDoc) enc.lookup("double");
200                log.debug("double: "+d);
201                d = (Double JavaDoc) ctx.lookup("java:comp/env/double");
202                log.debug("java:comp/env/double: "+d);
203                String JavaDoc s = (String JavaDoc) enc.lookup("string");
204                log.debug("string: "+s);
205                s = (String JavaDoc) ctx.lookup("java:comp/env/string");
206                log.debug("java:comp/env/string: "+s);
207                URL JavaDoc u = (URL JavaDoc) enc.lookup("url");
208                log.debug("url: "+u);
209                u = (URL JavaDoc) ctx.lookup("java:comp/env/url");
210                log.debug("java:comp/env/url: "+u);
211             }
212          }
213          catch(Exception JavaDoc e)
214          {
215             e.printStackTrace();
216          }
217       }
218    }
219
220    private static class NotSerializableObject
221    {
222       protected String JavaDoc id;
223       
224       public NotSerializableObject() {}
225       
226       public NotSerializableObject( String JavaDoc id )
227       {
228          this.id = id;
229       }
230       
231       public String JavaDoc getId()
232       {
233          return id;
234       }
235       
236       public String JavaDoc toString()
237       {
238          return "NotSerializableObject<" + getId() + ">";
239       }
240    }
241    
242    private static class SerializableObject extends NotSerializableObject
243       implements Serializable JavaDoc
244    {
245       private static long serialVersionUID = 1;
246
247       public SerializableObject () {}
248       
249       public SerializableObject (String JavaDoc id)
250       {
251          super( id );
252       }
253       
254       public String JavaDoc toString()
255       {
256          return "SerializableObject<" + getId() + ">";
257       }
258       
259       private void writeObject(ObjectOutputStream JavaDoc out)
260          throws IOException JavaDoc
261       {
262          out.writeObject(getId());
263       }
264       private void readObject(ObjectInputStream JavaDoc in)
265          throws IOException JavaDoc, ClassNotFoundException JavaDoc
266       {
267          id = (String JavaDoc) in.readObject();
268       }
269       
270    }
271    
272    
273    public static class TestFactory implements StateFactory JavaDoc, ObjectFactory JavaDoc
274    {
275       public Object JavaDoc getStateToBind (Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
276          Hashtable JavaDoc environment) throws NamingException JavaDoc
277       {
278          if( obj instanceof NotSerializableObject )
279          {
280             String JavaDoc id = ((NotSerializableObject) obj).getId();
281             return new SerializableObject( id );
282          }
283          
284          return null;
285       }
286
287       public Object JavaDoc getObjectInstance (Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
288          Hashtable JavaDoc env) throws Exception JavaDoc
289       {
290          log.debug("getObjectInstance, obj:" + obj + ", name: " + name
291             + ", nameCtx: " + nameCtx +", env: "+env);
292          if( obj instanceof SerializableObject )
293          {
294             String JavaDoc id = ((SerializableObject) obj).getId();
295             return new NotSerializableObject( id );
296          }
297          
298          return null;
299       }
300    }
301
302    public static Test suite()
303    {
304       TestSuite suite = new TestSuite();
305       suite.addTest(new TestSuite(ImplUnitTestCase.class));
306
307       // Create an initializer for the test suite
308
NamingServerSetup wrapper = new NamingServerSetup(suite);
309       return wrapper;
310    }
311
312    /** Used to run the testcase from the command line
313     *
314     * @param args The command line arguments
315     */

316    public static void main(String JavaDoc[] args)
317    {
318       TestRunner.run(ImplUnitTestCase.suite());
319    }
320 }
321
Popular Tags