1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .beans .factory .support ;
18
18
19
- import junit .framework . TestCase ;
19
+ import org . junit .Test ;
20
20
21
21
import org .springframework .beans .factory .config .BeanDefinition ;
22
22
import org .springframework .beans .factory .config .RuntimeBeanReference ;
23
23
import org .springframework .tests .sample .beans .TestBean ;
24
24
25
+ import static org .junit .Assert .*;
25
26
26
27
/**
28
+ * Unit tests for {@code equals()} and {@code hashCode()} in bean definitions.
29
+ *
27
30
* @author Rob Harrop
31
+ * @author Sam Brannen
28
32
*/
29
- public class DefinitionMetadataEqualsHashCodeTests extends TestCase {
33
+ @ SuppressWarnings ("serial" )
34
+ public class DefinitionMetadataEqualsHashCodeTests {
30
35
31
- @ SuppressWarnings ( "serial" )
32
- public void testRootBeanDefinitionEqualsAndHashCode () throws Exception {
36
+ @ Test
37
+ public void rootBeanDefinition () {
33
38
RootBeanDefinition master = new RootBeanDefinition (TestBean .class );
34
39
RootBeanDefinition equal = new RootBeanDefinition (TestBean .class );
35
40
RootBeanDefinition notEqual = new RootBeanDefinition (String .class );
36
- RootBeanDefinition subclass = new RootBeanDefinition (TestBean .class ) {};
41
+ RootBeanDefinition subclass = new RootBeanDefinition (TestBean .class ) {
42
+ };
37
43
setBaseProperties (master );
38
44
setBaseProperties (equal );
39
45
setBaseProperties (notEqual );
40
46
setBaseProperties (subclass );
41
47
42
- assertEqualsContract (master , equal , notEqual , subclass );
43
- assertEquals ("Hash code for equal instances should match" , master .hashCode (), equal .hashCode ());
48
+ assertEqualsAndHashCodeContracts (master , equal , notEqual , subclass );
44
49
}
45
50
46
- @ SuppressWarnings ("serial" )
47
- public void testChildBeanDefinitionEqualsAndHashCode () throws Exception {
51
+ /**
52
+ * @since 3.2.8
53
+ * @see <a href="https://jira.springsource.org/browse/SPR-11420">SPR-11420</a>
54
+ */
55
+ @ Test
56
+ public void rootBeanDefinitionAndMethodOverridesWithDifferentOverloadedValues () {
57
+ RootBeanDefinition master = new RootBeanDefinition (TestBean .class );
58
+ RootBeanDefinition equal = new RootBeanDefinition (TestBean .class );
59
+
60
+ setBaseProperties (master );
61
+ setBaseProperties (equal );
62
+
63
+ // Simulate AbstractBeanDefinition.validate() which delegates to
64
+ // AbstractBeanDefinition.prepareMethodOverrides():
65
+ master .getMethodOverrides ().getOverrides ().iterator ().next ().setOverloaded (false );
66
+ // But do not simulate validation of the 'equal' bean. As a consequence, a method
67
+ // override in 'equal' will be marked as overloaded, but the corresponding
68
+ // override in 'master' will not. But... the bean definitions should still be
69
+ // considered equal.
70
+
71
+ assertEquals ("Should be equal" , master , equal );
72
+ assertEquals ("Hash code for equal instances must match" , master .hashCode (), equal .hashCode ());
73
+ }
74
+
75
+ @ Test
76
+ public void childBeanDefinition () {
48
77
ChildBeanDefinition master = new ChildBeanDefinition ("foo" );
49
78
ChildBeanDefinition equal = new ChildBeanDefinition ("foo" );
50
79
ChildBeanDefinition notEqual = new ChildBeanDefinition ("bar" );
51
- ChildBeanDefinition subclass = new ChildBeanDefinition ("foo" ){};
80
+ ChildBeanDefinition subclass = new ChildBeanDefinition ("foo" ) {
81
+ };
52
82
setBaseProperties (master );
53
83
setBaseProperties (equal );
54
84
setBaseProperties (notEqual );
55
85
setBaseProperties (subclass );
56
86
57
- assertEqualsContract (master , equal , notEqual , subclass );
58
- assertEquals ("Hash code for equal instances should match" , master .hashCode (), equal .hashCode ());
87
+ assertEqualsAndHashCodeContracts (master , equal , notEqual , subclass );
59
88
}
60
89
61
- public void testRuntimeBeanReference () throws Exception {
90
+ @ Test
91
+ public void runtimeBeanReference () {
62
92
RuntimeBeanReference master = new RuntimeBeanReference ("name" );
63
93
RuntimeBeanReference equal = new RuntimeBeanReference ("name" );
64
94
RuntimeBeanReference notEqual = new RuntimeBeanReference ("someOtherName" );
65
- RuntimeBeanReference subclass = new RuntimeBeanReference ("name" ){};
66
- assertEqualsContract (master , equal , notEqual , subclass );
95
+ RuntimeBeanReference subclass = new RuntimeBeanReference ("name" ) {
96
+ };
97
+ assertEqualsAndHashCodeContracts (master , equal , notEqual , subclass );
67
98
}
99
+
68
100
private void setBaseProperties (AbstractBeanDefinition definition ) {
69
101
definition .setAbstract (true );
70
102
definition .setAttribute ("foo" , "bar" );
71
103
definition .setAutowireCandidate (false );
72
104
definition .setAutowireMode (AbstractBeanDefinition .AUTOWIRE_BY_TYPE );
73
- //definition.getConstructorArgumentValues().addGenericArgumentValue("foo");
105
+ // definition.getConstructorArgumentValues().addGenericArgumentValue("foo");
74
106
definition .setDependencyCheck (AbstractBeanDefinition .DEPENDENCY_CHECK_OBJECTS );
75
- definition .setDependsOn (new String []{ "foo" , "bar" });
107
+ definition .setDependsOn (new String [] { "foo" , "bar" });
76
108
definition .setDestroyMethodName ("destroy" );
77
109
definition .setEnforceDestroyMethod (false );
78
110
definition .setEnforceInitMethod (true );
@@ -89,10 +121,15 @@ private void setBaseProperties(AbstractBeanDefinition definition) {
89
121
definition .setSource ("foo" );
90
122
}
91
123
92
- private void assertEqualsContract (Object master , Object equal , Object notEqual , Object subclass ) {
124
+ private void assertEqualsAndHashCodeContracts (Object master , Object equal , Object notEqual , Object subclass ) {
93
125
assertEquals ("Should be equal" , master , equal );
94
- assertFalse ("Should not be equal" , master .equals (notEqual ));
126
+ assertEquals ("Hash code for equal instances should match" , master .hashCode (), equal .hashCode ());
127
+
128
+ assertNotEquals ("Should not be equal" , master , notEqual );
129
+ assertNotEquals ("Hash code for non-equal instances should not match" , master .hashCode (), notEqual .hashCode ());
130
+
95
131
assertEquals ("Subclass should be equal" , master , subclass );
132
+ assertEquals ("Hash code for subclass should match" , master .hashCode (), subclass .hashCode ());
96
133
}
97
134
98
135
}
0 commit comments