KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.test.JBossTestCase;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NameAlreadyBoundException JavaDoc;
29 import javax.naming.NameNotFoundException JavaDoc;
30 import javax.naming.NamingEnumeration JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.NameClassPair JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /** Simple unit tests for the jndi implementation. Note that there cannot
37  * be any security related tests in this file as typically it is not run
38  * with the right classpath resources for that.
39  */

40 public class SimpleUnitTestCase extends JBossTestCase
41 {
42    /**
43     * Constructor for the SimpleUnitTestCase object
44     *
45     * @param name Test name
46     */

47    public SimpleUnitTestCase(String JavaDoc name)
48    {
49       super(name);
50    }
51
52    /**
53     * Tests that the second time you create a subcontext you get an exception.
54     *
55     * @exception Exception Description of Exception
56     */

57    public void testCreateSubcontext() throws Exception JavaDoc
58    {
59       getLog().debug("+++ testCreateSubcontext");
60       InitialContext JavaDoc ctx = getInitialContext();
61       ctx.createSubcontext("foo");
62       try
63       {
64          ctx.createSubcontext("foo");
65          fail("Second createSubcontext(foo) did NOT fail");
66       }
67       catch (NameAlreadyBoundException JavaDoc e)
68       {
69          getLog().debug("Second createSubcontext(foo) failed as expected");
70       }
71       ctx.createSubcontext("foo/bar");
72       ctx.unbind("foo/bar");
73       ctx.unbind("foo");
74    }
75
76    /** Lookup a name to test basic connectivity and lookup of a known name
77     *
78     * @throws Exception
79     */

80    public void testLookup() throws Exception JavaDoc
81    {
82       getLog().debug("+++ testLookup");
83       InitialContext JavaDoc ctx = getInitialContext();
84       Object JavaDoc obj = ctx.lookup("");
85       getLog().debug("lookup('') = "+obj);
86    }
87
88    /** List the root context
89     *
90     * @throws Exception
91     */

92    public void testListing() throws Exception JavaDoc
93    {
94       log.debug("+++ testListing");
95       InitialContext JavaDoc ctx = getInitialContext();
96       NamingEnumeration JavaDoc names = ctx.list("");
97       int count = 0;
98       while( names.hasMore() )
99       {
100          NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) names.next();
101          log.info(ncp);
102          count ++;
103       }
104       assertTrue("list count > 0 ", count > 0);
105       ctx.close();
106    }
107
108    public void testNameChanges() throws Exception JavaDoc
109    {
110       getLog().debug("+++ testNameChanges");
111       InitialContext JavaDoc ctx = getInitialContext();
112       Name JavaDoc name = ctx.getNameParser("").parse("jnp://" + getServerHost() + "/jmx");
113       Name JavaDoc copy = (Name JavaDoc) name.clone();
114       Object JavaDoc obj = ctx.lookup(name);
115       getLog().debug("lookup("+name+") = "+obj);
116       assertTrue("name.equals(copy), name="+name, name.equals(copy));
117    }
118
119    /** Lookup a name to test basic connectivity and lookup of a known name
120     *
121     * @throws Exception
122     */

123    public void testLookupFailures() throws Exception JavaDoc
124    {
125       getLog().debug("+++ testLookupFailures");
126       // Look a name that does not exist
127
Properties JavaDoc env = new Properties JavaDoc();
128       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
129       try
130       {
131          Object JavaDoc obj = ctx.lookup("__bad_name__");
132          fail("lookup(__bad_name__) should have thrown an exception, obj="+obj);
133       }
134       catch(NameNotFoundException JavaDoc e)
135       {
136          getLog().debug("lookup(__bad_name__) failed as expected", e);
137       }
138
139       // Do a lookup on a server port that does not exist
140
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
141       env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":65535/");
142       env.setProperty("jnp.disableDiscovery", "true");
143       getLog().debug("Creating InitialContext with env="+env);
144       try
145       {
146          ctx = new InitialContext JavaDoc(env);
147          Object JavaDoc obj = ctx.lookup("");
148          fail("lookup('') should have thrown an exception, obj="+obj);
149       }
150       catch(NamingException JavaDoc e)
151       {
152          getLog().debug("lookup('') failed as expected", e);
153       }
154    }
155
156    public void testHaInvoker() throws Exception JavaDoc
157    {
158       getLog().debug("+++ testHaInvoker");
159       Properties JavaDoc env = new Properties JavaDoc();
160       env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1100/");
161       getLog().debug("Creating InitialContext with env="+env);
162       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
163       getLog().debug("Created InitialContext");
164       Object JavaDoc obj = ctx.lookup("jmx");
165       getLog().debug("lookup(jmx) : "+obj);
166       Context JavaDoc invokersCtx = (Context JavaDoc) obj;
167       NamingEnumeration JavaDoc list = invokersCtx.list("");
168       while( list.hasMore() )
169       {
170          Object JavaDoc entry = list.next();
171          getLog().debug(" + "+entry);
172       }
173       ctx.close();
174    }
175
176    /**
177     * Test the creation of HA-JNDI sub-context
178     *
179     * @throws Exception
180     */

