moving unit tests from .testsuite -> .beans

refactoring/prepping several classes for upcoming move to .beans
This commit is contained in:
Chris Beams 2008-12-15 18:18:50 +00:00
parent 97e400efd6
commit a4c23509e3
4 changed files with 56 additions and 66 deletions

View File

@ -1,54 +0,0 @@
/*
* 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.beans;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Rod Johnson
*/
public abstract class AbstractPropertyValuesTests extends TestCase {
/**
* Must contain: forname=Tony surname=Blair age=50
*/
protected void doTestTony(PropertyValues pvs) throws Exception {
assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
assertTrue("Contains forname", pvs.contains("forname"));
assertTrue("Contains surname", pvs.contains("surname"));
assertTrue("Contains age", pvs.contains("age"));
assertTrue("Doesn't contain tory", !pvs.contains("tory"));
PropertyValue[] ps = pvs.getPropertyValues();
Map m = new HashMap();
m.put("forname", "Tony");
m.put("surname", "Blair");
m.put("age", "50");
for (int i = 0; i < ps.length; i++) {
Object val = m.get(ps[i].getName());
assertTrue("Can't have unexpected value", val != null);
assertTrue("Val i string", val instanceof String);
assertTrue("val matches expected", val.equals(ps[i].getValue()));
m.remove(ps[i].getName());
}
assertTrue("Map size is 0", m.size() == 0);
}
}

View File

@ -16,24 +16,19 @@
package org.springframework.beans.factory.access;
import junit.framework.TestCase;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.ClassUtils;
/**
* @author Colin Sampaleanu
* @author Chris Beams
*/
public class SingletonBeanFactoryLocatorTests extends TestCase {
public void testBaseBeanFactoryDefs() {
// Just test the base BeanFactory/AppContext defs we are going to work with
// in other tests.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/org/springframework/beans/factory/access/beans*.xml");
}
public class SingletonBeanFactoryLocatorTests {
@Test
public void testBasicFunctionality() {
SingletonBeanFactoryLocator facLoc = new SingletonBeanFactoryLocator(
"classpath*:" + ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml"));
@ -74,6 +69,7 @@ public class SingletonBeanFactoryLocatorTests extends TestCase {
* 2nd and subsequent calls will actuall get back same locator instance. This is not
* an issue really, since the contained beanfactories will still be loaded and released.
*/
@Test
public void testGetInstance() {
// Try with and without 'classpath*:' prefix, and with 'classpath:' prefix.
BeanFactoryLocator facLoc = SingletonBeanFactoryLocator.getInstance(

View File

@ -16,6 +16,9 @@
package org.springframework.context.access;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
@ -28,9 +31,11 @@ import org.springframework.util.ClassUtils;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
* @author Chris Beams
*/
public class ContextSingletonBeanFactoryLocatorTests extends SingletonBeanFactoryLocatorTests {
@Test
public void testBaseBeanFactoryDefs() {
// Just test the base BeanFactory/AppContext defs we are going to work
// with in other tests.
@ -38,6 +43,7 @@ public class ContextSingletonBeanFactoryLocatorTests extends SingletonBeanFactor
new XmlBeanFactory(new ClassPathResource("/org/springframework/beans/factory/access/beans2.xml"));
}
@Test
public void testBasicFunctionality() {
// Just use definition file from the SingletonBeanFactoryLocator test,
// since it is completely valid.
@ -64,6 +70,7 @@ public class ContextSingletonBeanFactoryLocatorTests extends SingletonBeanFactor
* 2nd and subsequent calls will actuall get back same locator instance. This is not
* really an issue, since the contained bean factories will still be loaded and released.
*/
@Test
public void testGetInstance() {
// Try with and without 'classpath*:' prefix, and with 'classpath:' prefix.
BeanFactoryLocator facLoc = ContextSingletonBeanFactoryLocator.getInstance(

View File

@ -16,20 +16,28 @@
package org.springframework.web.bind;
import static org.junit.Assert.*;
import java.beans.PropertyEditorSupport;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.AbstractPropertyValuesTests;
import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.TestBean;
import org.springframework.mock.web.MockHttpServletRequest;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
*/
public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
public class ServletRequestDataBinderTests {
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
TestBean tb = new TestBean();
@ -49,6 +57,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertEquals("test", tb.getSpouse().getName());
}
@Test
public void testFieldPrefixCausesFieldReset() throws Exception {
TestBean target = new TestBean();
ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
@ -64,6 +73,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertFalse(target.isPostProcessed());
}
@Test
public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields() throws Exception {
TestBean target = new TestBean();
ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
@ -80,6 +90,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertFalse(target.isPostProcessed());
}
@Test
public void testWithCommaSeparatedStringArray() throws Exception {
TestBean target = new TestBean();
ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
@ -97,6 +108,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertEquals("Expected only 1 item to be bound", 1, target.getStringArray().length);
}
@Test
public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception {
TestBean tb = new TestBean();
@ -116,6 +128,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertEquals("test", tb.getSpouse().getName());
}
@Test
public void testNoPrefix() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("forname", "Tony");
@ -126,6 +139,7 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
doTestTony(pvs);
}
@Test
public void testPrefix() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("test_forname", "Tony");
@ -140,12 +154,14 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
doTestTony(pvs);
}
@Test
public void testNoParameters() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
assertTrue("Found no parameters", pvs.getPropertyValues().length == 0);
}
@Test
public void testMultipleValuesForParameter() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
String[] original = new String[] {"Tony", "Rod"};
@ -158,4 +174,29 @@ public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original));
}
/**
* Must contain: forname=Tony surname=Blair age=50
*/
protected void doTestTony(PropertyValues pvs) throws Exception {
assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
assertTrue("Contains forname", pvs.contains("forname"));
assertTrue("Contains surname", pvs.contains("surname"));
assertTrue("Contains age", pvs.contains("age"));
assertTrue("Doesn't contain tory", !pvs.contains("tory"));
PropertyValue[] ps = pvs.getPropertyValues();
Map<String, String> m = new HashMap<String, String>();
m.put("forname", "Tony");
m.put("surname", "Blair");
m.put("age", "50");
for (int i = 0; i < ps.length; i++) {
Object val = m.get(ps[i].getName());
assertTrue("Can't have unexpected value", val != null);
assertTrue("Val i string", val instanceof String);
assertTrue("val matches expected", val.equals(ps[i].getValue()));
m.remove(ps[i].getName());
}
assertTrue("Map size is 0", m.size() == 0);
}
}