1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/**
* Originally from http://www.cornetdesign.com/files/BeanTestCase.java.txt
*/
package org.connectbot.mock;
import junit.framework.TestCase;
import java.lang.reflect.Field;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class BeanAssertions {
private static final String TEST_STRING_VAL1 = "Some Value";
private static final String TEST_STRING_VAL2 = "Some Other Value";
private BeanAssertions() {
}
public static void assertMeetsEqualsContract(Class<?> classUnderTest,
String[] fieldNames) {
Object o1;
Object o2;
try {
// Get Instances
o1 = classUnderTest.newInstance();
o2 = classUnderTest.newInstance();
assertTrue(
"Instances with default constructor not equal (o1.equals(o2))",
o1.equals(o2));
assertTrue(
"Instances with default constructor not equal (o2.equals(o1))",
o2.equals(o1));
Field[] fields = getFieldsByNameOrAll(classUnderTest, fieldNames);
for (int i = 0; i < fields.length; i++) {
// Reset the instances
o1 = classUnderTest.newInstance();
o2 = classUnderTest.newInstance();
Field field = fields[i];
field.setAccessible(true);
if (field.getType() == String.class) {
field.set(o1, TEST_STRING_VAL1);
} else if (field.getType() == boolean.class) {
field.setBoolean(o1, true);
} else if (field.getType() == short.class) {
field.setShort(o1, (short) 1);
} else if (field.getType() == long.class) {
field.setLong(o1, (long) 1);
} else if (field.getType() == float.class) {
field.setFloat(o1, (float) 1);
} else if (field.getType() == int.class) {
field.setInt(o1, 1);
} else if (field.getType() == byte.class) {
field.setByte(o1, (byte) 1);
} else if (field.getType() == char.class) {
field.setChar(o1, (char) 1);
} else if (field.getType() == double.class) {
field.setDouble(o1, (double) 1);
} else if (field.getType().isEnum()) {
field.set(o1, field.getType().getEnumConstants()[0]);
} else if (Object.class.isAssignableFrom(field.getType())) {
field.set(o1, field.getType().newInstance());
} else {
fail("Don't know how to set a " + field.getType().getName());
}
assertFalse("Instances with o1 having " + field.getName()
+ " set and o2 having it not set are equal", o1
.equals(o2));
field.set(o2, field.get(o1));
assertTrue(
"After setting o2 with the value of the object in o1, the two objects in the field are not equal",
field.get(o1).equals(field.get(o2)));
assertTrue(
"Instances with o1 having "
+ field.getName()
+ " set and o2 having it set to the same object of type "
+ field.get(o2).getClass().getName()
+ " are not equal", o1.equals(o2));
if (field.getType() == String.class) {
field.set(o2, TEST_STRING_VAL2);
} else if (field.getType() == boolean.class) {
field.setBoolean(o2, false);
} else if (field.getType() == short.class) {
field.setShort(o2, (short) 0);
} else if (field.getType() == long.class) {
field.setLong(o2, (long) 0);
} else if (field.getType() == float.class) {
field.setFloat(o2, (float) 0);
} else if (field.getType() == int.class) {
field.setInt(o2, 0);
} else if (field.getType() == byte.class) {
field.setByte(o2, (byte) 0);
} else if (field.getType() == char.class) {
field.setChar(o2, (char) 0);
} else if (field.getType() == double.class) {
field.setDouble(o2, (double) 1);
} else if (field.getType().isEnum()) {
field.set(o2, field.getType().getEnumConstants()[1]);
} else if (Object.class.isAssignableFrom(field.getType())) {
field.set(o2, field.getType().newInstance());
} else {
fail("Don't know how to set a " + field.getType().getName());
}
if (field.get(o1).equals(field.get(o2))) {
// Even though we have different instances, they are equal.
// Let's walk one of them
// to see if we can find a field to set
Field[] paramFields = field.get(o1).getClass()
.getDeclaredFields();
for (int j = 0; j < paramFields.length; j++) {
paramFields[j].setAccessible(true);
if (paramFields[j].getType() == String.class) {
paramFields[j].set(field.get(o1), TEST_STRING_VAL1);
}
}
}
assertFalse(
"After setting o2 with a different object than what is in o1, the two objects in the field are equal. "
+ "This is after an attempt to walk the fields to make them different",
field.get(o1).equals(field.get(o2)));
assertFalse(
"Instances with o1 having "
+ field.getName()
+ " set and o2 having it set to a different object are equal",
o1.equals(o2));
}
} catch (InstantiationException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to construct an instance of the class under test");
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to construct an instance of the class under test");
} catch (SecurityException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to read the field from the class under test");
} catch (NoSuchFieldException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to find field in the class under test");
}
}
/**
* @param classUnderTest
* @param fieldNames
* @return
* @throws NoSuchFieldException
*/
private static Field[] getFieldsByNameOrAll(Class<?> classUnderTest,
String[] fieldNames) throws NoSuchFieldException {
Field fields[];
if (fieldNames == null) {
fields = classUnderTest.getDeclaredFields();
} else {
fields = new Field[fieldNames.length];
for (int i = 0; i < fieldNames.length; i++)
fields[i] = classUnderTest.getDeclaredField(fieldNames[i]);
}
return fields;
}
public static void assertMeetsHashCodeContract(Class<?> classUnderTest,
String[] fieldNames) {
try {
Field[] fields = getFieldsByNameOrAll(classUnderTest, fieldNames);
for (int i = 0; i < fields.length; i++) {
Object o1 = classUnderTest.newInstance();
int initialHashCode = o1.hashCode();
Field field = fields[i];
field.setAccessible(true);
if (field.getType() == String.class) {
field.set(o1, TEST_STRING_VAL1);
} else if (field.getType() == boolean.class) {
field.setBoolean(o1, true);
} else if (field.getType() == short.class) {
field.setShort(o1, (short) 1);
} else if (field.getType() == long.class) {
field.setLong(o1, (long) 1);
} else if (field.getType() == float.class) {
field.setFloat(o1, (float) 1);
} else if (field.getType() == int.class) {
field.setInt(o1, 1);
} else if (field.getType() == byte.class) {
field.setByte(o1, (byte) 1);
} else if (field.getType() == char.class) {
field.setChar(o1, (char) 1);
} else if (field.getType() == double.class) {
field.setDouble(o1, (double) 1);
} else if (field.getType().isEnum()) {
field.set(o1, field.getType().getEnumConstants()[0]);
} else if (Object.class.isAssignableFrom(field.getType())) {
field.set(o1, field.getType().newInstance());
} else {
fail("Don't know how to set a " + field.getType().getName());
}
int updatedHashCode = o1.hashCode();
assertFalse(
"The field "
+ field.getName()
+ " was not taken into account for the hashCode contract ",
initialHashCode == updatedHashCode);
}
} catch (InstantiationException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to construct an instance of the class under test");
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to construct an instance of the class under test");
} catch (NoSuchFieldException e) {
e.printStackTrace();
throw new AssertionError(
"Unable to find field in the class under test");
}
}
}
|