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

@ -16,22 +16,24 @@
package org.springframework.mail; package org.springframework.mail;
import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.test.AssertThrows;
/** /**
* @author Dmitriy Kopylenko * @author Dmitriy Kopylenko
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
* @since 10.09.2003 * @since 10.09.2003
*/ */
public final class SimpleMailMessageTests extends TestCase { public final class SimpleMailMessageTests {
@Test
public void testSimpleMessageCopyCtor() { public void testSimpleMessageCopyCtor() {
SimpleMailMessage message = new SimpleMailMessage(); SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("me@mail.org"); message.setFrom("me@mail.org");
@ -52,10 +54,10 @@ public final class SimpleMailMessageTests extends TestCase {
assertEquals("me@mail.org", message.getFrom()); assertEquals("me@mail.org", message.getFrom());
assertEquals("reply@mail.org", message.getReplyTo()); assertEquals("reply@mail.org", message.getReplyTo());
assertEquals("you@mail.org", message.getTo()[0]); assertEquals("you@mail.org", message.getTo()[0]);
List ccs = Arrays.asList(message.getCc()); List<String> ccs = Arrays.asList(message.getCc());
assertTrue(ccs.contains("he@mail.org")); assertTrue(ccs.contains("he@mail.org"));
assertTrue(ccs.contains("she@mail.org")); assertTrue(ccs.contains("she@mail.org"));
List bccs = Arrays.asList(message.getBcc()); List<String> bccs = Arrays.asList(message.getBcc());
assertTrue(bccs.contains("us@mail.org")); assertTrue(bccs.contains("us@mail.org"));
assertTrue(bccs.contains("them@mail.org")); assertTrue(bccs.contains("them@mail.org"));
assertEquals(sentDate, message.getSentDate()); assertEquals(sentDate, message.getSentDate());
@ -77,6 +79,7 @@ public final class SimpleMailMessageTests extends TestCase {
assertEquals("my text", messageCopy.getText()); assertEquals("my text", messageCopy.getText());
} }
@Test
public void testDeepCopyOfStringArrayTypedFieldsOnCopyCtor() throws Exception { public void testDeepCopyOfStringArrayTypedFieldsOnCopyCtor() throws Exception {
SimpleMailMessage original = new SimpleMailMessage(); SimpleMailMessage original = new SimpleMailMessage();
@ -98,6 +101,7 @@ public final class SimpleMailMessageTests extends TestCase {
/** /**
* Tests that two equal SimpleMailMessages have equal hash codes. * Tests that two equal SimpleMailMessages have equal hash codes.
*/ */
@Test
public final void testHashCode() { public final void testHashCode() {
SimpleMailMessage message1 = new SimpleMailMessage(); SimpleMailMessage message1 = new SimpleMailMessage();
message1.setFrom("from@somewhere"); message1.setFrom("from@somewhere");
@ -151,20 +155,14 @@ public final class SimpleMailMessageTests extends TestCase {
assertTrue(message1.equals(message2)); assertTrue(message1.equals(message2));
} }
@Test(expected=IllegalArgumentException.class)
public void testCopyCtorChokesOnNullOriginalMessage() throws Exception { public void testCopyCtorChokesOnNullOriginalMessage() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new SimpleMailMessage(null);
public void test() throws Exception {
new SimpleMailMessage(null);
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testCopyToChokesOnNullTargetMessage() throws Exception { public void testCopyToChokesOnNullTargetMessage() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new SimpleMailMessage().copyTo(null);
public void test() throws Exception {
new SimpleMailMessage().copyTo(null);
}
}.runTest();
} }
} }

View File

@ -16,12 +16,13 @@
package org.springframework.jndi; package org.springframework.jndi;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import junit.framework.TestCase; import org.junit.Test;
import org.easymock.MockControl;
import org.springframework.beans.DerivedTestBean; import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
@ -30,9 +31,11 @@ import org.springframework.mock.jndi.ExpectedLookupTemplate;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class JndiObjectFactoryBeanTests extends TestCase { public class JndiObjectFactoryBeanTests {
@Test
public void testNoJndiName() throws NamingException { public void testNoJndiName() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
try { try {
@ -43,6 +46,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithFullNameAndResourceRefTrue() throws Exception { public void testLookupWithFullNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -53,6 +57,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithFullNameAndResourceRefFalse() throws Exception { public void testLookupWithFullNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -63,6 +68,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithSchemeNameAndResourceRefTrue() throws Exception { public void testLookupWithSchemeNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -73,6 +79,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception { public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -83,6 +90,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithShortNameAndResourceRefTrue() throws Exception { public void testLookupWithShortNameAndResourceRefTrue() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -93,6 +101,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithShortNameAndResourceRefFalse() throws Exception { public void testLookupWithShortNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -108,6 +117,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception { public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -118,6 +128,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == o); assertTrue(jof.getObject() == o);
} }
@Test
public void testLookupWithExpectedTypeAndMatch() throws Exception { public void testLookupWithExpectedTypeAndMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = ""; String s = "";
@ -128,6 +139,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertTrue(jof.getObject() == s); assertTrue(jof.getObject() == s);
} }
@Test
public void testLookupWithExpectedTypeAndNoMatch() throws Exception { public void testLookupWithExpectedTypeAndNoMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object(); Object o = new Object();
@ -143,6 +155,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithDefaultObject() throws Exception { public void testLookupWithDefaultObject() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = ""; String s = "";
@ -154,6 +167,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals("myString", jof.getObject()); assertEquals("myString", jof.getObject());
} }
@Test
public void testLookupWithDefaultObjectAndExpectedType() throws Exception { public void testLookupWithDefaultObjectAndExpectedType() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = ""; String s = "";
@ -165,6 +179,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals("myString", jof.getObject()); assertEquals("myString", jof.getObject());
} }
@Test
public void testLookupWithDefaultObjectAndExpectedTypeNoMatch() throws Exception { public void testLookupWithDefaultObjectAndExpectedTypeNoMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
String s = ""; String s = "";
@ -181,6 +196,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithProxyInterface() throws Exception { public void testLookupWithProxyInterface() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean(); TestBean tb = new TestBean();
@ -195,6 +211,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals(99, tb.getAge()); assertEquals(99, tb.getAge());
} }
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception { public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean(); TestBean tb = new TestBean();
@ -211,6 +228,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception { public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean(); final TestBean tb = new TestBean();
@ -236,6 +254,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals(99, tb.getAge()); assertEquals(99, tb.getAge());
} }
@Test
public void testLookupWithProxyInterfaceWithNotCache() throws Exception { public void testLookupWithProxyInterfaceWithNotCache() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean(); final TestBean tb = new TestBean();
@ -263,6 +282,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals(4, tb.getAge()); assertEquals(4, tb.getAge());
} }
@Test
public void testLookupWithProxyInterfaceWithLazyLookupAndNotCache() throws Exception { public void testLookupWithProxyInterfaceWithLazyLookupAndNotCache() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
final TestBean tb = new TestBean(); final TestBean tb = new TestBean();
@ -294,6 +314,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals(4, tb.getAge()); assertEquals(4, tb.getAge());
} }
@Test
public void testLazyLookupWithoutProxyInterface() throws NamingException { public void testLazyLookupWithoutProxyInterface() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
jof.setJndiName("foo"); jof.setJndiName("foo");
@ -307,6 +328,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testNotCacheWithoutProxyInterface() throws NamingException { public void testNotCacheWithoutProxyInterface() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
jof.setJndiName("foo"); jof.setJndiName("foo");
@ -321,6 +343,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception { public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean(); TestBean tb = new TestBean();
@ -336,7 +359,8 @@ public class JndiObjectFactoryBeanTests extends TestCase {
assertEquals(99, tb.getAge()); assertEquals(99, tb.getAge());
} }
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() throws Exception { @Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean(); TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb)); jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
@ -352,16 +376,15 @@ public class JndiObjectFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testLookupWithExposeAccessContext() throws Exception { public void testLookupWithExposeAccessContext() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean(); TestBean tb = new TestBean();
MockControl ctxControl = MockControl.createControl(Context.class); final Context mockCtx = createMock(Context.class);
final Context mockCtx = (Context) ctxControl.getMock(); expect(mockCtx.lookup("foo")).andReturn(tb);
mockCtx.lookup("foo");
ctxControl.setReturnValue(tb);
mockCtx.close(); mockCtx.close();
ctxControl.setVoidCallable(2); expectLastCall().times(2);
ctxControl.replay(); replay(mockCtx);
jof.setJndiTemplate(new JndiTemplate() { jof.setJndiTemplate(new JndiTemplate() {
protected Context createInitialContext() { protected Context createInitialContext() {
return mockCtx; return mockCtx;
@ -379,7 +402,7 @@ public class JndiObjectFactoryBeanTests extends TestCase {
proxy.equals(proxy); proxy.equals(proxy);
proxy.hashCode(); proxy.hashCode();
proxy.toString(); proxy.toString();
ctxControl.verify(); verify(mockCtx);
} }
} }

View File

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

View File

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

View File

@ -16,6 +16,11 @@
package org.springframework.jndi; 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.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
@ -30,22 +35,23 @@ import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory; import javax.naming.spi.InitialContextFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.jndi.SimpleNamingContext; import org.springframework.mock.jndi.SimpleNamingContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder; import org.springframework.mock.jndi.SimpleNamingContextBuilder;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class SimpleNamingContextTests extends TestCase { public class SimpleNamingContextTests {
@Test
public void testNamingContextBuilder() throws NamingException { public void testNamingContextBuilder() throws NamingException {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
InitialContextFactory factory = builder.createInitialContextFactory(null); InitialContextFactory factory = builder.createInitialContextFactory(null);
DataSource ds = new DriverManagerDataSource(); DataSource ds = new StubDataSource();
builder.bind("java:comp/env/jdbc/myds", ds); builder.bind("java:comp/env/jdbc/myds", ds);
Object obj = new Object(); Object obj = new Object();
builder.bind("myobject", obj); builder.bind("myobject", obj);
@ -54,7 +60,7 @@ public class SimpleNamingContextTests extends TestCase {
assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds); assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
assertTrue("Correct Object registered", context1.lookup("myobject") == obj); assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
Hashtable env2 = new Hashtable(); Hashtable<String, String> env2 = new Hashtable<String, String>();
env2.put("key1", "value1"); env2.put("key1", "value1");
Context context2 = factory.getInitialContext(env2); Context context2 = factory.getInitialContext(env2);
assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds); assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
@ -110,37 +116,37 @@ public class SimpleNamingContextTests extends TestCase {
assertTrue("Correct Integer registered", context3.lookup("myinteger") == i); assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
assertTrue("Correct String registered", context3.lookup("mystring") == s); assertTrue("Correct String registered", context3.lookup("mystring") == s);
Map bindingMap = new HashMap(); Map<String, Binding> bindingMap = new HashMap<String, Binding>();
NamingEnumeration bindingEnum = context3.listBindings(""); NamingEnumeration<?> bindingEnum = context3.listBindings("");
while (bindingEnum.hasMoreElements()) { while (bindingEnum.hasMoreElements()) {
Binding binding = (Binding) bindingEnum.nextElement(); Binding binding = (Binding) bindingEnum.nextElement();
bindingMap.put(binding.getName(), binding); bindingMap.put(binding.getName(), binding);
} }
assertTrue("Correct jdbc subcontext", ((Binding) bindingMap.get("jdbc")).getObject() instanceof Context); assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(((Binding) bindingMap.get("jdbc")).getClassName())); assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
Context jdbcContext = (Context) context3.lookup("jdbc"); Context jdbcContext = (Context) context3.lookup("jdbc");
jdbcContext.bind("mydsX", ds); jdbcContext.bind("mydsX", ds);
Map subBindingMap = new HashMap(); Map<String, Binding> subBindingMap = new HashMap<String, Binding>();
NamingEnumeration subBindingEnum = jdbcContext.listBindings(""); NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
while (subBindingEnum.hasMoreElements()) { while (subBindingEnum.hasMoreElements()) {
Binding binding = (Binding) subBindingEnum.nextElement(); Binding binding = (Binding) subBindingEnum.nextElement();
subBindingMap.put(binding.getName(), binding); subBindingMap.put(binding.getName(), binding);
} }
assertTrue("Correct DataSource registered", ds.equals(((Binding) subBindingMap.get("myds")).getObject())); assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding) subBindingMap.get("myds")).getClassName())); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
assertTrue("Correct DataSource registered", ds.equals(((Binding) subBindingMap.get("mydsX")).getObject())); assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding) subBindingMap.get("mydsX")).getClassName())); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
assertTrue("Correct Integer registered", i.equals(((Binding) bindingMap.get("myinteger")).getObject())); assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
assertTrue("Correct Integer registered", Integer.class.getName().equals(((Binding) bindingMap.get("myinteger")).getClassName())); assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
assertTrue("Correct String registered", s.equals(((Binding) bindingMap.get("mystring")).getObject())); assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
assertTrue("Correct String registered", String.class.getName().equals(((Binding) bindingMap.get("mystring")).getClassName())); assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
context1.createSubcontext("jdbc").bind("sub/subds", ds); context1.createSubcontext("jdbc").bind("sub/subds", ds);
Map pairMap = new HashMap(); Map<String, String> pairMap = new HashMap<String, String>();
NamingEnumeration pairEnum = context2.list("jdbc"); NamingEnumeration<?> pairEnum = context2.list("jdbc");
while (pairEnum.hasMore()) { while (pairEnum.hasMore()) {
NameClassPair pair = (NameClassPair) pairEnum.next(); NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName()); pairMap.put(pair.getName(), pair.getClassName());
@ -148,16 +154,16 @@ public class SimpleNamingContextTests extends TestCase {
assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub"))); assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
Context subContext = (Context) context2.lookup("jdbc/sub"); Context subContext = (Context) context2.lookup("jdbc/sub");
Map subPairMap = new HashMap(); Map<String, String> subPairMap = new HashMap<String, String>();
NamingEnumeration subPairEnum = subContext.list(""); NamingEnumeration<?> subPairEnum = subContext.list("");
while (subPairEnum.hasMoreElements()) { while (subPairEnum.hasMoreElements()) {
NameClassPair pair = (NameClassPair) subPairEnum.next(); NameClassPair pair = (NameClassPair) subPairEnum.next();
subPairMap.put(pair.getName(), pair.getClassName()); subPairMap.put(pair.getName(), pair.getClassName());
} }
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(subPairMap.get("subds"))); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds"))); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX"))); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
pairMap.clear(); pairMap.clear();
pairEnum = context1.list("jdbc/"); pairEnum = context1.list("jdbc/");
@ -165,14 +171,15 @@ public class SimpleNamingContextTests extends TestCase {
NameClassPair pair = (NameClassPair) pairEnum.next(); NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName()); pairMap.put(pair.getName(), pair.getClassName());
} }
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds"))); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX"))); assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
} }
/** /**
* Demonstrates how emptyActivatedContextBuilder() method can be * Demonstrates how emptyActivatedContextBuilder() method can be
* used repeatedly, and how it affects creating a new InitialContext() * used repeatedly, and how it affects creating a new InitialContext()
*/ */
@Test
public void testCreateInitialContext() throws Exception { public void testCreateInitialContext() throws Exception {
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
String name = "foo"; String name = "foo";
@ -208,3 +215,39 @@ public class SimpleNamingContextTests extends TestCase {
} }
} }
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;
}
}

