KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > HAJndiTestCase


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.cluster.test;
23
24 import java.util.Hashtable JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import junit.framework.Test;
31
32 import org.jboss.test.cluster.ejb.CalledHome;
33 import org.jboss.test.cluster.ejb.CalledRemote;
34 import org.jboss.test.JBossClusteredTestCase;
35
36 /**
37  * HA-JNDI clustering tests.
38  *
39  * @author Jerry Gauthier
40  * @version $Revision: 45407 $
41  */

42 public class HAJndiTestCase
43       extends JBossClusteredTestCase
44 {
45    // ENVIRONMENT PROPERTIES
46
private static final String JavaDoc NODE0 = System.getProperty("node0");
47    private static final String JavaDoc NODE0_JNDI = System.getProperty("node0.jndi.url");
48    private static final String JavaDoc NODE1_JNDI = System.getProperty("node1.jndi.url");
49    private static final String JavaDoc NODE0_HAJNDI = System.getProperty("node0.hajndi.url");
50    private static final String JavaDoc NODE1_HAJNDI = System.getProperty("node1.hajndi.url");
51    
52    // BINDING SUBCONTEXT NAMES
53
private static final String JavaDoc SUBCON1 = "subcontext1";
54    private static final String JavaDoc SUBCON1A = "subcontext1a";
55    private static final String JavaDoc SUBCON2 = "subcontext2";
56    
57    // BINDING KEYS and VALUES
58
private static final String JavaDoc LOCAL0_KEY = "org.jboss.test.cluster.test.Local0Key";
59    private static final String JavaDoc LOCAL0_VALUE = "Local0Value";
60    private static final String JavaDoc LOCAL1_KEY = "org.jboss.test.cluster.test.Local1Key";
61    private static final String JavaDoc LOCAL1_VALUE = "Local1Value";
62    private static final String JavaDoc GLOBAL0_KEY = "org.jboss.test.cluster.test.Global0Key";
63    private static final String JavaDoc GLOBAL0_VALUE = "Global0Value";
64    private static final String JavaDoc CALLED_HOME_KEY = "cluster.ejb.CalledHome";
65    private static final String JavaDoc JNDI_KEY = "org.jboss.test.cluster.test.JNDIKey";
66    private static final String JavaDoc JNDI_KEY3 = "org.jboss.test.cluster.test.JNDIKey3";
67    private static final String JavaDoc SUB1_KEY = "org.jboss.test.cluster.test.Sub1Key";
68    private static final String JavaDoc SUB2_KEY = "org.jboss.test.cluster.test.Sub2Key";
69    private static final String JavaDoc SUB1A_KEY = "org.jboss.test.cluster.test.Sub1aKey";
70    private static final String JavaDoc JNDI_VALUE1 = "JNDIValue1";
71    private static final String JavaDoc JNDI_VALUE2 = "JNDIValue2";
72    private static final String JavaDoc JNDI_VALUE3 = "JNDIValue3";
73    private static final String JavaDoc SUB1_VALUE = "Sub1Value";
74    private static final String JavaDoc SUB2_VALUE = "Sub2Value";
75    private static final String JavaDoc SUB1A_VALUE = "Sub1aValue";
76     
77    public HAJndiTestCase(String JavaDoc name)
78    {
79       super(name);
80    }
81
82    public static Test suite() throws Exception JavaDoc
83    {
84       Test t1 = JBossClusteredTestCase.getDeploySetup(HAJndiTestCase.class, "cross-server.jar");
85       return t1;
86    }
87
88    /**
89     * Test local bindings using local and HA-JNDI lookups
90     *
91     * @throws Exception
92     */

93    public void testLocalBinding()
94       throws Exception JavaDoc
95    {
96       getLog().debug("HAJndiTestCase.testLocalBinding()");
97       validateUrls();
98       
99       // bind to node0 locally
100
Context JavaDoc naming = getContext(NODE0_JNDI);
101       naming.bind(LOCAL0_KEY, LOCAL0_VALUE);
102       closeContext(naming);
103       
104       // lookup binding locally on Node0 - should succeed
105
naming = getContext(NODE0_JNDI);
106       String JavaDoc value = (String JavaDoc)lookup(naming, LOCAL0_KEY);
107       closeContext(naming);
108       assertEquals("lookup local binding on same server", LOCAL0_VALUE, value);
109       
110       // lookup binding locally on Node1 - should fail
111
naming = getContext(NODE1_JNDI);
112       value = (String JavaDoc)lookup(naming, LOCAL0_KEY);
113       closeContext(naming);
114       assertNull("lookup local binding on different server in cluster", value);
115       
116       // lookup binding using HA-JNDI on Node0 - should succeed
117
naming = getContext(NODE0_HAJNDI);
118       value = (String JavaDoc)lookup(naming, LOCAL0_KEY);
119       closeContext(naming);
120       assertEquals("lookup local binding on same server using HA-JNDI", LOCAL0_VALUE, value);
121       
122       // lookup binding using HA-JNDI on Node1 - should succeed
123
naming = getContext(NODE1_HAJNDI);
124       value = (String JavaDoc)lookup(naming, LOCAL0_KEY);
125       closeContext(naming);
126       assertEquals("lookup local binding on different server in cluster using HA-JNDI", LOCAL0_VALUE, value);
127    }
128    
129    /**
130     * Test HA-JNDI bindings using local and HA-JNDI lookups
131     *
132     * @throws Exception
133     */

134    public void testHAJndiBinding()
135       throws Exception JavaDoc
136    {
137       getLog().debug("HAJndiTestCase.testHAJndiBinding()");
138       validateUrls();
139       
140       // bind to node0 using HA-JNDI
141
Context JavaDoc naming = getContext(NODE0_HAJNDI);
142       naming.bind(GLOBAL0_KEY, GLOBAL0_VALUE);
143       closeContext(naming);
144       
145       // lookup binding locally on Node0 - should fail
146
naming = getContext(NODE0_JNDI);
147       String JavaDoc value = (String JavaDoc)lookup(naming, GLOBAL0_KEY);
148       closeContext(naming);
149       assertNull("lookup HA-JNDI binding on same server using local JNDI", value);
150       
151       // lookup binding locally on Node1 - should fail
152
naming = getContext(NODE1_JNDI);
153       value = (String JavaDoc)lookup(naming, GLOBAL0_KEY);
154       closeContext(naming);
155       assertNull("lookup HA-JNDI binding on different server in cluster using local JNDI", value);
156       
157       // lookup binding using HA-JNDI on Node0 - should succeed
158
naming = getContext(NODE0_HAJNDI);
159       value = (String JavaDoc)lookup(naming, GLOBAL0_KEY);
160       closeContext(naming);
161       assertEquals("lookup HA-JNDI binding on same server using HA-JNDI", GLOBAL0_VALUE, value);
162       
163       // lookup binding using HA-JNDI on Node1 - should succeed
164
naming = getContext(NODE1_HAJNDI);
165       value = (String JavaDoc)lookup(naming, GLOBAL0_KEY);
166       closeContext(naming);
167       assertEquals("lookup HA-JNDI binding on different server in cluster using HA-JNDI", GLOBAL0_VALUE, value);
168  
169    }
170    
171    /**
172     * Test HA-JNDI operations
173     *
174     * @throws Exception
175     */

176    public void testHAJndiOperations()
177       throws Exception JavaDoc
178    {
179       getLog().debug("HAJndiTestCase.testHAJndiOperations()");
180       validateUrls();
181       
182       // bind to node0 using HA-JNDI
183
Context JavaDoc naming = getContext(NODE0_HAJNDI);
184       naming.bind(JNDI_KEY, JNDI_VALUE1);
185      
186       // lookup binding using HA-JNDI on Node0 - should succeed
187
String JavaDoc value = (String JavaDoc)lookup(naming, JNDI_KEY);
188       assertEquals("lookup after initial HA-JNDI binding operation", JNDI_VALUE1, value);
189       
190       // bind it again - this should fail with NameAlreadyBoundException
191
try
192       {
193          naming.bind(JNDI_KEY, JNDI_VALUE1);
194          fail("binding key a second time in HA-JNDI should throw NamingException");
195       }
196       catch (NamingException JavaDoc ne)
197       {
198          assertTrue("binding key a second time in HA-JNDI should throw NamingException", ne instanceof NamingException JavaDoc);
199       }
200       
201       // rebind it using a different value
202
naming.rebind(JNDI_KEY, JNDI_VALUE2);
203       
204       // lookup binding - should return new value
205
value = (String JavaDoc)lookup(naming, JNDI_KEY);
206       assertEquals("lookup after HA-JNDI rebind operation", JNDI_VALUE2, value);
207       
208       // unbind it
209
naming.unbind(JNDI_KEY);
210       
211       // lookup binding - should fail with NamingException
212
value = (String JavaDoc)lookup(naming, JNDI_KEY);
213       assertNull("lookup after HA-JNDI unbind operation", value);
214       
215       closeContext(naming);
216    }
217    
218    /**
219     * Test HA-JNDI bindings using subcontexts
220     *
221     * @throws Exception
222     */

223    public void testHAJndiSubcontexts()
224       throws Exception JavaDoc
225    {
226       getLog().debug("HAJndiTestCase.testHAJndiSubcontexts()");
227       validateUrls();
228       
229       // create subcontexts
230
Context JavaDoc naming = getContext(NODE0_HAJNDI);
231       Context JavaDoc sub1 = naming.createSubcontext(SUBCON1);
232       Context JavaDoc sub2 = naming.createSubcontext(SUBCON2);
233       Context JavaDoc sub1a = sub1.createSubcontext(SUBCON1A);
234       
235       // bind something to each subcontext
236
sub1.bind(SUB1_KEY, SUB1_VALUE);
237       sub2.bind(SUB2_KEY, SUB2_VALUE);
238       sub1a.bind(SUB1A_KEY, SUB1A_VALUE);
239      
240       // close contexts
241
naming.close();
242       sub1.close();
243       sub2.close();
244       sub1a.close();
245      
246       // lookup bindings using HA-JNDI on Node0 - should succeed
247
naming = getContext(NODE0_HAJNDI);
248       sub1 = (Context JavaDoc)lookup(naming, SUBCON1);
249       String JavaDoc value = (String JavaDoc)lookup(sub1, SUB1_KEY);
250       assertEquals("lookup subcontext HA-JNDI binding on same server in cluster using HA-JNDI", SUB1_VALUE, value);
251       
252       // lookup bindings using HA-JNDI on Node1 - should succeed
253
naming = getContext(NODE1_HAJNDI);
254       sub2 = (Context JavaDoc)lookup(naming, SUBCON2);
255       value = (String JavaDoc)lookup(sub2, SUB2_KEY);
256       assertEquals("lookup subcontext HA-JNDI binding on different server in cluster using HA-JNDI", SUB2_VALUE, value);
257       
258       sub1a = (Context JavaDoc)lookup(naming, SUBCON1 + "/" + SUBCON1A);
259       value = (String JavaDoc)lookup(sub1a, SUB1A_KEY);
260       assertEquals("lookup nested subcontext HA-JNDI binding on different server in cluster using HA-JNDI", SUB1A_VALUE, value);
261       
262       naming.close();
263       sub1.close();
264       sub2.close();
265       sub1a.close();
266  
267    }
268
269    /**
270     * Test EJB Bindings using local and HA-JNDI lookups
271     *
272     * @throws Exception
273     */

274    public void testEJBBinding()
275       throws Exception JavaDoc
276    {
277       getLog().debug("HAJndiTestCase.testEJBBinding()");
278       validateUrls();
279       
280       // CalledHome EJB is contained in cross-server.jar and bound during deployment
281

282       // lookup binding locally on Node0 - should succeed
283
Context JavaDoc naming = getContext(NODE0_JNDI);
284       CalledHome home = (CalledHome)lookup(naming, CALLED_HOME_KEY);
285       if (home != null)
286       { // ensure that EJB is operational
287
CalledRemote remote = home.create();
288          remote.remove();
289       }
290       closeContext(naming);
291       assertNotNull("lookup EJB binding on same server using local JNDI", home);
292       
293       // lookup binding locally on Node1 - should succeed
294
naming = getContext(NODE1_JNDI);
295       home = (CalledHome)lookup(naming, CALLED_HOME_KEY);
296       if (home != null)
297       { // ensure that EJB is operational
298
CalledRemote remote = home.create();
299          remote.remove();
300       }
301       closeContext(naming);
302       assertNotNull("lookup EJB binding on different server in cluster using local JNDI", home);
303       
304       // lookup binding using HA-JNDI on Node0 - should succeed
305
naming = getContext(NODE0_HAJNDI);
306       home = (CalledHome)lookup(naming, CALLED_HOME_KEY);
307       if (home != null)
308       { // ensure that EJB is operational
309
CalledRemote remote = home.create();
310          remote.remove();
311       }
312       closeContext(naming);
313       assertNotNull("lookup EJB binding on same server using HA-JNDI", home);
314       
315       // lookup binding using HA-JNDI on Node1 - should succeed
316
naming = getContext(NODE1_HAJNDI);
317       home = (CalledHome)lookup(naming, CALLED_HOME_KEY);
318       if (home != null)
319       { // ensure that EJB is operational
320
CalledRemote remote = home.create();
321          remote.remove();
322       }
323       closeContext(naming);
324       assertNotNull("lookup EJB binding on different server in cluster using HA-JNDI", home);
325
326    }
327    
328    /**
329     * Test HA-JNDI AutoDiscovery
330     *
331     * @throws Exception
332     */

333    public void testAutoDiscovery()
334       throws Exception JavaDoc
335    {
336       getLog().debug("HAJndiTestCase.testAutoDiscovery()");
337       validateUrls();
338       
339       // this test doesn't run properly if node0=localhost
340
if (NODE0 != null && NODE0.equalsIgnoreCase("localhost") )
341       {
342          getLog().debug("testAutoDiscovery() - test skipped because node0=localhost");
343          return;
344       }
345       
346       // bind to node1 locally
347
Context JavaDoc naming = getContext(NODE1_JNDI);
348       naming.bind(LOCAL1_KEY, LOCAL1_VALUE);
349       closeContext(naming);
350       
351       // bind to node0 using HA-JNDI
352
naming = getContext(NODE0_HAJNDI);
353       naming.bind(JNDI_KEY3, JNDI_VALUE3);
354       closeContext(naming);
355      
356       //create context with AutoDiscovery enabled
357
naming = getAutoDiscoveryContext(false);
358       
359       // lookup local binding using HA-JNDI AutoDiscovery - should succeed
360
String JavaDoc value = (String JavaDoc)lookup(naming, LOCAL1_KEY);
361       assertEquals("local lookup with AutoDiscovery enabled", LOCAL1_VALUE, value);
362       
363       // lookup HA binding using HA-JNDI AutoDiscovery - should succeed
364
value = (String JavaDoc)lookup(naming, JNDI_KEY3);
365       assertEquals("lookup of HA-JNDI binding with AutoDiscovery enabled", JNDI_VALUE3, value);
366       
367       // now disable AutoDiscovery and confirm that the same lookups fail
368
closeContext(naming);
369       naming = getAutoDiscoveryContext(true);
370       
371       // lookup local binding without HA-JNDI AutoDiscovery - should fail
372
value = (String JavaDoc)lookup(naming, LOCAL1_KEY);
373       assertNull("local lookup with AutoDiscovery disabled", value);
374       
375       // lookup HA binding without HA-JNDI AutoDiscovery - should fail
376
value = (String JavaDoc)lookup(naming, JNDI_KEY3);
377       assertNull("llookup of HA-JNDI binding with AutoDiscovery disabled", value);
378       
379       closeContext(naming);
380
381    }
382   
383    private Context JavaDoc getContext(String JavaDoc url)
384       throws Exception JavaDoc
385    {
386       Hashtable JavaDoc env = new Hashtable JavaDoc();
387       env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
388       env.put(Context.PROVIDER_URL, url);
389       env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
390         
391       Context JavaDoc naming = new InitialContext JavaDoc (env);
392       return naming;
393
394    }
395    
396    private Context JavaDoc getAutoDiscoveryContext(boolean autoDisabled)
397       throws Exception JavaDoc
398    {
399       // do not add any urls to the context
400
Hashtable JavaDoc env = new Hashtable JavaDoc();
401       env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
402       env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
403       if (autoDisabled)
404          env.put("jnp.disableDiscovery", "true");
405         
406       Context JavaDoc naming = new InitialContext JavaDoc (env);
407       return naming;
408
409    }
410     
411    private void closeContext(Context JavaDoc context)
412    {
413       try
414       {
415          context.close();
416       }
417       catch (NamingException JavaDoc e)
418       {
419          // no action required
420
}
421    }
422    
423    private Object JavaDoc lookup(Context JavaDoc context, String JavaDoc name)
424    {
425       try
426       {
427          Object JavaDoc o = context.lookup(name);
428          log.info(name + " binding value: " + o);
429          return o;
430       }
431       catch (NamingException JavaDoc e)
432       {
433          log.info("Name " + name + " not found.");
434          return null;
435       }
436    }
437    
438    private void validateUrls()
439       throws Exception JavaDoc
440    {
441       if (NODE0_JNDI == null)
442          throw new Exception JavaDoc("node0.jndi.url not defined.");
443          
444       if (NODE1_JNDI == null)
445          throw new Exception JavaDoc("node1.jndi.url not defined.");
446          
447       if (NODE0_HAJNDI == null)
448          throw new Exception JavaDoc("node0.hajndi.url not defined.");
449          
450       if (NODE1_HAJNDI == null)
451          throw new Exception JavaDoc("node1.hajndi.url not defined.");
452
453    }
454
455 }
456
Popular Tags