Moved tests from testsuite to core

This commit is contained in:
Arjen Poutsma
2008-10-31 09:11:09 +00:00
parent 87a2905c6e
commit e1536fcabb
3 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
/*
* 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.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
/**
* Unit tests for the {@link Assert} class.
*
* @author Keith Donald
* @author Erwin Vervaet
* @author Rick Evans
* @author Arjen Poutsma
*/
public class AssertTests {
@Test(expected = IllegalArgumentException.class)
public void instanceOf() {
final Set set = new HashSet();
Assert.isInstanceOf(HashSet.class, set);
Assert.isInstanceOf(HashMap.class, set);
}
@Test
public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() {
Assert.isNull(null, "Bla");
}
@Test
public void isNullDoesNotThrowExceptionIfArgumentIsNull() {
Assert.isNull(null);
}
@Test(expected = IllegalArgumentException.class)
public void isNullThrowsExceptionIfArgumentIsNotNull() {
Assert.isNull(new Object());
}
@Test(expected = IllegalArgumentException.class)
public void isTrueWithFalseExpressionThrowsException() throws Exception {
Assert.isTrue(false);
}
@Test
public void isTrueWithTrueExpressionSunnyDay() throws Exception {
Assert.isTrue(true);
}
@Test(expected = IllegalArgumentException.class)
public void testHasLengthWithNullStringThrowsException() throws Exception {
Assert.hasLength(null);
}
@Test(expected = IllegalArgumentException.class)
public void hasLengthWithEmptyStringThrowsException() throws Exception {
Assert.hasLength("");
}
@Test
public void hasLengthWithWhitespaceOnlyStringDoesNotThrowException() throws Exception {
Assert.hasLength("\t ");
}
@Test
public void hasLengthSunnyDay() throws Exception {
Assert.hasLength("I Heart ...");
}
@Test
public void doesNotContainWithNullSearchStringDoesNotThrowException() throws Exception {
Assert.doesNotContain(null, "rod");
}
@Test
public void doesNotContainWithNullSubstringDoesNotThrowException() throws Exception {
Assert.doesNotContain("A cool chick's name is Brod. ", null);
}
@Test
public void doesNotContainWithEmptySubstringDoesNotThrowException() throws Exception {
Assert.doesNotContain("A cool chick's name is Brod. ", "");
}
@Test(expected = IllegalArgumentException.class)
public void assertNotEmptyWithNullCollectionThrowsException() throws Exception {
Assert.notEmpty((Collection) null);
}
@Test(expected = IllegalArgumentException.class)
public void assertNotEmptyWithEmptyCollectionThrowsException() throws Exception {
Assert.notEmpty(new ArrayList());
}
@Test
public void assertNotEmptyWithCollectionSunnyDay() throws Exception {
List<String> collection = new ArrayList<String>();
collection.add("");
Assert.notEmpty(collection);
}
@Test(expected = IllegalArgumentException.class)
public void assertNotEmptyWithNullMapThrowsException() throws Exception {
Assert.notEmpty((Map) null);
}
@Test(expected = IllegalArgumentException.class)
public void assertNotEmptyWithEmptyMapThrowsException() throws Exception {
Assert.notEmpty(new HashMap());
}
@Test
public void assertNotEmptyWithMapSunnyDay() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("", "");
Assert.notEmpty(map);
}
@Test(expected = IllegalArgumentException.class)
public void isInstanceofClassWithNullInstanceThrowsException() throws Exception {
Assert.isInstanceOf(String.class, null);
}
@Test(expected = IllegalStateException.class)
public void stateWithFalseExpressionThrowsException() throws Exception {
Assert.state(false);
}
@Test
public void stateWithTrueExpressionSunnyDay() throws Exception {
Assert.state(true);
}
}

View File

