1 22 package org.jboss.test.naming.test; 23 24 import org.jboss.test.JBossTestCase; 25 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NameAlreadyBoundException ; 29 import javax.naming.NameNotFoundException ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 import javax.naming.Name ; 33 import javax.naming.NameClassPair ; 34 import java.util.Properties ; 35 36 40 public class SimpleUnitTestCase extends JBossTestCase 41 { 42 47 public SimpleUnitTestCase(String name) 48 { 49 super(name); 50 } 51 52 57 public void testCreateSubcontext() throws Exception 58 { 59 getLog().debug("+++ testCreateSubcontext"); 60 InitialContext 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 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 80 public void testLookup() throws Exception 81 { 82 getLog().debug("+++ testLookup"); 83 InitialContext ctx = getInitialContext(); 84 Object obj = ctx.lookup(""); 85 getLog().debug("lookup('') = "+obj); 86 } 87 88 92 public void testListing() throws Exception 93 { 94 log.debug("+++ testListing"); 95 InitialContext ctx = getInitialContext(); 96 NamingEnumeration names = ctx.list(""); 97 int count = 0; 98 while( names.hasMore() ) 99 { 100 NameClassPair ncp = (NameClassPair ) 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 109 { 110 getLog().debug("+++ testNameChanges"); 111 InitialContext ctx = getInitialContext(); 112 Name name = ctx.getNameParser("").parse("jnp://" + getServerHost() + "/jmx"); 113 Name copy = (Name ) name.clone(); 114 Object obj = ctx.lookup(name); 115 getLog().debug("lookup("+name+") = "+obj); 116 assertTrue("name.equals(copy), name="+name, name.equals(copy)); 117 } 118 119 123 public void testLookupFailures() throws Exception 124 { 125 getLog().debug("+++ testLookupFailures"); 126 Properties env = new Properties (); 128 InitialContext ctx = new InitialContext (env); 129 try 130 { 131 Object obj = ctx.lookup("__bad_name__"); 132 fail("lookup(__bad_name__) should have thrown an exception, obj="+obj); 133 } 134 catch(NameNotFoundException e) 135 { 136 getLog().debug("lookup(__bad_name__) failed as expected", e); 137 } 138 139 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 (env); 147 Object obj = ctx.lookup(""); 148 fail("lookup('') should have thrown an exception, obj="+obj); 149 } 150 catch(NamingException e) 151 { 152 getLog().debug("lookup('') failed as expected", e); 153 } 154 } 155 156 public void testHaInvoker() throws Exception 157 { 158 getLog().debug("+++ testHaInvoker"); 159 Properties env = new Properties (); 160 env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1100/"); 161 getLog().debug("Creating InitialContext with env="+env); 162 InitialContext ctx = new InitialContext (env); 163 getLog().debug("Created InitialContext"); 164 Object obj = ctx.lookup("jmx"); 165 getLog().debug("lookup(jmx) : "+obj); 166 Context invokersCtx = (Context ) obj; 167 NamingEnumeration list = invokersCtx.list(""); 168 while( list.hasMore() ) 169 { 170 Object entry = list.next(); 171 getLog().debug(" + "+entry); 172 } 173 ctx.close(); 174 } 175 176 181 public void testCreateHaJndiSubcontext() throws Exception 182 { 183 getLog().debug("+++ testCreateHaJndiSubcontext"); 184 java.util.Properties env = new java.util.Properties (); 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 ctx = new javax.naming.InitialContext (env); 191 Object 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 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 e) 220 { 221 getLog().debug("lookup(foo) failed as expected", e); 222 } 223 } 224 225 229 public void testHaPartitionName() throws Exception 230 { 231 getLog().debug("+++ testHaPartitionName"); 232 Properties env = new Properties (); 233 String 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 ctx = new InitialContext (env); 239 getLog().debug("Created InitialContext"); 240 Object obj = ctx.lookup("invokers"); 241 getLog().debug("lookup(invokers) : "+obj); 242 Context invokersCtx = (Context ) obj; 243 NamingEnumeration list = invokersCtx.list(""); 244 while( list.hasMore() ) 245 { 246 Object entry = list.next(); 247 getLog().debug(" + "+entry); 248 } 249 ctx.close(); 250 251 env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":65535/"); 253 env.setProperty("jnp.partitionName", "__NotTheDefaultPartition__"); 254 try 255 { 256 ctx = new InitialContext (env); 257 getLog().debug("Created InitialContext"); 258 obj = ctx.lookup("invokers"); 259 fail("Was able to lookup(invokers): "+obj); 260 } 261 catch(NamingException e) 262 { 263 getLog().debug("Partition specific discovery failed as expected", e); 264 } 265 } 266 267 271 public void testDiscoveryPort() throws Exception 272 { 273 getLog().debug("+++ testDiscoveryPort"); 274 Properties env = new Properties (); 275 String 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 ctx = new InitialContext (env); 281 getLog().debug("Created InitialContext"); 282 Object obj = ctx.lookup("invokers"); 283 getLog().debug("lookup(invokers) : "+obj); 284 Context invokersCtx = (Context ) obj; 285 NamingEnumeration list = invokersCtx.list(""); 286 while( list.hasMore() ) 287 { 288 Object entry = list.next(); 289 getLog().debug(" + "+entry); 290 } 291 ctx.close(); 292 } 293 294 public void testHttpInvoker() throws Exception 295 { 296 getLog().debug("+++ testHttpInvoker"); 297 Properties env = new Properties (); 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 ctx = new InitialContext (env); 302 getLog().debug("Created InitialContext"); 303 Object obj = ctx.lookup("invokers"); 304 getLog().debug("lookup(invokers) : "+obj); 305 Context invokersCtx = (Context ) obj; 306 NamingEnumeration list = invokersCtx.list(""); 307 while( list.hasMore() ) 308 { 309 Object entry = list.next(); 310 getLog().debug(" + "+entry); 311 } 312 ctx.close(); 313 } 314 315 } 316 | Popular Tags |