moving .jndi, .mail and .mock.web unit tests from .testsuite to .context, .context.support, and .test bundles respectively

This commit is contained in:
Chris Beams
2008-12-17 02:20:01 +00:00
parent b326565ce5
commit 10be5f08a5
6 changed files with 199 additions and 131 deletions

View File

@@ -0,0 +1,408 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jndi;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import javax.naming.Context;
import javax.naming.NamingException;
import org.junit.Test;
import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.mock.jndi.ExpectedLookupTemplate;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
*/
public class JndiObjectFactoryBeanTests {
@Test
public void testNoJndiName() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
}
}
@Test
public void testLookupWithFullNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
jof.setJndiName("java:comp/env/foo");
jof.setResourceRef(true);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithFullNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
jof.setJndiName("java:comp/env/foo");
jof.setResourceRef(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithSchemeNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
jof.setJndiName("java:foo");
jof.setResourceRef(true);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
jof.setJndiName("java:foo");
jof.setResourceRef(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithShortNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
jof.setJndiName("foo");
jof.setResourceRef(true);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithShortNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
jof.setJndiName("foo");
jof.setResourceRef(false);
try {
jof.afterPropertiesSet();
fail("Should have thrown NamingException");
}
catch (NamingException ex) {
// expected
}
}
@Test
public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o));
jof.setJndiName("foo");
jof.setResourceRef(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == o);
}
@Test
public void testLookupWithExpectedTypeAndMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = "";
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s));
jof.setJndiName("foo");
jof.setExpectedType(String.class);
jof.afterPropertiesSet();
assertTrue(jof.getObject() == s);
}
@Test
public void testLookupWithExpectedTypeAndNoMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o));
jof.setJndiName("foo");
jof.setExpectedType(String.class);
try {
jof.afterPropertiesSet();
fail("Should have thrown NamingException");
}
catch (NamingException ex) {
assertTrue(ex.getMessage().indexOf("java.lang.String") != -1);
}
}
@Test
public void testLookupWithDefaultObject() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = "";
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s));
jof.setJndiName("myFoo");
jof.setExpectedType(String.class);
jof.setDefaultObject("myString");
jof.afterPropertiesSet();
assertEquals("myString", jof.getObject());
}
@Test
public void testLookupWithDefaultObjectAndExpectedType() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = "";
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s));
jof.setJndiName("myFoo");
jof.setExpectedType(String.class);
jof.setDefaultObject("myString");
jof.afterPropertiesSet();
assertEquals("myString", jof.getObject());
}
@Test
public void testLookupWithDefaultObjectAndExpectedTypeNoMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = "";
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s));
jof.setJndiName("myFoo");
jof.setExpectedType(String.class);
jof.setDefaultObject(Boolean.TRUE);
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
}
@Test
public void testLookupWithProxyInterface() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
jof.setJndiName("foo");
jof.setProxyInterface(ITestBean.class);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertEquals(0, tb.getAge());
proxy.setAge(99);
assertEquals(99, tb.getAge());
}
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
jof.setJndiName("myFoo");
jof.setProxyInterface(ITestBean.class);
jof.setDefaultObject(Boolean.TRUE);
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
}
@Test
public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean();
jof.setJndiTemplate(new JndiTemplate() {
public Object lookup(String name) {
if ("foo".equals(name)) {
tb.setName("tb");
return tb;
}
return null;
}
});
jof.setJndiName("foo");
jof.setProxyInterface(ITestBean.class);
jof.setLookupOnStartup(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertNull(tb.getName());
assertEquals(0, tb.getAge());
proxy.setAge(99);
assertEquals("tb", tb.getName());
assertEquals(99, tb.getAge());
}
@Test
public void testLookupWithProxyInterfaceWithNotCache() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean();
jof.setJndiTemplate(new JndiTemplate() {
public Object lookup(String name) {
if ("foo".equals(name)) {
tb.setName("tb");
tb.setAge(tb.getAge() + 1);
return tb;
}
return null;
}
});
jof.setJndiName("foo");
jof.setProxyInterface(ITestBean.class);
jof.setCache(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertEquals("tb", tb.getName());
assertEquals(1, tb.getAge());
proxy.returnsThis();
assertEquals(2, tb.getAge());
proxy.haveBirthday();
assertEquals(4, tb.getAge());
}
@Test
public void testLookupWithProxyInterfaceWithLazyLookupAndNotCache() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean();
jof.setJndiTemplate(new JndiTemplate() {
public Object lookup(String name) {
if ("foo".equals(name)) {
tb.setName("tb");
tb.setAge(tb.getAge() + 1);
return tb;
}
return null;
}
});
jof.setJndiName("foo");
jof.setProxyInterface(ITestBean.class);
jof.setLookupOnStartup(false);
jof.setCache(false);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertNull(tb.getName());
assertEquals(0, tb.getAge());
proxy.returnsThis();
assertEquals("tb", tb.getName());
assertEquals(1, tb.getAge());
proxy.returnsThis();
assertEquals(2, tb.getAge());
proxy.haveBirthday();
assertEquals(4, tb.getAge());
}
@Test
public void testLazyLookupWithoutProxyInterface() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
jof.setJndiName("foo");
jof.setLookupOnStartup(false);
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void testNotCacheWithoutProxyInterface() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
jof.setJndiName("foo");
jof.setCache(false);
jof.setLookupOnStartup(false);
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
jof.setJndiName("foo");
jof.setExpectedType(TestBean.class);
jof.setProxyInterface(ITestBean.class);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertEquals(0, tb.getAge());
proxy.setAge(99);
assertEquals(99, tb.getAge());
}
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
jof.setJndiName("foo");
jof.setExpectedType(DerivedTestBean.class);
jof.setProxyInterface(ITestBean.class);
try {
jof.afterPropertiesSet();
fail("Should have thrown NamingException");
}
catch (NamingException ex) {
assertTrue(ex.getMessage().indexOf("org.springframework.beans.DerivedTestBean") != -1);
}
}
@Test
public void testLookupWithExposeAccessContext() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
final Context mockCtx = createMock(Context.class);
expect(mockCtx.lookup("foo")).andReturn(tb);
mockCtx.close();
expectLastCall().times(2);
replay(mockCtx);
jof.setJndiTemplate(new JndiTemplate() {
protected Context createInitialContext() {
return mockCtx;
}
});
jof.setJndiName("foo");
jof.setProxyInterface(ITestBean.class);
jof.setExposeAccessContext(true);
jof.afterPropertiesSet();
assertTrue(jof.getObject() instanceof ITestBean);
ITestBean proxy = (ITestBean) jof.getObject();
assertEquals(0, tb.getAge());
proxy.setAge(99);
assertEquals(99, tb.getAge());
proxy.equals(proxy);
proxy.hashCode();
proxy.toString();
verify(mockCtx);
}
}

