com.openpojo.validation.Validator Java Examples

The following examples show how to use com.openpojo.validation.Validator. 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: OpenPojoEntityTest.java    From tessera with Apache License 2.0 7 votes vote down vote up
@Test
public void executeOpenPojoValidationsWithSetter() {

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

    pojoValidator.validate(PojoClassFactory.getPojoClass(MessageHash.class));
}
 
Example #2
Source File: IssueTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotFail() {
  Validator validator = ValidatorBuilder
      .create()
      .with(new GetterTester())
      .with(new SetterTester())
      .build();

  final PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithXMLGregorianCalendar.class);
  validator.validate(pojoClass);

  Assert.assertThat(appender.getEventsForLogger(GetterTester.class).size(), is(1));
  Assert.assertThat(appender.getEventsForLogger(SetterTester.class).size(), is(1));

  PojoField xmlGregorianCalendarPojoField = pojoClass.getPojoFields().get(0);
  final String message = "Testing Field [" + xmlGregorianCalendarPojoField + "] with value [";

  validateLogMessages(appender, GetterTester.class, message);
  validateLogMessages(appender, SetterTester.class, message);
}
 
Example #3
Source File: IdentityFactoryRaceConditionTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void runWithManyThreads() {
  int numberOfThreads = 1500;

  ReportingThreadExecutionPool tpe = getReportingThreadExecutionPool(numberOfThreads);
  CyclicBarrier cb = new CyclicBarrier(numberOfThreads);

  Validator validator = getValidator();

  for (int i = 0; i < numberOfThreads; i++)
    tpe.execute(new Worker(cb, validator));

  tpe.shutdown();

  while (!tpe.isTerminated())
    try {
      TimeUnit.MILLISECONDS.sleep(100);
    } catch (InterruptedException ignored) {
    }

  Assert.assertEquals("Some threads failed to complete successfully", numberOfThreads, tpe.getCompletedSuccessfully());
}
 
Example #4
Source File: IdentityFactoryRaceConditionTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
private Validator getValidator() {
  return ValidatorBuilder.create()
      .with(new BusinessKeyMustExistRule())
      .with(new GetterMustExistRule())
      .with(new NoFieldShadowingRule())
      .with(new NoNestedClassRule())
      .with(new NoPrimitivesRule())
      .with(new NoPublicFieldsExceptStaticFinalRule())
      .with(new NoPublicFieldsRule())
      .with(new NoStaticExceptFinalRule())
      .with(new SerializableMustHaveSerialVersionUIDRule())
      .with(new SetterMustExistRule())
      .with(new BusinessIdentityTester())
      .with(new DefaultValuesNullTester())
      .with(new GetterTester())
      .with(new SetterTester())
      .build();
}
 
Example #5
Source File: AbstractUnitTest.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Check all classes in package.
 *
 * @param string the string
 * @return true, if successful
 */
protected final boolean checkAllClassesInPackage(final String string) {
	final List<PojoClass> pojoClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string,
			new FilterTestClasses());

	final Validator validator = ValidatorBuilder.create().with(new SetterMustExistRule(), new GetterMustExistRule())
			.with(new SetterTester(), new GetterTester()).with(new InvokeToStringTester())
			.with(new InvokeHashcodeTester()).with(new DummyEqualsTester()).with(new WithTester())
			.with(new ObjectFactoryTester()).with(new EqualsAndHashCodeMatchRule()).build();
	validator.validate(pojoClassesRecursively);

	final List<PojoClass> enumClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string,
			new FilterNonEnumClasses());

	final Validator enumValidator = ValidatorBuilder.create().with(new EnumTester()).build();
	enumValidator.validate(enumClassesRecursively);

	return true;
}
 
Example #6
Source File: ValidationHelperTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReportMissingASMProperly() {
  Validator validator = ValidatorBuilder.create()
      .with(new Tester() {
        public void run(PojoClass pojoClass) {
          throw ASMNotLoadedException.getInstance();
        }
      }).build();

  LogHelper.initialize(MockAppenderLog4J.class);
  validator.validate(PojoClassFactory.getPojoClass(this.getClass()));
  List<LogEvent> warnEvents = LogHelper.getWarnEvents(MockAppenderLog4J.class, DefaultValidator.class.getName());
  Assert.assertEquals(1, warnEvents.size());
  String expectedMessage = "ASM not loaded while attempting to execute behavioural tests on non-constructable class["
      + this.getClass() + "], either filter abstract classes or add asm to your classpath.";
  Assert.assertEquals(expectedMessage, warnEvents.get(0).getMessage());
}
 
