com.openpojo.validation.rule.Rule Java Examples
The following examples show how to use
com.openpojo.validation.rule.Rule.
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 |
/** * 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: PojoFieldPrefixedFieldsTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void unRegisteringAPrefix_reflectsOnMethodLookup() { AttributeHelper.registerFieldPrefix("m"); PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithFieldsPrefixed.class); Rule getterRule = new GetterMustExistRule(); getterRule.evaluate(pojoClass); AttributeHelper.unregisterFieldPrefix("m"); PojoCache.clear(); pojoClass = PojoClassFactory.getPojoClass(AClassWithFieldsPrefixed.class); boolean unregisterdSuccessfully = false; try { getterRule.evaluate(pojoClass); } catch (AssertionError ae) { unregisterdSuccessfully = true; } if (!unregisterdSuccessfully) Assert.fail("unregistering failed?!"); }
Example #3
Source File: DefaultValidatorTest.java From openpojo with Apache License 2.0 | 6 votes |
@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 #4
Source File: DefaultValidatorTest.java From openpojo with Apache License 2.0 | 6 votes |
@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 #5
Source File: PojoFieldPrefixedFieldsTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void registeringNullOrEmptyPrefixIsIgnored() { AttributeHelper.registerFieldPrefix(null); AttributeHelper.registerFieldPrefix(""); PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithFieldsNotPrefixed.class); Rule getterRule = new GetterMustExistRule(); getterRule.evaluate(pojoClass); AttributeHelper.registerFieldPrefix("m"); pojoClass = PojoClassFactory.getPojoClass(AClassWithFieldsPrefixed.class); getterRule.evaluate(pojoClass); }
Example #6
Source File: PojoFieldPrefixedFieldsTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void unmatchedPrefixesHaveNoEffect() { AttributeHelper.registerFieldPrefix("n"); PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithFieldsNotPrefixed.class); Rule getterRule = new GetterMustExistRule(); getterRule.evaluate(pojoClass); }
Example #7
Source File: PojoValidatorTest.java From openpojo with Apache License 2.0 | 5 votes |
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 #8
Source File: CommonCode.java From openpojo with Apache License 2.0 | 5 votes |
public static void shouldFailRuleValidation(final Rule rule, final Class<?>... failClasses) { for (Class<?> clazz : failClasses) { try { rule.evaluate(PojoClassFactory.getPojoClass(clazz)); Affirm.fail(String.format("Rule = [%s] failed to detect error while evaluating class= [%s]", rule, clazz)); } catch (AssertionError ae) { if (ae.getMessage().contains("Rule = [")) { throw ae; } } } }
Example #9
Source File: EqualsAndHashCodeMatchRuleTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void mustImplementRule() { PojoClass equalsAndHashcodeMatchRule = PojoClassFactory.getPojoClass(EqualsAndHashCodeMatchRule.class); List<PojoClass> interfaces = equalsAndHashcodeMatchRule.getInterfaces(); Assert.assertTrue("Expected interface=[Rule] to be implemented, but was not", interfaces.contains(PojoClassFactory .getPojoClass(Rule.class))); }
Example #10
Source File: ValidatorBuilderTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void build_returnsValidator() { ValidatorBuilder validatorBuilder = ValidatorBuilder.create(); validatorBuilder.with(RandomFactory.getRandomValue(Rule.class)); Object validator = validatorBuilder.build(); Assert.assertNotNull(validator); Assert.assertTrue(Validator.class.isAssignableFrom(validator.getClass())); }
Example #11
Source File: ValidatorBuilder.java From openpojo with Apache License 2.0 | 5 votes |
public ValidatorBuilder with(Rule... rules) { if (rules != null) for (Rule rule : rules) { if (rule != null) this.rules.add(rule); } return this; }
Example #12
Source File: ValidationHelper.java From openpojo with Apache License 2.0 | 5 votes |
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 #13
Source File: ValidatorBuilderTest.java From openpojo with Apache License 2.0 | 4 votes |
@Test public void withRules_persistRules() { Rule anyRule = RandomFactory.getRandomValue(Rule.class); ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with(anyRule, null); Assert.assertEquals(1, validatorBuilder.getRules().size()); }
Example #14
Source File: ValidatorBuilderTest.java From openpojo with Apache License 2.0 | 4 votes |
@Test public void withRules_ignoresNullArrayEntries() { ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with(new Rule[] { null, null }); Assert.assertEquals(0, validatorBuilder.getRules().size()); }
Example #15
Source File: CommonCode.java From openpojo with Apache License 2.0 | 4 votes |
public static void shouldPassRuleValidation(final Rule rule, final Class<?>... passClasses) { for (Class<?> clazz : passClasses) { rule.evaluate(PojoClassFactory.getPojoClass(clazz)); } }
Example #16
Source File: ValidatorBuilderTest.java From openpojo with Apache License 2.0 | 4 votes |
@Test public void withRules_ignoresNullArray() { ValidatorBuilder validatorBuilder = ValidatorBuilder.create().with((Rule[]) null); Assert.assertEquals(0, validatorBuilder.getRules().size()); }
Example #17
Source File: DefaultValidator.java From openpojo with Apache License 2.0 | 4 votes |
public DefaultValidator(List<Rule> rules, List<Tester> testers) { this.rules.addAll(rules); this.testers.addAll(testers); }
Example #18
Source File: ValidatorBuilder.java From openpojo with Apache License 2.0 | 4 votes |
public List<Rule> getRules() { return rules; }
Example #19
Source File: ParallecPojoClassTest.java From parallec with Apache License 2.0 | 4 votes |
/** * Gets the validation rules. * * @return the validation rules */ public List<Class<? extends Rule>> getValidationRules() { List<Class<? extends Rule>> ruleClasses = new ArrayList<Class<? extends Rule>>(); return ruleClasses; }
Example #20
Source File: PojoTest.java From taskana with Apache License 2.0 | 4 votes |
private void validateWithRules(Class<?> cl, Rule... rules) { ValidatorBuilder.create().with(rules).build().validate(PojoClassFactory.getPojoClass(cl)); }