View File

@ -16,42 +16,50 @@
package org.springframework.mock.web; package org.springframework.mock.web;
import static org.junit.Assert.*;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 19.02.2006 * @since 19.02.2006
*/ */
public class MockServletContextTests extends TestCase { public class MockServletContextTests {
@Test
public void testListFiles() { public void testListFiles() {
MockServletContext sc = new MockServletContext("org/springframework/mock"); MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web"); Set<?> paths = sc.getResourcePaths("/web");
assertNotNull(paths); assertNotNull(paths);
assertTrue(paths.contains("/web/MockServletContextTests.class")); assertTrue(paths.contains("/web/MockServletContextTests.class"));
} }
@Test
public void testListSubdirectories() { public void testListSubdirectories() {
MockServletContext sc = new MockServletContext("org/springframework/mock"); MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/"); Set<?> paths = sc.getResourcePaths("/");
assertNotNull(paths); assertNotNull(paths);
assertTrue(paths.contains("/web/")); assertTrue(paths.contains("/web/"));
} }
@Test
public void testListNonDirectory() { public void testListNonDirectory() {
MockServletContext sc = new MockServletContext("org/springframework/mock"); MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web/MockServletContextTests.class"); Set<?> paths = sc.getResourcePaths("/web/MockServletContextTests.class");
assertNull(paths); assertNull(paths);
} }
@Test
public void testListInvalidPath() { public void testListInvalidPath() {
MockServletContext sc = new MockServletContext("org/springframework/mock"); MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web/invalid"); Set<?> paths = sc.getResourcePaths("/web/invalid");
assertNull(paths); assertNull(paths);
} }
@Test
public void testGetContext() { public void testGetContext() {
MockServletContext sc = new MockServletContext(); MockServletContext sc = new MockServletContext();
MockServletContext sc2 = new MockServletContext(); MockServletContext sc2 = new MockServletContext();
@ -61,6 +69,7 @@ public class MockServletContextTests extends TestCase {
assertSame(sc2, sc.getContext("/second")); assertSame(sc2, sc.getContext("/second"));
} }
@Test
public void testGetMimeType() { public void testGetMimeType() {
MockServletContext sc = new MockServletContext(); MockServletContext sc = new MockServletContext();
assertEquals("text/html", sc.getMimeType("test.html")); assertEquals("text/html", sc.getMimeType("test.html"));