/** * Resolve validation context for provided target class. * @see #addClassChecks(Class) */ public static ValidationContext resolveFor(final Class<?> target) { ValidationContext vc = new ValidationContext(); vc.addClassChecks(target); return vc; }
protected ValidationContext mockValidationContext(Map<String, List<Check>> constraints) { ValidationContext res = new ValidationContext(); res.map.putAll(constraints); return res; } }
@Test void testCollectPropertyAnnotationChecks(){ ValidationContext context = spy(new ValidationContext()); PropertyDescriptor propertyDescriptor = mock(PropertyDescriptor.class); List<Check> list = new ArrayList<>(); context.collectPropertyAnnotationChecks(list, propertyDescriptor); verify(context, never()).collectAnnotationChecks(any(List.class), any(Class.class),any(String.class), any(Annotation[].class)); }
@Test void testAddClassChecksWithCache() throws Exception { ValidationContext context = spy(new ValidationContext()); try { context.addClassChecks(ClassForCheck1.class); context.addClassChecks(ClassForCheck1.class); } finally { context.clearCache(); } //collectProperty must be invoked only for first call of addClassChecks. Two calls for two annotations verify(context, times(2)).collectPropertyAnnotationChecks(any(List.class), any(PropertyDescriptor.class)); }
@Test void testManualConfig() { ValidationContext vctx = new ValidationContext(); vctx.add(new Check("string", new MinLengthConstraint(2))); Vtor vtor = new Vtor(); vtor.validate(vctx, new Foo()); assertTrue(vtor.hasViolations()); List<Violation> vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(1, vlist.size()); Violation v = vlist.get(0); assertEquals("string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertNull(v.getCheck().getMessage()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); // new context that contains previous ValidationContext vctx2 = new ValidationContext(); vctx2.add(new Check("foo", new AssertValidConstraint(vctx))); vtor = new Vtor(); vtor.validate(vctx2, new Boo()); vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(1, vlist.size()); v = vlist.get(0); assertEquals("foo.string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); }
@Test void testManualAddViolation() { ValidationContext vctx = new ValidationContext(); vctx.add(new Check("string", new MinLengthConstraint(2))); Vtor vtor = new Vtor(); Foo foo = new Foo(); vtor.validate(vctx, foo); vtor.addViolation(new Violation("number", foo, null)); List<Violation> vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(2, vlist.size()); Violation v = vlist.get(0); assertEquals("string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); v = vlist.get(1); assertEquals("number", v.getName()); assertNull(v.getInvalidValue()); assertNull(v.getCheck()); assertNull(v.getConstraint()); } }
/** * Resolve validation context for provided target class. * @see #addClassChecks(Class) */ public static ValidationContext resolveFor(Class<?> target) { ValidationContext vc = new ValidationContext(); vc.addClassChecks(target); return vc; }