Moved tests over from testsuite to beans

This commit is contained in:
Arjen Poutsma
2008-10-29 12:43:13 +00:00
parent c9940742fd
commit 7a49f023d1
25 changed files with 1325 additions and 3 deletions

View File

@@ -0,0 +1,54 @@
/*
* 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

@@ -0,0 +1,309 @@
/*
* 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.beans;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
/**
* @author Juergen Hoeller
* @author Rob Harrop
* @since 19.05.2003
*/
public class BeanUtilsTests extends TestCase {
public void testInstantiateClass() {
// give proper class
BeanUtils.instantiateClass(ArrayList.class);
try {
// give interface
BeanUtils.instantiateClass(List.class);
fail("Should have thrown FatalBeanException");
}
catch (FatalBeanException ex) {
// expected
}
try {
// give class without default constructor
BeanUtils.instantiateClass(CustomDateEditor.class);
fail("Should have thrown FatalBeanException");
}
catch (FatalBeanException ex) {
// expected
}
}
public void testGetPropertyDescriptors() throws Exception {
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors();
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class);
assertNotNull("Descriptors should not be null", descriptors);
assertEquals("Invalid number of descriptors returned", actual.length, descriptors.length);
}
public void testBeanPropertyIsArray() {
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class);
for (int i = 0; i < descriptors.length; i++) {
PropertyDescriptor descriptor = descriptors[i];
if ("containedBeans".equals(descriptor.getName())) {
assertTrue("Property should be an array", descriptor.getPropertyType().isArray());
assertEquals(descriptor.getPropertyType().getComponentType(), ContainedBean.class);
}
}
}
public void testFindEditorByConvention() {
assertEquals(ResourceEditor.class, BeanUtils.findEditorByConvention(Resource.class).getClass());
}
public void testCopyProperties() throws Exception {
TestBean tb = new TestBean();
tb.setName("rod");
tb.setAge(32);
tb.setTouchy("touchy");
TestBean tb2 = new TestBean();
assertTrue("Name empty", tb2.getName() == null);
assertTrue("Age empty", tb2.getAge() == 0);
assertTrue("Touchy empty", tb2.getTouchy() == null);
BeanUtils.copyProperties(tb, tb2);
assertTrue("Name copied", tb2.getName().equals(tb.getName()));
assertTrue("Age copied", tb2.getAge() == tb.getAge());
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
}
public void testCopyPropertiesWithDifferentTypes1() throws Exception {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("rod");
tb.setAge(32);
tb.setTouchy("touchy");
TestBean tb2 = new TestBean();
assertTrue("Name empty", tb2.getName() == null);
assertTrue("Age empty", tb2.getAge() == 0);
assertTrue("Touchy empty", tb2.getTouchy() == null);
BeanUtils.copyProperties(tb, tb2);
assertTrue("Name copied", tb2.getName().equals(tb.getName()));
assertTrue("Age copied", tb2.getAge() == tb.getAge());
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
}
public void testCopyPropertiesWithDifferentTypes2() throws Exception {
TestBean tb = new TestBean();
tb.setName("rod");
tb.setAge(32);
tb.setTouchy("touchy");
DerivedTestBean tb2 = new DerivedTestBean();
assertTrue("Name empty", tb2.getName() == null);
assertTrue("Age empty", tb2.getAge() == 0);
assertTrue("Touchy empty", tb2.getTouchy() == null);
BeanUtils.copyProperties(tb, tb2);
assertTrue("Name copied", tb2.getName().equals(tb.getName()));
assertTrue("Age copied", tb2.getAge() == tb.getAge());
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
}
public void testCopyPropertiesWithEditable() throws Exception {
TestBean tb = new TestBean();
assertTrue("Name empty", tb.getName() == null);
tb.setAge(32);
tb.setTouchy("bla");
TestBean tb2 = new TestBean();
tb2.setName("rod");
assertTrue("Age empty", tb2.getAge() == 0);
assertTrue("Touchy empty", tb2.getTouchy() == null);
// "touchy" should not be copied: it's not defined in ITestBean
BeanUtils.copyProperties(tb, tb2, ITestBean.class);
assertTrue("Name copied", tb2.getName() == null);
assertTrue("Age copied", tb2.getAge() == 32);
assertTrue("Touchy still empty", tb2.getTouchy() == null);
}
public void testCopyPropertiesWithIgnore() throws Exception {
TestBean tb = new TestBean();
assertTrue("Name empty", tb.getName() == null);
tb.setAge(32);
tb.setTouchy("bla");
TestBean tb2 = new TestBean();
tb2.setName("rod");
assertTrue("Age empty", tb2.getAge() == 0);
assertTrue("Touchy empty", tb2.getTouchy() == null);
// "spouse", "touchy", "age" should not be copied
BeanUtils.copyProperties(tb, tb2, new String[]{"spouse", "touchy", "age"});
assertTrue("Name copied", tb2.getName() == null);
assertTrue("Age still empty", tb2.getAge() == 0);
assertTrue("Touchy still empty", tb2.getTouchy() == null);
}
public void testCopyPropertiesWithIgnoredNonExistingProperty() {
NameAndSpecialProperty source = new NameAndSpecialProperty();
source.setName("name");
TestBean target = new TestBean();
BeanUtils.copyProperties(source, target, new String[]{"specialProperty"});
assertEquals(target.getName(), "name");
}
public void testResolveSimpleSignature() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething", null);
assertSignatureEquals(desiredMethod, "doSomething");
assertSignatureEquals(desiredMethod, "doSomething()");
}
public void testResolveInvalidSignature() throws Exception {
try {
BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class);
fail("Should not be able to parse with opening but no closing paren.");
}
catch (IllegalArgumentException ex) {
// success
}
try {
BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class);
fail("Should not be able to parse with closing but no opening paren.");
}
catch (IllegalArgumentException ex) {
// success
}
}
public void testResolveWithAndWithoutArgList() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
assertSignatureEquals(desiredMethod, "doSomethingElse");
assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class));
}
public void testResolveTypedSignature() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)");
}
public void testResolveOverloadedSignature() throws Exception {
// test resolve with no args
Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded", null);
assertSignatureEquals(desiredMethod, "overloaded()");
// resolve with single arg
desiredMethod = MethodSignatureBean.class.getMethod("overloaded", new Class[]{String.class});
assertSignatureEquals(desiredMethod, "overloaded(java.lang.String)");
// resolve with two args
desiredMethod = MethodSignatureBean.class.getMethod("overloaded", new Class[]{String.class, BeanFactory.class});
assertSignatureEquals(desiredMethod, "overloaded(java.lang.String, org.springframework.beans.factory.BeanFactory)");
}
public void testResolveSignatureWithArray() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", new Class[]{String[].class});
assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])");
desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAMultiDimensionalArray", new Class[]{String[][].class});
assertSignatureEquals(desiredMethod, "doSomethingWithAMultiDimensionalArray(java.lang.String[][])");
}
private void assertSignatureEquals(Method desiredMethod, String signature) {
assertEquals(desiredMethod, BeanUtils.resolveSignature(signature, MethodSignatureBean.class));
}
private static class NameAndSpecialProperty {
private String name;
private int specialProperty;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setSpecialProperty(int specialProperty) {
this.specialProperty = specialProperty;
}
public int getSpecialProperty() {
return specialProperty;
}
}
private static class ContainerBean {
private ContainedBean[] containedBeans;
public ContainedBean[] getContainedBeans() {
return containedBeans;
}
public void setContainedBeans(ContainedBean[] containedBeans) {
this.containedBeans = containedBeans;
}
}
private static class ContainedBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private static class MethodSignatureBean {
public void doSomething() {
}
public void doSomethingElse(String s, int x) {
}
public void overloaded() {
}
public void overloaded(String s) {
}
public void overloaded(String s, BeanFactory beanFactory) {
}
public void doSomethingWithAnArray(String[] strings) {
}
public void doSomethingWithAMultiDimensionalArray(String[][] strings) {
}
}
}

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.beans;
/**
* @author Juergen Hoeller
* @since 10.06.2003
*/
public class BooleanTestBean {
private boolean bool1;
private Boolean bool2;
public boolean isBool1() {
return bool1;
}
public void setBool1(boolean bool1) {
this.bool1 = bool1;
}
public Boolean getBool2() {
return bool2;
}
public void setBool2(Boolean bool2) {
this.bool2 = bool2;
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.beans;
import junit.framework.TestCase;
import org.springframework.core.OverridingClassLoader;
/**
* @author Juergen Hoeller
*/
public class CachedIntrospectionResultsTests extends TestCase {
public void testAcceptClassLoader() throws Exception {
BeanWrapper bw = new BeanWrapperImpl(TestBean.class);
assertTrue(bw.isWritableProperty("name"));
assertTrue(bw.isWritableProperty("age"));
assertTrue(CachedIntrospectionResults.classCache.containsKey(TestBean.class));
ClassLoader child = new OverridingClassLoader(getClass().getClassLoader());
Class tbClass = child.loadClass("org.springframework.beans.TestBean");
assertFalse(CachedIntrospectionResults.classCache.containsKey(tbClass));
CachedIntrospectionResults.acceptClassLoader(child);
bw = new BeanWrapperImpl(tbClass);
assertTrue(bw.isWritableProperty("name"));
assertTrue(bw.isWritableProperty("age"));
assertTrue(CachedIntrospectionResults.classCache.containsKey(tbClass));
CachedIntrospectionResults.clearClassLoader(child);
assertFalse(CachedIntrospectionResults.classCache.containsKey(tbClass));
assertTrue(CachedIntrospectionResults.classCache.containsKey(TestBean.class));
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.beans;
import org.springframework.core.enums.ShortCodedLabeledEnum;
/**
* @author Rob Harrop
*/
public class Colour extends ShortCodedLabeledEnum {
public static final Colour RED = new Colour(0, "RED");
public static final Colour BLUE = new Colour(1, "BLUE");
public static final Colour GREEN = new Colour(2, "GREEN");
public static final Colour PURPLE = new Colour(3, "PURPLE");
private Colour(int code, String label) {
super(code, label);
}
}

View File

@@ -0,0 +1,145 @@
/*
* 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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Guillaume Poirier
* @author Juergen Hoeller
* @since 08.03.2004
*/
public class ConcurrentBeanWrapperTests extends TestCase {
private final Log logger = LogFactory.getLog(getClass());
private Set set = Collections.synchronizedSet(new HashSet());
private Throwable ex = null;
public void testSingleThread() {
for (int i = 0; i < 100; i++) {
performSet();
}
}
public void testConcurrent() {
for (int i = 0; i < 10; i++) {
TestRun run = new TestRun(this);
set.add(run);
Thread t = new Thread(run);
t.setDaemon(true);
t.start();
}
logger.info("Thread creation over, " + set.size() + " still active.");
synchronized (this) {
while (!set.isEmpty() && ex == null) {
try {
wait();
}
catch (InterruptedException e) {
logger.info(e.toString());
}
logger.info(set.size() + " threads still active.");
}
}
if (ex != null) {
fail(ex.getMessage());
}
}
private static void performSet() {
TestBean bean = new TestBean();
Properties p = (Properties) System.getProperties().clone();
assertTrue("The System properties must not be empty", p.size() != 0);
for (Iterator i = p.entrySet().iterator(); i.hasNext();) {
i.next();
if (Math.random() > 0.9) {
i.remove();
}
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
p.store(buffer, null);
}
catch (IOException e) {
// ByteArrayOutputStream does not throw
// any IOException
}
String value = new String(buffer.toByteArray());
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
wrapper.setPropertyValue("properties", value);
assertEquals(p, bean.getProperties());
}
private static class TestRun implements Runnable {
private ConcurrentBeanWrapperTests test;
public TestRun(ConcurrentBeanWrapperTests test) {
this.test = test;
}
public void run() {
try {
for (int i = 0; i < 100; i++) {
performSet();
}
}
catch (Throwable e) {
test.ex = e;
}
finally {
synchronized (test) {
test.set.remove(this);
test.notifyAll();
}
}
}
}
private static class TestBean {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.beans;
import java.io.Serializable;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
/**
* @author Juergen Hoeller
* @since 21.08.2003
*/
public class DerivedTestBean extends TestBean implements Serializable, BeanNameAware, DisposableBean {
private String beanName;
private boolean initialized;
private boolean destroyed;
public DerivedTestBean() {
}
public DerivedTestBean(String[] names) {
if (names == null || names.length < 2) {
throw new IllegalArgumentException("Invalid names array");
}
setName(names[0]);
setBeanName(names[1]);
}
public static DerivedTestBean create(String[] names) {
return new DerivedTestBean(names);
}
public void setBeanName(String beanName) {
if (this.beanName == null || beanName == null) {
this.beanName = beanName;
}
}
public String getBeanName() {
return beanName;
}
public void setSpouseRef(String name) {
setSpouse(new TestBean(name));
}
public void initialize() {
this.initialized = true;
}
public boolean wasInitialized() {
return initialized;
}
public void destroy() {
this.destroyed = true;
}
public boolean wasDestroyed() {
return destroyed;
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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;
public class Employee extends TestBean {
private String co;
/**
* Constructor for Employee.
*/
public Employee() {
super();
}
public String getCompany() {
return co;
}
public void setCompany(String co) {
this.co = co;
}
}

View File

@@ -0,0 +1,23 @@
/*
* 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;
public interface INestedTestBean {
public String getCompany();
}

View File

@@ -0,0 +1,24 @@
/*
* 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;
public interface IOther {
void absquatulate();
}

View File

@@ -0,0 +1,71 @@
/*
* 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.beans;
import java.io.IOException;
/**
* Interface used for {@link org.springframework.beans.TestBean}.
*
* <p>Two methods are the same as on Person, but if this
* extends person it breaks quite a few tests..
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
public interface ITestBean {
int getAge();
void setAge(int age);
String getName();
void setName(String name);
ITestBean getSpouse();
void setSpouse(ITestBean spouse);
ITestBean[] getSpouses();
String[] getStringArray();
void setStringArray(String[] stringArray);
/**
* Throws a given (non-null) exception.
*/
void exceptional(Throwable t) throws Throwable;
Object returnsThis();
INestedTestBean getDoctor();
INestedTestBean getLawyer();
IndexedTestBean getNestedIndexedBean();
/**
* Increment the age by one.
* @return the previous age
*/
int haveBirthday();
void unreliableFileOperation() throws IOException;
}

View File

@@ -0,0 +1,145 @@
/*
* 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.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* @author Juergen Hoeller
* @since 11.11.2003
*/
public class IndexedTestBean {
private TestBean[] array;
private Collection collection;
private List list;
private Set set;
private SortedSet sortedSet;
private Map map;
private SortedMap sortedMap;
public IndexedTestBean() {
this(true);
}
public IndexedTestBean(boolean populate) {
if (populate) {
populate();
}
}
public void populate() {
TestBean tb0 = new TestBean("name0", 0);
TestBean tb1 = new TestBean("name1", 0);
TestBean tb2 = new TestBean("name2", 0);
TestBean tb3 = new TestBean("name3", 0);
TestBean tb4 = new TestBean("name4", 0);
TestBean tb5 = new TestBean("name5", 0);
TestBean tb6 = new TestBean("name6", 0);
TestBean tb7 = new TestBean("name7", 0);
TestBean tbX = new TestBean("nameX", 0);
TestBean tbY = new TestBean("nameY", 0);
this.array = new TestBean[] {tb0, tb1};
this.list = new ArrayList();
this.list.add(tb2);
this.list.add(tb3);
this.set = new TreeSet();
this.set.add(tb6);
this.set.add(tb7);
this.map = new HashMap();
this.map.put("key1", tb4);
this.map.put("key2", tb5);
this.map.put("key.3", tb5);
List list = new ArrayList();
list.add(tbX);
list.add(tbY);
this.map.put("key4", list);
}
public TestBean[] getArray() {
return array;
}
public void setArray(TestBean[] array) {
this.array = array;
}
public Collection getCollection() {
return collection;
}
public void setCollection(Collection collection) {
this.collection = collection;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public SortedSet getSortedSet() {
return sortedSet;
}
public void setSortedSet(SortedSet sortedSet) {
this.sortedSet = sortedSet;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public SortedMap getSortedMap() {
return sortedMap;
}
public void setSortedMap(SortedMap sortedMap) {
this.sortedMap = sortedMap;
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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 junit.framework.Assert;
/**
* Tests for MutablePropertyValues.
*
* @author Rod Johnson
*/
public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
public void testValid() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50"));
doTestTony(pvs);
MutablePropertyValues deepCopy = new MutablePropertyValues(pvs);
doTestTony(deepCopy);
deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0);
doTestTony(pvs);
Assert.assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue());
}
public void addOrOverride() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50"));
doTestTony(pvs);
PropertyValue addedPv = new PropertyValue("rod", "Rod");
pvs.addPropertyValue(addedPv);
Assert.assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
PropertyValue changedPv = new PropertyValue("forname", "Greg");
pvs.addPropertyValue(changedPv);
Assert.assertTrue(pvs.getPropertyValue("forename").equals(changedPv));
}
public void testChangesOnEquals() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50"));
MutablePropertyValues pvs2 = pvs;
PropertyValues changes = pvs2.changesSince(pvs);
Assert.assertTrue("changes are empty", changes.getPropertyValues().length == 0);
}
public void testChangeOfOneField() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50"));
MutablePropertyValues pvs2 = new MutablePropertyValues(pvs);
PropertyValues changes = pvs2.changesSince(pvs);
Assert.assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
changes.getPropertyValues().length == 0);
pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
changes = pvs2.changesSince(pvs);
Assert.assertEquals("1 change", 1, changes.getPropertyValues().length);
PropertyValue fn = changes.getPropertyValue("forname");
Assert.assertTrue("change is forname", fn != null);
Assert.assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
changes = pvs3.changesSince(pvs);
Assert.assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
changes.getPropertyValues().length == 0);
// add new
pvs3.addPropertyValue(new PropertyValue("foo", "bar"));
pvs3.addPropertyValue(new PropertyValue("fi", "fum"));
changes = pvs3.changesSince(pvs);
Assert.assertTrue("2 change", changes.getPropertyValues().length == 2);
fn = changes.getPropertyValue("foo");
Assert.assertTrue("change in foo", fn != null);
Assert.assertTrue("new value is bar", fn.getValue().equals("bar"));
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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;
/**
* Simple nested test bean used for testing bean factories, AOP framework etc.
*
* @author Trevor D. Cook
* @since 30.09.2003
*/
public class NestedTestBean implements INestedTestBean {
private String company = "";
public NestedTestBean() {
}
public NestedTestBean(String company) {
setCompany(company);
}
public void setCompany(String company) {
this.company = (company != null ? company : "");
}
public String getCompany() {
return company;
}
public boolean equals(Object obj) {
if (!(obj instanceof NestedTestBean)) {
return false;
}
NestedTestBean ntb = (NestedTestBean) obj;
return this.company.equals(ntb.company);
}
public int hashCode() {
return this.company.hashCode();
}
public String toString() {
return "NestedTestBean: " + this.company;
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.math.BigDecimal;
import java.math.BigInteger;
/**
* @author Juergen Hoeller
* @since 10.06.2003
*/
public class NumberTestBean {
private short short1;
private Short short2;
private int int1;
private Integer int2;
private long long1;
private Long long2;
private BigInteger bigInteger;
private float float1;
private Float float2;
private double double1;
private Double double2;
private BigDecimal bigDecimal;
public short getShort1() {
return short1;
}
public void setShort1(short short1) {
this.short1 = short1;
}
public Short getShort2() {
return short2;
}
public void setShort2(Short short2) {
this.short2 = short2;
}
public int getInt1() {
return int1;
}
public void setInt1(int int1) {
this.int1 = int1;
}
public Integer getInt2() {
return int2;
}
public void setInt2(Integer int2) {
this.int2 = int2;
}
public long getLong1() {
return long1;
}
public void setLong1(long long1) {
this.long1 = long1;
}
public Long getLong2() {
return long2;
}
public void setLong2(Long long2) {
this.long2 = long2;
}
public BigInteger getBigInteger() {
return bigInteger;
}
public void setBigInteger(BigInteger bigInteger) {
this.bigInteger = bigInteger;
}
public float getFloat1() {
return float1;
}
public void setFloat1(float float1) {
this.float1 = float1;
}
public Float getFloat2() {
return float2;
}
public void setFloat2(Float float2) {
this.float2 = float2;
}
public double getDouble1() {
return double1;
}
public void setDouble1(double double1) {
this.double1 = double1;
}
public Double getDouble2() {
return double2;
}
public void setDouble2(Double double2) {
this.double2 = double2;
}
public BigDecimal getBigDecimal() {
return bigDecimal;
}
public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.beans;
import java.util.Arrays;
import junit.framework.TestCase;
/**
* @author Juergen Hoeller
*/
public class PropertyAccessorUtilsTests extends TestCase {
public void testCanonicalPropertyName() {
assertEquals("map", PropertyAccessorUtils.canonicalPropertyName("map"));
assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map[key1]"));
assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map['key1']"));
assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"]"));
assertEquals("map[key1][key2]", PropertyAccessorUtils.canonicalPropertyName("map[key1][key2]"));
assertEquals("map[key1][key2]", PropertyAccessorUtils.canonicalPropertyName("map['key1'][\"key2\"]"));
assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map[key1].name"));
assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map['key1'].name"));
assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"].name"));
}
public void testCanonicalPropertyNames() {
String[] original =
new String[] {"map", "map[key1]", "map['key1']", "map[\"key1\"]", "map[key1][key2]",
"map['key1'][\"key2\"]", "map[key1].name", "map['key1'].name", "map[\"key1\"].name"};
String[] canonical =
new String[] {"map", "map[key1]", "map[key1]", "map[key1]", "map[key1][key2]",
"map[key1][key2]", "map[key1].name", "map[key1].name", "map[key1].name"};
assertTrue(Arrays.equals(canonical, PropertyAccessorUtils.canonicalPropertyNames(original)));
}
}

View File

@@ -0,0 +1,437 @@
/*
* 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.beans;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.ObjectUtils;
/**
* Simple test bean used for testing bean factories, the AOP framework etc.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 15 April 2001
*/
public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable {
private String beanName;
private String country;
private BeanFactory beanFactory;
private boolean postProcessed;
private String name;
private String sex;
private int age;
private boolean jedi;
private ITestBean[] spouses;
private String touchy;
private String[] stringArray;
private Integer[] someIntegerArray;
private Date date = new Date();
private Float myFloat = new Float(0.0);
private Collection friends = new LinkedList();
private Set someSet = new HashSet();
private Map someMap = new HashMap();
private List someList = new ArrayList();
private Properties someProperties = new Properties();
private INestedTestBean doctor = new NestedTestBean();
private INestedTestBean lawyer = new NestedTestBean();
private IndexedTestBean nestedIndexedBean;
private boolean destroyed;
private Number someNumber;
private Colour favouriteColour;
private Boolean someBoolean;
private List otherColours;
private List pets;
public TestBean() {
}
public TestBean(String name) {
this.name = name;
}
public TestBean(ITestBean spouse) {
this.spouses = new ITestBean[] {spouse};
}
public TestBean(String name, int age) {
this.name = name;
this.age = age;
}
public TestBean(ITestBean spouse, Properties someProperties) {
this.spouses = new ITestBean[] {spouse};
this.someProperties = someProperties;
}
public TestBean(List someList) {
this.someList = someList;
}
public TestBean(Set someSet) {
this.someSet = someSet;
}
public TestBean(Map someMap) {
this.someMap = someMap;
}
public TestBean(Properties someProperties) {
this.someProperties = someProperties;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setPostProcessed(boolean postProcessed) {
this.postProcessed = postProcessed;
}
public boolean isPostProcessed() {
return postProcessed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
if (this.name == null) {
this.name = sex;
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isJedi() {
return jedi;
}
public void setJedi(boolean jedi) {
this.jedi = jedi;
}
public ITestBean getSpouse() {
return (spouses != null ? spouses[0] : null);
}
public void setSpouse(ITestBean spouse) {
this.spouses = new ITestBean[] {spouse};
}
public ITestBean[] getSpouses() {
return spouses;
}
public String getTouchy() {
return touchy;
}
public void setTouchy(String touchy) throws Exception {
if (touchy.indexOf('.') != -1) {
throw new Exception("Can't contain a .");
}
if (touchy.indexOf(',') != -1) {
throw new NumberFormatException("Number format exception: contains a ,");
}
this.touchy = touchy;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String[] getStringArray() {
return stringArray;
}
public void setStringArray(String[] stringArray) {
this.stringArray = stringArray;
}
public Integer[] getSomeIntegerArray() {
return someIntegerArray;
}
public void setSomeIntegerArray(Integer[] someIntegerArray) {
this.someIntegerArray = someIntegerArray;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Float getMyFloat() {
return myFloat;
}
public void setMyFloat(Float myFloat) {
this.myFloat = myFloat;
}
public Collection getFriends() {
return friends;
}
public void setFriends(Collection friends) {
this.friends = friends;
}
public Set getSomeSet() {
return someSet;
}
public void setSomeSet(Set someSet) {
this.someSet = someSet;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Properties getSomeProperties() {
return someProperties;
}
public void setSomeProperties(Properties someProperties) {
this.someProperties = someProperties;
}
public INestedTestBean getDoctor() {
return doctor;
}
public void setDoctor(INestedTestBean doctor) {
this.doctor = doctor;
}
public INestedTestBean getLawyer() {
return lawyer;
}
public void setLawyer(INestedTestBean lawyer) {
this.lawyer = lawyer;
}
public Number getSomeNumber() {
return someNumber;
}
public void setSomeNumber(Number someNumber) {
this.someNumber = someNumber;
}
public Colour getFavouriteColour() {
return favouriteColour;
}
public void setFavouriteColour(Colour favouriteColour) {
this.favouriteColour = favouriteColour;
}
public Boolean getSomeBoolean() {
return someBoolean;
}
public void setSomeBoolean(Boolean someBoolean) {
this.someBoolean = someBoolean;
}
public IndexedTestBean getNestedIndexedBean() {
return nestedIndexedBean;
}
public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
this.nestedIndexedBean = nestedIndexedBean;
}
public List getOtherColours() {
return otherColours;
}
public void setOtherColours(List otherColours) {
this.otherColours = otherColours;
}
public List getPets() {
return pets;
}
public void setPets(List pets) {
this.pets = pets;
}
/**
* @see ITestBean#exceptional(Throwable)
*/
public void exceptional(Throwable t) throws Throwable {
if (t != null) {
throw t;
}
}
public void unreliableFileOperation() throws IOException {
throw new IOException();
}
/**
* @see ITestBean#returnsThis()
*/
public Object returnsThis() {
return this;
}
/**
* @see IOther#absquatulate()
*/
public void absquatulate() {
}
public int haveBirthday() {
return age++;
}
public void destroy() {
this.destroyed = true;
}
public boolean wasDestroyed() {
return destroyed;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof TestBean)) {
return false;
}
TestBean tb2 = (TestBean) other;
return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
}
public int hashCode() {
return this.age;
}
public int compareTo(Object other) {
if (this.name != null && other instanceof TestBean) {
return this.name.compareTo(((TestBean) other).getName());
}
else {
return 1;
}
}
public String toString() {
return this.name;
}
}

View File

@@ -0,0 +1,159 @@
/*
* 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.beans.propertyeditors;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import junit.framework.TestCase;
/**
* Test the conversion of Strings to {@link java.util.Properties} objects,
* and other property editors.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rick Evans
*/
public class PropertiesEditorTests extends TestCase {
public void testOneProperty() {
String s = "foo=bar";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains one entry", p.entrySet().size() == 1);
assertTrue("foo=bar", p.get("foo").equals("bar"));
}
public void testTwoProperties() {
String s = "foo=bar with whitespace\n" +
"me=mi";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains two entries", p.entrySet().size() == 2);
assertTrue("foo=bar with whitespace", p.get("foo").equals("bar with whitespace"));
assertTrue("me=mi", p.get("me").equals("mi"));
}
public void testHandlesEqualsInValue() {
String s = "foo=bar\n" +
"me=mi\n" +
"x=y=z";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains two entries", p.entrySet().size() == 3);
assertTrue("foo=bar", p.get("foo").equals("bar"));
assertTrue("me=mi", p.get("me").equals("mi"));
assertTrue("x='y=z'", p.get("x").equals("y=z"));
}
public void testHandlesEmptyProperty() {
String s = "foo=bar\nme=mi\nx=";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains two entries", p.entrySet().size() == 3);
assertTrue("foo=bar", p.get("foo").equals("bar"));
assertTrue("me=mi", p.get("me").equals("mi"));
assertTrue("x='y=z'", p.get("x").equals(""));
}
public void testHandlesEmptyPropertyWithoutEquals() {
String s = "foo\nme=mi\nx=x";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains three entries", p.entrySet().size() == 3);
assertTrue("foo is empty", p.get("foo").equals(""));
assertTrue("me=mi", p.get("me").equals("mi"));
}
/**
* Comments begin with #
*/
public void testIgnoresCommentLinesAndEmptyLines() {
String s = "#Ignore this comment\n" +
"foo=bar\n" +
"#Another=comment more junk /\n" +
"me=mi\n" +
"x=x\n" +
"\n";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains three entries", p.entrySet().size() == 3);
assertTrue("foo is bar", p.get("foo").equals("bar"));
assertTrue("me=mi", p.get("me").equals("mi"));
}
/**
* We'll typically align by indenting with tabs or spaces.
* These should be ignored if at the beginning of a line.
* We must ensure that comment lines beginning with whitespace are
* still ignored: The standard syntax doesn't allow this on JDK 1.3.
*/
public void testIgnoresLeadingSpacesAndTabs() {
String s = " #Ignore this comment\n" +
"\t\tfoo=bar\n" +
"\t#Another comment more junk \n" +
" me=mi\n" +
"x=x\n" +
"\n";
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(s);
Properties p = (Properties) pe.getValue();
assertTrue("contains 3 entries, not " + p.size(), p.size() == 3);
assertTrue("foo is bar", p.get("foo").equals("bar"));
assertTrue("me=mi", p.get("me").equals("mi"));
}
public void testNull() {
PropertiesEditor pe= new PropertiesEditor();
pe.setAsText(null);
Properties p = (Properties) pe.getValue();
assertEquals(0, p.size());
}
public void testEmptyString() {
PropertiesEditor pe = new PropertiesEditor();
pe.setAsText("");
Properties p = (Properties) pe.getValue();
assertTrue("empty string means empty properties", p.isEmpty());
}
public void testUsingMapAsValueSource() throws Exception {
Map map = new HashMap();
map.put("one", "1");
map.put("two", "2");
map.put("three", "3");
PropertiesEditor pe = new PropertiesEditor();
pe.setValue(map);
Object value = pe.getValue();
assertNotNull(value);
assertTrue(value instanceof Properties);
Properties props = (Properties) value;
assertEquals(3, props.size());
assertEquals("1", props.getProperty("one"));
assertEquals("2", props.getProperty("two"));
assertEquals("3", props.getProperty("three"));
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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.beans.propertyeditors;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class StringArrayPropertyEditorTests extends TestCase {
public void testWithDefaultSeparator() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
editor.setAsText("0,1,2");
Object value = editor.getValue();
assertNotNull(value);
assertTrue(value instanceof String[]);
String[] array = (String[]) value;
for (int i = 0; i < array.length; ++i) {
assertEquals("" + i, array[i]);
}
assertEquals("0,1,2", editor.getAsText());
}
public void testWithCustomSeparator() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":");
editor.setAsText("0:1:2");
Object value = editor.getValue();
assertTrue(value instanceof String[]);
String[] array = (String[]) value;
for (int i = 0; i < array.length; ++i) {
assertEquals("" + i, array[i]);
}
assertEquals("0:1:2", editor.getAsText());
}
public void testWithCharsToDelete() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", "\r\n", false);
editor.setAsText("0\r,1,\n2");
Object value = editor.getValue();
assertTrue(value instanceof String[]);
String[] array = (String[]) value;
for (int i = 0; i < array.length; ++i) {
assertEquals("" + i, array[i]);
}
assertEquals("0,1,2", editor.getAsText());
}
public void testWithEmptyArray() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
editor.setAsText("");
Object value = editor.getValue();
assertTrue(value instanceof String[]);
assertEquals(0, ((String[]) value).length);
}
public void testWithEmptyArrayAsNull() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", true);
editor.setAsText("");
assertNull(editor.getValue());
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.beans.propertyeditors;
import java.beans.PropertyEditor;
import java.net.URI;
import junit.framework.TestCase;
import org.springframework.util.ClassUtils;
/**
* @author Juergen Hoeller
*/
public class URIEditorTests extends TestCase {
public void testStandardURI() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("mailto:juergen.hoeller@interface21.com");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
}
public void testStandardURL() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("http://www.springframework.org");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
}
public void testStandardURLWithWhitespace() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(" http://www.springframework.org ");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
}
public void testClasspathURL() throws Exception {
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
"/" + ClassUtils.getShortName(getClass()) + ".class");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
assertTrue(!uri.getScheme().startsWith("classpath"));
}
public void testClasspathURLWithWhitespace() throws Exception {
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
"/" + ClassUtils.getShortName(getClass()) + ".class ");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
assertTrue(!uri.getScheme().startsWith("classpath"));
}
public void testClasspathURLAsIs() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("classpath:test.txt");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
assertTrue(uri.getScheme().startsWith("classpath"));
}
public void testWithNonExistentResource() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
}
public void testSetAsTextWithNull() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(null);
assertNull(uriEditor.getValue());
assertEquals("", uriEditor.getAsText());
}
public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
PropertyEditor uriEditor = new URIEditor();
assertEquals("", uriEditor.getAsText());
}
}

