boolean first = true; for (Path.Node node : path) { if (node.isInIterable()) { sb.append('['); Object index = node.getIndex();
boolean first = true; for (Path.Node node : path) { if (node.isInIterable()) { sb.append('['); Object index = node.getIndex();
if ( p2Node.isInIterable() != p1Node.isInIterable() ) { return false;
/** * Append a Node to the specified StringBuilder. * * @param node * @param to * @return to */ public static StringBuilder appendNode(Node node, StringBuilder to) { if (node.isInIterable()) { to.append(INDEX_OPEN); if (node.getIndex() != null) { to.append(node.getIndex()); } else if (node.getKey() != null) { to.append(node.getKey()); } to.append(INDEX_CLOSE); } if (node.getName() != null) { if (to.length() > 0) { to.append(PathImpl.PROPERTY_PATH_SEPARATOR); } to.append(node.getName()); } return to; }
private static int compareIterability(Node quid, Node quo, boolean strict) { if (quid.isInIterable()) { if (quo.isInIterable()) { if (quid.getKey() != null) { return Comparator.comparing(Node::getKey, KEY_COMPARATOR).compare(quid, quo); } if (quo.getKey() != null) { return -1; } if (quid.getIndex() == null) { if (strict) { // this method cannot consistently order iterables without key or index; the first argument is // always assumed to be less: return -1; } return quo.getIndex() == null ? 0 : -1; } return quo.getIndex() == null ? 1 : quid.getIndex().compareTo(quo.getIndex()); } return 1; } return quo.isInIterable() ? -1 : 0; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); Iterator<Path.Node> iter = iterator(); boolean first = true; while ( iter.hasNext() ) { Node node = iter.next(); if ( node.isInIterable() ) { appendIndex( builder, node ); } if ( node.getName() != null ) { if ( !first ) { builder.append( PROPERTY_PATH_SEPARATOR ); } builder.append( node.getName() ); } first = false; } return builder.toString(); }
@Test @SpecAssertions({ @SpecAssertion(section = "4.2", id = "f"), @SpecAssertion(section = "4.2", id = "h"), @SpecAssertion(section = "4.2", id = "l") }) public void testPropertyPathTraversedObject() { Validator validator = TestUtil.getValidatorUnderTest(); Engine engine = new Engine(); engine.setSerialNumber( "ABCDEFGH1234" ); Set<ConstraintViolation<Engine>> constraintViolations = validator.validate( engine ); assertCorrectNumberOfViolations( constraintViolations, 1 ); ConstraintViolation<Engine> constraintViolation = constraintViolations.iterator().next(); Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator(); assertTrue( nodeIter.hasNext() ); Path.Node node = nodeIter.next(); assertEquals( node.getName(), "serialNumber" ); assertFalse( node.isInIterable() ); assertFalse( nodeIter.hasNext() ); }
@Override public String toString() { StringBuilder builder = new StringBuilder(); Iterator<Path.Node> iter = iterator(); boolean first = true; while ( iter.hasNext() ) { Node node = iter.next(); if ( node.isInIterable() ) { appendIndex( builder, node ); } if ( node.getName() != null ) { if ( !first ) { builder.append( PROPERTY_PATH_SEPARATOR ); } builder.append( node.getName() ); } first = false; } return builder.toString(); }
private void checkActorViolations(Set<ConstraintViolation<Actor>> constraintViolations) { assertCorrectNumberOfViolations( constraintViolations, 1 ); ConstraintViolation<Actor> constraintViolation = constraintViolations.iterator().next(); Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator(); assertTrue( nodeIter.hasNext() ); Path.Node node = nodeIter.next(); assertEquals( node.getName(), "playedWith" ); assertFalse( node.isInIterable() ); node = nodeIter.next(); assertEquals( node.getName(), "playedWith" ); assertTrue( node.isInIterable() ); assertEquals( node.getIndex(), new Integer( 0 ) ); node = nodeIter.next(); assertEquals( node.getName(), "lastName" ); assertTrue( node.isInIterable() ); assertEquals( node.getIndex(), new Integer( 1 ) ); assertFalse( nodeIter.hasNext() ); }
private PathExpectation(Path propertyPath) { for ( Path.Node node : propertyPath ) { Integer parameterIndex = null; if ( node.getKind() == ElementKind.PARAMETER ) { parameterIndex = node.as( Path.ParameterNode.class ).getParameterIndex(); } nodes.add( new NodeExpectation( node.getName(), node.getKind(), node.isInIterable(), node.getKey(), node.getIndex(), parameterIndex ) ); } }
/** * Create a new NodeImpl instance. * * @param node */ NodeImpl(Path.Node node) { this(node.getName()); this.inIterable = node.isInIterable(); this.index = node.getIndex(); this.key = node.getKey(); if (node instanceof NodeImpl) { final NodeImpl n = (NodeImpl) node; this.parameterIndex = n.parameterIndex; this.parameterTypes = n.parameterTypes; this.containerType = n.containerType; this.typeArgumentIndex = n.typeArgumentIndex; } }
@Test @SpecAssertions({ @SpecAssertion(section = "4.2", id = "f"), @SpecAssertion(section = "4.2", id = "g"), @SpecAssertion(section = "4.2", id = "m") }) public void testPropertyPathWithConstraintViolationForRootObject() { Validator validator = TestUtil.getValidatorUnderTest(); Set<ConstraintViolation<VerySpecialClass>> constraintViolations = validator.validate( new VerySpecialClass() ); assertCorrectNumberOfViolations( constraintViolations, 1 ); ConstraintViolation<VerySpecialClass> constraintViolation = constraintViolations.iterator().next(); Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator(); assertTrue( nodeIter.hasNext() ); Path.Node node = nodeIter.next(); assertEquals( node.getName(), null ); assertFalse( node.isInIterable() ); assertFalse( nodeIter.hasNext() ); }
/** * Create a new NodeImpl instance. * * @param node */ NodeImpl(Path.Node node) { this(node.getName()); this.inIterable = node.isInIterable(); this.index = node.getIndex(); this.key = node.getKey(); if (node instanceof NodeImpl) { final NodeImpl n = (NodeImpl) node; this.parameterIndex = n.parameterIndex; this.parameterTypes = n.parameterTypes; this.containerType = n.containerType; this.typeArgumentIndex = n.typeArgumentIndex; } }
/** * Learn whether this {@link PathImpl} points to the root of its graph. * * @return true if no child nodes */ // our implementation stores a nameless root node. public boolean isRootPath() { if (nodeList.size() != 1) { return false; } Path.Node first = nodeList.get(0); return !first.isInIterable() && first.getName() == null; }
/** * Learn whether this {@link PathImpl} points to the root of its graph. * * @return true if no child nodes */ // our implementation stores a nameless root node. public boolean isRootPath() { if (nodeList.size() != 1) { return false; } final Path.Node first = nodeList.peekFirst(); return !first.isInIterable() && first.getName() == null; }
/** * Learn whether this {@link PathImpl} points to the root of its graph. * * @return true if no child nodes */ // our implementation stores a nameless root node. public boolean isRootPath() { if (nodeList.size() != 1) { return false; } final Path.Node first = nodeList.peekFirst(); return !first.isInIterable() && first.getName() == null; }
/** * Create a new NodeImpl instance. * @param node */ NodeImpl(Path.Node node) { this.name = node.getName(); this.inIterable = node.isInIterable(); this.index = node.getIndex(); this.key = node.getKey(); }
@Override public boolean isInIterable() { boolean localInIterable = this.inIterable; for(Node node : this) { if(localInIterable) { break; } localInIterable = localInIterable || node.isInIterable(); } return localInIterable; }
private void assertNode(Path.Node actualNode, String expectedName, ElementKind expectedKind, boolean expectedInIterable, Integer expectedIndex, Object expectedKey) { assertEquals( actualNode.getName(), expectedName ); assertEquals( actualNode.getKind(), expectedKind ); assertEquals( actualNode.isInIterable(), expectedInIterable ); assertEquals( actualNode.getIndex(), expectedIndex ); assertEquals( actualNode.getKey(), expectedKey ); }
private PathExpectation(Path propertyPath) { for ( Path.Node node : propertyPath ) { Integer parameterIndex = null; if ( node.getKind() == ElementKind.PARAMETER ) { parameterIndex = node.as( Path.ParameterNode.class ).getParameterIndex(); } Class<?> containerClass = getContainerClass( node ); Integer typeArgumentIndex = getTypeArgumentIndex( node ); nodes.add( new NodeExpectation( node.getName(), node.getKind(), node.isInIterable(), node.getKey(), node.getIndex(), parameterIndex, containerClass, typeArgumentIndex ) ); } }