Java 类javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder 实例源码

项目:nexus-public    文件:ConstraintViolationFactory.java   
@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;
}
项目:lab-insurance    文件:OrderValidator.java   
@Override
public boolean isValid(Order order, ConstraintValidatorContext ctx) {
    if (order.getProcessInfo() == null) {
        order.setProcessInfo(new OrderProcessInfo());
    }
    List<ConstraintViolationBuilder> errors = new ArrayList<ConstraintViolationBuilder>();
    addIfNotNull(errors, checkNotNullValue(order.getDates(), "order.validation.missingDates", ctx));
    addIfNotNull(errors, checkNotNullValue(order.getType(), "order.validation.missingType", ctx));
    if (order.getDates() != null) {
        addIfNotNull(errors, checkNotNullValue(order.getDates().getEffective(), "order.validation.missingEffectiveDate", ctx));
    }
    if (order.getType() != null) {
        switch (order.getType()) {
        case ADDITIONAL_PAYMENT:
        case INITIAL_PAYMENT:
        case REGULAR_PAYMENT:
            validatePayment(order, ctx, errors);
            break;
        default:
            break;
        }
    }
    if (errors.isEmpty()) {
        return true;
    } else {
        for (ConstraintViolationBuilder i : errors) {
            i.addConstraintViolation();
        }
        return false;
    }
}
项目:syndesis    文件:UniquePropertyValidatorTest.java   
@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);
}
项目:syndesis-rest    文件:UniquePropertyValidatorTest.java   
@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);
}
项目:opencucina    文件:UsernameValidatorTest.java   
/**
 * 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();
}
项目:lab-insurance    文件:AbstractValidator.java   
protected ConstraintViolationBuilder checkPositiveValue(BigDecimal value, String message, ConstraintValidatorContext ctx) {
    ConstraintViolationBuilder result = null;
    if (value != null && value.compareTo(BigDecimal.ZERO) < 0) {
        result = ctx.buildConstraintViolationWithTemplate(message);
    }
    return result;
}
项目:lab-insurance    文件:AbstractValidator.java   
protected ConstraintViolationBuilder checkNullValue(Object entity, String message, ConstraintValidatorContext ctx) {
    ConstraintViolationBuilder result = null;
    if (entity != null) {
        result = ctx.buildConstraintViolationWithTemplate(message);
    }
    return result;
}
项目:lab-insurance    文件:AbstractValidator.java   
protected ConstraintViolationBuilder checkNotNullValue(Object entity, String message, ConstraintValidatorContext ctx) {
    ConstraintViolationBuilder result = null;
    if (entity == null) {
        result = ctx.buildConstraintViolationWithTemplate(message);
    }
    return result;
}
项目:unique-validator    文件:UniqueValidatorTest.java   
@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));
}
项目:cloudbreak    文件:AbstractValidatorTest.java   
ConstraintViolationBuilder getConstraintViolationBuilder() {
    return new ConstraintValidatorContextImpl(
            Collections.emptyList(), null,
            PathImpl.createRootPath(),
            new DummyConstraintDescriptor()
    ).buildConstraintViolationWithTemplate("dummytemplate");
}
项目:sejda    文件:SingleOutputValidatorTest.java   
@Test
public void destinationExistsPolicyFail() throws IOException {
    ConstraintValidatorContext context = mock(ConstraintValidatorContext.class);
    ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder);
    SetMetadataParameters params = new SetMetadataParameters();
    params.setOutput(new FileTaskOutput(folder.newFile()));
    params.setExistingOutputPolicy(ExistingOutputPolicy.FAIL);
    assertFalse(victim.isValid(params, context));
    verify(context).buildConstraintViolationWithTemplate(contains("File destination already exists"));
}
项目:sejda    文件:SingleOutputValidatorTest.java   
@Test
public void destinationExistsPolicySkip() throws IOException {
    ConstraintValidatorContext context = mock(ConstraintValidatorContext.class);
    ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder);
    SetMetadataParameters params = new SetMetadataParameters();
    params.setOutput(new FileTaskOutput(folder.newFile()));
    params.setExistingOutputPolicy(ExistingOutputPolicy.SKIP);
    assertFalse(victim.isValid(params, context));
    verify(context).buildConstraintViolationWithTemplate(contains("File destination already exists"));
}
项目:sejda    文件:PdfVersionValidatorTest.java   
@Test
public void testInvalidValue() {
    ConstraintValidatorContext context = mock(ConstraintValidatorContext.class);
    ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder);
    NodeBuilderDefinedContext nodeBuilderContext = mock(NodeBuilderDefinedContext.class);
    when(builder.addNode(anyString())).thenReturn(nodeBuilderContext);
    when(params.getVersion()).thenReturn(PdfVersion.VERSION_1_2);
    when(params.getMinRequiredPdfVersion()).thenReturn(PdfVersion.VERSION_1_5);
    assertFalse(victim.isValid(params, context));
    verify(context).buildConstraintViolationWithTemplate(anyString());
}
项目:lab-insurance    文件:MarketOrderValidator.java   
@Override
public boolean isValid(MarketOrder entity, ConstraintValidatorContext ctx) {
    List<ConstraintViolationBuilder> errors = new ArrayList<ConstraintViolationBuilder>();
    if (entity.getSource() == MarketOrderSource.AMOUNT
            && (entity.getGrossAmount() == null || entity.getGrossAmount().equals(BigDecimal.ZERO))) {
        errors.add(ctx.buildConstraintViolationWithTemplate("marketOrder.validation.missingAmount"));
    }
    else if (entity.getSource() == MarketOrderSource.UNITS
            && (entity.getUnits() == null || entity.getUnits().equals(BigDecimal.ZERO))) {
        errors.add(ctx.buildConstraintViolationWithTemplate("marketOrder.validation.missingUnits"));
    }
    addIfNotNull(errors,
            checkPositiveValue(entity.getGrossAmount(), "marketOrder.validation.negativeGrossAmount", ctx));
    addIfNotNull(errors,
            checkPositiveValue(entity.getNetAmount(), "marketOrder.validation.negativeNetAmount", ctx));
    addIfNotNull(errors, checkPositiveValue(entity.getUnits(), "marketOrder.validation.negativeUnits", ctx));
    addIfNotNull(errors, checkPositiveValue(entity.getNav(), "marketOrder.validation.negativeNav", ctx));
    // Comprobamos la integridad de las fechas y los estados
    if (entity.getCurrentState() != null && entity.getDates() != null) {
        MarketOrder.States state = MarketOrder.States.valueOf(entity.getCurrentState().getCode());
        switch (state) {
        case INITIAL:
            addIfNotNull(errors, checkNullValue(entity.getDates().getProcessed(),
                    Validation.StateInitialWithProcessedDate, ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getValued(),
                    "marketOrder.validation.stateInitialWithValuedDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(),
                    "marketOrder.validation.stateInitialWithAcountedDate", ctx));
            break;
        case PROCESSED:
            addIfNotNull(errors, checkNullValue(entity.getDates().getProcessed(),
                    "marketOrder.validation.stateInitialWithProcessedDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getValued(),
                    "marketOrder.validation.stateInitialWithValuedDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(),
                    "marketOrder.validation.stateInitialWithAcountedDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(),
                    "marketOrder.validation.stateProcessedWithoutProcessedDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(),
                    "marketOrder.validation.stateProcessedWithoutValueDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(),
                    "marketOrder.validation.stateProcessedWithAcountedDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getValued(),
                    "marketOrder.validation.stateProcessedWithValuedDate", ctx));
            break;
        case VALUED:
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(),
                    "marketOrder.validation.stateValuedWithoutProcessedDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(),
                    "marketOrder.validation.stateValuedWithoutValueDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getValued(),
                    "marketOrder.validation.stateValuedWithoutValuedDate", ctx));
            addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(),
                    "marketOrder.validation.stateValuedWithAccountedDate", ctx));
            break;
        case ACCOUNTED:
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(),
                    "marketOrder.validation.stateAccountedWithoutProcessedDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(),
                    "marketOrder.validation.stateAccountedWithoutValueDate", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getValued(),
                    "marketOrder.validation.stateAccountedWithoutValued", ctx));
            addIfNotNull(errors, checkNotNullValue(entity.getDates().getAccounted(),
                    "marketOrder.validation.stateAccountedWithoutAccountedDate", ctx));
            break;
        default:
            break;
        }
    }
    if (errors.isEmpty()) {
        return true;
    }
    else {
        for (ConstraintViolationBuilder builder : errors) {
            builder.addConstraintViolation();

        }
        return false;
    }
}
项目:lab-insurance    文件:OrderValidator.java   
private void validatePayment(Order order, ConstraintValidatorContext ctx, List<ConstraintViolationBuilder> errors) {
    if (order.getBuyDistribution() == null || order.getBuyDistribution().isEmpty()) {
        errors.add(ctx.buildConstraintViolationWithTemplate("order.validation.missingBuyDistribution"));
    }
}