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

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

View File

@@ -0,0 +1,103 @@
/*
* 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;
import static org.junit.Assert.*;
import java.beans.PropertyEditor;
import java.io.File;
import org.junit.Test;
import org.springframework.util.ClassUtils;
/**
* @author Thomas Risberg
* @author Chris Beams
*/
public final class FileEditorTests {
@Test
public void testClasspathFileName() throws Exception {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/"
+ ClassUtils.getShortName(getClass()) + ".class");
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(file.exists());
}
@Test(expected=IllegalArgumentException.class)
public void testWithNonExistentResource() throws Exception {
PropertyEditor propertyEditor = new FileEditor();
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc");
}
@Test
public void testWithNonExistentFile() throws Exception {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(!file.exists());
}
@Test
public void testAbsoluteFileName() throws Exception {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(!file.exists());
}
@Test
public void testUnqualifiedFileNameFound() throws Exception {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
+ ".class";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(file.exists());
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertTrue(absolutePath.endsWith(fileName));
}
@Test
public void testUnqualifiedFileNameNotFound() throws Exception {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
+ ".clazz";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertFalse(file.exists());
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertTrue(absolutePath.endsWith(fileName));
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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 static org.junit.Assert.*;
import java.io.InputStream;
import org.junit.Test;
import org.springframework.util.ClassUtils;
/**
* Unit tests for the {@link InputStreamEditor} class.
*
* @author Rick Evans
* @author Chris Beams
*/
public final class InputStreamEditorTests {
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullResourceEditor() throws Exception {
new InputStreamEditor(null);
}
@Test
public void testSunnyDay() throws Exception {
InputStream stream = null;
try {
String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class";
InputStreamEditor editor = new InputStreamEditor();
editor.setAsText(resource);
Object value = editor.getValue();
assertNotNull(value);
assertTrue(value instanceof InputStream);
stream = (InputStream) value;
assertTrue(stream.available() > 0);
} finally {
if (stream != null) {
stream.close();
}
}
}
@Test(expected=IllegalArgumentException.class)
public void testWhenResourceDoesNotExist() throws Exception {
String resource = "classpath:bingo!";
InputStreamEditor editor = new InputStreamEditor();
editor.setAsText(resource);
}
@Test
public void testGetAsTextReturnsNullByDefault() throws Exception {
assertNull(new InputStreamEditor().getAsText());
String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class";
InputStreamEditor editor = new InputStreamEditor();
editor.setAsText(resource);
assertNull(editor.getAsText());
}
}

View File

@@ -0,0 +1,121 @@
/*
* 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 static org.junit.Assert.*;
import java.util.ResourceBundle;
import org.junit.Test;
/**
* Unit tests for the {@link ResourceBundleEditor} class.
*
* @author Rick Evans
* @author Chris Beams
*/
public final class ResourceBundleEditorTests {
private static final String BASE_NAME = ResourceBundleEditorTests.class.getName();
private static final String MESSAGE_KEY = "punk";
@Test
public void testSetAsTextWithJustBaseName() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME);
Object value = editor.getValue();
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
ResourceBundle bundle = (ResourceBundle) value;
String string = bundle.getString(MESSAGE_KEY);
assertEquals(MESSAGE_KEY, string);
}
@Test
public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "_");
Object value = editor.getValue();
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
ResourceBundle bundle = (ResourceBundle) value;
String string = bundle.getString(MESSAGE_KEY);
assertEquals(MESSAGE_KEY, string);
}
@Test
public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "Lang" + "_en");
Object value = editor.getValue();
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
ResourceBundle bundle = (ResourceBundle) value;
String string = bundle.getString(MESSAGE_KEY);
assertEquals("yob", string);
}
@Test
public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB");
Object value = editor.getValue();
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
ResourceBundle bundle = (ResourceBundle) value;
String string = bundle.getString(MESSAGE_KEY);
assertEquals("chav", string);
}
@Test
public void testSetAsTextWithTheKitchenSink() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW");
Object value = editor.getValue();
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
ResourceBundle bundle = (ResourceBundle) value;
String string = bundle.getString(MESSAGE_KEY);
assertEquals("ned", string);
}
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithNull() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(null);
}
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithEmptyString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText("");
}
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithWhiteSpaceString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(" ");
}
@Test(expected=IllegalArgumentException.class)
public void testSetAsTextWithJustSeparatorString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText("_");
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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 static org.junit.Assert.*;
import java.beans.PropertyEditor;
import java.net.URL;
import org.junit.Test;
import org.springframework.util.ClassUtils;
/**
* @author Rick Evans
* @author Chris Beams
*/
public final class URLEditorTests {
@Test
public void testStandardURI() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("mailto:juergen.hoeller@interface21.com");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
}
@Test
public void testStandardURL() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("http://www.springframework.org");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
}
@Test
public void testClasspathURL() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
"/" + ClassUtils.getShortName(getClass()) + ".class");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
assertTrue(!url.getProtocol().startsWith("classpath"));
}
@Test(expected=IllegalArgumentException.class)
public void testWithNonExistentResource() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
}
@Test
public void testSetAsTextWithNull() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText(null);
assertNull(urlEditor.getValue());
assertEquals("", urlEditor.getAsText());
}
@Test
public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
PropertyEditor urlEditor = new URLEditor();
assertEquals("", urlEditor.getAsText());
}
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullResourceEditor() throws Exception {
new URLEditor(null);
}
}