com.openpojo.validation.affirm.Affirm Java Examples
The following examples show how to use
com.openpojo.validation.affirm.Affirm.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void multipleAnnotationsShouldBeReturned() { for (PojoMethod pojoMethod : pojoMethods) { if (pojoMethod.getName().equals("methodWithMultipleAnnotations")) { Affirm.affirmEquals(String.format("Annotations added/removed from method=[%s]", pojoMethod), 2, pojoMethod.getAnnotations().size()); List<Class<?>> expectedAnnotations = new LinkedList<Class<?>>(); expectedAnnotations.add(SomeAnnotation.class); expectedAnnotations.add(AnotherAnnotation.class); for (Annotation annotation : pojoMethod.getAnnotations()) { Affirm.affirmTrue(String.format("Expected annotations [%s] not found, instead found [%s]", expectedAnnotations, annotation.annotationType()), expectedAnnotations.contains(annotation.annotationType())); } return; } } Affirm.fail(String.format("methodWithMultipleAnnotations renamed? expected in [%s]", pojoClass)); }
Example #2
Source File: PojoClassFactoryTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void shouldGenerateAppropriateLinkErrorForInvalidASMGeneratedClasses() throws Exception { final SimpleClassLoader simpleClassLoader = new SimpleClassLoader(); final String className = this.getClass().getPackage().getName() + ".AClassWithBadMethod" + GENERATED_CLASS_POSTFIX; final String classNameAsPath = className.replace(Java.PACKAGE_DELIMITER, Java.PATH_DELIMITER); final Class<?> clazz = simpleClassLoader.loadThisClass(AClassWithBadMethodDump.dump(classNameAsPath), className); Affirm.affirmNotNull("Failed to generate class!", clazz); try { PojoClassFactory.getPojoClass(clazz); Affirm.fail("Should have thrown RuntimeException"); } catch (VerifyError expected) { expected.printStackTrace(); Affirm.affirmEquals("Invalid Message in exception", "(class: " + classNameAsPath + ", " + "method: badMethod signature: ()V) Wrong return type in function", expected.getMessage()); } }
Example #3
Source File: EnumRandomGeneratorTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("ConstantConditions") public void shouldGenerateRandomEnum() { RandomGenerator randomGenerator = EnumRandomGenerator.getInstance(); Enum someEnum = (Enum) randomGenerator.doGenerate(Enum.class); Affirm.affirmTrue("should never generate null", someEnum != null); Enum anotherEnum = (Enum) randomGenerator.doGenerate(Enum.class); try { Affirm.affirmFalse("Enum's should be different", someEnum.equals(anotherEnum)); } catch (AssertionError error) { // on occasion they may be the same - 1% chance, try one more time. anotherEnum = (Enum) randomGenerator.doGenerate(Enum.class); Affirm.affirmFalse("Enum's should be different", someEnum.equals(anotherEnum)); } }
Example #4
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void multipleAnnotationsShouldBeReturned() { final Class<?> aClassWithAnnotations = AClassWithAnnotations.class; final PojoClass pojoClass = getPojoClassImplForClass(aClassWithAnnotations); Affirm.affirmEquals(String.format("Annotations added/removed from Class=[%s]", aClassWithAnnotations), 2, pojoClass .getAnnotations().size()); final List<Class<?>> expectedAnnotations = new LinkedList<Class<?>>(); expectedAnnotations.add(SomeAnnotation.class); expectedAnnotations.add(AnotherAnnotation.class); for (final Annotation annotation : pojoClass.getAnnotations()) { Affirm.affirmTrue(String.format("Expected annotations [%s] not found, instead found [%s]", expectedAnnotations, annotation.annotationType()), expectedAnnotations.contains(annotation.annotationType())); } }
Example #5
Source File: SerializableTester.java From openpojo with Apache License 2.0 | 6 votes |
public void run(PojoClass pojoClass) { final Class<?> clazz = pojoClass.getClazz(); if (Serializable.class.isAssignableFrom(clazz)) { Object instance = RandomFactory.getRandomValue(clazz); ensureNoFieldsAreNull(pojoClass, instance); try { byte[] serializedObject = serialize(pojoClass, instance); Object instance2 = deSerialize(serializedObject, instance.getClass()); Affirm.affirmNotNull("Failed to load serialized object [" + instance + "]", instance2); } catch (Exception e) { Affirm.fail("Failed to run " + this.getClass().getName() + " - Got exception [" + e + "] on PojoClass " + pojoClass); } } else { logger.warn("Class [" + clazz + "] is not serializable, skipping validation"); } }
Example #6
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void testCopy() { final AClassWithEquality first = new AClassWithEquality(RandomFactory.getRandomValue(String.class), RandomFactory .getRandomValue(Integer.class)); final AClassWithEquality second = new AClassWithEquality(); Affirm.affirmFalse(String.format("Class with data=[%s], evaluated equals to one without=[%s]!!", BusinessIdentity.toString (first), BusinessIdentity.toString(second)), first.equals(second)); final PojoClass pojoClass = getPojoClassImplForClass(first.getClass()); pojoClass.copy(first, second); Affirm.affirmTrue(String.format("Class=[%s] copied to=[%s] and still equals returned false using PojoClass" + " " + "implementation=[%s]!!", BusinessIdentity.toString(first), BusinessIdentity.toString(second), pojoClass), first.equals (second)); }
Example #7
Source File: JARPackageLoaderTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public final void shouldGetJarSubClasses() { JARPackageLoader jarPackage = getJarPackageLoader(packageName); Set<String> classesNames = new LinkedHashSet<String>(); for (Type type : jarPackage.getTypes()) { classesNames.add(((Class<?>) type).getName()); } Affirm.affirmEquals(MessageFormatter.format("Classes added/removed to {0} found[{1}]?!", packageName, classesNames), expectedClassesNames.length, classesNames.size()); for (String expectedClassName : classesNames) { Affirm.affirmTrue(MessageFormatter.format("Expected class[{0}] not found", expectedClassName), classesNames.contains(expectedClassName)); } }
Example #8
Source File: PojoMethodImplGenericParametersTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void shouldGetMethodWithGenericParameter() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithGenericParameterMethod.class); List<PojoMethod> allMethodsAndConstructors = pojoClass.getPojoMethods(); List<PojoMethod> methods = new LinkedList<PojoMethod>(); for (PojoMethod method : allMethodsAndConstructors) { if (method.isConstructor()) continue; methods.add(method); } Affirm.affirmEquals(pojoClass.getName() + " should have only one generic parameterized method", 1, methods.size()); shouldHaveOneParameterizedParameter(methods, Integer.class); }
Example #9
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void testIsSyntheticOnSyntheticClass() { PojoClass syntheticPojoClass = getPojoClassImplForClass(AClassWithSythetics.class); Affirm.affirmEquals("Expected 2 constructors", 2, syntheticPojoClass.getPojoConstructors().size()); PojoMethod constructor = null; for (PojoMethod constructorEntry : syntheticPojoClass.getPojoConstructors()) { if (constructorEntry.getParameterTypes().length > 0) constructor = constructorEntry; } Assert.assertNotNull(constructor); Affirm.affirmTrue("Failed to find synthetic constructor", constructor.isSynthetic()); Affirm.affirmEquals("Synthetic Constructor should have just one parameter", 1, constructor.getParameterTypes().length); PojoClass aSyntheticClass = getPojoClassImplForClass(constructor.getParameterTypes()[0]); Affirm.affirmTrue("Parameter to synthetic constructor should be synthetic class", aSyntheticClass.isSynthetic()); }
Example #10
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void testIsInterfaceIsAbstractIsConcrete() { final String message = "Class type check failed on [%s], actual class returned [%s], PojoClass returned [%s]!!"; for (final PojoClass pojoClass : PojoClassFactory.getPojoClassesRecursively(SAMPLE_CLASSES_PKG, null)) { final Class<?> actualClass = pojoClass.getClazz(); Affirm.affirmTrue(String.format(message, actualClass.getName() + ".isInterface()", actualClass.isInterface(), pojoClass.isInterface()), pojoClass.isInterface() == actualClass.isInterface()); Affirm.affirmTrue(String.format(message, actualClass.getName() + ".isAbstract()", Modifier.isAbstract(actualClass.getModifiers()) && !Modifier.isInterface(actualClass.getModifiers()), pojoClass.isAbstract()), pojoClass.isAbstract() == (Modifier.isAbstract(actualClass.getModifiers()) && !Modifier.isInterface(actualClass.getModifiers()))); final boolean expectedValue = !(Modifier.isAbstract(actualClass.getModifiers()) || actualClass.isInterface() || actualClass.isEnum()); final boolean actualValue = pojoClass.isConcrete(); Affirm.affirmTrue(String.format(message, actualClass.getName() + ".isConcrete()", expectedValue, actualValue), actualValue == expectedValue); } }
Example #11
Source File: NoFieldShadowingRule.java From openpojo with Apache License 2.0 | 6 votes |
public void evaluate(final PojoClass pojoClass) { final List<PojoField> parentPojoFields = new LinkedList<PojoField>(); PojoClass parentPojoClass = pojoClass.getSuperClass(); while (parentPojoClass != null) { parentPojoFields.addAll(parentPojoClass.getPojoFields()); parentPojoClass = parentPojoClass.getSuperClass(); } final List<PojoField> childPojoFields = pojoClass.getPojoFields(); for (final PojoField childPojoField : childPojoFields) { if (!childPojoField.isSynthetic() && !isSerializable(childPojoField, pojoClass) && !inSkipList(childPojoField)) if (contains(childPojoField.getName(), parentPojoFields)) Affirm.fail(MessageFormatter.format("Field=[{0}] shadows field with the same name in parent class=[{1}]", childPojoField, parentPojoFields)); } }
Example #12
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsPublic() { String prefix = "publicMethod"; PojoMethod pojoMethod = getPojoMethodStartingWith(prefix); Affirm.affirmNotNull("method not found [" + prefix + "]", pojoMethod); Affirm.affirmTrue("isPublic() check on method=[" + pojoMethod + "] returned false!!", pojoMethod.isPublic()); Affirm.affirmFalse("isPrivate() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPackagePrivate()); Affirm.affirmFalse("isProtected() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isProtected()); }
Example #13
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testGetClazz() { final Class<?> clazz = this.getClass(); final PojoClass pojoClass = getPojoClassImplForClass(clazz); Affirm.affirmTrue(String.format("PojoClass parsing for [%s] returned different class=[%s] in getClazz() call" + " for " + "PojoClass implementation=[%s]", clazz, pojoClass.getClazz(), pojoClass), clazz.equals(pojoClass.getClazz())); }
Example #14
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
/** * Test method for {@link com.openpojo.reflection.impl.PojoMethodImpl#isFinal()}. */ @Test public void testIsFinal() { for (PojoMethod pojoMethod : pojoMethods) { if (pojoMethod.getName().equals("finalMethod")) { Affirm.affirmTrue("Failed to check final", pojoMethod.isFinal()); return; } } Affirm.fail("finalMethod missing!!"); }
Example #15
Source File: PojoPackageImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldReturnAnnotationList() { final PojoPackage pojoPackage = PojoPackageFactory.getPojoPackage(this.getClass().getPackage().getName() + ".packagemanyannotations"); Affirm.affirmEquals(String.format("Annotations added/removed? [%s]", pojoPackage), 2, pojoPackage.getAnnotations().size()); final List<Class<?>> expectedAnnotations = new LinkedList<Class<?>>(); expectedAnnotations.add(SomeAnnotation.class); expectedAnnotations.add(AnotherAnnotation.class); for (final Annotation annotation : pojoPackage.getAnnotations()) { Affirm.affirmTrue(String.format("Expected annotations [%s] not found, instead found [%s]", expectedAnnotations, annotation.annotationType()), expectedAnnotations.contains(annotation.annotationType())); } }
Example #16
Source File: PojoPackageImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldReturnAnAnnotation() { final PojoPackage pojoPackage = PojoPackageFactory.getPojoPackage(this.getClass().getPackage().getName() + ".packagemanyannotations"); final Class<? extends Annotation> expectedAnnotationClass = SomeAnnotation.class; Affirm.affirmNotNull(String.format("[%s] removed from package [%s]?", expectedAnnotationClass, pojoPackage), pojoPackage .getAnnotation(expectedAnnotationClass)); }
Example #17
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testEqualityAndHashCodeBasedOnIdentityNotInstance() { final PojoClass first = getPojoClassImplForClass(this.getClass()); final PojoClass second = getPojoClassImplForClass(this.getClass()); Affirm.affirmEquals("PojoClassImpl equals is instance based!! Should be business equality based.", first, second); Affirm.affirmEquals("PojoClassImpl hashCode is instance based!! Should be business equality based.", first.hashCode(), second.hashCode()); }
Example #18
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsStatic() { for (PojoMethod pojoMethod : pojoMethods) { if (pojoMethod.getName().equals("staticMethod")) { Affirm.affirmTrue("Failed to check static method", pojoMethod.isStatic()); return; } } Affirm.fail("staticMethod missing!!"); }
Example #19
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsStatic() { PojoClass pojoClass = getPojoClassImplForClass(AClassWithTwoChildClassesOneStaticAndOneNot.APublicNonStaticClass.class); Affirm.affirmTrue("Nested class not detected nested", pojoClass.isNestedClass()); Affirm.affirmFalse("Nested non-static nested class detected as static", pojoClass.isStatic()); pojoClass = getPojoClassImplForClass(AClassWithTwoChildClassesOneStaticAndOneNot.AStaticClass.class); Affirm.affirmTrue("Nested class not detected nested", pojoClass.isNestedClass()); Affirm.affirmTrue("Nested static class not seen as static", pojoClass.isStatic()); }
Example #20
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testGetAnnotations() { final Class<?> aClassWithAnnotations = AClassWithAnnotations.class; final PojoClass pojoClass = getPojoClassImplForClass(aClassWithAnnotations); Affirm.affirmEquals(String.format("Annotations added/removed from Class=[%s]", aClassWithAnnotations), 2, pojoClass .getAnnotations().size()); }
Example #21
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsProtected() { String prefix = "protectedMethod"; PojoMethod pojoMethod = getPojoMethodStartingWith(prefix); Affirm.affirmNotNull("method not found [" + prefix + "]", pojoMethod); Affirm.affirmTrue("isProtected() check on method=[" + pojoMethod + "] returned false!!", pojoMethod.isProtected()); Affirm.affirmFalse("isPrivate() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPackagePrivate()); Affirm.affirmFalse("isPublic() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPublic()); }
Example #22
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsFinalOnFinalClass() { final Class<?> aFinalClass = AFinalClass.class; final PojoClass pojoClass = getPojoClassImplForClass(aFinalClass); Affirm.affirmTrue(String.format("IsFinal on final=[%s] returned false for PojoClass implementation=[%s]!!", aFinalClass, pojoClass), pojoClass.isFinal()); }
Example #23
Source File: CollectionAndMapPackageRandomGeneratorsTest.java From openpojo with Apache License 2.0 | 5 votes |
/** * This test will test every random generator against its declared types, for every type returned from getTypes. */ @Test public void shouldReturnRandomInstanceForDeclaredType() { for (final PojoClass randomGeneratorPojoClass : collectionRandomGenerators) { final RandomGenerator randomGenerator = (RandomGenerator) InstanceFactory.getInstance(randomGeneratorPojoClass); final Collection<Class<?>> generatorTypes = randomGenerator.getTypes(); for (final Class<?> type : generatorTypes) { LoggerFactory.getLogger(this.getClass()).debug("Generating Type [" + type + "]"); Object firstInstance = randomGenerator.doGenerate(type); Affirm.affirmNotNull(MessageFormatter.format("[{0}] returned null for type [{1}]", randomGenerator.getClass(), type), firstInstance); Affirm.affirmTrue(MessageFormatter.format("[{0}] returned incompatible type [{1}] when requesting type [{2}]", randomGenerator.getClass(), firstInstance.getClass(), type), type.isAssignableFrom(firstInstance.getClass())); int counter = 10; Object secondInstance = null; while (counter > 0) { firstInstance = randomGenerator.doGenerate(type); secondInstance = randomGenerator.doGenerate(type); if (!firstInstance.equals(secondInstance)) { break; } counter--; } Affirm.affirmFalse(MessageFormatter.format("[{0}] returned identical instances for type [{1}]", randomGenerator.getClass(), type), firstInstance.equals(secondInstance)); } } }
Example #24
Source File: PojoMethodImplGenericParametersTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldHaveTwoParametersWithWhenOneParameterDeclared() { PojoClass pojoclass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClassWithOneParamConstructor.class); List<PojoMethod> pojoConstructors = pojoclass.getPojoConstructors(); Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size()); PojoMethod constructor = pojoConstructors.get(0); List<PojoParameter> pojoParameters = constructor.getPojoParameters(); Affirm.affirmEquals("Should have 2 parameter", 2, pojoParameters.size()); Affirm.affirmFalse("Should be nonParameterized parameter", pojoParameters.get(0).isParameterized()); Affirm.affirmEquals("Should be enclosing type", constructor.getParameterTypes()[0], pojoclass.getEnclosingClass().getClazz()); Affirm.affirmEquals("Should be int type", constructor.getParameterTypes()[1], int.class); }
Example #25
Source File: PojoFieldImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsProtected() { String prefix = "protected"; PojoField pojoField = getFieldStartingWith(prefix); Affirm.affirmNotNull("Field not found [" + prefix + "]", pojoField); Affirm.affirmTrue("isProtected() check on field=[" + pojoField + "] returned false!!", pojoField.isProtected()); Affirm.affirmFalse("isPrivate() check on field=[" + pojoField + "] returned true!!", pojoField.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on field=[" + pojoField + "] returned true!!", pojoField.isPackagePrivate()); Affirm.affirmFalse("isPublic() check on field=[" + pojoField + "] returned true!!", pojoField.isPublic()); }
Example #26
Source File: AbstractUnitTest.java From cia with Apache License 2.0 | 5 votes |
public void run(final PojoClass pojoClass) { final Object instance = randomValues(pojoClass); Affirm.affirmFalse("EqualsCompareNullFailure", instance.equals(null)); Affirm.affirmFalse("EqualsCompareWrongClassFailure", instance.equals("WrongClass")); Affirm.affirmTrue("EqualsCompareSelfFailure", instance.equals(instance)); final Object instance2 = randomValues(pojoClass); instance.equals(instance2); }
Example #27
Source File: PojoMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsPrivate() { String prefix = "privateMethod"; PojoMethod pojoMethod = getPojoMethodStartingWith(prefix); Affirm.affirmNotNull("method not found [" + prefix + "]", pojoMethod); Affirm.affirmTrue("isPrivate() check on method=[" + pojoMethod + "] returned false!!", pojoMethod.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPackagePrivate()); Affirm.affirmFalse("isProtected() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isProtected()); Affirm.affirmFalse("isPublic() check on method=[" + pojoMethod + "] returned true!!", pojoMethod.isPublic()); }
Example #28
Source File: PojoFieldImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testIsTransient() { for (PojoField pojoField : pojoClass.getPojoFields()) { if (pojoField.getName().equals("transientString")) { Affirm.affirmTrue(String.format("isTransient() check on field=[%s] returned false!!", pojoField), pojoField.isTransient ()); } } }
Example #29
Source File: CloverPojoClassAdapterTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void ensureCloverInstrumentedClassNotChanged() { int expectedFieldCount = 4; if (Clover4.getInstance().isLoaded()) expectedFieldCount++; Affirm.affirmEquals("Fields added/removed?", expectedFieldCount, cloverInstrumentedPojoClass.getPojoFields().size()); Affirm.affirmEquals("Methods added/removed?", 3, cloverInstrumentedPojoClass.getPojoMethods().size()); }
Example #30
Source File: InstanceFactoryTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldConstructUsingMaximumParameterCount() { final PojoClass pojoClass = getPojoClass(ClassWithLessThanGreaterThanConstructors.class); final ClassWithLessThanGreaterThanConstructors instance = (ClassWithLessThanGreaterThanConstructors) InstanceFactory.getMostCompleteInstance(pojoClass); Affirm.affirmEquals("Should've used constructor with single Parameter", 3, instance.getParameterCountUsedForConstruction()); }