| 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 |
|
| 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 |
public class PropertySensei { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public PropertyValidateAsserter versions(VCardVersion... versions) { |
| 81 |
this.versions = versions; |
| 82 |
return this; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public PropertyValidateAsserter vcard(VCard vcard) { |
| 91 |
vcard.addProperty(property); |
| 92 |
this.vcard = vcard; |
| 93 |
return this; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 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 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
|
| 219 |
|
| 220 |
|
| 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 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 |
|
| 292 |
|
| 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 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public static void assertNothingIsEqual(VCardProperty... properties) { |
| 337 |
assertNothingIsEqual(Arrays.asList(properties)); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 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 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
} |