@@ -0,0 +1,298 @@
/*
* 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.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.TestBean;
/**
* <p> JUnit 4 based unit tests for {@link ReflectionUtils}. </p>
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Sam Brannen
* @author Arjen Poutsma
*/
public class ReflectionUtilsTests {
@Test
public void findField() {
Field field = ReflectionUtils.findField(TestBeanSubclassWithPublicField.class, "publicField", String.class);
assertNotNull(field);
assertEquals("publicField", field.getName());
assertEquals(String.class, field.getType());
assertTrue("Field should be public.", Modifier.isPublic(field.getModifiers()));
field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "prot", String.class);
assertNotNull(field);
assertEquals("prot", field.getName());
assertEquals(String.class, field.getType());
assertTrue("Field should be protected.", Modifier.isProtected(field.getModifiers()));
field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
assertNotNull(field);
assertEquals("name", field.getName());
assertEquals(String.class, field.getType());
assertTrue("Field should be private.", Modifier.isPrivate(field.getModifiers()));
}
@Test
public void setField() {
final TestBeanSubclassWithNewField testBean = new TestBeanSubclassWithNewField();
final Field field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, testBean, "FooBar");
assertNotNull(testBean.getName());
assertEquals("FooBar", testBean.getName());
ReflectionUtils.setField(field, testBean, null);
assertNull(testBean.getName());
}
@Test(expected = IllegalStateException.class)
public void setFieldIllegal() {
final TestBeanSubclassWithNewField testBean = new TestBeanSubclassWithNewField();
final Field field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
ReflectionUtils.setField(field, testBean, "FooBar");
}
@Test
public void invokeMethod() throws Exception {
String rob = "Rob Harrop";
TestBean bean = new TestBean();
bean.setName(rob);
Method getName = TestBean.class.getMethod("getName", (Class[]) null);
Method setName = TestBean.class.getMethod("setName", new Class[]{String.class});
Object name = ReflectionUtils.invokeMethod(getName, bean);
assertEquals("Incorrect name returned", rob, name);
String juergen = "Juergen Hoeller";
ReflectionUtils.invokeMethod(setName, bean, new Object[]{juergen});
assertEquals("Incorrect name set", juergen, bean.getName());
}
@Test
public void declaresException() throws Exception {
Method remoteExMethod = A.class.getDeclaredMethod("foo", new Class[]{Integer.class});
assertTrue(ReflectionUtils.declaresException(remoteExMethod, RemoteException.class));
assertTrue(ReflectionUtils.declaresException(remoteExMethod, ConnectException.class));
assertFalse(ReflectionUtils.declaresException(remoteExMethod, NoSuchMethodException.class));
assertFalse(ReflectionUtils.declaresException(remoteExMethod, Exception.class));
Method illegalExMethod = B.class.getDeclaredMethod("bar", new Class[]{String.class});
assertTrue(ReflectionUtils.declaresException(illegalExMethod, IllegalArgumentException.class));
assertTrue(ReflectionUtils.declaresException(illegalExMethod, NumberFormatException.class));
assertFalse(ReflectionUtils.declaresException(illegalExMethod, IllegalStateException.class));
assertFalse(ReflectionUtils.declaresException(illegalExMethod, Exception.class));
}
@Test
public void copySrcToDestinationOfIncorrectClass() {
TestBean src = new TestBean();
String dest = new String();
try {
ReflectionUtils.shallowCopyFieldState(src, dest);
fail();
}
catch (IllegalArgumentException ex) {
// Ok
}
}
@Test
public void rejectsNullSrc() {
TestBean src = null;
String dest = new String();
try {
ReflectionUtils.shallowCopyFieldState(src, dest);
fail();
}
catch (IllegalArgumentException ex) {
// Ok
}
}
@Test
public void rejectsNullDest() {
TestBean src = new TestBean();
String dest = null;
try {
ReflectionUtils.shallowCopyFieldState(src, dest);
fail();
}
catch (IllegalArgumentException ex) {
// Ok
}
}
@Test
public void validCopy() {
TestBean src = new TestBean();
TestBean dest = new TestBean();
testValidCopy(src, dest);
}
@Test
public void validCopyOnSubTypeWithNewField() {
TestBeanSubclassWithNewField src = new TestBeanSubclassWithNewField();
TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
src.magic = 11;
// Will check inherited fields are copied
testValidCopy(src, dest);
// Check subclass fields were copied
assertEquals(src.magic, dest.magic);
assertEquals(src.prot, dest.prot);
}
@Test
public void validCopyToSubType() {
TestBean src = new TestBean();
TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
dest.magic = 11;
testValidCopy(src, dest);
// Should have left this one alone
assertEquals(11, dest.magic);
}
@Test
public void validCopyToSubTypeWithFinalField() {
TestBeanSubclassWithFinalField src = new TestBeanSubclassWithFinalField();
TestBeanSubclassWithFinalField dest = new TestBeanSubclassWithFinalField();
// Check that this doesn't fail due to attempt to assign final
testValidCopy(src, dest);
}
private void testValidCopy(TestBean src, TestBean dest) {
src.setName("freddie");
src.setAge(15);
src.setSpouse(new TestBean());
assertFalse(src.getAge() == dest.getAge());
ReflectionUtils.shallowCopyFieldState(src, dest);
assertEquals(src.getAge(), dest.getAge());
assertEquals(src.getSpouse(), dest.getSpouse());
assertEquals(src.getDoctor(), dest.getDoctor());
}
static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback {
private List<String> methodNames = new LinkedList<String>();
private List<Method> methods = new LinkedList<Method>();
public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException {
this.methodNames.add(m.getName());
this.methods.add(m);
}
public List getMethodNames() {
return this.methodNames;
}
public List getMethods() {
return this.methods;
}
}
public void testDoWithProtectedMethods() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestBean.class, mc, new ReflectionUtils.MethodFilter() {
public boolean matches(Method m) {
return Modifier.isProtected(m.getModifiers());
}
});
assertFalse(mc.getMethodNames().isEmpty());
assertTrue("Must find protected method on Object", mc.getMethodNames().contains("clone"));
assertTrue("Must find protected method on Object", mc.getMethodNames().contains("finalize"));
assertFalse("Public, not protected", mc.getMethodNames().contains("hashCode"));
assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate"));
}
public static class TestBeanSubclass extends TestBean {
@Override
public void absquatulate() {
throw new UnsupportedOperationException();
}
}
public void testDuplicatesFound() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestBeanSubclass.class, mc);
int absquatulateCount = 0;
for (Iterator it = mc.getMethodNames().iterator(); it.hasNext();) {
String name = (String) it.next();
if (name.equals("absquatulate")) {
++absquatulateCount;
}
}
assertEquals("Found 2 absquatulates", 2, absquatulateCount);
}
public void testFindMethod() throws Exception {
assertNotNull(ReflectionUtils.findMethod(B.class, "bar", new Class[]{String.class}));
assertNotNull(ReflectionUtils.findMethod(B.class, "foo", new Class[]{Integer.class}));
}
public static class TestBeanSubclassWithPublicField extends TestBean {
public String publicField = "foo";
}
public static class TestBeanSubclassWithNewField extends TestBean {
private int magic;
protected String prot = "foo";
}
public static class TestBeanSubclassWithFinalField extends TestBean {
private final String foo = "will break naive copy that doesn't exclude statics";
}
private static class A {
private void foo(Integer i) throws RemoteException {
}
}
private static class B extends A {
void bar(String s) throws IllegalArgumentException {
}
}
}

