diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java b/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java index d152719261c..88b10f412f1 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java +++ b/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java @@ -1,6 +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. @@ -29,9 +28,7 @@ public class NopInterceptor implements MethodInterceptor { private int count; - /** - * @see org.aopalliance.intercept.MethodInterceptor#invoke(MethodInvocation) - */ + @Override public Object invoke(MethodInvocation invocation) throws Throwable { increment(); @@ -46,6 +43,8 @@ public class NopInterceptor implements MethodInterceptor { ++count; } + + @Override public boolean equals(Object other) { if (!(other instanceof NopInterceptor)) { return false; @@ -56,4 +55,9 @@ public class NopInterceptor implements MethodInterceptor { return this.count == ((NopInterceptor) other).count; } + @Override + public int hashCode() { + return NopInterceptor.class.hashCode(); + } + } diff --git a/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java b/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java index 2730d50c8ec..f909ee704cd 100644 --- a/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java +++ b/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java @@ -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. @@ -29,8 +29,10 @@ import org.springframework.util.ObjectUtils; public class SerializablePerson implements Person, Serializable { private String name; + private int age; + @Override public int getAge() { return age; @@ -59,6 +61,7 @@ public class SerializablePerson implements Person, Serializable { return o; } + public boolean equals(Object other) { if (!(other instanceof SerializablePerson)) { return false; @@ -67,4 +70,9 @@ public class SerializablePerson implements Person, Serializable { return p.age == age && ObjectUtils.nullSafeEquals(name, p.name); } + @Override + public int hashCode() { + return SerializablePerson.class.hashCode(); + } + } diff --git a/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java b/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java index b581a05f032..b749d868edb 100644 --- a/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java +++ b/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java @@ -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. @@ -36,13 +36,15 @@ import org.springframework.beans.factory.parsing.ReaderEventListener; */ public class CollectingReaderEventListener implements ReaderEventListener { - private final List defaults = new LinkedList(); + private final List defaults = new LinkedList(); - private final Map componentDefinitions = new LinkedHashMap<>(8); + private final Map componentDefinitions = + new LinkedHashMap(8); - private final Map aliasMap = new LinkedHashMap<>(8); + private final Map> aliasMap = + new LinkedHashMap>(8); - private final List imports = new LinkedList(); + private final List imports = new LinkedList(); @Override @@ -50,7 +52,7 @@ public class CollectingReaderEventListener implements ReaderEventListener { this.defaults.add(defaultsDefinition); } - public List getDefaults() { + public List getDefaults() { return Collections.unmodifiableList(this.defaults); } @@ -60,27 +62,27 @@ public class CollectingReaderEventListener implements ReaderEventListener { } public ComponentDefinition getComponentDefinition(String name) { - return (ComponentDefinition) this.componentDefinitions.get(name); + return this.componentDefinitions.get(name); } public ComponentDefinition[] getComponentDefinitions() { - Collection collection = this.componentDefinitions.values(); - return (ComponentDefinition[]) collection.toArray(new ComponentDefinition[collection.size()]); + Collection collection = this.componentDefinitions.values(); + return collection.toArray(new ComponentDefinition[collection.size()]); } @Override public void aliasRegistered(AliasDefinition aliasDefinition) { - List aliases = (List) this.aliasMap.get(aliasDefinition.getBeanName()); - if(aliases == null) { - aliases = new ArrayList(); + List aliases = this.aliasMap.get(aliasDefinition.getBeanName()); + if (aliases == null) { + aliases = new ArrayList(); this.aliasMap.put(aliasDefinition.getBeanName(), aliases); } aliases.add(aliasDefinition); } - public List getAliases(String beanName) { - List aliases = (List) this.aliasMap.get(beanName); - return aliases == null ? null : Collections.unmodifiableList(aliases); + public List getAliases(String beanName) { + List aliases = this.aliasMap.get(beanName); + return (aliases != null ? Collections.unmodifiableList(aliases) : null); } @Override @@ -88,7 +90,7 @@ public class CollectingReaderEventListener implements ReaderEventListener { this.imports.add(importDefinition); } - public List getImports() { + public List getImports() { return Collections.unmodifiableList(this.imports); } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java b/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java index cb276da9cbd..5d1bcad7ac6 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java +++ b/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java @@ -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. @@ -76,13 +76,13 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt private Float myFloat = new Float(0.0); - private Collection friends = new LinkedList<>(); + private Collection friends = new LinkedList(); - private Set someSet = new HashSet<>(); + private Set someSet = new HashSet(); - private Map someMap = new HashMap<>(); + private Map someMap = new HashMap(); - private List someList = new ArrayList<>(); + private List someList = new ArrayList(); private Properties someProperties = new Properties(); @@ -255,10 +255,12 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt this.stringArray = stringArray; } + @Override public Integer[] getSomeIntegerArray() { return someIntegerArray; } + @Override public void setSomeIntegerArray(Integer[] someIntegerArray) { this.someIntegerArray = someIntegerArray; } @@ -461,6 +463,7 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt } + @Override public boolean equals(Object other) { if (this == other) { return true; @@ -472,6 +475,7 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age); } + @Override public int hashCode() { return this.age; } @@ -486,6 +490,7 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt } } + @Override public String toString() { return this.name; } diff --git a/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java b/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java index bcc22879a09..b3bb1d4f58d 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java @@ -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. @@ -58,6 +58,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + AsyncMethodBean asyncTest = context.getBean("asyncTest", AsyncMethodBean.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -73,6 +74,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + SimpleInterface asyncTest = context.getBean("asyncTest", SimpleInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -91,6 +93,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.refresh(); + AsyncMethodWithQualifierBean asyncTest = context.getBean("asyncTest", AsyncMethodWithQualifierBean.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -111,6 +114,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class)); context.refresh(); + SimpleInterface asyncTest = context.getBean("asyncTest", SimpleInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -128,6 +132,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + AsyncClassBean asyncTest = context.getBean("asyncTest", AsyncClassBean.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -141,6 +146,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + AsyncClassBean asyncTest = context.getBean("asyncTest", AsyncClassBean.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -155,6 +161,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + RegularInterface asyncTest = context.getBean("asyncTest", RegularInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -168,6 +175,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassBeanWithInterface.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + RegularInterface asyncTest = context.getBean("asyncTest", RegularInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -182,6 +190,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -195,6 +204,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -209,6 +219,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -222,6 +233,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(DynamicAsyncInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -236,6 +248,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); + AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -250,6 +263,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodsInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); @@ -264,6 +278,7 @@ public class AsyncExecutionTests { context.registerBeanDefinition("asyncTest", new RootBeanDefinition(DynamicAsyncMethodsInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); + AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class); asyncTest.doSomething(10); Future future = asyncTest.returnSomething(20); @@ -461,7 +476,7 @@ public class AsyncExecutionTests { private final AsyncInterface proxy; public DynamicAsyncInterfaceBean() { - ProxyFactory pf = new ProxyFactory(new HashMap<>()); + ProxyFactory pf = new ProxyFactory(new HashMap()); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { @@ -531,7 +546,7 @@ public class AsyncExecutionTests { private final AsyncMethodsInterface proxy; public DynamicAsyncMethodsInterfaceBean() { - ProxyFactory pf = new ProxyFactory(new HashMap<>()); + ProxyFactory pf = new ProxyFactory(new HashMap()); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java index df65665564d..f2dcc480c4f 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java @@ -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 list = new ArrayList<>(); + List list = new ArrayList(); list.add(new B()); list.add(new A()); AnnotationAwareOrderComparator.sort(list); @@ -47,7 +47,7 @@ public class AnnotationAwareOrderComparatorTests { @Test public void sortInstancesWithSubclass() { - List list = new ArrayList<>(); + List list = new ArrayList(); list.add(new B()); list.add(new C()); AnnotationAwareOrderComparator.sort(list); @@ -57,7 +57,7 @@ public class AnnotationAwareOrderComparatorTests { @Test public void sortClasses() { - List list = new ArrayList<>(); + List list = new ArrayList(); list.add(B.class); list.add(A.class); AnnotationAwareOrderComparator.sort(list); @@ -67,7 +67,7 @@ public class AnnotationAwareOrderComparatorTests { @Test public void sortClassesWithSubclass() { - List list = new ArrayList<>(); + List list = new ArrayList(); list.add(B.class); list.add(C.class); AnnotationAwareOrderComparator.sort(list); diff --git a/spring-core/src/test/java/org/springframework/core/style/ToStringCreatorTests.java b/spring-core/src/test/java/org/springframework/core/style/ToStringCreatorTests.java index f353d34fb1d..ea5e7d704ae 100644 --- a/spring-core/src/test/java/org/springframework/core/style/ToStringCreatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/style/ToStringCreatorTests.java @@ -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 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 getMap() { + Map map = new LinkedHashMap(); 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[A, B, C]]", str); + assertEquals("[@" + ObjectUtils.getIdentityHexString(array) + + " array[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[0, 1, 2, 3, 4]]", str); } - public void testList() { - List list = new ArrayList(); + @Test + public void appendList() { + List list = new ArrayList(); 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 set = new LinkedHashSet(); 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 { - } } diff --git a/spring-core/src/test/java/org/springframework/util/MethodInvokerTests.java b/spring-core/src/test/java/org/springframework/util/MethodInvokerTests.java index 679ab9566ed..c3b380bd899 100644 --- a/spring-core/src/test/java/org/springframework/util/MethodInvokerTests.java +++ b/spring-core/src/test/java/org/springframework/util/MethodInvokerTests.java @@ -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(), 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.setArguments(new Object[] {new ArrayList(), new ArrayList(), "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(), new ArrayList(), "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 { } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java b/spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java index 1cf222e0029..e6804c570c3 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/object/GenericSqlQuery.java @@ -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. @@ -18,39 +18,44 @@ package org.springframework.jdbc.object; import java.util.Map; +import org.springframework.beans.BeanUtils; import org.springframework.jdbc.core.RowMapper; import org.springframework.util.Assert; -import org.springframework.dao.InvalidDataAccessResourceUsageException; -public class GenericSqlQuery extends SqlQuery { +/** + * A concrete variant of {@link SqlQuery} which can be configured + * with a {@link RowMapper}. + * + * @author Thomas Risberg + * @author Juergen Hoeller + * @since 3.0 + * @see #setRowMapperClass + */ +public class GenericSqlQuery extends SqlQuery { - Class rowMapperClass; + @SuppressWarnings("rawtypes") + private Class rowMapperClass; - RowMapper rowMapper; - public void setRowMapperClass(Class rowMapperClass) - throws IllegalAccessException, InstantiationException { + /** + * Set a {@link RowMapper} class for this query, creating a fresh + * {@link RowMapper} instance per execution. + */ + @SuppressWarnings("rawtypes") + public void setRowMapperClass(Class rowMapperClass) { this.rowMapperClass = rowMapperClass; - if (!RowMapper.class.isAssignableFrom(rowMapperClass)) - throw new IllegalStateException("The specified class '" + - rowMapperClass.getName() + " is not a sub class of " + - "'org.springframework.jdbc.core.RowMapper'"); } public void afterPropertiesSet() { super.afterPropertiesSet(); - Assert.notNull(rowMapperClass, "The 'rowMapperClass' property is required"); + Assert.notNull(this.rowMapperClass, "'rowMapperClass' is required"); } - protected RowMapper newRowMapper(Object[] parameters, Map context) { - try { - return (RowMapper) rowMapperClass.newInstance(); - } - catch (InstantiationException e) { - throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e); - } - catch (IllegalAccessException e) { - throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e); - } + + @Override + @SuppressWarnings("unchecked") + protected RowMapper newRowMapper(Object[] parameters, Map context) { + return BeanUtils.instantiateClass(this.rowMapperClass); } + } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java index b43b6895725..1d4311d44ae 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java @@ -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. @@ -16,7 +16,6 @@ package org.springframework.jdbc.object; - import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -25,13 +24,11 @@ import java.sql.Types; import java.util.HashMap; import java.util.List; import java.util.Map; - import javax.sql.DataSource; import org.junit.Before; import org.junit.Test; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; + import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; @@ -43,13 +40,14 @@ import static org.mockito.BDDMockito.*; /** * @author Thomas Risberg + * @author Juergen Hoeller */ -public class GenericSqlQueryTests { +public class GenericSqlQueryTests { private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED = - "select id, forename from custmr where id = ? and country = ?"; + "select id, forename from custmr where id = ? and country = ?"; - private BeanFactory beanFactory; + private DefaultListableBeanFactory beanFactory; private Connection connection; @@ -57,10 +55,11 @@ public class GenericSqlQueryTests { private ResultSet resultSet; + @Before public void setUp() throws Exception { this.beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory).loadBeanDefinitions( + new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml")); DataSource dataSource = mock(DataSource.class); this.connection = mock(Connection.class); @@ -72,18 +71,18 @@ public class GenericSqlQueryTests { } @Test - public void testPlaceHoldersCustomerQuery() throws SQLException { - SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithPlaceHolders"); + public void testCustomerQueryWithPlaceholders() throws SQLException { + SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithPlaceholders"); doTestCustomerQuery(query, false); } @Test - public void testNamedParameterCustomerQuery() throws SQLException { - SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithNamedParameters"); + public void testCustomerQueryWithNamedParameters() throws SQLException { + SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithNamedParameters"); doTestCustomerQuery(query, true); } - private void doTestCustomerQuery(SqlQuery query, boolean namedParameters) throws SQLException { + private void doTestCustomerQuery(SqlQuery query, boolean namedParameters) throws SQLException { given(resultSet.next()).willReturn(true); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -91,15 +90,15 @@ public class GenericSqlQueryTests { given(preparedStatement.executeQuery()).willReturn(resultSet); given(connection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED)).willReturn(preparedStatement); - List queryResults; + List queryResults; if (namedParameters) { Map params = new HashMap(2); - params.put("id", new Integer(1)); + params.put("id", 1); params.put("country", "UK"); queryResults = query.executeByNamedParam(params); } else { - Object[] params = new Object[] {new Integer(1), "UK"}; + Object[] params = new Object[] {1, "UK"}; queryResults = query.execute(params); } assertTrue("Customer was returned correctly", queryResults.size() == 1); @@ -108,7 +107,7 @@ public class GenericSqlQueryTests { assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod")); verify(resultSet).close(); - verify(preparedStatement).setObject(1, new Integer(1), Types.INTEGER); + verify(preparedStatement).setObject(1, 1, Types.INTEGER); verify(preparedStatement).setString(2, "UK"); verify(preparedStatement).close(); } diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml b/spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml index eda2e448099..e3711a096f8 100644 --- a/spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml +++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml @@ -1,12 +1,12 @@ - + @@ -15,7 +15,7 @@ - + @@ -47,7 +47,7 @@ - + \ No newline at end of file diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java index b0551921054..f5d9c684c71 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -63,7 +63,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @Before public void setUp() { - converter = new Jaxb2CollectionHttpMessageConverter<>(); + converter = new Jaxb2CollectionHttpMessageConverter>(); rootElementListType = new ParameterizedTypeReference>() {}.getType(); rootElementSetType = new ParameterizedTypeReference>() {}.getType(); typeListType = new ParameterizedTypeReference>() {}.getType(); @@ -157,7 +157,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { assertEquals("", result.iterator().next().external); } catch (HttpMessageNotReadableException ex) { - // Some parsers raise exception by default + // Some parsers raise an exception } } @@ -212,7 +212,6 @@ public class Jaxb2CollectionHttpMessageConverterTests { } - @SuppressWarnings("unused") @XmlRootElement public static class RootElement { @@ -247,6 +246,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { } } + @XmlType public static class TestType { diff --git a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java index 5a8fdd59f61..13fd1f1061e 100644 --- a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java @@ -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. @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.web.accept; -import static org.junit.Assert.assertEquals; +package org.springframework.web.accept; import java.util.Arrays; import java.util.Collections; @@ -24,6 +23,7 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; + import org.springframework.http.MediaType; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockServletContext; @@ -31,8 +31,11 @@ import org.springframework.util.StringUtils; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.ServletWebRequest; +import static org.junit.Assert.*; + /** * Test fixture for {@link ContentNegotiationManagerFactoryBean} tests. + * * @author Rossen Stoyanchev */ public class ContentNegotiationManagerFactoryBeanTests { @@ -43,6 +46,7 @@ public class ContentNegotiationManagerFactoryBeanTests { private MockHttpServletRequest servletRequest; + @Before public void setup() { TestServletContext servletContext = new TestServletContext(); @@ -55,6 +59,7 @@ public class ContentNegotiationManagerFactoryBeanTests { this.factoryBean.setServletContext(this.servletRequest.getServletContext()); } + @Test public void defaultSettings() throws Exception { this.factoryBean.afterPropertiesSet(); @@ -149,17 +154,14 @@ public class ContentNegotiationManagerFactoryBeanTests { assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); // SPR-10513 - this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE); - assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); } private static class TestServletContext extends MockServletContext { - private final Map mimeTypes = new HashMap<>(); - + private final Map mimeTypes = new HashMap(); public Map getMimeTypes() { return this.mimeTypes;