View File

@@ -0,0 +1,25 @@
/*
* 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.support;
/**
* @author Juergen Hoeller
* @since 29.07.2004
*/
public class DerivedFromProtectedBaseBean extends ProtectedBaseBean {
}

View File

@@ -0,0 +1,219 @@
/*
* 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.support;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import junit.framework.TestCase;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.TestBean;
/**
* @author Juergen Hoeller
* @author Jean-Pierre PAWLAK
* @since 20.05.2003
*/
public class PagedListHolderTests extends TestCase {
public void testPagedListHolder() {
TestBean tb1 = new TestBean();
tb1.setName("eva");
tb1.setAge(25);
TestBean tb2 = new TestBean();
tb2.setName("juergen");
tb2.setAge(99);
TestBean tb3 = new TestBean();
tb3.setName("Rod");
tb3.setAge(32);
List tbs = new ArrayList();
tbs.add(tb1);
tbs.add(tb2);
tbs.add(tb3);
PagedListHolder holder = new PagedListHolder(tbs);
assertTrue("Correct source", holder.getSource() == tbs);
assertTrue("Correct number of elements", holder.getNrOfElements() == 3);
assertTrue("Correct number of pages", holder.getPageCount() == 1);
assertTrue("Correct page size", holder.getPageSize() == PagedListHolder.DEFAULT_PAGE_SIZE);
assertTrue("Correct page number", holder.getPage() == 0);
assertTrue("First page", holder.isFirstPage());
assertTrue("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 0);
assertTrue("Correct first element", holder.getLastElementOnPage() == 2);
assertTrue("Correct page list size", holder.getPageList().size() == 3);
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2);
assertTrue("Correct page list contents", holder.getPageList().get(2) == tb3);
holder.setPageSize(2);
assertTrue("Correct number of pages", holder.getPageCount() == 2);
assertTrue("Correct page size", holder.getPageSize() == 2);
assertTrue("Correct page number", holder.getPage() == 0);
assertTrue("First page", holder.isFirstPage());
assertFalse("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 0);
assertTrue("Correct last element", holder.getLastElementOnPage() == 1);
assertTrue("Correct page list size", holder.getPageList().size() == 2);
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2);
holder.setPage(1);
assertTrue("Correct page number", holder.getPage() == 1);
assertFalse("First page", holder.isFirstPage());
assertTrue("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 2);
assertTrue("Correct last element", holder.getLastElementOnPage() == 2);
assertTrue("Correct page list size", holder.getPageList().size() == 1);
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3);
holder.setPageSize(3);
assertTrue("Correct number of pages", holder.getPageCount() == 1);
assertTrue("Correct page size", holder.getPageSize() == 3);
assertTrue("Correct page number", holder.getPage() == 0);
assertTrue("First page", holder.isFirstPage());
assertTrue("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 0);
assertTrue("Correct last element", holder.getLastElementOnPage() == 2);
holder.setPage(1);
holder.setPageSize(2);
assertTrue("Correct number of pages", holder.getPageCount() == 2);
assertTrue("Correct page size", holder.getPageSize() == 2);
assertTrue("Correct page number", holder.getPage() == 1);
assertFalse("First page", holder.isFirstPage());
assertTrue("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 2);
assertTrue("Correct last element", holder.getLastElementOnPage() == 2);
holder.setPageSize(2);
holder.setPage(1);
((MutableSortDefinition) holder.getSort()).setProperty("name");
((MutableSortDefinition) holder.getSort()).setIgnoreCase(false);
holder.resort();
assertTrue("Correct source", holder.getSource() == tbs);
assertTrue("Correct number of elements", holder.getNrOfElements() == 3);
assertTrue("Correct number of pages", holder.getPageCount() == 2);
assertTrue("Correct page size", holder.getPageSize() == 2);
assertTrue("Correct page number", holder.getPage() == 0);
assertTrue("First page", holder.isFirstPage());
assertFalse("Last page", holder.isLastPage());
assertTrue("Correct first element", holder.getFirstElementOnPage() == 0);
assertTrue("Correct last element", holder.getLastElementOnPage() == 1);
assertTrue("Correct page list size", holder.getPageList().size() == 2);
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1);
((MutableSortDefinition) holder.getSort()).setProperty("name");
holder.resort();
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1);
((MutableSortDefinition) holder.getSort()).setProperty("name");
holder.resort();
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1);
holder.setPage(1);
assertTrue("Correct page list size", holder.getPageList().size() == 1);
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2);
((MutableSortDefinition) holder.getSort()).setProperty("age");
holder.resort();
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3);
((MutableSortDefinition) holder.getSort()).setIgnoreCase(true);
holder.resort();
assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1);
assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3);
holder.nextPage();
assertEquals(1, holder.getPage());
holder.previousPage();
assertEquals(0, holder.getPage());
holder.nextPage();
assertEquals(1, holder.getPage());
holder.nextPage();
assertEquals(1, holder.getPage());
holder.previousPage();
assertEquals(0, holder.getPage());
holder.previousPage();
assertEquals(0, holder.getPage());
}
public static class MockFilter {
private String name = "";
private String age = "";
private String extendedInfo = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getExtendedInfo() {
return extendedInfo;
}
public void setExtendedInfo(String extendedInfo) {
this.extendedInfo = extendedInfo;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MockFilter)) return false;
final MockFilter mockFilter = (MockFilter) o;
if (!age.equals(mockFilter.age)) return false;
if (!extendedInfo.equals(mockFilter.extendedInfo)) return false;
if (!name.equals(mockFilter.name)) return false;
return true;
}
public int hashCode() {
int result;
result = name.hashCode();
result = 29 * result + age.hashCode();
result = 29 * result + extendedInfo.hashCode();
return result;
}
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.support;
/**
* @author Juergen Hoeller
* @since 29.07.2004
*/
class ProtectedBaseBean {
private String someProperty;
public void setSomeProperty(String someProperty) {
this.someProperty = someProperty;
}
public String getSomeProperty() {
return someProperty;
}
}