Example #7
Source File: UnitPojo.java    From orders with Apache License 2.0 6 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    Validator validator = ValidatorBuilder.create()
            // Add Rules to validate structure for POJO_PACKAGE
            // See com.openpojo.validation.rule.impl for more ...
            .with(new GetterMustExistRule())
            .with(new SetterMustExistRule())
            // Add Testers to validate behaviour for POJO_PACKAGE
            // See com.openpojo.validation.test.impl for more ...
            .with(new SetterTester())
            .with(new GetterTester())
            // Static fields must be final
            .with(new NoStaticExceptFinalRule())
            // Don't shadow parent's field names.
            .with(new NoFieldShadowingRule())
            // What about public fields, use one of the following rules
            // allow them only if they are static and final.
            .with(new NoPublicFieldsExceptStaticFinalRule())
            .build();

    validator.validate(POJO_PACKAGE, filter);
}
 
Example #8
Source File: RecordPojoTest.java    From cerberus with Apache License 2.0 6 votes vote down vote up
@Test
public void test_pojo_structure_and_behavior() {

  List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses("com.nike.cerberus.record");

  Assert.assertEquals(15, pojoClasses.size());

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

  validator.validate(pojoClasses);
}
 
Example #9
Source File: UnitPojo.java    From shipping with Apache License 2.0 6 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    Validator validator = ValidatorBuilder.create()
            // Add Rules to validate structure for POJO_PACKAGE
            // See com.openpojo.validation.rule.impl for more ...
            .with(new GetterMustExistRule())
            .with(new SetterMustExistRule())
            // Add Testers to validate behaviour for POJO_PACKAGE
            // See com.openpojo.validation.test.impl for more ...
            .with(new SetterTester())
            .with(new GetterTester())
            // Static fields must be final
            .with(new NoStaticExceptFinalRule())
            // Don't shadow parent's field names.
            .with(new NoFieldShadowingRule())
            // What about public fields, use one of the following rules
            // allow them only if they are static and final.
            .with(new NoPublicFieldsExceptStaticFinalRule())
            .build();

    validator.validate(POJO_PACKAGE, filter);
}
 
Example #10
Source File: DomainTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
void testPojoStructureAndBehavior() {

    List<PojoClass> pojoClasses = new ArrayList<>();
    pojoClasses.add(PojoClassFactory.getPojoClass(Region.class));
    pojoClasses.add(PojoClassFactory.getPojoClass(CompactView.class));
    pojoClasses.add(PojoClassFactory.getPojoClass(EnvironmentAuthentication.class));

    Validator validator = ValidatorBuilder.create()
            .with(new SetterMustExistRule(),
                    new GetterMustExistRule())
            .with(new GetterTester())
            .with(new NoPublicFieldsExceptStaticFinalRule())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .with(new NoFieldShadowingRule())
            .build();
    validator.validate(pojoClasses);

    validator = ValidatorBuilder.create()
            .with(new SetterTester())
            .build();
    validator.validate(pojoClasses);
    // openpojo will do for now (seems buggy) but later would worth experimenting with pojo-tester (https://www.pojo.pl/)
}
 
Example #11
Source File: OpenPojoTest.java    From tessera with Apache License 2.0 6 votes vote down vote up
@Test
public void executeOpenPojoValidations() {
    final Validator pojoValidator =
            ValidatorBuilder.create()
                    .with(new GetterMustExistRule())
                    .with(new GetterTester())
                    .with(new SetterTester())
                    .build();

    final PojoClassFilter[] filters =
            new PojoClassFilter[] {
                pc -> !pc.getClazz().getName().contains(KeyVaultConfigTest.class.getSimpleName()),
                pc -> !pc.getClazz().isAssignableFrom(ObjectFactory.class),
                pc -> !pc.getClazz().getName().startsWith(JaxbConfigFactory.class.getName()),
                pc -> !pc.getClazz().isAssignableFrom(ConfigException.class),
                pc -> !pc.getClazz().getName().contains(ConfigItem.class.getName()),
                pc -> !pc.getClazz().getSimpleName().contains("Test"),
                pc -> !pc.isNestedClass()
            };

    pojoValidator.validate("com.quorum.tessera.config", filters);
}
 
