Moved tests over from testsuite to beans
This commit is contained in:
parent
97e1cdd5fc
commit
6f0342388c
|
|
@ -0,0 +1,388 @@
|
|||
/*
|
||||
* 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.factory.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
|
||||
import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
|
||||
import org.springframework.util.MethodInvoker;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.11.2003
|
||||
*/
|
||||
public class MethodInvokingFactoryBeanTests extends TestCase {
|
||||
|
||||
public void testParameterValidation() throws Exception {
|
||||
String validationError = "improper validation of input properties";
|
||||
|
||||
// assert that only static OR non static are set, but not both or none
|
||||
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(this);
|
||||
mcfb.setTargetMethod("whatever");
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// bogus static method
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("some.bogus.Method.name");
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// bogus static method
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("method1");
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// missing method
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(this);
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// bogus method
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(this);
|
||||
mcfb.setTargetMethod("bogus");
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail(validationError);
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// static method
|
||||
TestClass1._staticField1 = 0;
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("staticMethod1");
|
||||
mcfb.afterPropertiesSet();
|
||||
|
||||
// non-static method
|
||||
TestClass1 tc1 = new TestClass1();
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(tc1);
|
||||
mcfb.setTargetMethod("method1");
|
||||
mcfb.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testGetObjectType() throws Exception {
|
||||
TestClass1 tc1 = new TestClass1();
|
||||
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(tc1);
|
||||
mcfb.setTargetMethod("method1");
|
||||
mcfb.afterPropertiesSet();
|
||||
assertTrue(int.class.equals(mcfb.getObjectType()));
|
||||
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("voidRetvalMethod");
|
||||
mcfb.afterPropertiesSet();
|
||||
Class objType = mcfb.getObjectType();
|
||||
assertTrue(objType.equals(void.class));
|
||||
|
||||
// verify that we can call a method with args that are subtypes of the
|
||||
// target method arg types
|
||||
TestClass1._staticField1 = 0;
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes");
|
||||
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello"});
|
||||
mcfb.afterPropertiesSet();
|
||||
mcfb.getObjectType();
|
||||
|
||||
// fail on improper argument types at afterPropertiesSet
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.registerCustomEditor(String.class, new StringTrimmerEditor(false));
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes");
|
||||
mcfb.setArguments(new Object[] {"1", new Object()});
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail("Should have thrown NoSuchMethodException");
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetObject() throws Exception {
|
||||
// singleton, non-static
|
||||
TestClass1 tc1 = new TestClass1();
|
||||
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(tc1);
|
||||
mcfb.setTargetMethod("method1");
|
||||
mcfb.afterPropertiesSet();
|
||||
Integer i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
|
||||
// non-singleton, non-static
|
||||
tc1 = new TestClass1();
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetObject(tc1);
|
||||
mcfb.setTargetMethod("method1");
|
||||
mcfb.setSingleton(false);
|
||||
mcfb.afterPropertiesSet();
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(2, i.intValue());
|
||||
|
||||
// singleton, static
|
||||
TestClass1._staticField1 = 0;
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("staticMethod1");
|
||||
mcfb.afterPropertiesSet();
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
|
||||
// non-singleton, static
|
||||
TestClass1._staticField1 = 0;
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setStaticMethod("org.springframework.beans.factory.config.MethodInvokingFactoryBeanTests$TestClass1.staticMethod1");
|
||||
mcfb.setSingleton(false);
|
||||
mcfb.afterPropertiesSet();
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(1, i.intValue());
|
||||
i = (Integer) mcfb.getObject();
|
||||
assertEquals(2, i.intValue());
|
||||
|
||||
// void return value
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("voidRetvalMethod");
|
||||
mcfb.afterPropertiesSet();
|
||||
assertNull(mcfb.getObject());
|
||||
|
||||
// now see if we can match methods with arguments that have supertype arguments
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes");
|
||||
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello"});
|
||||
// should pass
|
||||
mcfb.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testArgumentConversion() throws Exception {
|
||||
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes");
|
||||
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", "bogus"});
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail("Matched method with wrong number of args");
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes");
|
||||
mcfb.setArguments(new Object[] {new Integer(1), new Object()});
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
mcfb.getObject();
|
||||
fail("Should have failed on getObject with mismatched argument types");
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes2");
|
||||
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", "bogus"});
|
||||
mcfb.afterPropertiesSet();
|
||||
assertEquals("hello", mcfb.getObject());
|
||||
|
||||
mcfb = new MethodInvokingFactoryBean();
|
||||
mcfb.setTargetClass(TestClass1.class);
|
||||
mcfb.setTargetMethod("supertypes2");
|
||||
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), new Object()});
|
||||
try {
|
||||
mcfb.afterPropertiesSet();
|
||||
fail("Matched method when shouldn't have matched");
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvokeWithNullArgument() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("nullArgument");
|
||||
methodInvoker.setArguments(new Object[] {null});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
}
|
||||
|
||||
public void testInvokeWithIntArgument() throws Exception {
|
||||
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArgument");
|
||||
methodInvoker.setArguments(new Object[] {new Integer(5)});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArgument");
|
||||
methodInvoker.setArguments(new Object[] {"5"});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
}
|
||||
|
||||
public void testInvokeWithIntArguments() throws Exception {
|
||||
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new Object[] {new Integer[] {new Integer(5), new Integer(10)}});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new Object[] {new String[] {"5", "10"}});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new Integer[] {new Integer(5), new Integer(10)});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new String[] {"5", "10"});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new Object[] {new Integer(5), new Integer(10)});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
|
||||
methodInvoker = new ArgumentConvertingMethodInvoker();
|
||||
methodInvoker.setTargetClass(TestClass1.class);
|
||||
methodInvoker.setTargetMethod("intArguments");
|
||||
methodInvoker.setArguments(new Object[] {"5", "10"});
|
||||
methodInvoker.prepare();
|
||||
methodInvoker.invoke();
|
||||
}
|
||||
|
||||
public static class TestClass1 {
|
||||
|
||||
public static int _staticField1;
|
||||
|
||||
public int _field1 = 0;
|
||||
|
||||
public int method1() {
|
||||
return ++_field1;
|
||||
}
|
||||
|
||||
public static int staticMethod1() {
|
||||
return ++TestClass1._staticField1;
|
||||
}
|
||||
|
||||
public static void voidRetvalMethod() {
|
||||
}
|
||||
|
||||
public static void nullArgument(Object arg) {
|
||||
}
|
||||
|
||||
public static void intArgument(int arg) {
|
||||
}
|
||||
|
||||
public static void intArguments(int[] arg) {
|
||||
}
|
||||
|
||||
public static String supertypes(Collection c, Integer i) {
|
||||
return i.toString();
|
||||
}
|
||||
|
||||
public static String supertypes(Collection c, List l, String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String supertypes2(Collection c, List l, Integer i) {
|
||||
return i.toString();
|
||||
}
|
||||
|
||||
public static String supertypes2(Collection c, List l, String s, Integer i) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String supertypes2(Collection c, List l, String s, String s2) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* The Spring Framework is published under the terms
|
||||
* of the Apache Software License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.NotSerializableException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Utilities for testing serializability of objects.
|
||||
* Exposes static methods for use in other test cases.
|
||||
* Extends TestCase only to test itself.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class SerializationTestUtils extends TestCase {
|
||||
|
||||
public static void testSerialization(Object o) throws IOException {
|
||||
OutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
}
|
||||
|
||||
public static boolean isSerializable(Object o) throws IOException {
|
||||
try {
|
||||
testSerialization(o);
|
||||
return true;
|
||||
}
|
||||
catch (NotSerializableException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
oos.flush();
|
||||
baos.flush();
|
||||
byte[] bytes = baos.toByteArray();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
Object o2 = ois.readObject();
|
||||
|
||||
return o2;
|
||||
}
|
||||
|
||||
public SerializationTestUtils(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public void testWithNonSerializableObject() throws IOException {
|
||||
TestBean o = new TestBean();
|
||||
assertFalse(o instanceof Serializable);
|
||||
|
||||
assertFalse(isSerializable(o));
|
||||
|
||||
try {
|
||||
testSerialization(o);
|
||||
fail();
|
||||
}
|
||||
catch (NotSerializableException ex) {
|
||||
// Ok
|
||||
}
|
||||
}
|
||||
|
||||
public void testWithSerializableObject() throws Exception {
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
Point p = new Point(x, y);
|
||||
assertTrue(p instanceof Serializable);
|
||||
|
||||
testSerialization(p);
|
||||
|
||||
assertTrue(isSerializable(p));
|
||||
|
||||
Point p2 = (Point) serializeAndDeserialize(p);
|
||||
assertNotSame(p, p2);
|
||||
assertEquals(x, (int) p2.getX());
|
||||
assertEquals(y, (int) p2.getY());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue