com.openpojo.validation.test.Tester Java Examples

The following examples show how to use com.openpojo.validation.test.Tester. 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: ParallecPojoClassTest.java    From parallec with Apache License 2.0 6 votes vote down vote up
/**
 * Unit Test the POJO classes.
 *
 * @throws ClassNotFoundException
 *             the class not found exception
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IllegalAccessException
 *             the illegal access exception
 */

@Test
public void testPojoStructureAndBehavior() throws ClassNotFoundException,
        InstantiationException, IllegalAccessException {
    final PojoValidator pojoValidator = new PojoValidator();
    for (final Class<? extends Rule> ruleClass : getValidationRules()) {
        final Rule rule = ruleClass.newInstance();
        pojoValidator.addRule(rule);
    }

    // Load tester classes
    for (Class<? extends Tester> testerClass : getTesters()) {
        final Tester testerInstance = testerClass.newInstance();
        pojoValidator.addTester(testerInstance);
    }

    for (final Class<?> c : getPOJOClasses()) {
        final PojoClass pojoClass = PojoClassFactory.getPojoClass(c);

        pojoValidator.runValidation(pojoClass);
    }
}
 
Example #2
Source File: DefaultValidatorTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void rulesAndTestersAreTriggeredWhenValidationIsRunRecursively() {
  TesterSpy testerSpy = new TesterSpy();
  List<Tester> testers = new ArrayList<Tester>();
  testers.add(testerSpy);

  RuleSpy ruleSpy = new RuleSpy();
  List<Rule> rules = new ArrayList<Rule>();
  rules.add(ruleSpy);

  FilterSpy filterSpy = new FilterSpy();

  DefaultValidator defaultValidator = new DefaultValidator(rules, testers);
  String packageName = this.getClass().getPackage().getName() + ".sample";
  List<PojoClass> validatedPojoClasses = defaultValidator.validateRecursively(packageName, filterSpy);

  Assert.assertEquals(2, validatedPojoClasses.size());
  Assert.assertTrue(validatedPojoClasses.contains(PojoClassFactory.getPojoClass(DummyClass.class)));
  Assert.assertTrue(validatedPojoClasses.contains(PojoClassFactory.getPojoClass(AnotherDummyClass.class)));

  assertInvokedClasses(testerSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName());
  assertInvokedClasses(ruleSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName());
  assertInvokedClasses(filterSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName());
}
 
Example #3
Source File: DefaultValidatorTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void rulesAndTestersAreTriggeredWhenValidation() {
  TesterSpy testerSpy = new TesterSpy();
  List<Tester> testers = new ArrayList<Tester>();
  testers.add(testerSpy);

  RuleSpy ruleSpy = new RuleSpy();
  List<Rule> rules = new ArrayList<Rule>();
  rules.add(ruleSpy);

  FilterSpy filterSpy = new FilterSpy();

  DefaultValidator defaultValidator = new DefaultValidator(rules, testers);
  String packageName = this.getClass().getPackage().getName() + ".sample";
  List<PojoClass> validatedPojoClasses = defaultValidator.validate(packageName, filterSpy);

  Assert.assertEquals(1, validatedPojoClasses.size());
  Assert.assertEquals(validatedPojoClasses.get(0), PojoClassFactory.getPojoClass(DummyClass.class));
  assertInvokedClasses(testerSpy.getInvocations(), DummyClass.class.getName());
  assertInvokedClasses(ruleSpy.getInvocations(), DummyClass.class.getName());
  assertInvokedClasses(filterSpy.getInvocations(), DummyClass.class.getName());
}
 
Example #4
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 #5
Source File: PojoValidatorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInvokeRuleOnAbstract() {
  MethodValueReturn methodValueReturn = new MethodValueReturn();
  methodValueReturn.isAbstract = true;
  RuleTesterMock ruleTesterMock = new RuleTesterMock();

  pojoValidator = ValidatorBuilder.create()
      .with((Tester) ruleTesterMock)
      .build();

  pojoValidator.validate(PojoStubFactory.getStubPojoClass(methodValueReturn));
  Assert.assertTrue("Tester not called", ruleTesterMock.runCalled);
}
 
Example #6
Source File: PojoValidatorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
private void ensureRuleInvokedTesterNotInvoked(MethodValueReturn methodValueReturn, String pojoType) {
  RuleTesterMock ruleTesterMock = new RuleTesterMock();
  pojoValidator = ValidatorBuilder.create()
      .with((Rule) ruleTesterMock)
      .with((Tester) ruleTesterMock)
      .build();
  pojoValidator.validate(PojoStubFactory.getStubPojoClass(methodValueReturn));
  Assert.assertTrue("Evaluate not run on " + pojoType + " class", ruleTesterMock.evaluateCalled);
  Assert.assertTrue("Rule called on " + pojoType + " class", !ruleTesterMock.runCalled);
}
 