View File

@@ -17,13 +17,17 @@
package org.springframework.jndi;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class JndiTemplateEditorTests extends TestCase {
public class JndiTemplateEditorTests {
@Test
public void testNullIsIllegalArgument() {
try {
new JndiTemplateEditor().setAsText(null);
@@ -34,6 +38,7 @@ public class JndiTemplateEditorTests extends TestCase {
}
}
@Test
public void testEmptyStringMeansNullEnvironment() {
JndiTemplateEditor je = new JndiTemplateEditor();
je.setAsText("");
@@ -41,6 +46,7 @@ public class JndiTemplateEditorTests extends TestCase {
assertTrue(jt.getEnvironment() == null);
}
@Test
public void testCustomEnvironment() {
JndiTemplateEditor je = new JndiTemplateEditor();
// These properties are meaningless for JNDI, but we don't worry about that:

View File

@@ -16,55 +16,54 @@
package org.springframework.jndi;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.junit.Test;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @since 08.07.2003
*/
public class JndiTemplateTests extends TestCase {
public class JndiTemplateTests {
@Test
public void testLookupSucceeds() throws Exception {
Object o = new Object();
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.lookup(name);
mc.setReturnValue(o);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(o);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
Object o2 = jt.lookup(name);
assertEquals(o, o2);
mc.verify();
verify(context);
}
@Test
public void testLookupFails() throws Exception {
NameNotFoundException ne = new NameNotFoundException();
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.lookup(name);
mc.setThrowable(ne);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
expect(context.lookup(name)).andThrow(ne);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
@@ -75,22 +74,20 @@ public class JndiTemplateTests extends TestCase {
catch (NameNotFoundException ex) {
// Ok
}
mc.verify();
verify(context);
}
@Test
public void testLookupReturnsNull() throws Exception {
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.lookup(name);
mc.setReturnValue(null);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(null);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
@@ -101,23 +98,21 @@ public class JndiTemplateTests extends TestCase {
catch (NameNotFoundException ex) {
// Ok
}
mc.verify();
verify(context);
}
@Test
public void testLookupFailsWithTypeMismatch() throws Exception {
Object o = new Object();
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.lookup(name);
mc.setReturnValue(o);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(o);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
@@ -128,69 +123,63 @@ public class JndiTemplateTests extends TestCase {
catch (TypeMismatchNamingException ex) {
// Ok
}
mc.verify();
verify(context);
}
@Test
public void testBind() throws Exception {
Object o = new Object();
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.bind(name, o);
mc.setVoidCallable(1);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
context.bind(name, o);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
jt.bind(name, o);
mc.verify();
verify(context);
}
@Test
public void testRebind() throws Exception {
Object o = new Object();
String name = "foo";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.rebind(name, o);
mc.setVoidCallable(1);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
context.rebind(name, o);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
jt.rebind(name, o);
mc.verify();
verify(context);
}
@Test
public void testUnbind() throws Exception {
String name = "something";
MockControl mc = MockControl.createControl(Context.class);
final Context mock = (Context) mc.getMock();
mock.unbind(name);
mc.setVoidCallable(1);
mock.close();
mc.setVoidCallable(1);
mc.replay();
final Context context = createMock(Context.class);
context.unbind(name);
context.close();
replay(context);
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
return context;
}
};
jt.unbind(name);
mc.verify();
verify(context);
}
}

View File

@@ -0,0 +1,253 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jndi;
import static org.junit.Assert.*;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.mock.jndi.SimpleNamingContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
/**
* @author Juergen Hoeller
* @author Chris Beams
*/
public class SimpleNamingContextTests {
@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<String, String>();
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<String, Binding>();
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<String, Binding>();
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<String, String>();
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<String, String>();
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")));
}
/**
* 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);
}
}
class StubDataSource implements DataSource {
public Connection getConnection() throws SQLException {
return null;
}
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
public PrintWriter getLogWriter() throws SQLException {
return null;
}
public int getLoginTimeout() throws SQLException {
return 0;
}
public void setLogWriter(PrintWriter arg0) throws SQLException {
}
public void setLoginTimeout(int arg0) throws SQLException {
}
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
return false;
}
public <T> T unwrap(Class<T> arg0) throws SQLException {
return null;
}
}