Example #12
Source File: AwsStsPojoTest.java    From cerberus with Apache License 2.0 6 votes vote down vote up
@Test
public void test_pojo_structure_and_behavior() {

  List<Class> classes =
      Lists.newArrayList(
          GetCallerIdentityFullResponse.class,
          GetCallerIdentityResponse.class,
          AwsStsHttpHeader.class);

  List<PojoClass> pojoClasses =
      classes.stream().map(PojoClassFactory::getPojoClass).collect(Collectors.toList());

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

  validator.validate(pojoClasses);
}
 
Example #13
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(MODEL_PACKAGE, BASE_CLASS_FILTER);

    Validator validator = ValidatorBuilder.create()
            .with(new SetterMustExistRule(),
                    new GetterMustExistRule())
            .with(new GetterTester())
            .with(new NoPublicFieldsExceptStaticFinalRule())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .with(new NoFieldShadowingRule())
            .build();
    validator.validate(pojoClasses);

    validator = ValidatorBuilder.create()
            .with(new SetterTester())
            .build();
    validator.validate(pojoClasses);
    // openpojo will do for now (seems buggy) but later would worth experimenting with pojo-tester (https://www.pojo.pl/)
}
 
Example #14
Source File: CollectionRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void exhaustiveTest() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithExhaustiveCollection.class);
  Validator pojoValidator = ValidatorBuilder.create()
      .with(new SetterMustExistRule())
      .with(new SetterTester())
      .build();

  pojoValidator.validate(pojoClass);
}
 
Example #15
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    Validator validator = ValidatorBuilder.create()
            .with(new SetterMustExistRule(),
                    new GetterMustExistRule())
            .with(new SetterTester(),
                    new GetterTester())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .build();
    validator.validate(DOMAIN_PACKAGE, new FilterPackageInfo());
}
 
Example #16
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassValidationIfChildBusinessKeysAreNotNull() {
  Validator validator = ValidatorBuilder
      .create()
      .with(new SetterTester())
      .with(new GetterTester())
      .build();

  validator.validate(PojoClassFactory.getPojoClass(AClassWithBussinessKeySet.class));
}
 
Example #17
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void end2endTest() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(classWithCredentialsDumperClass);
  Validator validator = ValidatorBuilder.create()
      .with(new GetterTester())
      .with(new SetterTester())
      .build();
  validator.validate(pojoClass);
}
 
Example #18
Source File: CollectionsTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnmodifiableCollectionsGetterReturned() {
  Validator pojoValidator = ValidatorBuilder.create()
      .with(new GetterTester())
      .with(new SetterTester())
      .build();

  PojoClass pojoClass = PojoClassFactory.getPojoClass(CollectionContainingClass.class);
  pojoValidator.validate(pojoClass);
}
 
Example #19
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void ensureClassIsDefinedCorrectly() {
  Validator validator = ValidatorBuilder.create()
      .with(new GetterMustExistRule())
      .with(new SetterMustExistRule())
      .with(new AllPimitivesAsArraysDeclaredRule())
      .with(new GetterTester())
      .with(new SetterTester())
      .build();
  validator.validate(pojoClass);
}
 
Example #20
Source File: SSLContextPropertiesTest.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Test
public void executeOpenPojoValidationsNoSetter() {

    final Validator pojoValidator = ValidatorBuilder.create()
        .with(new GetterMustExistRule())
        .with(new GetterTester())
        .with(new EqualsAndHashCodeMatchRule())
        .with(new NoPublicFieldsExceptStaticFinalRule())
        .build();

    pojoValidator.validateRecursively("com.quorum.tessera.ssl.context.model");

}
 
