@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);
  }
Ejemplo n.º 2
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 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());
  }
    /**
     * 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;
 }