181    public void testCreateHaJndiSubcontext() throws Exception JavaDoc
182    {
183       getLog().debug("+++ testCreateHaJndiSubcontext");
184       // Lookup a name that does not exist
185
java.util.Properties JavaDoc env = new java.util.Properties JavaDoc();
186       env.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
187       env.setProperty(javax.naming.Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1100/");
188       getLog().debug("Creating InitialContext with env="+env);
189
190       InitialContext JavaDoc ctx = new javax.naming.InitialContext JavaDoc(env);
191       Object JavaDoc obj = ctx.lookup("");
192       getLog().debug("lookup('') against HA-JNDI succeeded as expected, obj="+obj);
193
194       ctx.createSubcontext("foo");
195       try
196       {
197          ctx.createSubcontext("foo");
198          fail("Second createSubcontext(foo) against HA-JNDI did NOT fail");
199       }
200       catch (javax.naming.NameAlreadyBoundException JavaDoc e)
201       {
202          getLog().debug("Second createSubcontext(foo) against HA-JNDI failed as expected");
203       }
204
205       getLog().debug("binding foo/bar");
206       ctx.createSubcontext("foo/bar");
207
208       getLog().debug("unbinding foo/bar");
209       ctx.unbind("foo/bar");
210
211       getLog().debug("unbinding foo");
212       ctx.unbind("foo");
213
214       try
215       {
216          obj = ctx.lookup("foo");
217          fail("lookup(foo) should have thrown an exception");
218       }
219       catch(NameNotFoundException JavaDoc e)
220       {
221          getLog().debug("lookup(foo) failed as expected", e);
222       }
223    }
224
225    /** Test discovery with the partition name specified
226     *
227     * @throws Exception
228     */

229    public void testHaPartitionName() throws Exception JavaDoc
230    {
231       getLog().debug("+++ testHaPartitionName");
232       Properties JavaDoc env = new Properties JavaDoc();
233       String JavaDoc serverHost = getServerHost();
234       env.setProperty(Context.PROVIDER_URL, "jnp://" + serverHost + ":65535/");
235       env.setProperty("jnp.localAddress", serverHost);
236       env.setProperty("jnp.partitionName", "DefaultPartition");
237       getLog().debug("Creating InitialContext with env="+env);
238       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
239       getLog().debug("Created InitialContext");
240       Object JavaDoc obj = ctx.lookup("invokers");
241       getLog().debug("lookup(invokers) : "+obj);
242       Context JavaDoc invokersCtx = (Context JavaDoc) obj;
243       NamingEnumeration JavaDoc list = invokersCtx.list("");
244       while( list.hasMore() )
245       {
246          Object JavaDoc entry = list.next();
247          getLog().debug(" + "+entry);
248       }
249       ctx.close();
250
251       // Now test discovery with a non-existent partition name
252
env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":65535/");
253       env.setProperty("jnp.partitionName", "__NotTheDefaultPartition__");
254       try
255       {
256          ctx = new InitialContext JavaDoc(env);
257          getLog().debug("Created InitialContext");
258          obj = ctx.lookup("invokers");
259          fail("Was able to lookup(invokers): "+obj);
260       }
261       catch(NamingException JavaDoc e)
262       {
263          getLog().debug("Partition specific discovery failed as expected", e);
264       }
265    }
266
267    /** Test naming discovery with an explicit port
268     *
269     * @throws Exception
270     */

271    public void testDiscoveryPort() throws Exception JavaDoc
272    {
273       getLog().debug("+++ testDiscoveryPort");
274       Properties JavaDoc env = new Properties JavaDoc();
275       String JavaDoc serverHost = getServerHost();
276       env.setProperty(Context.PROVIDER_URL, "jnp://" + serverHost + ":65535/");
277       env.setProperty("jnp.localAddress", serverHost);
278       env.setProperty("jnp.discoveryPort", "1102");
279       getLog().debug("Creating InitialContext with env="+env);
280       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
281       getLog().debug("Created InitialContext");
282       Object JavaDoc obj = ctx.lookup("invokers");
283       getLog().debug("lookup(invokers) : "+obj);
284       Context JavaDoc invokersCtx = (Context JavaDoc) obj;
285       NamingEnumeration JavaDoc list = invokersCtx.list("");
286       while( list.hasMore() )
287       {
288          Object JavaDoc entry = list.next();
289          getLog().debug(" + "+entry);
290       }
291       ctx.close();
292    }
293
294    public void testHttpInvoker() throws Exception JavaDoc
295    {
296       getLog().debug("+++ testHttpInvoker");
297       Properties JavaDoc env = new Properties JavaDoc();
298       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
299       env.setProperty(Context.PROVIDER_URL, "http://" + getServerHost() + ":8080/invoker/JNDIFactory");
300       getLog().debug("Creating InitialContext with env="+env);
301       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
302       getLog().debug("Created InitialContext");
303       Object JavaDoc obj = ctx.lookup("invokers");
304       getLog().debug("lookup(invokers) : "+obj);
305       Context JavaDoc invokersCtx = (Context JavaDoc) obj;
306       NamingEnumeration JavaDoc list = invokersCtx.list("");
307       while( list.hasMore() )
308       {
309          Object JavaDoc entry = list.next();
310          getLog().debug(" + "+entry);
311       }
312       ctx.close();
313    }
314
315 }
316
Popular Tags