@Override public boolean isValid(final HelperBean bean, final ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); // build a custom property path ConstraintViolationBuilder builder = context.buildConstraintViolationWithTemplate(bean.getMessage()); NodeBuilderCustomizableContext nodeBuilder = null; for (String part : bean.getPath().split("\\.")) { if (nodeBuilder == null) { nodeBuilder = builder.addPropertyNode(part); } else { nodeBuilder = nodeBuilder.addPropertyNode(part); } } if (nodeBuilder != null) { nodeBuilder.addConstraintViolation(); } return false; }
@Test public void shouldAscertainPropertyUniqueness() { final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class); when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context); when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context); when(context.getDefaultConstraintMessageTemplate()).thenReturn("template"); final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder); when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class)); assertThat(validator.isValid(connection, context)).isEqualTo(validity); }
/** * JAVADOC Method Level Comments */ public void testInvalid() { UsernameValidator validator = new UsernameValidator(); ValidUsername vu = mock(ValidUsername.class); when(vu.message()).thenReturn("Oops"); validator.initialize(vu); UsernameValidatingPlugin plugin = mock(UsernameValidatingPlugin.class); when(plugin.isValid("username")).thenReturn(false); // TODO autowire plugin ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder cvb = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate("Oops")).thenReturn(cvb); NodeBuilderCustomizableContext nbdc = mock(NodeBuilderCustomizableContext.class); when(cvb.addPropertyNode("username")).thenReturn(nbdc); when(nbdc.addConstraintViolation()).thenReturn(context); assertFalse("Should be unique", validator.isValid("username", context)); verify(context).buildConstraintViolationWithTemplate("Oops"); verify(nbdc).addConstraintViolation(); verify(context).disableDefaultConstraintViolation(); }
@Test public void test() { final ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); final NodeBuilderCustomizableContext node = mock(NodeBuilderCustomizableContext.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); when(builder.addPropertyNode(anyString())).thenReturn(node); mockCountToReturn(2l); final ConstraintValidator<Unique, Object> validator = new UniqueValidator(); final Unique annotation = Model2.class.getAnnotation(Unique.class); validator.initialize(annotation); assertFalse(validator.isValid("somevalue", context)); }
@Override public NodeBuilderCustomizableContext addPropertyNode(final String pname) { this.context.basePath.addPropertyNode(pname); return new NodeBuilderCustomizableContextImpl(this, this.messageTemplate, this.context.basePath); }
@Override public NodeBuilderCustomizableContext addNode(final String name) { this.path.addPropertyNode(name); return new NodeBuilderCustomizableContextImpl(this.parent, this.messageTemplate, this.path); }
@Override public NodeBuilderCustomizableContext addPropertyNode(final String pname) { this.path.addPropertyNode(pname); return new NodeBuilderCustomizableContextImpl(this.parent, this.messageTemplate, this.path); }
@Override public NodeBuilderCustomizableContext inContainer(final Class<?> containerClass, final Integer typeArgumentIndex) { throw new UnsupportedOperationException( "GWT Validation does not support inContainer(Class, Integer)."); }