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

The following examples show how to use com.openpojo.validation.affirm.Affirm#affirmNotNull() . 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: RandomFactoryArrayTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateRandomArray() {
  final Class<?> type = anyArrayType();
  final Object firstInstance = RandomFactory.getRandomValue(type);
  Affirm.affirmNotNull("Failed to get random array", firstInstance);

  final Object secondInstance = RandomFactory.getRandomValue(type);

  if (Array.getLength(firstInstance) == Array.getLength(secondInstance)) {
    boolean equals = true;
    for (int idx = 0; idx < Array.getLength(firstInstance); idx++) {
      final Object firstInstanceElement = Array.get(firstInstance, idx);
      final Object secondInstanceElement = Array.get(secondInstance, idx);
      equals = equals && ((firstInstanceElement != null && firstInstanceElement.equals(secondInstanceElement)) ||
          (firstInstanceElement == null && secondInstanceElement == null));
    }
  }
}
 
Example 2
Source File: SerializableTester.java    From openpojo with Apache License 2.0 6 votes vote down vote up
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 3
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateInstanceOneParameterConstructor() {
  final PojoClass pojoClass = getPojoClassImplForClass(MultiplePublicAndPrivateWithManyParamsConstructor.class);
  final Object instance = InstanceFactory.getInstance(pojoClass, RandomFactory.getRandomValue(String.class));
  Affirm.affirmNotNull(String.format("Failed to create a new instance using single parameter constructor for " +
      "class=[%s]", pojoClass), instance);
}
 
Example 4
Source File: RandomInstanceFromInterfaceRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldImplementAccuratetoStringAndhashCode() {
  final String toString = aSimpleInterface.toString();
  Affirm.affirmNotNull("toString() on proxy returned null", toString);
  Affirm.affirmTrue(String.format("toString returned [%s] expected it to begin with [%s] and contain [@]", toString, "$Proxy"),
      toString.contains("$Proxy") && toString.contains("@"));

  Affirm.affirmTrue("toString() doesn't end with hashCode()", toString.endsWith(String.valueOf(aSimpleInterface.hashCode())));

  final ASimpleInterface anotherSimpleInterface = proxyGenerator.doGenerate(ASimpleInterface.class);
  Affirm.affirmTrue("Generated Proxy hashCode() should not return equal values across instances",
      aSimpleInterface.hashCode() != anotherSimpleInterface.hashCode());
}
 
Example 5
Source File: LoggerFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public final void shouldReturnDefaultCategoryByClass() {
  Logger log = LoggerFactory.getLogger((Class<?>) null);
  Affirm.affirmNotNull("Null logger returned when requested with null class", log);
  log = LoggerFactory.getLogger((String) null);
  Affirm.affirmNotNull("Null logger returned when requested with null category", log);
}
 
Example 6
Source File: EnumRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("ConstantConditions")
public void endToEndTest() {
  Enum someEnum = RandomFactory.getRandomValue(Enum.class);
  Affirm.affirmNotNull("Should generate Enum", someEnum);
  Affirm.affirmTrue("Should use SomeEnum when generating", someEnum.getClass() == SomeEnum.class);
}
 
Example 7
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void canGetEnclosingClass() {
  PojoClass aClassWithNested = PojoClassFactory.getPojoClass(AClassWithNestedClass.class);
  PojoClass nestedClass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClass.class);
  Affirm.affirmTrue("Class should be nested", nestedClass.isNestedClass());

  PojoClass enclosingClass = nestedClass.getEnclosingClass();
  Affirm.affirmNotNull("Enclosing should not be null", enclosingClass);
  Affirm.affirmTrue("Invalid enclosing class", enclosingClass.getClazz().equals(aClassWithNested.getClazz()));
}
 
Example 8
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateInstanceMultipleParameterConstructor() {
  final PojoClass pojoClass = getPojoClassImplForClass(MultiplePublicAndPrivateWithManyParamsConstructor.class);
  final Object instance = InstanceFactory.getInstance(pojoClass, RandomFactory.getRandomValue(String.class), RandomFactory
      .getRandomValue(Integer.class));
  Affirm.affirmNotNull(String.format("Failed to create a new instance using multiple parameter constructor for " +
      "class=[%s]", pojoClass), instance);
}
 
Example 10
Source File: PojoFieldImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void isPackagePrivate() {
  String prefix = "packagePrivate";
  PojoField pojoField = getFieldStartingWith(prefix);
  Affirm.affirmNotNull("Field not found [" + prefix + "]", pojoField);
  Affirm.affirmTrue("isPackagePrivate() check on field=[" + pojoField + "] returned false!!", pojoField.isPackagePrivate());
  Affirm.affirmFalse("isPrivate() check on field=[" + pojoField + "] returned true!!", pojoField.isPrivate());
  Affirm.affirmFalse("isPublic() check on field=[" + pojoField + "] returned true!!", pojoField.isPublic());
  Affirm.affirmFalse("isProtected() check on field=[" + pojoField + "] returned true!!", pojoField.isProtected());
}
 
Example 11
Source File: PojoFieldImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: PojoClassFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the factory utilizes pojo filter.
 */
