Polishing

This commit is contained in:
Juergen Hoeller
2016-08-17 20:43:41 +02:00
parent 52f21bdf54
commit b9a2d0af98
13 changed files with 217 additions and 156 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -37,7 +37,7 @@ public class AnnotationAwareOrderComparatorTests {
@Test
public void sortInstances() {
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<Object>();
list.add(new B());
list.add(new A());
AnnotationAwareOrderComparator.sort(list);
@@ -47,7 +47,7 @@ public class AnnotationAwareOrderComparatorTests {
@Test
public void sortInstancesWithSubclass() {
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<Object>();
list.add(new B());
list.add(new C());
AnnotationAwareOrderComparator.sort(list);
@@ -57,7 +57,7 @@ public class AnnotationAwareOrderComparatorTests {
@Test
public void sortClasses() {
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<Object>();
list.add(B.class);
list.add(A.class);
AnnotationAwareOrderComparator.sort(list);
@@ -67,7 +67,7 @@ public class AnnotationAwareOrderComparatorTests {
@Test
public void sortClassesWithSubclass() {
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<Object>();
list.add(B.class);
list.add(C.class);
AnnotationAwareOrderComparator.sort(list);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,71 +23,83 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.ObjectUtils;
import static org.junit.Assert.*;
/**
* @author Keith Donald
*/
public class ToStringCreatorTests extends TestCase {
public class ToStringCreatorTests {
private SomeObject s1, s2, s3;
@Override
protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
s1 = new SomeObject() {
@Override
public String toString() {
return "A";
}
};
s2 = new SomeObject() {
@Override
public String toString() {
return "B";
}
};
s3 = new SomeObject() {
@Override
public String toString() {
return "C";
}
};
}
public void testDefaultStyleMap() {
final Map map = getMap();
@Test
public void defaultStyleMap() {
final Map<String, String> map = getMap();
Object stringy = new Object() {
@Override
public String toString() {
return new ToStringCreator(this).append("familyFavoriteSport", map).toString();
}
};
assertEquals("[ToStringCreatorTests.4@" + ObjectUtils.getIdentityHexString(stringy)
+ " familyFavoriteSport = map['Keri' -> 'Softball', 'Scot' -> 'Fishing', 'Keith' -> 'Flag Football']]",
assertEquals("[ToStringCreatorTests.4@" + ObjectUtils.getIdentityHexString(stringy) +
" familyFavoriteSport = map['Keri' -> 'Softball', 'Scot' -> 'Fishing', 'Keith' -> 'Flag Football']]",
stringy.toString());
}
private Map getMap() {
Map map = new LinkedHashMap(3);
private Map<String, String> getMap() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("Keri", "Softball");
map.put("Scot", "Fishing");
map.put("Keith", "Flag Football");
return map;
}
public void testDefaultStyleArray() {
SomeObject[] array = new SomeObject[] { s1, s2, s3 };
@Test
public void defaultStyleArray() {
SomeObject[] array = new SomeObject[] {s1, s2, s3};
String str = new ToStringCreator(array).toString();
assertEquals("[@" + ObjectUtils.getIdentityHexString(array)
+ " array<ToStringCreatorTests.SomeObject>[A, B, C]]", str);
assertEquals("[@" + ObjectUtils.getIdentityHexString(array) +
" array<ToStringCreatorTests.SomeObject>[A, B, C]]", str);
}
public void testPrimitiveArrays() {
int[] integers = new int[] { 0, 1, 2, 3, 4 };
@Test
public void primitiveArrays() {
int[] integers = new int[] {0, 1, 2, 3, 4};
String str = new ToStringCreator(integers).toString();
assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<Integer>[0, 1, 2, 3, 4]]", str);
}
public void testList() {
List list = new ArrayList();
@Test
public void appendList() {
List<SomeObject> list = new ArrayList<SomeObject>();
list.add(s1);
list.add(s2);
list.add(s3);
@@ -96,32 +108,32 @@ public class ToStringCreatorTests extends TestCase {
str);
}
public void testSet() {
Set set = new LinkedHashSet<>(3);
@Test
public void appendSet() {
Set<SomeObject> set = new LinkedHashSet<SomeObject>();
set.add(s1);
set.add(s2);
set.add(s3);
String str = new ToStringCreator(this).append("myLetters", set).toString();
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) + " myLetters = set[A, B, C]]",
str);
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) + " myLetters = set[A, B, C]]", str);
}
public void testClass() {
@Test
public void appendClass() {
String str = new ToStringCreator(this).append("myClass", this.getClass()).toString();
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this)
+ " myClass = ToStringCreatorTests]", str);
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) +
" myClass = ToStringCreatorTests]", str);
}
public void testMethod() throws Exception {
String str = new ToStringCreator(this).append("myMethod", this.getClass().getMethod("testMethod"))
.toString();
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this)
+ " myMethod = testMethod@ToStringCreatorTests]", str);
@Test
public void appendMethod() throws Exception {
String str = new ToStringCreator(this).append("myMethod", this.getClass().getMethod("appendMethod")).toString();
assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) +
" myMethod = appendMethod@ToStringCreatorTests]", str);
}
public static class SomeObject {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -20,16 +20,26 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
* @author Sam Brannen
* @since 21.11.2003
*/
public class MethodInvokerTests extends TestCase {
public class MethodInvokerTests {
public void testPlainMethodInvoker() throws Exception {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void plainMethodInvoker() throws Exception {
// sanity check: singleton, non-static should work
TestClass1 tc1 = new TestClass1();
MethodInvoker mi = new MethodInvoker();
@@ -43,14 +53,14 @@ public class MethodInvokerTests extends TestCase {
mi = new MethodInvoker();
mi.setTargetClass(TestClass1.class);
mi.setTargetMethod("supertypes");
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello"});
mi.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "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.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", "bogus"});
mi.prepare();
assertEquals("hello", mi.invoke());
@@ -58,31 +68,25 @@ public class MethodInvokerTests extends TestCase {
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
}
mi.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", Boolean.TRUE});
exception.expect(NoSuchMethodException.class);
mi.prepare();
}
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
}
@Test
public void stringWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {"no match"});
exception.expect(NoSuchMethodException.class);
methodInvoker.prepare();
}
public void testPurchaserWithMethodInvoker() throws Exception {
@Test
public void purchaserWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -92,7 +96,8 @@ public class MethodInvokerTests extends TestCase {
assertEquals("purchaser: hello", greeting);
}
public void testShopperWithMethodInvoker() throws Exception {
@Test
public void shopperWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -102,7 +107,8 @@ public class MethodInvokerTests extends TestCase {
assertEquals("purchaser: may I help you?", greeting);
}
public void testSalesmanWithMethodInvoker() throws Exception {
@Test
public void salesmanWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -112,7 +118,8 @@ public class MethodInvokerTests extends TestCase {
assertEquals("greetable: how are sales?", greeting);
}
public void testCustomerWithMethodInvoker() throws Exception {
@Test
public void customerWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -122,7 +129,8 @@ public class MethodInvokerTests extends TestCase {
assertEquals("customer: good day", greeting);
}
public void testRegularWithMethodInvoker() throws Exception {
@Test
public void regularWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -132,7 +140,8 @@ public class MethodInvokerTests extends TestCase {
assertEquals("regular: welcome back Kotter", greeting);
}
public void testVIPWithMethodInvoker() throws Exception {
@Test
public void vipWithMethodInvoker() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet");
@@ -216,13 +225,13 @@ public class MethodInvokerTests extends TestCase {
}
private static interface Greetable {
private interface Greetable {
String getGreeting();
}
private static interface Person extends Greetable {
private interface Person extends Greetable {
}