Moved tests from testsuite to context and context support

This commit is contained in:
Arjen Poutsma
2008-10-30 16:24:14 +00:00
parent a1545e3f06
commit 100a80e112
11 changed files with 104 additions and 11 deletions

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2005 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.context.access;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Unit test for ContextBeanFactoryReference
*
* @author Colin Sampaleanu
*/
public class ContextBeanFactoryReferenceTests extends TestCase {
public void testAllOperations() {
MockControl control = MockControl.createControl(ConfigurableApplicationContext.class);
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) control.getMock();
ctx.close();
control.replay();
ContextBeanFactoryReference bfr = new ContextBeanFactoryReference(ctx);
assertNotNull(bfr.getFactory());
bfr.release();
try {
bfr.getFactory();
}
catch (IllegalStateException e) {
// expected
}
control.verify();
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2005 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.context.access;
import junit.framework.TestCase;
import org.springframework.beans.factory.access.BeanFactoryLocator;
/**
* @author Colin Sampaleanu
*/
public class DefaultLocatorFactoryTests extends TestCase {
/*
* Class to test for BeanFactoryLocator getInstance()
*/
public void testGetInstance() {
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance();
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance();
assertTrue(bf.equals(bf2));
}
/*
* Class to test for BeanFactoryLocator getInstance(String)
*/
public void testGetInstanceString() {
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
assertTrue(bf.equals(bf2));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 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.context.config;
import java.util.Date;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Arjen Poutsma
* @since 2.5.6
*/
public class ContextNamespaceHandlerTests {
private ApplicationContext applicationContext;
@Before
public void createAppContext() {
applicationContext = new ClassPathXmlApplicationContext("contextNamespaceHandlerTests.xml", getClass());
}
@Test
public void propertyPlaceholder() throws Exception {
Map beans = applicationContext.getBeansOfType(PropertyPlaceholderConfigurer.class);
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string");
assertEquals("No properties replaced", "bar", s);
}
@Test
public void propertyOverride() throws Exception {
Map beans = applicationContext.getBeansOfType(PropertyOverrideConfigurer.class);
assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty());
Date date = (Date) applicationContext.getBean("date");
assertEquals("No properties overriden", 42, date.getMinutes());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2005 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 junit.framework.TestCase;
/**
* @author Rod Johnson
*/
public class JndiTemplateEditorTests extends TestCase {
public void testNullIsIllegalArgument() {
try {
new JndiTemplateEditor().setAsText(null);
fail("Null is illegal");
}
catch (IllegalArgumentException ex) {
// OK
}
}
public void testEmptyStringMeansNullEnvironment() {
JndiTemplateEditor je = new JndiTemplateEditor();
je.setAsText("");
JndiTemplate jt = (JndiTemplate) je.getValue();
assertTrue(jt.getEnvironment() == null);
}
public void testCustomEnvironment() {
JndiTemplateEditor je = new JndiTemplateEditor();
// These properties are meaningless for JNDI, but we don't worry about that:
// the underlying JNDI implementation will throw exceptions when the user tries
// to look anything up
je.setAsText("jndiInitialSomethingOrOther=org.springframework.myjndi.CompleteRubbish\nfoo=bar");
JndiTemplate jt = (JndiTemplate) je.getValue();
assertTrue(jt.getEnvironment().size() == 2);
assertTrue(jt.getEnvironment().getProperty("jndiInitialSomethingOrOther").equals("org.springframework.myjndi.CompleteRubbish"));
assertTrue(jt.getEnvironment().getProperty("foo").equals("bar"));
}
}

View File

@@ -0,0 +1,196 @@
/*
* Copyright 2002-2006 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.NameNotFoundException;
import junit.framework.TestCase;
import org.easymock.MockControl;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @since 08.07.2003
*/
public class JndiTemplateTests extends TestCase {
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
Object o2 = jt.lookup(name);
assertEquals(o, o2);
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
try {
jt.lookup(name);
fail("Should have thrown NamingException");
}
catch (NameNotFoundException ex) {
// Ok
}
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
try {
jt.lookup(name);
fail("Should have thrown NamingException");
}
catch (NameNotFoundException ex) {
// Ok
}
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
try {
jt.lookup(name, String.class);
fail("Should have thrown TypeMismatchNamingException");
}
catch (TypeMismatchNamingException ex) {
// Ok
}
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
jt.bind(name, o);
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
jt.rebind(name, o);
mc.verify();
}
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();
JndiTemplate jt = new JndiTemplate() {
protected Context createInitialContext() {
return mock;
}
};
jt.unbind(name);
mc.verify();
}
}