Search in sources :

Example 1 with SimpleNamingContextBuilder

use of org.springframework.tests.mock.jndi.SimpleNamingContextBuilder in project spring-framework by spring-projects.

the class SimpleNamingContextTests method testNamingContextBuilder.

@Test
public void testNamingContextBuilder() throws NamingException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    InitialContextFactory factory = builder.createInitialContextFactory(null);
    DataSource ds = new StubDataSource();
    builder.bind("java:comp/env/jdbc/myds", ds);
    Object obj = new Object();
    builder.bind("myobject", obj);
    Context context1 = factory.getInitialContext(null);
    assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
    Hashtable<String, String> env2 = new Hashtable<>();
    env2.put("key1", "value1");
    Context context2 = factory.getInitialContext(env2);
    assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
    assertTrue("Correct environment", context2.getEnvironment() != env2);
    assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
    Integer i = new Integer(0);
    context1.rebind("myinteger", i);
    String s = "";
    context2.bind("mystring", s);
    Context context3 = (Context) context2.lookup("");
    context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
    context3.unbind("myobject");
    assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
    context3.addToEnvironment("key2", "value2");
    assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
    context3.removeFromEnvironment("key1");
    assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
    assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
    try {
        context1.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
    assertTrue("Correct String registered", context1.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
    try {
        context2.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
    assertTrue("Correct String registered", context2.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
    try {
        context3.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
    assertTrue("Correct String registered", context3.lookup("mystring") == s);
    Map<String, Binding> bindingMap = new HashMap<>();
    NamingEnumeration<?> bindingEnum = context3.listBindings("");
    while (bindingEnum.hasMoreElements()) {
        Binding binding = (Binding) bindingEnum.nextElement();
        bindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
    assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
    Context jdbcContext = (Context) context3.lookup("jdbc");
    jdbcContext.bind("mydsX", ds);
    Map<String, Binding> subBindingMap = new HashMap<>();
    NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
    while (subBindingEnum.hasMoreElements()) {
        Binding binding = (Binding) subBindingEnum.nextElement();
        subBindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
    assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
    assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
    assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
    assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
    context1.createSubcontext("jdbc").bind("sub/subds", ds);
    Map<String, String> pairMap = new HashMap<>();
    NamingEnumeration<?> pairEnum = context2.list("jdbc");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
    Context subContext = (Context) context2.lookup("jdbc/sub");
    Map<String, String> subPairMap = new HashMap<>();
    NamingEnumeration<?> subPairEnum = subContext.list("");
    while (subPairEnum.hasMoreElements()) {
        NameClassPair pair = (NameClassPair) subPairEnum.next();
        subPairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
    pairMap.clear();
    pairEnum = context1.list("jdbc/");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) InitialContext(javax.naming.InitialContext) SimpleNamingContext(org.springframework.tests.mock.jndi.SimpleNamingContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) InitialContextFactory(javax.naming.spi.InitialContextFactory) DataSource(javax.sql.DataSource) NameClassPair(javax.naming.NameClassPair) Test(org.junit.Test)

Example 2 with SimpleNamingContextBuilder

use of org.springframework.tests.mock.jndi.SimpleNamingContextBuilder in project spring-framework by spring-projects.

the class SimpleNamingContextTests method testCreateInitialContext.

/**
	 * Demonstrates how emptyActivatedContextBuilder() method can be
	 * used repeatedly, and how it affects creating a new InitialContext()
	 */
@Test
public void testCreateInitialContext() throws Exception {
    SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    String name = "foo";
    Object o = new Object();
    builder.bind(name, o);
    // Check it affects JNDI
    Context ctx = new InitialContext();
    assertTrue(ctx.lookup(name) == o);
    // Check it returns mutable contexts
    ctx.unbind(name);
    try {
        ctx = new InitialContext();
        ctx.lookup(name);
        fail("Should have thrown NamingException");
    } catch (NamingException ex) {
    // expected
    }
    // Check the same call will work again, but the context is empty
    builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    try {
        ctx = new InitialContext();
        ctx.lookup(name);
        fail("Should have thrown NamingException");
    } catch (NamingException ex) {
    // expected
    }
    Object o2 = new Object();
    builder.bind(name, o2);
    assertEquals(ctx.lookup(name), o2);
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) InitialContext(javax.naming.InitialContext) SimpleNamingContext(org.springframework.tests.mock.jndi.SimpleNamingContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Aggregations

Context (javax.naming.Context)2 InitialContext (javax.naming.InitialContext)2 Test (org.junit.Test)2 SimpleNamingContext (org.springframework.tests.mock.jndi.SimpleNamingContext)2 SimpleNamingContextBuilder (org.springframework.tests.mock.jndi.SimpleNamingContextBuilder)2 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Binding (javax.naming.Binding)1 NameClassPair (javax.naming.NameClassPair)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 InitialContextFactory (javax.naming.spi.InitialContextFactory)1 DataSource (javax.sql.DataSource)1