moving unit tests from .testsuite -> .beans

This commit is contained in:
Chris Beams 2008-12-15 08:41:35 +00:00
parent 248a7de73f
commit 52ac3cea8c
10 changed files with 194 additions and 142 deletions

View File

@ -16,81 +16,77 @@
package org.springframework.beans.propertyeditors; package org.springframework.beans.propertyeditors;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.springframework.test.AssertThrows;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.junit.Test;
/** /**
* Unit tests for the {@link CustomCollectionEditor} class. * Unit tests for the {@link CustomCollectionEditor} class.
* *
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
*/ */
public final class CustomCollectionEditorTests extends TestCase { public final class CustomCollectionEditorTests {
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullCollectionType() throws Exception { public void testCtorWithNullCollectionType() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new CustomCollectionEditor(null);
public void test() throws Exception {
new CustomCollectionEditor(null);
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNonCollectionType() throws Exception { public void testCtorWithNonCollectionType() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new CustomCollectionEditor(String.class);
public void test() throws Exception {
new CustomCollectionEditor(String.class);
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testWithCollectionTypeThatDoesNotExposeAPublicNoArgCtor() throws Exception { public void testWithCollectionTypeThatDoesNotExposeAPublicNoArgCtor() throws Exception {
new AssertThrows(IllegalArgumentException.class) { CustomCollectionEditor editor = new CustomCollectionEditor(CollectionTypeWithNoNoArgCtor.class);
public void test() throws Exception { editor.setValue("1");
CustomCollectionEditor editor = new CustomCollectionEditor(CollectionTypeWithNoNoArgCtor.class);
editor.setValue("1");
}
}.runTest();
} }
@Test
public void testSunnyDaySetValue() throws Exception { public void testSunnyDaySetValue() throws Exception {
CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class);
editor.setValue(new int[] {0, 1, 2}); editor.setValue(new int[] {0, 1, 2});
Object value = editor.getValue(); Object value = editor.getValue();
assertNotNull(value); assertNotNull(value);
assertTrue(value instanceof ArrayList); assertTrue(value instanceof ArrayList);
List list = (List) value; List<?> list = (List<?>) value;
assertEquals("There must be 3 elements in the converted collection", 3, list.size()); assertEquals("There must be 3 elements in the converted collection", 3, list.size());
assertEquals(new Integer(0), list.get(0)); assertEquals(new Integer(0), list.get(0));
assertEquals(new Integer(1), list.get(1)); assertEquals(new Integer(1), list.get(1));
assertEquals(new Integer(2), list.get(2)); assertEquals(new Integer(2), list.get(2));
} }
@Test
public void testWhenTargetTypeIsExactlyTheCollectionInterfaceUsesFallbackCollectionType() throws Exception { public void testWhenTargetTypeIsExactlyTheCollectionInterfaceUsesFallbackCollectionType() throws Exception {
CustomCollectionEditor editor = new CustomCollectionEditor(Collection.class); CustomCollectionEditor editor = new CustomCollectionEditor(Collection.class);
editor.setValue("0, 1, 2"); editor.setValue("0, 1, 2");
Collection value = (Collection) editor.getValue(); Collection<?> value = (Collection<?>) editor.getValue();
assertNotNull(value); assertNotNull(value);
assertEquals("There must be 1 element in the converted collection", 1, value.size()); assertEquals("There must be 1 element in the converted collection", 1, value.size());
assertEquals("0, 1, 2", value.iterator().next()); assertEquals("0, 1, 2", value.iterator().next());
} }
@Test
public void testSunnyDaySetAsTextYieldsSingleValue() throws Exception { public void testSunnyDaySetAsTextYieldsSingleValue() throws Exception {
CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class);
editor.setValue("0, 1, 2"); editor.setValue("0, 1, 2");
Object value = editor.getValue(); Object value = editor.getValue();
assertNotNull(value); assertNotNull(value);
assertTrue(value instanceof ArrayList); assertTrue(value instanceof ArrayList);
List list = (List) value; List<?> list = (List<?>) value;
assertEquals("There must be 1 element in the converted collection", 1, list.size()); assertEquals("There must be 1 element in the converted collection", 1, list.size());
assertEquals("0, 1, 2", list.get(0)); assertEquals("0, 1, 2", list.get(0));
} }
private static final class CollectionTypeWithNoNoArgCtor extends ArrayList { @SuppressWarnings("serial")
private static final class CollectionTypeWithNoNoArgCtor extends ArrayList<Object> {
public CollectionTypeWithNoNoArgCtor(String anArg) { public CollectionTypeWithNoNoArgCtor(String anArg) {
} }
} }

View File

@ -16,6 +16,8 @@
package org.springframework.beans.propertyeditors; package org.springframework.beans.propertyeditors;
import static org.junit.Assert.*;
import java.beans.PropertyEditor; import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport; import java.beans.PropertyEditorSupport;
import java.beans.PropertyVetoException; import java.beans.PropertyVetoException;
@ -36,8 +38,7 @@ import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
@ -48,7 +49,6 @@ import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NumberTestBean; import org.springframework.beans.NumberTestBean;
import org.springframework.beans.PropertyValue; import org.springframework.beans.PropertyValue;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.test.AssertThrows;
/** /**
* Unit tests for the various PropertyEditors in Spring. * Unit tests for the various PropertyEditors in Spring.
@ -57,11 +57,13 @@ import org.springframework.test.AssertThrows;
* @author Rick Evans * @author Rick Evans
* @author Rob Harrop * @author Rob Harrop
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Chris Beams
* *
* @since 10.06.2003 * @since 10.06.2003
*/ */
public class CustomEditorTests extends TestCase { public class CustomEditorTests {
@Test
public void testComplexObject() { public void testComplexObject() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
String newName = "Rod"; String newName = "Rod";
@ -80,6 +82,7 @@ public class CustomEditorTests extends TestCase {
tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34); tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
} }
@Test
public void testComplexObjectWithOldValueAccess() { public void testComplexObjectWithOldValueAccess() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
String newName = "Rod"; String newName = "Rod";
@ -104,6 +107,7 @@ public class CustomEditorTests extends TestCase {
assertSame("Should have remained same object", spouse, tb.getSpouse()); assertSame("Should have remained same object", spouse, tb.getSpouse());
} }
@Test
public void testCustomEditorForSingleProperty() { public void testCustomEditorForSingleProperty() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -120,6 +124,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("value", tb.getTouchy()); assertEquals("value", tb.getTouchy());
} }
@Test
public void testCustomEditorForAllStringProperties() { public void testCustomEditorForAllStringProperties() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -136,6 +141,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("prefixvalue", tb.getTouchy()); assertEquals("prefixvalue", tb.getTouchy());
} }
@Test
public void testCustomEditorForSingleNestedProperty() { public void testCustomEditorForSingleNestedProperty() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setSpouse(new TestBean()); tb.setSpouse(new TestBean());
@ -153,6 +159,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("value", tb.getTouchy()); assertEquals("value", tb.getTouchy());
} }
@Test
public void testCustomEditorForAllNestedStringProperties() { public void testCustomEditorForAllNestedStringProperties() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setSpouse(new TestBean()); tb.setSpouse(new TestBean());
@ -170,6 +177,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("prefixvalue", tb.getTouchy()); assertEquals("prefixvalue", tb.getTouchy());
} }
@Test
public void testDefaultBooleanEditorForPrimitiveType() { public void testDefaultBooleanEditorForPrimitiveType() {
BooleanTestBean tb = new BooleanTestBean(); BooleanTestBean tb = new BooleanTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -215,6 +223,7 @@ public class CustomEditorTests extends TestCase {
} }
} }
@Test
public void testDefaultBooleanEditorForWrapperType() { public void testDefaultBooleanEditorForWrapperType() {
BooleanTestBean tb = new BooleanTestBean(); BooleanTestBean tb = new BooleanTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -249,6 +258,7 @@ public class CustomEditorTests extends TestCase {
assertNull("Correct bool2 value", tb.getBool2()); assertNull("Correct bool2 value", tb.getBool2());
} }
@Test
public void testCustomBooleanEditorWithAllowEmpty() { public void testCustomBooleanEditorWithAllowEmpty() {
BooleanTestBean tb = new BooleanTestBean(); BooleanTestBean tb = new BooleanTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -285,6 +295,7 @@ public class CustomEditorTests extends TestCase {
assertTrue("Correct bool2 value", tb.getBool2() == null); assertTrue("Correct bool2 value", tb.getBool2() == null);
} }
@Test
public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Exception { public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Exception {
final String trueString = "pechorin"; final String trueString = "pechorin";
final String falseString = "nash"; final String falseString = "nash";
@ -306,6 +317,7 @@ public class CustomEditorTests extends TestCase {
assertEquals(falseString, editor.getAsText()); assertEquals(falseString, editor.getAsText());
} }
@Test
public void testDefaultNumberEditor() { public void testDefaultNumberEditor() {
NumberTestBean tb = new NumberTestBean(); NumberTestBean tb = new NumberTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -349,6 +361,7 @@ public class CustomEditorTests extends TestCase {
assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal())); assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
} }
@Test
public void testCustomNumberEditorWithoutAllowEmpty() { public void testCustomNumberEditorWithoutAllowEmpty() {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
NumberTestBean tb = new NumberTestBean(); NumberTestBean tb = new NumberTestBean();
@ -405,22 +418,17 @@ public class CustomEditorTests extends TestCase {
assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal())); assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
} }
@Test(expected=IllegalArgumentException.class)
public void testCustomNumberEditorCtorWithNullNumberType() throws Exception { public void testCustomNumberEditorCtorWithNullNumberType() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new CustomNumberEditor(null, true);
public void test() throws Exception {
new CustomNumberEditor(null, true);
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testCustomNumberEditorCtorWithNonNumberType() throws Exception { public void testCustomNumberEditorCtorWithNonNumberType() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new CustomNumberEditor(String.class, true);
public void test() throws Exception {
new CustomNumberEditor(String.class, true);
}
}.runTest();
} }
@Test
public void testCustomNumberEditorWithAllowEmpty() { public void testCustomNumberEditorWithAllowEmpty() {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
NumberTestBean tb = new NumberTestBean(); NumberTestBean tb = new NumberTestBean();
@ -450,6 +458,7 @@ public class CustomEditorTests extends TestCase {
} }
} }
@Test
public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception { public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH); NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
NumberTestBean tb = new NumberTestBean(); NumberTestBean tb = new NumberTestBean();
@ -463,15 +472,18 @@ public class CustomEditorTests extends TestCase {
assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f); assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
} }
@Test
public void testParseShortGreaterThanMaxValueWithoutNumberFormat() { public void testParseShortGreaterThanMaxValueWithoutNumberFormat() {
new AssertThrows(NumberFormatException.class, Short.MAX_VALUE + 1 + " is greater than max value") { try {
public void test() throws Exception { CustomNumberEditor editor = new CustomNumberEditor(Short.class, true);
CustomNumberEditor editor = new CustomNumberEditor(Short.class, true); editor.setAsText(String.valueOf(Short.MAX_VALUE + 1));
editor.setAsText(String.valueOf(Short.MAX_VALUE + 1)); fail(Short.MAX_VALUE + 1 + " is greater than max value");
} } catch (NumberFormatException ex) {
}.runTest(); // expected
}
} }
@Test
public void testByteArrayPropertyEditor() { public void testByteArrayPropertyEditor() {
PrimitiveArrayBean bean = new PrimitiveArrayBean(); PrimitiveArrayBean bean = new PrimitiveArrayBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -479,6 +491,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("myvalue", new String(bean.getByteArray())); assertEquals("myvalue", new String(bean.getByteArray()));
} }
@Test
public void testCharArrayPropertyEditor() { public void testCharArrayPropertyEditor() {
PrimitiveArrayBean bean = new PrimitiveArrayBean(); PrimitiveArrayBean bean = new PrimitiveArrayBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -486,6 +499,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("myvalue", new String(bean.getCharArray())); assertEquals("myvalue", new String(bean.getCharArray()));
} }
@Test
public void testCharacterEditor() { public void testCharacterEditor() {
CharBean cb = new CharBean(); CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb); BeanWrapper bw = new BeanWrapperImpl(cb);
@ -507,6 +521,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("M", editor.getAsText()); assertEquals("M", editor.getAsText());
} }
@Test
public void testCharacterEditorWithAllowEmpty() { public void testCharacterEditorWithAllowEmpty() {
CharBean cb = new CharBean(); CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb); BeanWrapper bw = new BeanWrapperImpl(cb);
@ -528,15 +543,13 @@ public class CustomEditorTests extends TestCase {
assertNull(cb.getMyCharacter()); assertNull(cb.getMyCharacter());
} }
@Test(expected=IllegalArgumentException.class)
public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception { public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception {
new AssertThrows(IllegalArgumentException.class) { PropertyEditor charEditor = new CharacterEditor(false);
public void test() throws Exception { charEditor.setAsText("ColdWaterCanyon");
PropertyEditor charEditor = new CharacterEditor(false);
charEditor.setAsText("ColdWaterCanyon");
}
}.runTest();
} }
@Test
public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception { public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception {
PropertyEditor charEditor = new CharacterEditor(false); PropertyEditor charEditor = new CharacterEditor(false);
assertEquals("", charEditor.getAsText()); assertEquals("", charEditor.getAsText());
@ -549,15 +562,13 @@ public class CustomEditorTests extends TestCase {
assertEquals(" ", charEditor.getAsText()); assertEquals(" ", charEditor.getAsText());
} }
@Test(expected=IllegalArgumentException.class)
public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception { public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception {
new AssertThrows(IllegalArgumentException.class) { PropertyEditor charEditor = new CharacterEditor(false);
public void test() throws Exception { charEditor.setAsText(null);
PropertyEditor charEditor = new CharacterEditor(false);
charEditor.setAsText(null);
}
}.runTest();
} }
@Test
public void testClassEditor() { public void testClassEditor() {
PropertyEditor classEditor = new ClassEditor(); PropertyEditor classEditor = new ClassEditor();
classEditor.setAsText("org.springframework.beans.TestBean"); classEditor.setAsText("org.springframework.beans.TestBean");
@ -572,15 +583,13 @@ public class CustomEditorTests extends TestCase {
assertEquals("", classEditor.getAsText()); assertEquals("", classEditor.getAsText());
} }
@Test(expected=IllegalArgumentException.class)
public void testClassEditorWithNonExistentClass() throws Exception { public void testClassEditorWithNonExistentClass() throws Exception {
new AssertThrows(IllegalArgumentException.class) { PropertyEditor classEditor = new ClassEditor();
public void test() throws Exception { classEditor.setAsText("hairdresser.on.Fire");
PropertyEditor classEditor = new ClassEditor();
classEditor.setAsText("hairdresser.on.Fire");
}
}.runTest();
} }
@Test
public void testClassEditorWithArray() { public void testClassEditorWithArray() {
PropertyEditor classEditor = new ClassEditor(); PropertyEditor classEditor = new ClassEditor();
classEditor.setAsText("org.springframework.beans.TestBean[]"); classEditor.setAsText("org.springframework.beans.TestBean[]");
@ -591,6 +600,7 @@ public class CustomEditorTests extends TestCase {
/* /*
* SPR_2165 - ClassEditor is inconsistent with multidimensional arrays * SPR_2165 - ClassEditor is inconsistent with multidimensional arrays
*/ */
@Test
public void testGetAsTextWithTwoDimensionalArray() throws Exception { public void testGetAsTextWithTwoDimensionalArray() throws Exception {
String[][] chessboard = new String[8][8]; String[][] chessboard = new String[8][8];
ClassEditor editor = new ClassEditor(); ClassEditor editor = new ClassEditor();
@ -601,6 +611,7 @@ public class CustomEditorTests extends TestCase {
/* /*
* SPR_2165 - ClassEditor is inconsistent with multidimensional arrays * SPR_2165 - ClassEditor is inconsistent with multidimensional arrays
*/ */
@Test
public void testGetAsTextWithRidiculousMultiDimensionalArray() throws Exception { public void testGetAsTextWithRidiculousMultiDimensionalArray() throws Exception {
String[][][][][] ridiculousChessboard = new String[8][4][0][1][3]; String[][][][][] ridiculousChessboard = new String[8][4][0][1][3];
ClassEditor editor = new ClassEditor(); ClassEditor editor = new ClassEditor();
@ -608,6 +619,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("java.lang.String[][][][][]", editor.getAsText()); assertEquals("java.lang.String[][][][][]", editor.getAsText());
} }
@Test
public void testFileEditor() { public void testFileEditor() {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:myfile.txt"); fileEditor.setAsText("file:myfile.txt");
@ -615,6 +627,7 @@ public class CustomEditorTests extends TestCase {
assertEquals((new File("myfile.txt")).getPath(), fileEditor.getAsText()); assertEquals((new File("myfile.txt")).getPath(), fileEditor.getAsText());
} }
@Test
public void testFileEditorWithRelativePath() { public void testFileEditorWithRelativePath() {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
try { try {
@ -626,6 +639,7 @@ public class CustomEditorTests extends TestCase {
} }
} }
@Test
public void testFileEditorWithAbsolutePath() { public void testFileEditorWithAbsolutePath() {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
// testing on Windows // testing on Windows
@ -640,6 +654,7 @@ public class CustomEditorTests extends TestCase {
} }
} }
@Test
public void testLocaleEditor() { public void testLocaleEditor() {
PropertyEditor localeEditor = new LocaleEditor(); PropertyEditor localeEditor = new LocaleEditor();
localeEditor.setAsText("en_CA"); localeEditor.setAsText("en_CA");
@ -650,6 +665,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", localeEditor.getAsText()); assertEquals("", localeEditor.getAsText());
} }
@Test
public void testPatternEditor() { public void testPatternEditor() {
final String REGEX = "a.*"; final String REGEX = "a.*";
@ -666,6 +682,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", patternEditor.getAsText()); assertEquals("", patternEditor.getAsText());
} }
@Test
public void testCustomBooleanEditor() { public void testCustomBooleanEditor() {
CustomBooleanEditor editor = new CustomBooleanEditor(false); CustomBooleanEditor editor = new CustomBooleanEditor(false);
editor.setAsText("true"); editor.setAsText("true");
@ -679,6 +696,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCustomBooleanEditorWithEmptyAsNull() { public void testCustomBooleanEditorWithEmptyAsNull() {
CustomBooleanEditor editor = new CustomBooleanEditor(true); CustomBooleanEditor editor = new CustomBooleanEditor(true);
editor.setAsText("true"); editor.setAsText("true");
@ -692,6 +710,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCustomDateEditor() { public void testCustomDateEditor() {
CustomDateEditor editor = new CustomDateEditor(null, false); CustomDateEditor editor = new CustomDateEditor(null, false);
editor.setValue(null); editor.setValue(null);
@ -699,6 +718,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCustomDateEditorWithEmptyAsNull() { public void testCustomDateEditorWithEmptyAsNull() {
CustomDateEditor editor = new CustomDateEditor(null, true); CustomDateEditor editor = new CustomDateEditor(null, true);
editor.setValue(null); editor.setValue(null);
@ -706,6 +726,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCustomDateEditorWithExactDateLength() { public void testCustomDateEditorWithExactDateLength() {
int maxLength = 10; int maxLength = 10;
String validDate = "01/01/2005"; String validDate = "01/01/2005";
@ -733,6 +754,7 @@ public class CustomEditorTests extends TestCase {
} }
} }
@Test
public void testCustomNumberEditor() { public void testCustomNumberEditor() {
CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false);
editor.setAsText("5"); editor.setAsText("5");
@ -743,12 +765,14 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCustomNumberEditorWithHex() { public void testCustomNumberEditorWithHex() {
CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false);
editor.setAsText("0x" + Integer.toHexString(64)); editor.setAsText("0x" + Integer.toHexString(64));
assertEquals(new Integer(64), editor.getValue()); assertEquals(new Integer(64), editor.getValue());
} }
@Test
public void testCustomNumberEditorWithEmptyAsNull() { public void testCustomNumberEditorWithEmptyAsNull() {
CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true); CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true);
editor.setAsText("5"); editor.setAsText("5");
@ -762,6 +786,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testStringTrimmerEditor() { public void testStringTrimmerEditor() {
StringTrimmerEditor editor = new StringTrimmerEditor(false); StringTrimmerEditor editor = new StringTrimmerEditor(false);
editor.setAsText("test"); editor.setAsText("test");
@ -779,6 +804,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testStringTrimmerEditorWithEmptyAsNull() { public void testStringTrimmerEditorWithEmptyAsNull() {
StringTrimmerEditor editor = new StringTrimmerEditor(true); StringTrimmerEditor editor = new StringTrimmerEditor(true);
editor.setAsText("test"); editor.setAsText("test");
@ -794,6 +820,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testStringTrimmerEditorWithCharsToDelete() { public void testStringTrimmerEditorWithCharsToDelete() {
StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false); StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false);
editor.setAsText("te\ns\ft"); editor.setAsText("te\ns\ft");
@ -809,6 +836,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() { public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() {
StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true); StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true);
editor.setAsText("te\ns\ft"); editor.setAsText("te\ns\ft");
@ -824,6 +852,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testIndexedPropertiesWithCustomEditorForType() { public void testIndexedPropertiesWithCustomEditorForType() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -875,6 +904,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name")); assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name"));
} }
@Test
public void testIndexedPropertiesWithCustomEditorForProperty() { public void testIndexedPropertiesWithCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(false); IndexedTestBean bean = new IndexedTestBean(false);
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -938,6 +968,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("mapname0", bw.getPropertyValue("map['key2'].name")); assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
} }
@Test
public void testIndexedPropertiesWithIndividualCustomEditorForProperty() { public void testIndexedPropertiesWithIndividualCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(false); IndexedTestBean bean = new IndexedTestBean(false);
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -1016,6 +1047,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name")); assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name"));
} }
@Test
public void testNestedIndexedPropertiesWithCustomEditorForProperty() { public void testNestedIndexedPropertiesWithCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
TestBean tb0 = bean.getArray()[0]; TestBean tb0 = bean.getArray()[0];
@ -1093,6 +1125,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name")); assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name"));
} }
@Test
public void testNestedIndexedPropertiesWithIndexedCustomEditorForProperty() { public void testNestedIndexedPropertiesWithIndexedCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
TestBean tb0 = bean.getArray()[0]; TestBean tb0 = bean.getArray()[0];
@ -1140,6 +1173,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()); assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
} }
@Test
public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() { public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -1187,6 +1221,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName()); assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
} }
@Test
public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() { public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
@ -1261,12 +1296,13 @@ public class CustomEditorTests extends TestCase {
assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName()); assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName());
} }
@Test
public void testIndexedPropertiesWithListPropertyEditor() { public void testIndexedPropertiesWithListPropertyEditor() {
IndexedTestBean bean = new IndexedTestBean(); IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() { bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
List result = new ArrayList(); List<TestBean> result = new ArrayList<TestBean>();
result.add(new TestBean("list" + text, 99)); result.add(new TestBean("list" + text, 99));
setValue(result); setValue(result);
} }
@ -1277,6 +1313,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("test", bean.getList().get(0)); assertEquals("test", bean.getList().get(0));
} }
@Test
public void testConversionToOldCollections() throws PropertyVetoException { public void testConversionToOldCollections() throws PropertyVetoException {
OldCollectionsBean tb = new OldCollectionsBean(); OldCollectionsBean tb = new OldCollectionsBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -1293,13 +1330,14 @@ public class CustomEditorTests extends TestCase {
assertEquals("bar", tb.getHashtable().get("foo")); assertEquals("bar", tb.getHashtable().get("foo"));
} }
@Test
public void testUninitializedArrayPropertyWithCustomEditor() { public void testUninitializedArrayPropertyWithCustomEditor() {
IndexedTestBean bean = new IndexedTestBean(false); IndexedTestBean bean = new IndexedTestBean(false);
BeanWrapper bw = new BeanWrapperImpl(bean); BeanWrapper bw = new BeanWrapperImpl(bean);
PropertyEditor pe = new CustomNumberEditor(Integer.class, true); PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
bw.registerCustomEditor(null, "list.age", pe); bw.registerCustomEditor(null, "list.age", pe);
TestBean tb = new TestBean(); TestBean tb = new TestBean();
bw.setPropertyValue("list", new ArrayList()); bw.setPropertyValue("list", new ArrayList<Object>());
bw.setPropertyValue("list[0]", tb); bw.setPropertyValue("list[0]", tb);
assertEquals(tb, bean.getList().get(0)); assertEquals(tb, bean.getList().get(0));
assertEquals(pe, bw.findCustomEditor(int.class, "list.age")); assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
@ -1308,6 +1346,7 @@ public class CustomEditorTests extends TestCase {
assertEquals(pe, bw.findCustomEditor(null, "list[0].age")); assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
} }
@Test
public void testArrayToArrayConversion() throws PropertyVetoException { public void testArrayToArrayConversion() throws PropertyVetoException {
IndexedTestBean tb = new IndexedTestBean(); IndexedTestBean tb = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -1322,6 +1361,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("b", tb.getArray()[1].getName()); assertEquals("b", tb.getArray()[1].getName());
} }
@Test
public void testArrayToStringConversion() throws PropertyVetoException { public void testArrayToStringConversion() throws PropertyVetoException {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb); BeanWrapper bw = new BeanWrapperImpl(tb);
@ -1334,10 +1374,11 @@ public class CustomEditorTests extends TestCase {
assertEquals("-a,b-", tb.getName()); assertEquals("-a,b-", tb.getName());
} }
@Test
public void testClassArrayEditorSunnyDay() throws Exception { public void testClassArrayEditorSunnyDay() throws Exception {
ClassArrayEditor classArrayEditor = new ClassArrayEditor(); ClassArrayEditor classArrayEditor = new ClassArrayEditor();
classArrayEditor.setAsText("java.lang.String,java.util.HashMap"); classArrayEditor.setAsText("java.lang.String,java.util.HashMap");
Class[] classes = (Class[]) classArrayEditor.getValue(); Class<?>[] classes = (Class<?>[]) classArrayEditor.getValue();
assertEquals(2, classes.length); assertEquals(2, classes.length);
assertEquals(String.class, classes[0]); assertEquals(String.class, classes[0]);
assertEquals(HashMap.class, classes[1]); assertEquals(HashMap.class, classes[1]);
@ -1346,10 +1387,11 @@ public class CustomEditorTests extends TestCase {
classArrayEditor.setAsText(classArrayEditor.getAsText()); classArrayEditor.setAsText(classArrayEditor.getAsText());
} }
@Test
public void testClassArrayEditorSunnyDayWithArrayTypes() throws Exception { public void testClassArrayEditorSunnyDayWithArrayTypes() throws Exception {
ClassArrayEditor classArrayEditor = new ClassArrayEditor(); ClassArrayEditor classArrayEditor = new ClassArrayEditor();
classArrayEditor.setAsText("java.lang.String[],java.util.Map[],int[],float[][][]"); classArrayEditor.setAsText("java.lang.String[],java.util.Map[],int[],float[][][]");
Class[] classes = (Class[]) classArrayEditor.getValue(); Class<?>[] classes = (Class<?>[]) classArrayEditor.getValue();
assertEquals(4, classes.length); assertEquals(4, classes.length);
assertEquals(String[].class, classes[0]); assertEquals(String[].class, classes[0]);
assertEquals(Map[].class, classes[1]); assertEquals(Map[].class, classes[1]);
@ -1360,6 +1402,7 @@ public class CustomEditorTests extends TestCase {
classArrayEditor.setAsText(classArrayEditor.getAsText()); classArrayEditor.setAsText(classArrayEditor.getAsText());
} }
@Test
public void testClassArrayEditorSetAsTextWithNull() throws Exception { public void testClassArrayEditorSetAsTextWithNull() throws Exception {
ClassArrayEditor editor = new ClassArrayEditor(); ClassArrayEditor editor = new ClassArrayEditor();
editor.setAsText(null); editor.setAsText(null);
@ -1367,6 +1410,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testClassArrayEditorSetAsTextWithEmptyString() throws Exception { public void testClassArrayEditorSetAsTextWithEmptyString() throws Exception {
ClassArrayEditor editor = new ClassArrayEditor(); ClassArrayEditor editor = new ClassArrayEditor();
editor.setAsText(""); editor.setAsText("");
@ -1374,6 +1418,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testClassArrayEditorSetAsTextWithWhitespaceString() throws Exception { public void testClassArrayEditorSetAsTextWithWhitespaceString() throws Exception {
ClassArrayEditor editor = new ClassArrayEditor(); ClassArrayEditor editor = new ClassArrayEditor();
editor.setAsText("\n"); editor.setAsText("\n");
@ -1381,6 +1426,7 @@ public class CustomEditorTests extends TestCase {
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test
public void testCharsetEditor() throws Exception { public void testCharsetEditor() throws Exception {
CharsetEditor editor = new CharsetEditor(); CharsetEditor editor = new CharsetEditor();
String name = "UTF-8"; String name = "UTF-8";
@ -1468,23 +1514,23 @@ public class CustomEditorTests extends TestCase {
private static class OldCollectionsBean { private static class OldCollectionsBean {
private Vector vector; private Vector<?> vector;
private Hashtable hashtable; private Hashtable<?, ?> hashtable;
public Vector getVector() { public Vector<?> getVector() {
return vector; return vector;
} }
public void setVector(Vector vector) { public void setVector(Vector<?> vector) {
this.vector = vector; this.vector = vector;
} }
public Hashtable getHashtable() { public Hashtable<?, ?> getHashtable() {
return hashtable; return hashtable;
} }
public void setHashtable(Hashtable hashtable) { public void setHashtable(Hashtable<?, ?> hashtable) {
this.hashtable = hashtable; this.hashtable = hashtable;
} }
} }

View File

@ -1,19 +1,36 @@
/*
* 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.propertyeditors; package org.springframework.beans.propertyeditors;
import static org.junit.Assert.*;
import java.beans.PropertyEditor; import java.beans.PropertyEditor;
import java.io.File; import java.io.File;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.test.AssertThrows;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
* @author Thomas Risberg * @author Thomas Risberg
* @author Chris Beams
*/ */
public final class FileEditorTests extends TestCase { public final class FileEditorTests {
@Test
public void testClasspathFileName() throws Exception { public void testClasspathFileName() throws Exception {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/"
@ -24,16 +41,13 @@ public final class FileEditorTests extends TestCase {
assertTrue(file.exists()); assertTrue(file.exists());
} }
@Test(expected=IllegalArgumentException.class)
public void testWithNonExistentResource() throws Exception { public void testWithNonExistentResource() throws Exception {
new AssertThrows(IllegalArgumentException.class) { PropertyEditor propertyEditor = new FileEditor();
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc");
public void test() throws Exception {
PropertyEditor propertyEditor = new FileEditor();
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc");
}
}.runTest();
} }
@Test
public void testWithNonExistentFile() throws Exception { public void testWithNonExistentFile() throws Exception {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:no_way_this_file_is_found.doc"); fileEditor.setAsText("file:no_way_this_file_is_found.doc");
@ -43,6 +57,7 @@ public final class FileEditorTests extends TestCase {
assertTrue(!file.exists()); assertTrue(!file.exists());
} }
@Test
public void testAbsoluteFileName() throws Exception { public void testAbsoluteFileName() throws Exception {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("/no_way_this_file_is_found.doc"); fileEditor.setAsText("/no_way_this_file_is_found.doc");
@ -52,6 +67,7 @@ public final class FileEditorTests extends TestCase {
assertTrue(!file.exists()); assertTrue(!file.exists());
} }
@Test
public void testUnqualifiedFileNameFound() throws Exception { public void testUnqualifiedFileNameFound() throws Exception {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
@ -68,6 +84,7 @@ public final class FileEditorTests extends TestCase {
assertTrue(absolutePath.endsWith(fileName)); assertTrue(absolutePath.endsWith(fileName));
} }
@Test
public void testUnqualifiedFileNameNotFound() throws Exception { public void testUnqualifiedFileNameNotFound() throws Exception {
PropertyEditor fileEditor = new FileEditor(); PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())

View File

@ -16,27 +16,27 @@
package org.springframework.beans.propertyeditors; package org.springframework.beans.propertyeditors;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.springframework.test.AssertThrows;
import org.springframework.util.ClassUtils;
import java.io.InputStream; import java.io.InputStream;
import org.junit.Test;
import org.springframework.util.ClassUtils;
/** /**
* Unit tests for the {@link InputStreamEditor} class. * Unit tests for the {@link InputStreamEditor} class.
* *
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
*/ */
public final class InputStreamEditorTests extends TestCase { public final class InputStreamEditorTests {
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullResourceEditor() throws Exception { public void testCtorWithNullResourceEditor() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new InputStreamEditor(null);
public void test() throws Exception {
new InputStreamEditor(null);
}
}.runTest();
} }
@Test
public void testSunnyDay() throws Exception { public void testSunnyDay() throws Exception {
InputStream stream = null; InputStream stream = null;
try { try {
@ -55,16 +55,14 @@ public final class InputStreamEditorTests extends TestCase {
} }
} }
@Test(expected=IllegalArgumentException.class)
public void testWhenResourceDoesNotExist() throws Exception { public void testWhenResourceDoesNotExist() throws Exception {
new AssertThrows(IllegalArgumentException.class) { String resource = "classpath:bingo!";
public void test() throws Exception { InputStreamEditor editor = new InputStreamEditor();
String resource = "classpath:bingo!"; editor.setAsText(resource);
InputStreamEditor editor = new InputStreamEditor();
editor.setAsText(resource);
}
}.runTest();
} }
@Test
public void testGetAsTextReturnsNullByDefault() throws Exception { public void testGetAsTextReturnsNullByDefault() throws Exception {
assertNull(new InputStreamEditor().getAsText()); assertNull(new InputStreamEditor().getAsText());
String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class";

View File

@ -16,22 +16,25 @@
package org.springframework.beans.propertyeditors; package org.springframework.beans.propertyeditors;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.springframework.test.AssertThrows;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import org.junit.Test;
/** /**
* Unit tests for the {@link ResourceBundleEditor} class. * Unit tests for the {@link ResourceBundleEditor} class.
* *
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
*/ */
public final class ResourceBundleEditorTests extends TestCase { public final class ResourceBundleEditorTests {
private static final String BASE_NAME = ResourceBundleEditorTests.class.getName(); private static final String BASE_NAME = ResourceBundleEditorTests.class.getName();
private static final String MESSAGE_KEY = "punk"; private static final String MESSAGE_KEY = "punk";
@Test
public void testSetAsTextWithJustBaseName() throws Exception { public void testSetAsTextWithJustBaseName() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME); editor.setAsText(BASE_NAME);
@ -43,6 +46,7 @@ public final class ResourceBundleEditorTests extends TestCase {
assertEquals(MESSAGE_KEY, string); assertEquals(MESSAGE_KEY, string);
} }
@Test
public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Exception { public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "_"); editor.setAsText(BASE_NAME + "_");
@ -54,6 +58,7 @@ public final class ResourceBundleEditorTests extends TestCase {
assertEquals(MESSAGE_KEY, string); assertEquals(MESSAGE_KEY, string);
} }
@Test
public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception { public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "Lang" + "_en"); editor.setAsText(BASE_NAME + "Lang" + "_en");
@ -65,6 +70,7 @@ public final class ResourceBundleEditorTests extends TestCase {
assertEquals("yob", string); assertEquals("yob", string);
} }
@Test
public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception { public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB"); editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB");
@ -76,6 +82,7 @@ public final class ResourceBundleEditorTests extends TestCase {
assertEquals("chav", string); assertEquals("chav", string);
} }
@Test
public void testSetAsTextWithTheKitchenSink() throws Exception { public void testSetAsTextWithTheKitchenSink() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW"); editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW");
@ -87,40 +94,28 @@ public final class ResourceBundleEditorTests extends TestCase {
assertEquals("ned", string); assertEquals("ned", string);
} }
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithNull() throws Exception { public void testSetAsTextWithNull() throws Exception {
new AssertThrows(IllegalArgumentException.class) { ResourceBundleEditor editor = new ResourceBundleEditor();
public void test() throws Exception { editor.setAsText(null);
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(null);
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithEmptyString() throws Exception { public void testSetAsTextWithEmptyString() throws Exception {
new AssertThrows(IllegalArgumentException.class) { ResourceBundleEditor editor = new ResourceBundleEditor();
public void test() throws Exception { editor.setAsText("");
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText("");
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithWhiteSpaceString() throws Exception { public void testSetAsTextWithWhiteSpaceString() throws Exception {
new AssertThrows(IllegalArgumentException.class) { ResourceBundleEditor editor = new ResourceBundleEditor();
public void test() throws Exception { editor.setAsText(" ");
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(" ");
}
}.runTest();
} }
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithJustSeparatorString() throws Exception { public void testSetAsTextWithJustSeparatorString() throws Exception {
new AssertThrows(IllegalArgumentException.class) { ResourceBundleEditor editor = new ResourceBundleEditor();
public void test() throws Exception { editor.setAsText("_");
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText("_");
}
}.runTest();
} }
} }

View File

@ -16,19 +16,21 @@
package org.springframework.beans.propertyeditors; package org.springframework.beans.propertyeditors;
import static org.junit.Assert.*;
import java.beans.PropertyEditor; import java.beans.PropertyEditor;
import java.net.URL; import java.net.URL;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.test.AssertThrows;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
*/ */
public final class URLEditorTests extends TestCase { public final class URLEditorTests {
@Test
public void testStandardURI() throws Exception { public void testStandardURI() throws Exception {
PropertyEditor urlEditor = new URLEditor(); PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("mailto:juergen.hoeller@interface21.com"); urlEditor.setAsText("mailto:juergen.hoeller@interface21.com");
@ -38,6 +40,7 @@ public final class URLEditorTests extends TestCase {
assertEquals(url.toExternalForm(), urlEditor.getAsText()); assertEquals(url.toExternalForm(), urlEditor.getAsText());
} }
@Test
public void testStandardURL() throws Exception { public void testStandardURL() throws Exception {
PropertyEditor urlEditor = new URLEditor(); PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("http://www.springframework.org"); urlEditor.setAsText("http://www.springframework.org");
@ -47,6 +50,7 @@ public final class URLEditorTests extends TestCase {
assertEquals(url.toExternalForm(), urlEditor.getAsText()); assertEquals(url.toExternalForm(), urlEditor.getAsText());
} }
@Test
public void testClasspathURL() throws Exception { public void testClasspathURL() throws Exception {
PropertyEditor urlEditor = new URLEditor(); PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
@ -58,15 +62,13 @@ public final class URLEditorTests extends TestCase {
assertTrue(!url.getProtocol().startsWith("classpath")); assertTrue(!url.getProtocol().startsWith("classpath"));
} }
@Test(expected=IllegalArgumentException.class)
public void testWithNonExistentResource() throws Exception { public void testWithNonExistentResource() throws Exception {
new AssertThrows(IllegalArgumentException.class) { PropertyEditor urlEditor = new URLEditor();
public void test() throws Exception { urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
}
}.runTest();
} }
@Test
public void testSetAsTextWithNull() throws Exception { public void testSetAsTextWithNull() throws Exception {
PropertyEditor urlEditor = new URLEditor(); PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText(null); urlEditor.setAsText(null);
@ -74,17 +76,15 @@ public final class URLEditorTests extends TestCase {
assertEquals("", urlEditor.getAsText()); assertEquals("", urlEditor.getAsText());
} }
@Test
public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception { public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
PropertyEditor urlEditor = new URLEditor(); PropertyEditor urlEditor = new URLEditor();
assertEquals("", urlEditor.getAsText()); assertEquals("", urlEditor.getAsText());
} }
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullResourceEditor() throws Exception { public void testCtorWithNullResourceEditor() throws Exception {
new AssertThrows(IllegalArgumentException.class) { new URLEditor(null);
public void test() throws Exception {
new URLEditor(null);
}
}.runTest();
} }
} }