Moved tests over from testsuite to core
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* 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.util.LinkedList;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AutoPopulatingListTests extends TestCase {
|
||||
|
||||
public void testWithClass() throws Exception {
|
||||
doTestWithClass(new AutoPopulatingList(TestBean.class));
|
||||
}
|
||||
|
||||
public void testWithClassAndUserSuppliedBackingList() throws Exception {
|
||||
doTestWithClass(new AutoPopulatingList(new LinkedList(), TestBean.class));
|
||||
}
|
||||
|
||||
public void testWithElementFactory() throws Exception {
|
||||
doTestWithElementFactory(new AutoPopulatingList(new MockElementFactory()));
|
||||
}
|
||||
|
||||
public void testWithElementFactoryAndUserSuppliedBackingList() throws Exception {
|
||||
doTestWithElementFactory(new AutoPopulatingList(new LinkedList(), new MockElementFactory()));
|
||||
}
|
||||
|
||||
private void doTestWithClass(AutoPopulatingList list) {
|
||||
Object lastElement = null;
|
||||
for (int x = 0; x < 10; x++) {
|
||||
Object element = list.get(x);
|
||||
assertNotNull("Element is null", list.get(x));
|
||||
assertTrue("Element is incorrect type", element instanceof TestBean);
|
||||
assertNotSame(lastElement, element);
|
||||
lastElement = element;
|
||||
}
|
||||
|
||||
String helloWorld = "Hello World!";
|
||||
list.add(10, null);
|
||||
list.add(11, helloWorld);
|
||||
assertEquals(helloWorld, list.get(11));
|
||||
|
||||
assertTrue(list.get(10) instanceof TestBean);
|
||||
assertTrue(list.get(12) instanceof TestBean);
|
||||
assertTrue(list.get(13) instanceof TestBean);
|
||||
assertTrue(list.get(20) instanceof TestBean);
|
||||
}
|
||||
|
||||
private void doTestWithElementFactory(AutoPopulatingList list) {
|
||||
doTestWithClass(list);
|
||||
|
||||
for(int x = 0; x < list.size(); x++) {
|
||||
Object element = list.get(x);
|
||||
if(element instanceof TestBean) {
|
||||
assertEquals(x, ((TestBean) element).getAge());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSerialization() throws Exception {
|
||||
AutoPopulatingList list = new AutoPopulatingList(TestBean.class);
|
||||
assertEquals(list, SerializationTestUtils.serializeAndDeserialize(list));
|
||||
}
|
||||
|
||||
|
||||
private static class MockElementFactory implements AutoPopulatingList.ElementFactory {
|
||||
|
||||
public Object createElement(int index) {
|
||||
TestBean bean = new TestBean();
|
||||
bean.setAge(index);
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,285 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.11.2003
|
||||
*/
|
||||
public class MethodInvokerTests extends TestCase {
|
||||
|
||||
public void testPlainMethodInvoker() throws Exception {
|
||||
// sanity check: singleton, non-static should work
|
||||
TestClass1 tc1 = new TestClass1();
|
||||
MethodInvoker mi = new MethodInvoker();
|
||||
mi.setTargetObject(tc1);
|
||||
mi.setTargetMethod("method1");
|
||||
mi.prepare();
|
||||
Integer i = (Integer) mi.invoke();
|
||||
assertEquals(1, i.intValue());
|
||||
|
||||
// sanity check: check that argument count matching works
|
||||
mi = new MethodInvoker();
|
||||
mi.setTargetClass(TestClass1.class);
|
||||
mi.setTargetMethod("supertypes");
|
||||
mi.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello"});
|
||||
mi.prepare();
|
||||
assertEquals("hello", mi.invoke());
|
||||
|
||||
mi = new MethodInvoker();
|
||||
mi.setTargetClass(TestClass1.class);
|
||||
mi.setTargetMethod("supertypes2");
|
||||
mi.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", "bogus"});
|
||||
mi.prepare();
|
||||
assertEquals("hello", mi.invoke());
|
||||
|
||||
// Sanity check: check that argument conversion doesn't work with plain MethodInvoker
|
||||
mi = new MethodInvoker();
|
||||
mi.setTargetClass(TestClass1.class);
|
||||
mi.setTargetMethod("supertypes2");
|
||||
mi.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", Boolean.TRUE});
|
||||
try {
|
||||
mi.prepare();
|
||||
fail("Shouldn't have matched without argument conversion");
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testStringWithMethodInvoker() throws Exception {
|
||||
try {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new String("no match")});
|
||||
methodInvoker.prepare();
|
||||
fail("Should have thrown a NoSuchMethodException");
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testPurchaserWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new Purchaser()});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("purchaser: hello", greeting);
|
||||
}
|
||||
|
||||
public void testShopperWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new Shopper()});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("purchaser: may I help you?", greeting);
|
||||
}
|
||||
|
||||
public void testSalesmanWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new Salesman()});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("greetable: how are sales?", greeting);
|
||||
}
|
||||
|
||||
public void testCustomerWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new Customer()});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("customer: good day", greeting);
|
||||
}
|
||||
|
||||
public void testRegularWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new Regular("Kotter")});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("regular: welcome back Kotter", greeting);
|
||||
}
|
||||
|
||||
public void testVIPWithMethodInvoker() throws Exception {
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
methodInvoker.setTargetObject(new Greeter());
|
||||
methodInvoker.setTargetMethod("greet");
|
||||
methodInvoker.setArguments(new Object[] {new VIP("Fonzie")});
|
||||
methodInvoker.prepare();
|
||||
String greeting = (String) methodInvoker.invoke();
|
||||
assertEquals("regular: whassup dude?", greeting);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Greeter {
|
||||
|
||||
// should handle Salesman (only interface)
|
||||
public String greet(Greetable greetable) {
|
||||
return "greetable: " + greetable.getGreeting();
|
||||
}
|
||||
|
||||
// should handle Shopper (beats Greetable since it is a class)
|
||||
protected String greet(Purchaser purchaser) {
|
||||
return "purchaser: " + purchaser.getGreeting();
|
||||
}
|
||||
|
||||
// should handle Customer (exact match)
|
||||
String greet(Customer customer) {
|
||||
return "customer: " + customer.getGreeting();
|
||||
}
|
||||
|
||||
// should handle Regular (exact) and VIP (closest match)
|
||||
private String greet(Regular regular) {
|
||||
return "regular: " + regular.getGreeting();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static interface Greetable {
|
||||
|
||||
String getGreeting();
|
||||
}
|
||||
|
||||
|
||||
private static interface Person extends Greetable {
|
||||
}
|
||||
|
||||
|
||||
private static class Purchaser implements Greetable {
|
||||
|
||||
public String getGreeting() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Shopper extends Purchaser implements Person {
|
||||
|
||||
public String getGreeting() {
|
||||
return "may I help you?";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Salesman implements Person {
|
||||
|
||||
public String getGreeting() {
|
||||
return "how are sales?";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Customer extends Shopper {
|
||||
|
||||
public String getGreeting() {
|
||||
return "good day";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Regular extends Customer {
|
||||
|
||||
private String name;
|
||||
|
||||
public Regular(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGreeting() {
|
||||
return "welcome back " + name ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class VIP extends Regular {
|
||||
|
||||
public VIP(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public String getGreeting() {
|
||||
return "whassup dude?";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user