Ejemplo n.º 1
0
 @SuppressWarnings("rawtypes")
 private String getPropertyName(ConstraintViolation violation) {
   Iterator iterator = violation.getPropertyPath().iterator();
   Path.Node property = (Path.Node) iterator.next();
   while (iterator.hasNext()) {
     property = (Path.Node) iterator.next();
   }
   return property.getName();
 }
 @Test
 public void testNonStringMapKey() {
   Validator validator = ValidatorUtil.getValidator();
   Container container = new Container();
   Key id = new Key();
   container.addItem(id, new Item(null));
   Set<ConstraintViolation<Container>> constraintViolations = validator.validate(container);
   assertNumberOfViolations(constraintViolations, 1);
   ConstraintViolation<Container> violation = constraintViolations.iterator().next();
   Path path = violation.getPropertyPath();
   Iterator<Path.Node> iter = path.iterator();
   iter.next();
   Path.Node node = iter.next();
   assertNotNull(node);
   assertTrue(node.isInIterable());
   assertEquals(node.getKey(), id);
 }
  private String getMessage(
      Member member, Object[] args, Set<? extends ConstraintViolation<?>> violations) {

    StringBuilder message = new StringBuilder();
    message.append(violations.size());
    message.append(" constraint violation(s) occurred during method validation.");
    message.append("\nConstructor or Method: ");
    message.append(member);
    message.append("\nArgument values: ");
    message.append(Arrays.toString(args));
    message.append("\nConstraint violations: ");

    int i = 1;
    for (ConstraintViolation<?> constraintViolation : violations) {
      Path.Node leafNode = getLeafNode(constraintViolation);

      message.append("\n (");
      message.append(i);
      message.append(")");
      message.append(" Kind: ");
      message.append(leafNode.getKind());
      if (leafNode.getKind() == ElementKind.PARAMETER) {
        message.append("\n parameter index: ");
        message.append(leafNode.as(Path.ParameterNode.class).getParameterIndex());
      }
      message.append("\n message: ");
      message.append(constraintViolation.getMessage());
      message.append("\n root bean: ");
      message.append(constraintViolation.getRootBean());
      message.append("\n property path: ");
      message.append(constraintViolation.getPropertyPath());
      message.append("\n constraint: ");
      message.append(constraintViolation.getConstraintDescriptor().getAnnotation());

      i++;
    }

    return message.toString();
  }
    /**
     * Called only if isReachable returns true
     *
     * @return false for any associatons of root object being validated true otherwise
     */
    public boolean isCascadable(
        Object traversableObject,
        Path.Node traversableProperty,
        Class<?> rootBeanType,
        Path pathToTraversableObject,
        ElementType elementType) {
      boolean cascadable = true;
      if (isRootObjectPath(pathToTraversableObject)) {
        String attributeName =
            traversableProperty
                .getName(); // Refer to section 4.2 of Bean Validation spec for more details about
                            // Path
        DatabaseMapping mapping = getMappingForAttributeName(attributeName);
        if (mapping != null && mapping.isForeignReferenceMapping()) {
          cascadable = false;
        }
      }

      return cascadable;
    }
 /** @return false for any lazily loaded property of root object being validated */
 public boolean isReachable(
     Object traversableObject,
     Path.Node traversableProperty,
     Class<?> rootBeanType,
     Path pathToTraversableObject,
     ElementType elementType) {
   boolean reachable = true;
   String attributeName = null;
   if (isRootObjectPath(pathToTraversableObject)) {
     attributeName =
         traversableProperty
             .getName(); // Refer to section 4.2 of Bean Validation spec for more details about
                         // Path.Node
     DatabaseMapping mapping = getMappingForAttributeName(attributeName);
     if (mapping != null) {
       if (mapping.isForeignReferenceMapping()) {
         // For lazy relationships check whether it is instantiated
         if (mapping.isLazy()) {
           Object attributeValue =
               mapping.getAttributeAccessor().getAttributeValueFromObject(traversableObject);
           reachable =
               ((ForeignReferenceMapping) mapping)
                   .getIndirectionPolicy()
                   .objectIsInstantiatedOrChanged(attributeValue);
         }
       } else {
         // For lazy non relationship attributes, check whether it is fetched
         FetchGroupManager fetchGroupManager = descriptor.getFetchGroupManager();
         if (fetchGroupManager != null) {
           reachable = fetchGroupManager.isAttributeFetched(traversableObject, attributeName);
         }
       }
     }
   }
   return reachable;
 }
  @Test
  public void testParseMapBasedProperty() {
    String property = "order[foo].deliveryAddress";
    Path path = PathImpl.createPathFromString(property);
    Iterator<Path.Node> propIter = path.iterator();

    assertTrue(propIter.hasNext());
    Path.Node elem = propIter.next();
    assertEquals("order", elem.getName());
    assertFalse(elem.isInIterable());

    assertTrue(propIter.hasNext());
    elem = propIter.next();
    assertEquals("deliveryAddress", elem.getName());
    assertTrue(elem.isInIterable());
    assertEquals("foo", elem.getKey());

    assertFalse(propIter.hasNext());
  }
  @Test
  public void testParsing() {
    String property = "orders[3].deliveryAddress.addressline[1]";
    Path path = PathImpl.createPathFromString(property);
    Iterator<Path.Node> propIter = path.iterator();

    assertTrue(propIter.hasNext());
    Path.Node elem = propIter.next();
    assertEquals(elem.getName(), "orders");
    assertFalse(elem.isInIterable());

    assertTrue(propIter.hasNext());
    elem = propIter.next();
    assertEquals(elem.getName(), "deliveryAddress");
    assertTrue(elem.isInIterable());
    assertEquals(elem.getIndex(), new Integer(3));

    assertTrue(propIter.hasNext());
    elem = propIter.next();
    assertEquals(elem.getName(), "addressline");
    assertFalse(elem.isInIterable());

    assertTrue(propIter.hasNext());
    elem = propIter.next();
    assertEquals(elem.getName(), null);
    assertTrue(elem.isInIterable());
    assertEquals(elem.getIndex(), new Integer(1));

    assertFalse(propIter.hasNext());

    assertEquals(path.toString(), property);
  }