Example #7
Source File: CommonCode.java    From openpojo with Apache License 2.0 5 votes vote down vote up
public static void shouldFailTesterValidation(final Tester tester, final Class<?>... failClasses) {
  for (Class<?> clazz : failClasses) {
    try {
      tester.run(PojoClassFactory.getPojoClass(clazz));
      Affirm.fail(String.format("Tester = [%s] failed to detect error while evaluating class= [%s]", tester, clazz));
    } catch (AssertionError ae) {
      if (ae.getMessage().contains("Tester = [")) {
        throw ae;
      }
    }
  }
}
 
Example #8
Source File: ValidatorBuilder.java    From openpojo with Apache License 2.0 5 votes vote down vote up
public ValidatorBuilder with(Tester... testers) {
  if (testers != null)
    for (Tester tester : testers) {
      if (tester != null)
        this.testers.add(tester);
    }
  return this;
}
 
Example #9
Source File: ValidationHelper.java    From openpojo with Apache License 2.0 5 votes vote down vote up
public static void runValidation(PojoClass pojoClass, List<Rule> rules, List<Tester> testers) {
  final Logger logger = LoggerFactory.getLogger(DefaultValidator.class);

  if (pojoClass.isSynthetic()) {
    logger.warn("Attempt to validate synthetic class=[{0}] ignored, consider using FilterSyntheticClasses filter when " +
        "calling PojoClassFactory", pojoClass.getClazz());
    return;
  }

  for (final Rule rule : rules) {
    rule.evaluate(pojoClass);
  }

  if ((pojoClass.isInterface() || pojoClass.isEnum()) && testers.size() > 0) {
    logger.warn("Attempt to execute behavioural test on non-constructable class=[{0}] ignored", pojoClass.getClazz());
    return;
  }

  try {
    for (final Tester tester : testers) {
      tester.run(pojoClass);
    }
  } catch (ASMNotLoadedException asmNotLoaded) {
    logger.warn("ASM not loaded while attempting to execute behavioural tests on non-constructable class[{0}], either " +
        "filter " + "abstract classes or add asm to your classpath.", pojoClass.getClazz());
  }
}
 
Example #10
Source File: ParallecPojoClassTest.java    From parallec with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the testers.
 *
 * @return the testers
 */
public List<Class<? extends Tester>> getTesters() {
    // Testers to validate behavior for POJO_PACKAGE
    List<Class<? extends Tester>> testers = new ArrayList<Class<? extends Tester>>();
    testers.add(SetterTester.class);
    testers.add(GetterTester.class);

    return testers;
}
 
Example #11
Source File: DefaultValidator.java    From openpojo with Apache License 2.0 4 votes vote down vote up
public DefaultValidator(List<Rule> rules, List<Tester> testers) {
  this.rules.addAll(rules);
  this.testers.addAll(testers);
}
 
Example #12
Source File: ValidatorBuilderTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void withTesters_ignoresNullArray() {
  ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with((Tester[]) null);
  Assert.assertEquals(0, validatorBuilder.getTesters().size());
}
 
Example #13
Source File: ValidatorBuilderTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void withTesters_ignoresNullArrayEntries() {
  ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with(new Tester[] { null, null });
  Assert.assertEquals(0, validatorBuilder.getTesters().size());
}
 
Example #14
Source File: ValidatorBuilderTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void withTesters_persistRules() {
  Tester anyTester = RandomFactory.getRandomValue(Tester.class);
  ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with(anyTester, null);
  Assert.assertEquals(1, validatorBuilder.getTesters().size());
}
 
Example #15
Source File: CommonCode.java    From openpojo with Apache License 2.0 4 votes vote down vote up
public static void shouldPassTesterValidation(final Tester tester, final Class<?>... passClasses) {
  for (Class<?> clazz : passClasses) {
    tester.run(PojoClassFactory.getPojoClass(clazz));
  }
}
 
Example #16
Source File: ValidatorBuilder.java    From openpojo with Apache License 2.0 4 votes vote down vote up
public List<Tester> getTesters() {
  return testers;
}
 
Example #17
Source File: SetterTesterTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
protected Tester getTester() {
  return new SetterTester();
}
 
Example #18
Source File: GetterTesterAndSetterTesterTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
private void invokeRun(final PojoClass classToTest, final Tester tester) {
  tester.run(classToTest);
}
 
Example #19
Source File: GetterTesterTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
protected Tester getTester() {
  return new GetterTester();
}
 
Example #20
Source File: PojoTest.java    From taskana with Apache License 2.0 4 votes vote down vote up
private void validateWithTester(Class<?> cl, Tester... testers) {
  ValidatorBuilder.create().with(testers).build().validate(PojoClassFactory.getPojoClass(cl));
}
 
Example #21
Source File: LoggingTesterTest.java    From openpojo with Apache License 2.0 votes vote down vote up
protected abstract Tester getTester();