Debian Codegraph @

src/test/java/ezvcard/property/PropertySensei.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import static ezvcard.util.TestUtils.assertEqualsAndHash;
4 import static ezvcard.util.TestUtils.assertEqualsMethodEssentials;
5 import static ezvcard.util.TestUtils.assertNotEqualsBothWays;
6 import static ezvcard.util.TestUtils.checkCodes;
7 import static org.junit.Assert.assertNotEquals;
8 import static org.junit.Assert.assertNotSame;
9 import static org.junit.Assert.fail;
10
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import ezvcard.VCard;
18 import ezvcard.VCardVersion;
19 import ezvcard.ValidationWarning;
20 import ezvcard.util.TestUtils;
21
22 /*
23 Copyright (c) 2012-2023, Michael Angstadt
24 All rights reserved.
25
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28
29 1. Redistributions of source code must retain the above copyright notice, this
30 list of conditions and the following disclaimer.
31 2. Redistributions in binary form must reproduce the above copyright notice,
32 this list of conditions and the following disclaimer in the documentation
33 and/or other materials provided with the distribution.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
39 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 The views and conclusions contained in the software and documentation are those
47 of the authors and should not be interpreted as representing official policies,
48 either expressed or implied, of the FreeBSD Project.
49 */
50
51 /**
52 * Contains methods for testing property classes.
53 * @author Michael Angstadt
54 */
55 public class PropertySensei {
56 /**
57 * Asserts the validation of a property object.
58 * @param property the property object
59 * @return chaining class for carrying out the test
60 */
61 public static PropertyValidateAsserter assertValidate(VCardProperty property) {
62 return new PropertyValidateAsserter(property);
63 }
64
65 public static class PropertyValidateAsserter {
66 private final VCardProperty property;
67 private VCard vcard;
68 private VCardVersion versions[] = VCardVersion.values();
69
70 public PropertyValidateAsserter(VCardProperty property) {
71 this.property = property;
72 vcard(new VCard());
73 }
74
75 /**
76 * Defines the versions to check (defaults to all versions).
77 * @param versions the versions to check
78 * @return this
79 */
80 public PropertyValidateAsserter versions(VCardVersion... versions) {
81 this.versions = versions;
82 return this;
83 }
84
85 /**
86 * Defines the vCard instance to use (defaults to an empty vCard).
87 * @param vcard the vCard instance
88 * @return this
89 */
90 public PropertyValidateAsserter vcard(VCard vcard) {
91 vcard.addProperty(property);
92 this.vcard = vcard;
93 return this;
94 }
95
96 /**
97 * Performs the validation check.
98 * @param expectedCodes the expected warning codes
99 */
100 public void run(Integer... expectedCodes) {
101 for (VCardVersion version : versions) {
102 List<ValidationWarning> warnings = property.validate(version, vcard);
103 boolean passed = checkCodes(warnings, expectedCodes);
104 if (!passed) {
105 fail("For version " + version + ", expected codes were " + Arrays.toString(expectedCodes) + " but were actually:\n" + warnings);
106 }
107 }
108 }
109 }
110
111 /**
112 * Asserts that a property class's copy constructor and
113 * {@link VCardProperty#copy} method work correctly by reflectively calling
114 * these two methods and comparing the created objects to the original
115 * instance with "equals()".
116 * @param original the object to copy (note that this object is modified in
117 * order to test to make sure the fields in its {@link VCardProperty} parent
118 * class are also copied)
119 * @return chaining class for carrying out the test
120 */
121 public static CopyAsserter assertCopy(VCardProperty original) {
122 return new CopyAsserter(original);
123 }
124
125 public static class CopyAsserter {
126 private final VCardProperty original, copyFromConstructor, copyFromMethod;
127
128 public CopyAsserter(VCardProperty original) {
129 this.original = original;
130
131 //make sure it checks group and parameters
132 original.setGroup("group");
133 original.addParameter("PARAM", "value");
134
135 try {
136 Constructor<? extends VCardProperty> constructor = original.getClass().getConstructor(original.getClass());
137 this.copyFromConstructor = constructor.newInstance(original);
138 assertEqualsAndHash(original, copyFromConstructor);
139 } catch (Exception e) {
140 throw new RuntimeException(e);
141 }
142
143 this.copyFromMethod = original.copy();
144 assertNotSame(original, copyFromMethod);
145 assertEqualsAndHash(original, copyFromMethod);
146
147 assertEqualsAndHash(copyFromMethod, copyFromConstructor);
148 }
149
150 /**
151 * Asserts that a given method returns different objects when invoked
152 * from each copy. For example, this method asserts that a {@link List}
153 * field has been truly copied and that the property object copies are
154 * not just referencing the same object.
155 * @param methodName the name of the method to call (should be a
156 * "getter" method)
157 * @return this
158 */
159 public CopyAsserter notSame(String methodName) {
160 try {
161 Method method = original.getClass().getMethod(methodName);
162 Object fromOriginal = method.invoke(original);
163 String message = "Object returned by \"" + methodName + "\" is the same object, but it shouldn't be.";
164 assertNotSame(message, fromOriginal, method.invoke(copyFromConstructor));
165 assertNotSame(message, fromOriginal, method.invoke(copyFromMethod));
166 } catch (Exception e) {
167 throw new RuntimeException(e);
168 }
169 return this;
170 }
171 }
172
173 /**
174 * <p>
175 * Asserts some essential behaviors of the equals method (see
176 * {@link TestUtils#assertEqualsMethodEssentials}). Also asserts that the
177 * fields in the {@link VCardProperty} base class are checked.
178 * </p>
179 * <p>
180 * Calling the chaining methods of the object that this method returns will
181 * create identical instances of the given property and assert that the
182 * instances are equal and have identical hash codes.
183 * </p>
184 * @param clazz the property class
185 * @param constructorValues values to pass into the constructor for creating
186 * instances that test the equals method essentials
187 * @return a chainer object to perform the test
188 */
189 public static EqualsMethodAsserter assertEqualsMethod(Class<? extends VCardProperty> clazz, Object... constructorValues) {
190 return new EqualsMethodAsserter(clazz, constructorValues);
191 }
192
193 public static class EqualsMethodAsserter {
194 private final Class<? extends VCardProperty> clazz;
195 private Constructor<? extends VCardProperty> constructor;
196 private Object[] constructorValues;
197 private List<Method> methods = new ArrayList<>();
198 private List<Object[]> methodValues = new ArrayList<>();
199
200 public EqualsMethodAsserter(Class<? extends VCardProperty> clazz, Object... constructorValues) {
201 this.clazz = clazz;
202
203 try {
204 Constructor<? extends VCardProperty> constructor;
205 VCardProperty instance1, instance2;
206 constructor = clazz.getConstructor(toTypes(constructorValues));
207 instance1 = constructor.newInstance(constructorValues);
208 instance2 = constructor.newInstance(constructorValues);
209
210 assertEqualsMethodEssentials(instance1);
211 assertEqualsBaseClassFields(instance1, instance2);
212 } catch (Exception e) {
213 throw new RuntimeException(e);
214 }
215 }
216
217 /**
218 * Defines what constructor to use to create the objects.
219 * @param values the values to pass into the constructor
220 * @return this
221 */
222 public EqualsMethodAsserter constructor(Object... values) {
223 Class<?> parameterTypes[] = new Class<?>[values.length];
224 for (int i = 0; i < values.length; i++) {
225 parameterTypes[i] = values[i].getClass();
226 }
227
228 return constructor(parameterTypes, values);
229 }
230
231 /**
232 * Defines what constructor to use to create the objects.
233 * @param parameterTypes the constructor's parameter types
234 * @param values the values to pass into the constructor
235 * @return this
236 */
237 public EqualsMethodAsserter constructor(Class<?> parameterTypes[], Object... values) {
238 if (parameterTypes.length != values.length) {
239 throw new IllegalArgumentException("Number of parameter types must match number of values.");
240 }
241
242 try {
243 constructor = clazz.getConstructor(parameterTypes);
244 constructorValues = values;
245 } catch (Exception e) {
246 throw new RuntimeException(e);
247 }
248 return this;
249 }
250
251 /**
252 * Defines a method that should be called after creating each object.
253 * @param name the method name
254 * @param values the values to pass into the method
255 * @return this
256 */
257 public EqualsMethodAsserter method(String name, Object... values) {
258 Class<?> parameterTypes[] = new Class<?>[values.length];
259 for (int i = 0; i < values.length; i++) {
260 parameterTypes[i] = values[i].getClass();
261 }
262
263 return method(name, parameterTypes, values);
264 }
265
266 /**
267 * Defines a method that should be called after creating each object.
268 * @param name the method name
269 * @param parameterTypes the parameter types of the method (required if
270 * you're passing null values)
271 * @param values the values to pass into the method
272 * @return this
273 */
274 public EqualsMethodAsserter method(String name, Class<?> parameterTypes[], Object... values) {
275 if (parameterTypes.length != values.length) {
276 throw new IllegalArgumentException("Number of parameter types must match number of values.");
277 }
278
279 try {
280 Method method = clazz.getMethod(name, parameterTypes);
281 methods.add(method);
282 methodValues.add(values);
283 } catch (Exception e) {
284 throw new RuntimeException(e);
285 }
286
287 return this;
288 }
289
290 /**
291 * Executes the test using the specified constructor and methods.
292 * @return this
293 */
294 public EqualsMethodAsserter test() {
295 try {
296 VCardProperty instance1 = createInstance();
297 VCardProperty instance2 = createInstance();
298 methods.clear();
299 methodValues.clear();
300 return test(instance1, instance2);
301 } catch (Exception e) {
302 throw new RuntimeException(e);
303 }
304 }
305
306 public EqualsMethodAsserter test(VCardProperty instance1, VCardProperty instance2) {
307 assertEqualsAndHash(instance1, instance2);
308 return this;
309 }
310
311 private VCardProperty createInstance() throws Exception {
312 VCardProperty instance = constructor.newInstance(constructorValues);
313 for (int i = 0; i < methods.size(); i++) {
314 Method method = methods.get(i);
315 Object[] values = methodValues.get(i);
316 method.invoke(instance, values);
317 }
318 return instance;
319 }
320
321 private Class<?>[] toTypes(Object... values) {
322 Class<?> parameterTypes[] = new Class<?>[values.length];
323 for (int i = 0; i < values.length; i++) {
324 parameterTypes[i] = values[i].getClass();
325 }
326 return parameterTypes;
327 }
328 }
329
330 /**
331 * Asserts that none of the given property instances are equal to each
332 * other. Also creates copies of the first property, setting the fields in
333 * {@link VCardProperty} to assert that these fields are checked too.
334 * @param properties the properties
335 */
336 public static void assertNothingIsEqual(VCardProperty... properties) {
337 assertNothingIsEqual(Arrays.asList(properties));
338 }
339
340 /**
341 * Asserts that none of the given property instances are equal to each
342 * other. Also creates copies of the first property, setting the fields in
343 * {@link VCardProperty} to assert that these fields are checked too.
344 * @param properties the properties
345 */
346 public static void assertNothingIsEqual(Iterable<VCardProperty> properties) {
347 for (VCardProperty property : properties) {
348 for (VCardProperty property2 : properties) {
349 if (property != property2) {
350 assertNotEquals(property, property2);
351 }
352 }
353 }
354
355 VCardProperty first = properties.iterator().next();
356 VCardProperty copy = first.copy();
357 copy.setGroup("group");
358 assertNotEqualsBothWays(first, copy);
359
360 copy = first.copy();
361 copy.addParameter("PARAM", "value");
362 assertNotEqualsBothWays(first, copy);
363 }
364
365 /**
366 * Asserts that the fields in the parent {@link VCardProperty} class are
367 * checked for equality by the property class's "equals" method. Note that
368 * the property instances passed into this method will be modified as part
369 * of the test.
370 * @param instance1 the first property instance
371 * @param instance2 the second property instance
372 */
373 public static <T extends VCardProperty> void assertEqualsBaseClassFields(T instance1, T instance2) {
374 assertNotSame("You must pass two separate object instances into this method.", instance1, instance2);
375 assertEqualsAndHash(instance1, instance2);
376
377 instance1.setGroup("group");
378 assertNotEqualsBothWays(instance1, instance2);
379 instance2.setGroup("group");
380 assertEqualsAndHash(instance1, instance2);
381
382 instance1.setGroup("group2");
383 assertNotEqualsBothWays(instance1, instance2);
384 instance2.setGroup("group2");
385 assertEqualsAndHash(instance1, instance2);
386
387 instance1.setParameter("PARAM", "value");
388 assertNotEqualsBothWays(instance1, instance2);
389 instance2.setParameter("PARAM", "value");
390 assertEqualsAndHash(instance1, instance2);
391
392 instance1.setParameter("PARAM", "value2");
393 assertNotEqualsBothWays(instance1, instance2);
394 instance2.setParameter("PARAM", "value2");
395 assertEqualsAndHash(instance1, instance2);
396 }
397 }