@Test
public void testGetFilteredPojoClasses() {
  LoggingPojoClassFilter loggingPojoClassFilter = new LoggingPojoClassFilter();
  // Set Filter to reject all
  loggingPojoClassFilter.setReturnValue(false);

  List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(DUMMY_PACKAGE, loggingPojoClassFilter);
  Affirm.affirmNotNull(String.format(
      "PojoClassFactory returned null list while getting list for package=[%s] using filter=[%s] in filter out all mode!!",
      DUMMY_PACKAGE, loggingPojoClassFilter.getClass()), pojoClasses);

  Affirm.affirmEquals(String.format(
      "PojoClassFactory returned non-empty list for package=[%s] using filter=[%s] in filter out all mode!!",
      DUMMY_PACKAGE, loggingPojoClassFilter.getClass()), 0, pojoClasses.size());

  Affirm.affirmTrue(String.format(
      "Too few number of times filter was triggered while in filter-out all mode!! Classes removed from package=[%s] " +
          "found [%s]? expected at least 2 but was [%s]",
      DUMMY_PACKAGE, loggingPojoClassFilter
      .getPojoClassCallLogs(), loggingPojoClassFilter.getPojoClassCallLogs().size()), 2 <= loggingPojoClassFilter
      .getPojoClassCallLogs().size());

  // Set Filter to allow all
  loggingPojoClassFilter.setReturnValue(true);
  pojoClasses = PojoClassFactory.getPojoClasses(DUMMY_PACKAGE, loggingPojoClassFilter);
  Affirm.affirmNotNull(String.format(
      "PojoClassFactory returned null for package=[%s] using filter=[%s] in allow all mode!!", DUMMY_PACKAGE,
      loggingPojoClassFilter.getClass()), pojoClasses);

  Affirm.affirmEquals(String.format("Wrong number of classes retrieved!! Classes added/removed from package=[%s]?",
      DUMMY_PACKAGE), 2, pojoClasses.size());
}
 
Example 13
Source File: RandomFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRandomWithNoRegisteredRandomGenerator() {
  final Class<?> clazz = AClassWithNoRegisteredRandomGenerator.class;

  final Object someInstance = RandomFactory.getRandomValue(clazz);

  Affirm.affirmNotNull(String.format("Null value returned for random instance of [%s]", clazz.getName()), someInstance);

  Affirm.affirmFalse(String.format("Non randomized instance returned (i.e. same object) for [%s]", clazz.getName()),
      someInstance.equals(RandomFactory.getRandomValue(clazz)));
}
 
Example 14
Source File: RandomInstanceFromInterfaceRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnRandomNonNullValuesForInterfaceMethods() {
  final ASimpleInterface aSimpleInterface = proxyGenerator.doGenerate(ASimpleInterface.class);

  Affirm.affirmNotNull("Generated proxy getName() returned null", aSimpleInterface.getName());

  final String name = aSimpleInterface.getName();
  final String otherName = aSimpleInterface.getName();
  if (name.equals(otherName)) { // Just in case they are the same by chance.
    Affirm.affirmFalse(String.format("RandomProxyFactory=[%s] returned a non-Random Pojo Proxy",
        RandomInstanceFromInterfaceRandomGenerator.getInstance()), name.equals(aSimpleInterface.getName()));
  }
}
 
Example 15
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@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 17
Source File: PojoFieldImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsPrivate() {
  String prefix = "private";
  PojoField pojoField = getFieldStartingWith(prefix);
  Affirm.affirmNotNull("Field not found [" + prefix + "]", pojoField);
  Affirm.affirmTrue("isPrivate() check on field=[" + pojoField + "] returned false!!", pojoField.isPrivate());
  Affirm.affirmFalse("isPublic() check on field=[" + pojoField + "] returned true!!", pojoField.isPublic());
  Affirm.affirmFalse("isProtected() check on field=[" + pojoField + "] returned true!!", pojoField.isProtected());
  Affirm.affirmFalse("isPackagePrivate() check on field=[" + pojoField + "] returned true!!", pojoField.isPackagePrivate());
}
 
Example 18
Source File: PojoFieldImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void canAccessSetter() {
  boolean found = false;
  for (PojoField pojoField : pojoClass.getPojoFields()) {
    if (pojoField.hasSetter()) {
      PojoMethod setter = pojoField.getSetter();
      Affirm.affirmNotNull("Setter can't be retrieved", setter);
      Object randomInstance = RandomFactory.getRandomValue(pojoField.getType());
      setter.invoke(pojoClassInstance, randomInstance);
      Affirm.affirmSame("Expected same object in and out", randomInstance, pojoField.get(pojoClassInstance));
      found = true;
    }
  }
  Affirm.affirmTrue("No Setters were found!", found);
}
 
Example 19
Source File: PrimitivesTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnSameInstanceWhenGetInstance() {
  Primitives first = Primitives.getInstance();
  Primitives second = Primitives.getInstance();
  Affirm.affirmNotNull("Should return an instance", first);
  Affirm.affirmNotNull("Should return an instance", second);
  Affirm.affirmTrue("Should have been the exact same instance", first == second);
}
 
Example 20
Source File: RandomInstanceFromInterfaceRandomGeneratorTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnAProxy() {
  Affirm.affirmNotNull("Interface instance not generated", aSimpleInterface);
}