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

@@ -1,385 +0,0 @@
/*
* 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 javax.naming.Context;
import javax.naming.NamingException;
import junit.framework.TestCase;
import org.easymock.MockControl;
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
*/
public class JndiObjectFactoryBeanTests extends TestCase {
public void testNoJndiName() throws NamingException {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
try {
jof.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
}
}
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);
}
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);
}
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);
}
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);
}
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);
}
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
}
}
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);
}
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);
}
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);
}
}
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());
}
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());
}
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
}
}
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());
}
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
}
}
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());
}
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());
}
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());
}
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
}
}
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
}
}
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());
}
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() throws Exception {
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);
}
}
public void testLookupWithExposeAccessContext() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
MockControl ctxControl = MockControl.createControl(Context.class);
final Context mockCtx = (Context) ctxControl.getMock();
mockCtx.lookup("foo");
ctxControl.setReturnValue(tb);
mockCtx.close();
ctxControl.setVoidCallable(2);
ctxControl.replay();
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();
ctxControl.verify();
}
}

View File

@@ -1,210 +0,0 @@
/*
* 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 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 junit.framework.TestCase;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.jndi.SimpleNamingContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
/**
* @author Juergen Hoeller
*/
public class SimpleNamingContextTests extends TestCase {
public void testNamingContextBuilder() throws NamingException {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
InitialContextFactory factory = builder.createInitialContextFactory(null);
DataSource ds = new DriverManagerDataSource();
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 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 bindingMap = new HashMap();
NamingEnumeration bindingEnum = context3.listBindings("");
while (bindingEnum.hasMoreElements()) {
Binding binding = (Binding) bindingEnum.nextElement();
bindingMap.put(binding.getName(), binding);
}
assertTrue("Correct jdbc subcontext", ((Binding) bindingMap.get("jdbc")).getObject() instanceof Context);
assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(((Binding) bindingMap.get("jdbc")).getClassName()));
Context jdbcContext = (Context) context3.lookup("jdbc");
jdbcContext.bind("mydsX", ds);
Map 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(((Binding) subBindingMap.get("myds")).getObject()));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding) subBindingMap.get("myds")).getClassName()));
assertTrue("Correct DataSource registered", ds.equals(((Binding) subBindingMap.get("mydsX")).getObject()));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding) subBindingMap.get("mydsX")).getClassName()));
assertTrue("Correct Integer registered", i.equals(((Binding) bindingMap.get("myinteger")).getObject()));
assertTrue("Correct Integer registered", Integer.class.getName().equals(((Binding) bindingMap.get("myinteger")).getClassName()));
assertTrue("Correct String registered", s.equals(((Binding) bindingMap.get("mystring")).getObject()));
assertTrue("Correct String registered", String.class.getName().equals(((Binding) bindingMap.get("mystring")).getClassName()));
context1.createSubcontext("jdbc").bind("sub/subds", ds);
Map 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 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", DriverManagerDataSource.class.getName().equals(subPairMap.get("subds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.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", DriverManagerDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX")));
}
/**
* Demonstrates how emptyActivatedContextBuilder() method can be
* used repeatedly, and how it affects creating a new InitialContext()
*/
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);
}
}

View File

