Moved tests over from testsuite to beans
This commit is contained in:
@@ -1,97 +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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
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);
|
||||
assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
|
||||
PropertyValue changedPv = new PropertyValue("forname", "Greg");
|
||||
pvs.addPropertyValue(changedPv);
|
||||
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);
|
||||
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);
|
||||
assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
|
||||
changes.getPropertyValues().length == 0);
|
||||
|
||||
pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
|
||||
changes = pvs2.changesSince(pvs);
|
||||
assertEquals("1 change", 1, changes.getPropertyValues().length);
|
||||
PropertyValue fn = changes.getPropertyValue("forname");
|
||||
assertTrue("change is forname", fn != null);
|
||||
assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
|
||||
|
||||
MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
|
||||
changes = pvs3.changesSince(pvs);
|
||||
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);
|
||||
assertTrue("2 change", changes.getPropertyValues().length == 2);
|
||||
fn = changes.getPropertyValue("foo");
|
||||
assertTrue("change in foo", fn != null);
|
||||
assertTrue("new value is bar", fn.getValue().equals("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* 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.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.beans.SimpleBeanInfo;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
import org.springframework.core.JdkVersion;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 06.03.2006
|
||||
*/
|
||||
public class BeanInfoTests extends TestCase {
|
||||
|
||||
public void testComplexObject() {
|
||||
ValueBean bean = new ValueBean();
|
||||
BeanWrapper bw = new BeanWrapperImpl(bean);
|
||||
Integer value = new Integer(1);
|
||||
|
||||
bw.setPropertyValue("value", value);
|
||||
assertEquals("value not set correctly", bean.getValue(), value);
|
||||
|
||||
value = new Integer(2);
|
||||
bw.setPropertyValue("value", value.toString());
|
||||
assertEquals("value not converted", bean.getValue(), value);
|
||||
|
||||
bw.setPropertyValue("value", null);
|
||||
assertNull("value not null", bean.getValue());
|
||||
|
||||
bw.setPropertyValue("value", "");
|
||||
assertNull("value not converted to null", bean.getValue());
|
||||
}
|
||||
|
||||
|
||||
public static class ValueBean {
|
||||
|
||||
private Integer value;
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ValueBeanBeanInfo extends SimpleBeanInfo {
|
||||
|
||||
public PropertyDescriptor[] getPropertyDescriptors() {
|
||||
try {
|
||||
PropertyDescriptor pd = new PropertyDescriptor("value", ValueBean.class);
|
||||
pd.setPropertyEditorClass(MyNumberEditor.class);
|
||||
return new PropertyDescriptor[] {pd};
|
||||
}
|
||||
catch (IntrospectionException ex) {
|
||||
throw new FatalBeanException("Couldn't create PropertyDescriptor", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyNumberEditor extends CustomNumberEditor {
|
||||
|
||||
private Object target;
|
||||
|
||||
public MyNumberEditor() throws IllegalArgumentException {
|
||||
super(Integer.class, true);
|
||||
}
|
||||
|
||||
public MyNumberEditor(Object target) throws IllegalArgumentException {
|
||||
super(Integer.class, true);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_15) {
|
||||
Assert.isTrue(this.target instanceof ValueBean, "Target must be available on JDK 1.5+");
|
||||
}
|
||||
super.setAsText(text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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 junit.framework.TestCase;
|
||||
|
||||
import java.beans.PropertyEditor;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ByteArrayPropertyEditor} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
*/
|
||||
public final class ByteArrayPropertyEditorTests extends TestCase {
|
||||
|
||||
public void testSunnyDaySetAsText() throws Exception {
|
||||
final String text = "Hideous towns make me throw... up";
|
||||
|
||||
PropertyEditor byteEditor = new ByteArrayPropertyEditor();
|
||||
byteEditor.setAsText(text);
|
||||
|
||||
Object value = byteEditor.getValue();
|
||||
assertNotNull(value);
|
||||
assertTrue(value instanceof byte[]);
|
||||
byte[] bytes = (byte[]) value;
|
||||
for (int i = 0; i < text.length(); ++i) {
|
||||
assertEquals("cyte[] differs at index '" + i + "'", text.charAt(i), bytes[i]);
|
||||
}
|
||||
assertEquals(text, byteEditor.getAsText());
|
||||
}
|
||||
|
||||
public void testGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception {
|
||||
PropertyEditor byteEditor = new ByteArrayPropertyEditor();
|
||||
assertEquals("", byteEditor.getAsText());
|
||||
|
||||
byteEditor.setAsText(null);
|
||||
assertEquals("", byteEditor.getAsText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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 junit.framework.TestCase;
|
||||
|
||||
import java.beans.PropertyEditor;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link CharArrayPropertyEditor} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
*/
|
||||
public final class CharArrayPropertyEditorTests extends TestCase {
|
||||
|
||||
public void testSunnyDaySetAsText() throws Exception {
|
||||
final String text = "Hideous towns make me throw... up";
|
||||
|
||||
PropertyEditor charEditor = new CharArrayPropertyEditor();
|
||||
charEditor.setAsText(text);
|
||||
|
||||
Object value = charEditor.getValue();
|
||||
assertNotNull(value);
|
||||
assertTrue(value instanceof char[]);
|
||||
char[] chars = (char[]) value;
|
||||
for (int i = 0; i < text.length(); ++i) {
|
||||
assertEquals("char[] differs at index '" + i + "'", text.charAt(i), chars[i]);
|
||||
}
|
||||
assertEquals(text, charEditor.getAsText());
|
||||
}
|
||||
|
||||
public void testGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception {
|
||||
PropertyEditor charEditor = new CharArrayPropertyEditor();
|
||||
assertEquals("", charEditor.getAsText());
|
||||
|
||||
charEditor.setAsText(null);
|
||||
assertEquals("", charEditor.getAsText());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user