Example #21
Source File: DefaultIdentityEvaluatorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void SampleClassMustUseBusinessIdentity() {
  Validator pojoValidator = ValidatorBuilder.create()
      .with(new BusinessKeyMustExistRule())
      .with(new BusinessIdentityTester())
      .build();
  pojoValidator.validate(PojoClassFactory.getPojoClass(StringArrayCaseSensitive.class));
  pojoValidator.validate(PojoClassFactory.getPojoClass(StringArrayCaseInsensitive.class));
}
 
Example #22
Source File: StructuralTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotBeConstructed() {
  Validator validator = ValidatorBuilder.create()
      .with(new NonInstantiableTester())
      .build();

  for (Class<?> nonInstantiable : NON_INSTANTIABLES)
    validator.validate(getPojoClass(nonInstantiable));
}
 
Example #23
Source File: MapWithGenericsTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndToEndMap() {
  Validator pojoValidator = ValidatorBuilder.create()
      .with(new SetterMustExistRule())
      .with(new SetterTester())
      .build();

  pojoValidator.validate(PojoClassFactory.getPojoClass(ClassWithVariousGenericMap.class));
}
 
Example #24
Source File: PojoPackageTestBase.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    final Validator validator = ValidatorBuilder.create()
            // Add Testers to validate behaviour for pojoPackage
            // See com.openpojo.validation.test.impl for more ...
            .with(new SetterTester())
            .with(new GetterTester())
            .build();

    validator.validate(this.pojoPackage, this.filterChain);
}
 
Example #25
Source File: AbstractUnitTest.java    From cia with Apache License 2.0 5 votes vote down vote up
/**
 * Check all dto classes in package.
 *
 * @param string the string
 * @return true, if successful
 */
protected final boolean checkAllDtoClassesInPackage(final String string) {

	final List<PojoClass> pojoClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string,
			new FilterTestClasses());

	final Validator validator = ValidatorBuilder.create().with(new GetterMustExistRule()).with(new GetterTester())
			.with(new EqualsAndHashCodeMatchRule()).with(new InvokeToStringTester())
			.with(new InvokeHashcodeTester()).with(new DummyEqualsTester()).with(new WithTester()).build();
	validator.validate(pojoClassesRecursively);
	return true;
}
 
Example #26
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testPojoStructureAndBehavior() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(DOMAIN_PACKAGE, POJO_CLASS_FILTER);

    Validator validator = ValidatorBuilder.create()
            .with(new SetterMustExistRule(),
                    new GetterMustExistRule())
            .with(new SetterTester(),
                    new GetterTester())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .build();
    validator.validate(pojoClasses);
}
 
Example #27
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testImmutableStructureAndBehavior() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(DOMAIN_PACKAGE, IMMUTABLE_CLASS_FILTER);

    Validator validator = ValidatorBuilder.create()
            .with(new GetterMustExistRule())
            .with(new GetterTester())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .build();
    validator.validate(pojoClasses);
}
 
Example #28
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetterTypeChangerPojoStructureAndBehavior() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(DOMAIN_PACKAGE, GETTER_TYPE_CHANGER_CLASS_FILTER);

    Validator validator = ValidatorBuilder.create()
            .with(new SetterMustExistRule())
            .with(new SetterTester(),
                    new GetterTester())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .build();
    validator.validate(pojoClasses);
}
 
Example #29
Source File: ModelTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetterTypeChangerPojoStructureAndBehavior() {
    List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(DOMAIN_PACKAGE, SETTER_TYPE_CHANGER_CLASS_FILTER);

    Validator validator = ValidatorBuilder.create()
            .with(new GetterMustExistRule())
            .with(new SetterTester(),
                    new GetterTester())
            .with(new NoStaticExceptFinalRule())
            .with(new NoNestedClassRule())
            .build();
    validator.validate(pojoClasses);
}
 
Example #30
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldTestAllClasses() {
  Validator validator = ValidatorBuilder.create()
      .with(new GetterMustExistRule())
      .with(new SetterMustExistRule())
      .with(new GetterTester())
      .with(new SetterTester())
      .build();

  List<PojoClass> classes = getPojoClassesRecursively(this.getClass().getPackage().getName() + ".sample", null);

  Assert.assertThat(classes.size(), is(3));
  validator.validate(classes);
}