Java Code Examples for com.openpojo.validation.affirm.Affirm#affirmEquals()

The following examples show how to use com.openpojo.validation.affirm.Affirm#affirmEquals() . 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: SerializableMustHaveSerialVersionUIDRule.java    From openpojo with Apache License 2.0 6 votes vote down vote up
public void evaluate(final PojoClass pojoClass) {
  if (pojoClass.extendz(Serializable.class) && !pojoClass.isInterface()) {
    for (PojoField pojoField : pojoClass.getPojoFields()) {
      if (pojoField.getName().equalsIgnoreCase(SERIAL_VERSION_UID)) {
        if (!pojoField.getName().equals(SERIAL_VERSION_UID)) {
          Affirm.affirmEquals(String.format("Case miss-match on serialVersionUID field on Serializable class [%s]", pojoClass),
              SERIAL_VERSION_UID, pojoField.getName());
        }
        if (!(pojoField.isStatic() && pojoField.isFinal())) {
          Affirm.fail(String.format("[%s] must be defined as [static final] on Serializable class [%s]",
              SERIAL_VERSION_UID, pojoClass));
        }
        if (pojoField.getType() != long.class) {
          Affirm.fail(String.format("[%s] must be defined as [long] on Serializable class [%s]",
              SERIAL_VERSION_UID, pojoClass));
        }
        return;
      }
    }
    Affirm.fail(String.format("No [%s] field defined on Serializable class [%s]", SERIAL_VERSION_UID, pojoClass));
  }
}
 
Example 2
Source File: SetterTester.java    From openpojo with Apache License 2.0 6 votes vote down vote up
public void run(final PojoClass pojoClass) {
  final Object classInstance = ValidationHelper.getBasicInstance(pojoClass);
  for (final PojoField fieldEntry : pojoClass.getPojoFields()) {
    if (fieldEntry.hasSetter()) {
      final Object value;

      value = RandomFactory.getRandomValue(fieldEntry);

      SameInstanceIdentityHandlerStub.registerIdentityHandlerStubForValue(value);
      LoggerFactory.getLogger(this.getClass()).debug("Testing Field [{0}] with value [{1}]",
          fieldEntry, safeToString(value));

      fieldEntry.invokeSetter(classInstance, value);

      Affirm.affirmEquals("Setter test failed, non equal value for field=[" + fieldEntry + "]", value,
          fieldEntry.get(classInstance));

      SameInstanceIdentityHandlerStub.unregisterIdentityHandlerStubForValue(value);
    } else {
      LoggerFactory.getLogger(this.getClass()).debug("Field [{0}] has no setter skipping", fieldEntry);
    }
  }
}
 
Example 3
Source File: JARPackageLoaderTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: RandomFactoryTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRegisterHierarchyOfTypes() {
  RandomFactory.addRandomGenerator(SomeInterfaceRandomGenerator.getInstance());
  final Class<?> someInterface = SomeInterface.class;
  final Class<?> classImplementingSomeInterface = ClassImplementingSomeInterface.class;
  final Class<?> classExtendingClassImplmentingSomeInterface = ClassExtendingClassImplementingSomeInterface.class;
  Object instance = RandomFactory.getRandomValue(someInterface);

  Affirm.affirmNotNull(String.format("RandomFactory failed to retrieve random instance for interface [%s]", someInterface),
      instance);

  Affirm.affirmEquals("RandomFactory failed to lookup proper random generator from heirarchy",
      classExtendingClassImplmentingSomeInterface, instance.getClass());

  instance = RandomFactory.getRandomValue(classImplementingSomeInterface);

  Affirm.affirmEquals("RandomFactory failed to lookup proper random generator from heirarchy",
      classExtendingClassImplmentingSomeInterface, instance.getClass());
}
 
Example 5
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: DefaultRandomGeneratorServiceTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetRandomGeneratorBasedOnAssignability() {
  final Class<?> type = LinkedList.class;

  final DummyRandomGenerator dummyRandomGenerator = new DummyRandomGenerator();
  dummyRandomGenerator.setTypes(new Class<?>[] { type });
  defaultRandomGeneratorService.registerRandomGenerator(dummyRandomGenerator);

  defaultRandomGeneratorService.getRandomGeneratorByType(List.class).doGenerate(List.class);

  Affirm.affirmEquals("Incorrect random generator returned (doGenerate should've incremented call count)", 1,
      dummyRandomGenerator.getCounter());
}
 
Example 8
Source File: BusinessPojoHelperTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void shouldThrowExeptionIfConstructed() throws Throwable {
  PojoClass businessPojoHelper = PojoClassFactory.getPojoClass(BusinessPojoHelper.class);

  List<PojoMethod> pojoConstructors = businessPojoHelper.getPojoConstructors();
  Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size());
  Affirm.affirmTrue("Constructor must be private", pojoConstructors.get(0).isPrivate());

  try {
    businessPojoHelper.getPojoConstructors().get(0).invoke(null, (Object[]) null);
  } catch (ReflectionException re) {
    throw re.getCause().getCause();
  }
}
 
Example 9
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetEmptyListForGetInterfaces() {
  final PojoClass pojoClass = getPojoClassImplForClass(AClassWithoutInterfaces.class);
  Affirm.affirmNotNull(String.format("Expected empty list no null for getInterfaces() on [%s]?", pojoClass), pojoClass
      .getInterfaces());
  Affirm.affirmEquals(String.format("Interfaces added to [%s]?", pojoClass), 0, pojoClass.getInterfaces().size());
}
 