View File

@@ -0,0 +1,157 @@
/*
* 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.util.xml;
import java.util.Properties;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
/**
* @author Rick Evans
* @author Arjen Poutsma
*/
public class TransformerUtilsTests {
@Test
public void enableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(String.valueOf(TransformerUtils.DEFAULT_INDENT_AMOUNT), indentAmount);
}
@Test
public void enableIndentingSunnyDayWithCustomKosherIndentAmount() throws Exception {
final String indentAmountProperty = "10";
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer, Integer.valueOf(indentAmountProperty));
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(indentAmountProperty, indentAmount);
}
@Test
public void disableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.disableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("no", indent);
}
@Test(expected = IllegalArgumentException.class)
public void enableIndentingWithNullTransformer() throws Exception {
TransformerUtils.enableIndenting(null);
}
@Test(expected = IllegalArgumentException.class)
public void disableIndentingWithNullTransformer() throws Exception {
TransformerUtils.disableIndenting(null);
}
@Test(expected = IllegalArgumentException.class)
public void enableIndentingWithNegativeIndentAmount() throws Exception {
TransformerUtils.enableIndenting(new StubTransformer(), -21938);
}
@Test
public void enableIndentingWithZeroIndentAmount() throws Exception {
TransformerUtils.enableIndenting(new StubTransformer(), 0);
}
private static class StubTransformer extends Transformer {
private Properties outputProperties = new Properties();
@Override
public void transform(Source xmlSource, Result outputTarget) throws TransformerException {
throw new UnsupportedOperationException();
}
@Override
public void setParameter(String name, Object value) {
throw new UnsupportedOperationException();
}
@Override
public Object getParameter(String name) {
throw new UnsupportedOperationException();
}
@Override
public void clearParameters() {
throw new UnsupportedOperationException();
}
@Override
public void setURIResolver(URIResolver resolver) {
throw new UnsupportedOperationException();
}
@Override
public URIResolver getURIResolver() {
throw new UnsupportedOperationException();
}
@Override
public void setOutputProperties(Properties oformat) {
throw new UnsupportedOperationException();
}
@Override
public Properties getOutputProperties() {
return this.outputProperties;
}
@Override
public void setOutputProperty(String name, String value) throws IllegalArgumentException {
this.outputProperties.setProperty(name, value);
}
@Override
public String getOutputProperty(String name) throws IllegalArgumentException {
return this.outputProperties.getProperty(name);
}
@Override
public void setErrorListener(ErrorListener listener) throws IllegalArgumentException {
throw new UnsupportedOperationException();
}
@Override
public ErrorListener getErrorListener() {
throw new UnsupportedOperationException();
}
}
}