1 22 package org.jboss.test.cluster.test; 23 24 import java.util.Hashtable ; 25 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 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 42 public class HAJndiTestCase 43 extends JBossClusteredTestCase 44 { 45 private static final String NODE0 = System.getProperty("node0"); 47 private static final String NODE0_JNDI = System.getProperty("node0.jndi.url"); 48 private static final String NODE1_JNDI = System.getProperty("node1.jndi.url"); 49 private static final String NODE0_HAJNDI = System.getProperty("node0.hajndi.url"); 50 private static final String NODE1_HAJNDI = System.getProperty("node1.hajndi.url"); 51 52 private static final String SUBCON1 = "subcontext1"; 54 private static final String SUBCON1A = "subcontext1a"; 55 private static final String SUBCON2 = "subcontext2"; 56 57 private static final String LOCAL0_KEY = "org.jboss.test.cluster.test.Local0Key"; 59 private static final String LOCAL0_VALUE = "Local0Value"; 60 private static final String LOCAL1_KEY = "org.jboss.test.cluster.test.Local1Key"; 61 private static final String LOCAL1_VALUE = "Local1Value"; 62 private static final String GLOBAL0_KEY = "org.jboss.test.cluster.test.Global0Key"; 63 private static final String GLOBAL0_VALUE = "Global0Value"; 64 private static final String CALLED_HOME_KEY = "cluster.ejb.CalledHome"; 65 private static final String JNDI_KEY = "org.jboss.test.cluster.test.JNDIKey"; 66 private static final String JNDI_KEY3 = "org.jboss.test.cluster.test.JNDIKey3"; 67 private static final String SUB1_KEY = "org.jboss.test.cluster.test.Sub1Key"; 68 private static final String SUB2_KEY = "org.jboss.test.cluster.test.Sub2Key"; 69 private static final String SUB1A_KEY = "org.jboss.test.cluster.test.Sub1aKey"; 70 private static final String JNDI_VALUE1 = "JNDIValue1"; 71 private static final String JNDI_VALUE2 = "JNDIValue2"; 72 private static final String JNDI_VALUE3 = "JNDIValue3"; 73 private static final String SUB1_VALUE = "Sub1Value"; 74 private static final String SUB2_VALUE = "Sub2Value"; 75 private static final String SUB1A_VALUE = "Sub1aValue"; 76 77 public HAJndiTestCase(String name) 78 { 79 super(name); 80 } 81 82 public static Test suite() throws Exception 83 { 84 Test t1 = JBossClusteredTestCase.getDeploySetup(HAJndiTestCase.class, "cross-server.jar"); 85 return t1; 86 } 87 88 93 public void testLocalBinding() 94 throws Exception 95 { 96 getLog().debug("HAJndiTestCase.testLocalBinding()"); 97 validateUrls(); 98 99 Context naming = getContext(NODE0_JNDI); 101 naming.bind(LOCAL0_KEY, LOCAL0_VALUE); 102 closeContext(naming); 103 104 naming = getContext(NODE0_JNDI); 106 String value = (String )lookup(naming, LOCAL0_KEY); 107 closeContext(naming); 108 assertEquals("lookup local binding on same server", LOCAL0_VALUE, value); 109 110 naming = getContext(NODE1_JNDI); 112 value = (String )lookup(naming, LOCAL0_KEY); 113 closeContext(naming); 114 assertNull("lookup local binding on different server in cluster", value); 115 116 naming = getContext(NODE0_HAJNDI); 118 value = (String )lookup(naming, LOCAL0_KEY); 119 closeContext(naming); 120 assertEquals("lookup local binding on same server using HA-JNDI", LOCAL0_VALUE, value); 121 122 naming = getContext(NODE1_HAJNDI); 124 value = (String )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 134 public void testHAJndiBinding() 135 throws Exception 136 { 137 getLog().debug("HAJndiTestCase.testHAJndiBinding()"); 138 validateUrls(); 139 140 Context naming = getContext(NODE0_HAJNDI); 142 naming.bind(GLOBAL0_KEY, GLOBAL0_VALUE); 143 closeContext(naming); 144 145 naming = getContext(NODE0_JNDI); 147 String value = (String )lookup(naming, GLOBAL0_KEY); 148 closeContext(naming); 149 assertNull("lookup HA-JNDI binding on same server using local JNDI", value); 150 151 naming = getContext(NODE1_JNDI); 153 value = (String )lookup(naming, GLOBAL0_KEY); 154 closeContext(naming); 155 assertNull("lookup HA-JNDI binding on different server in cluster using local JNDI", value); 156 157 naming = getContext(NODE0_HAJNDI); 159 value = (String )lookup(naming, GLOBAL0_KEY); 160 closeContext(naming); 161 assertEquals("lookup HA-JNDI binding on same server using HA-JNDI", GLOBAL0_VALUE, value); 162 163 naming = getContext(NODE1_HAJNDI); 165 value = (String )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 176 public void testHAJndiOperations() 177 throws Exception 178 { 179 getLog().debug("HAJndiTestCase.testHAJndiOperations()"); 180 validateUrls(); 181 182 Context naming = getContext(NODE0_HAJNDI); 184 naming.bind(JNDI_KEY, JNDI_VALUE1); 185 186 String value = (String )lookup(naming, JNDI_KEY); 188 assertEquals("lookup after initial HA-JNDI binding operation", JNDI_VALUE1, value); 189 190 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 ne) 197 { 198 assertTrue("binding key a second time in HA-JNDI should throw NamingException", ne instanceof NamingException ); 199 } 200 201 naming.rebind(JNDI_KEY, JNDI_VALUE2); 203 204 value = (String )lookup(naming, JNDI_KEY); 206 assertEquals("lookup after HA-JNDI rebind operation", JNDI_VALUE2, value); 207 208 naming.unbind(JNDI_KEY); 210 211 value = (String )lookup(naming, JNDI_KEY); 213 assertNull("lookup after HA-JNDI unbind operation", value); 214 215 closeContext(naming); 216 } 217 218 223 public void testHAJndiSubcontexts() 224 throws Exception 225 { 226 getLog().debug("HAJndiTestCase.testHAJndiSubcontexts()"); 227 validateUrls(); 228 229 Context naming = getContext(NODE0_HAJNDI); 231 Context sub1 = naming.createSubcontext(SUBCON1); 232 Context sub2 = naming.createSubcontext(SUBCON2); 233 Context sub1a = sub1.createSubcontext(SUBCON1A); 234 235 sub1.bind(SUB1_KEY, SUB1_VALUE); 237 sub2.bind(SUB2_KEY, SUB2_VALUE); 238 sub1a.bind(SUB1A_KEY, SUB1A_VALUE); 239 240 naming.close(); 242 sub1.close(); 243 sub2.close(); 244 sub1a.close(); 245 246 naming = getContext(NODE0_HAJNDI); 248 sub1 = (Context )lookup(naming, SUBCON1); 249 String value = (String )lookup(sub1, SUB1_KEY); 250 assertEquals("lookup subcontext HA-JNDI binding on same server in cluster using HA-JNDI", SUB1_VALUE, value); 251 252 naming = getContext(NODE1_HAJNDI); 254 sub2 = (Context )lookup(naming, SUBCON2); 255 value = (String )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 )lookup(naming, SUBCON1 + "/" + SUBCON1A); 259 value = (String )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 274 public void testEJBBinding() 275 throws Exception 276 { 277 getLog().debug("HAJndiTestCase.testEJBBinding()"); 278 validateUrls(); 279 280 282 Context naming = getContext(NODE0_JNDI); 284 CalledHome home = (CalledHome)lookup(naming, CALLED_HOME_KEY); 285 if (home != null) 286 { 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 naming = getContext(NODE1_JNDI); 295 home = (CalledHome)lookup(naming, CALLED_HOME_KEY); 296 if (home != null) 297 { 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 naming = getContext(NODE0_HAJNDI); 306 home = (CalledHome)lookup(naming, CALLED_HOME_KEY); 307 if (home != null) 308 { 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 naming = getContext(NODE1_HAJNDI); 317 home = (CalledHome)lookup(naming, CALLED_HOME_KEY); 318 if (home != null) 319 { 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 333 public void testAutoDiscovery() 334 throws Exception 335 { 336 getLog().debug("HAJndiTestCase.testAutoDiscovery()"); 337 validateUrls(); 338 339 if (NODE0 != null && NODE0.equalsIgnoreCase("localhost") ) 341 { 342 getLog().debug("testAutoDiscovery() - test skipped because node0=localhost"); 343 return; 344 } 345 346 Context naming = getContext(NODE1_JNDI); 348 naming.bind(LOCAL1_KEY, LOCAL1_VALUE); 349 closeContext(naming); 350 351 naming = getContext(NODE0_HAJNDI); 353 naming.bind(JNDI_KEY3, JNDI_VALUE3); 354 closeContext(naming); 355 356 naming = getAutoDiscoveryContext(false); 358 359 String value = (String )lookup(naming, LOCAL1_KEY); 361 assertEquals("local lookup with AutoDiscovery enabled", LOCAL1_VALUE, value); 362 363 value = (String )lookup(naming, JNDI_KEY3); 365 assertEquals("lookup of HA-JNDI binding with AutoDiscovery enabled", JNDI_VALUE3, value); 366 367 closeContext(naming); 369 naming = getAutoDiscoveryContext(true); 370 371 value = (String )lookup(naming, LOCAL1_KEY); 373 assertNull("local lookup with AutoDiscovery disabled", value); 374 375 value = (String )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 getContext(String url) 384 throws Exception 385 { 386 Hashtable env = new Hashtable (); 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 naming = new InitialContext (env); 392 return naming; 393 394 } 395 396 private Context getAutoDiscoveryContext(boolean autoDisabled) 397 throws Exception 398 { 399 Hashtable env = new Hashtable (); 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 naming = new InitialContext (env); 407 return naming; 408 409 } 410 411 private void closeContext(Context context) 412 { 413 try 414 { 415 context.close(); 416 } 417 catch (NamingException e) 418 { 419 } 421 } 422 423 private Object lookup(Context context, String name) 424 { 425 try 426 { 427 Object o = context.lookup(name); 428 log.info(name + " binding value: " + o); 429 return o; 430 } 431 catch (NamingException e) 432 { 433 log.info("Name " + name + " not found."); 434 return null; 435 } 436 } 437 438 private void validateUrls() 439 throws Exception 440 { 441 if (NODE0_JNDI == null) 442 throw new Exception ("node0.jndi.url not defined."); 443 444 if (NODE1_JNDI == null) 445 throw new Exception ("node1.jndi.url not defined."); 446 447 if (NODE0_HAJNDI == null) 448 throw new Exception ("node0.hajndi.url not defined."); 449 450 if (NODE1_HAJNDI == null) 451 throw new Exception ("node1.hajndi.url not defined."); 452 453 } 454 455 } 456 | Popular Tags |