Example 10
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testBooleanVariations() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(ClassWithBooleanFields.class);

  Affirm.affirmEquals("Fields must be 4", 4, pojoClass.getPojoFields().size());

  int countOfbooleans = 0;
  int countOfBooleans = 0;

  for (PojoField pojoField : pojoClass.getPojoFields()) {
    if (pojoField.isPrimitive() && pojoField.getType() == boolean.class) {
      countOfbooleans++;
    }
    if (!pojoField.isPrimitive() && pojoField.getType() == Boolean.class) {
      countOfBooleans++;
    }
  }

  Affirm.affirmEquals("2 boolean fields must exist", 2, countOfbooleans);
  Affirm.affirmEquals("2 Boolean fields must exist", 2, countOfBooleans);

  Validator pojoValidator = ValidatorBuilder.create()
      .with(new GetterMustExistRule())
      .with(new SetterMustExistRule())
      .with(new GetterTester())
      .with(new SetterTester())
      .build();

  pojoValidator.validate(pojoClass);
}
 
Example 11
Source File: PojoClassFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the factory is able to create a pojoClass correctly mapping back to PojoClassFactoryTest.
 */
@Test
public void testGetPojoClass() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(this.getClass());
  Affirm.affirmNotNull(String.format("PojoClassFactory failed to create PojoClass for [%s]", this.getClass()), pojoClass);
  Affirm.affirmEquals(String.format("PojoClassFactory returned invalid meta-definition PojoClass for [%s]", this.getClass()),
      this.getClass(), pojoClass.getClazz());
}
 
Example 12
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetInterfaces() {
  final PojoClass pojoClass = getPojoClassImplForClass(AClassWithInterfaces.class);
  Affirm.affirmEquals(String.format("Interfaces added/removed from [%s]?", pojoClass), 2, pojoClass.getInterfaces().size());

  final List<Class<?>> expectedInterfaces = new LinkedList<Class<?>>();
  expectedInterfaces.add(FirstInterfaceForAClassWithInterfaces.class);
  expectedInterfaces.add(SecondInterfaceForAClassWithInterfaces.class);
  for (final PojoClass pojoInterface : pojoClass.getInterfaces()) {
    Affirm.affirmTrue(String.format("Expected interfaces [%s] not found, instead found [%s]", expectedInterfaces,
        pojoInterface.getClazz()), expectedInterfaces.contains(pojoInterface.getClazz()));
  }
}
 
Example 13
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPojoMethodsAnnotatedWith() {
  PojoClass pojoClass = getPojoClassImplForClass(AClassWithAnnotatedMethods.class);
  Affirm.affirmEquals("Expected 5 methods", 4 + 1 /* constructor */, pojoClass.getPojoMethods().size());

  List<PojoMethod> annotatedPojoFields = pojoClass.getPojoMethodsAnnotatedWith(SomeAnnotation.class);
  Affirm.affirmEquals("Expected 2 annotated methods", 2, annotatedPojoFields.size());

}
 
Example 14
Source File: InstanceFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConstructUsingMinimalParameterCount() {
  final PojoClass pojoClass = getPojoClass(ClassWithLessThanGreaterThanConstructors.class);
  final ClassWithLessThanGreaterThanConstructors instance =
      (ClassWithLessThanGreaterThanConstructors) InstanceFactory.getLeastCompleteInstance(pojoClass);
  Affirm.affirmEquals("Should've used constructor with single Parameter", 1, instance.getParameterCountUsedForConstruction());
}
 
Example 15
Source File: UnitPojo.java    From orders with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureExpectedPojoCount() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(POJO_PACKAGE, filter);
    Affirm.affirmEquals("Classes added / removed?", EXPECTED_CLASS_COUNT, pojoClasses.size());
}
 
Example 16
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldBeAbleToConstructNestedChildWithNoParameters() {
  PojoClass pojoclass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClass.class);
  Object instance = RandomFactory.getRandomValue(pojoclass.getClazz());
  Affirm.affirmEquals("Should be same type", pojoclass.getClazz(), instance.getClass());
}
 
Example 17
Source File: PrimitivesTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldHavePrivateConstructor() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(Primitives.class);
  Affirm.affirmEquals("Should only have one constructor", 1, pojoClass.getPojoConstructors().size());
  Affirm.affirmTrue("Constructor must be private", pojoClass.getPojoConstructors().get(0).isPrivate());
}
 
Example 18
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldBeAbletoConstructNestedChileWithOneParameter() {
  PojoClass pojoclass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClassWithOneParamConstructor.class);
  Object instance = RandomFactory.getRandomValue(pojoclass.getClazz());
  Affirm.affirmEquals("Should be same type", pojoclass.getClazz(), instance.getClass());
}
 
Example 19
Source File: UnitPojo.java    From shipping with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureExpectedPojoCount() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(POJO_PACKAGE, filter);
    Affirm.affirmEquals("Classes added / removed?", EXPECTED_CLASS_COUNT, pojoClasses.size());
}
 
Example 20
Source File: FacadeFactoryTest.java    From openpojo with Apache License 2.0 3 votes vote down vote up
private final void checkReturnedFacade(final Class<?> expected, final String... facades) {
  final PojoClass facade = FacadeFactory.getLoadedFacadePojoClass(facades);
  Affirm.affirmNotNull(String.format("Failed to load from the valid list of facades [%s]?!", Arrays.toString(facades)), facade);

  Affirm.affirmEquals("Wrong facade returned!!", expected, facade.getClazz());

}