@@ -1,170 +0,0 @@
/*
* Copyright 2002-2007 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.mail;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.test.AssertThrows;
/**
* @author Dmitriy Kopylenko
* @author Juergen Hoeller
* @author Rick Evans
* @since 10.09.2003
*/
public final class SimpleMailMessageTests extends TestCase {
public void testSimpleMessageCopyCtor() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("me@mail.org");
message.setTo("you@mail.org");
SimpleMailMessage messageCopy = new SimpleMailMessage(message);
assertEquals("me@mail.org", messageCopy.getFrom());
assertEquals("you@mail.org", messageCopy.getTo()[0]);
message.setReplyTo("reply@mail.org");
message.setCc(new String[]{"he@mail.org", "she@mail.org"});
message.setBcc(new String[]{"us@mail.org", "them@mail.org"});
Date sentDate = new Date();
message.setSentDate(sentDate);
message.setSubject("my subject");
message.setText("my text");
assertEquals("me@mail.org", message.getFrom());
assertEquals("reply@mail.org", message.getReplyTo());
assertEquals("you@mail.org", message.getTo()[0]);
List ccs = Arrays.asList(message.getCc());
assertTrue(ccs.contains("he@mail.org"));
assertTrue(ccs.contains("she@mail.org"));
List bccs = Arrays.asList(message.getBcc());
assertTrue(bccs.contains("us@mail.org"));
assertTrue(bccs.contains("them@mail.org"));
assertEquals(sentDate, message.getSentDate());
assertEquals("my subject", message.getSubject());
assertEquals("my text", message.getText());
messageCopy = new SimpleMailMessage(message);
assertEquals("me@mail.org", messageCopy.getFrom());
assertEquals("reply@mail.org", messageCopy.getReplyTo());
assertEquals("you@mail.org", messageCopy.getTo()[0]);
ccs = Arrays.asList(messageCopy.getCc());
assertTrue(ccs.contains("he@mail.org"));
assertTrue(ccs.contains("she@mail.org"));
bccs = Arrays.asList(message.getBcc());
assertTrue(bccs.contains("us@mail.org"));
assertTrue(bccs.contains("them@mail.org"));
assertEquals(sentDate, messageCopy.getSentDate());
assertEquals("my subject", messageCopy.getSubject());
assertEquals("my text", messageCopy.getText());
}
public void testDeepCopyOfStringArrayTypedFieldsOnCopyCtor() throws Exception {
SimpleMailMessage original = new SimpleMailMessage();
original.setTo(new String[]{"fiona@mail.org", "apple@mail.org"});
original.setCc(new String[]{"he@mail.org", "she@mail.org"});
original.setBcc(new String[]{"us@mail.org", "them@mail.org"});
SimpleMailMessage copy = new SimpleMailMessage(original);
original.getTo()[0] = "mmm@mmm.org";
original.getCc()[0] = "mmm@mmm.org";
original.getBcc()[0] = "mmm@mmm.org";
assertEquals("fiona@mail.org", copy.getTo()[0]);
assertEquals("he@mail.org", copy.getCc()[0]);
assertEquals("us@mail.org", copy.getBcc()[0]);
}
/**
* Tests that two equal SimpleMailMessages have equal hash codes.
*/
public final void testHashCode() {
SimpleMailMessage message1 = new SimpleMailMessage();
message1.setFrom("from@somewhere");
message1.setReplyTo("replyTo@somewhere");
message1.setTo("to@somewhere");
message1.setCc("cc@somewhere");
message1.setBcc("bcc@somewhere");
message1.setSentDate(new Date());
message1.setSubject("subject");
message1.setText("text");
// Copy the message
SimpleMailMessage message2 = new SimpleMailMessage(message1);
assertEquals(message1, message2);
assertEquals(message1.hashCode(), message2.hashCode());
}
public final void testEqualsObject() {
SimpleMailMessage message1;
SimpleMailMessage message2;
// Same object is equal
message1 = new SimpleMailMessage();
message2 = message1;
assertTrue(message1.equals(message2));
// Null object is not equal
message1 = new SimpleMailMessage();
message2 = null;
assertTrue(!(message1.equals(message2)));
// Different class is not equal
assertTrue(!(message1.equals(new Object())));
// Equal values are equal
message1 = new SimpleMailMessage();
message2 = new SimpleMailMessage();
assertTrue(message1.equals(message2));
message1 = new SimpleMailMessage();
message1.setFrom("from@somewhere");
message1.setReplyTo("replyTo@somewhere");
message1.setTo("to@somewhere");
message1.setCc("cc@somewhere");
message1.setBcc("bcc@somewhere");
message1.setSentDate(new Date());
message1.setSubject("subject");
message1.setText("text");
message2 = new SimpleMailMessage(message1);
assertTrue(message1.equals(message2));
}
public void testCopyCtorChokesOnNullOriginalMessage() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new SimpleMailMessage(null);
}
}.runTest();
}
public void testCopyToChokesOnNullTargetMessage() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new SimpleMailMessage().copyTo(null);
}
}.runTest();
}
}

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2002-2007 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.mock.web;
import java.util.Set;
import junit.framework.TestCase;
/**
* @author Juergen Hoeller
* @since 19.02.2006
*/
public class MockServletContextTests extends TestCase {
public void testListFiles() {
MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web");
assertNotNull(paths);
assertTrue(paths.contains("/web/MockServletContextTests.class"));
}
public void testListSubdirectories() {
MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/");
assertNotNull(paths);
assertTrue(paths.contains("/web/"));
}
public void testListNonDirectory() {
MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web/MockServletContextTests.class");
assertNull(paths);
}
public void testListInvalidPath() {
MockServletContext sc = new MockServletContext("org/springframework/mock");
Set paths = sc.getResourcePaths("/web/invalid");
assertNull(paths);
}
public void testGetContext() {
MockServletContext sc = new MockServletContext();
MockServletContext sc2 = new MockServletContext();
sc.setContextPath("/");
sc.registerContext("/second", sc2);
assertSame(sc, sc.getContext("/"));
assertSame(sc2, sc.getContext("/second"));
}
public void testGetMimeType() {
MockServletContext sc = new MockServletContext();
assertEquals("text/html", sc.getMimeType("test.html"));
assertEquals("image/gif", sc.getMimeType("test.gif"));
}
}