Author: jverhaeg(a)redhat.com
Date: 2008-06-02 16:19:51 -0400 (Mon, 02 Jun 2008)
New Revision: 234
Modified:
trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/PathFactory.java
trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/PathValueFactory.java
trunk/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicPathTest.java
Log:
DNA-79: Changed both PathFactory (interface) and PathValueFactory (impl) to have create()
and createRelativePath() methods.
DNA-93: Added missing createSegment(String, TextEncoder) method by moving code from
createSegment(String). Also modified method to correctly build indexed segments from
string.
DNA-95: Changed create method to throw correct exceptions
Modified: trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/PathFactory.java
===================================================================
--- trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/PathFactory.java 2008-06-02
20:12:41 UTC (rev 233)
+++ trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/PathFactory.java 2008-06-02
20:19:51 UTC (rev 234)
@@ -2,7 +2,7 @@
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
+ * distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
@@ -21,110 +21,159 @@
*/
package org.jboss.dna.spi.graph;
+import org.jboss.dna.common.text.TextEncoder;
+
/**
* A factory for creating {@link Path paths}. This interface extends the {@link
ValueFactory} generic interface and adds specific
* methods for creating paths (and relative paths) from a series of names, segments, or
combinations.
+ *
* @author Randall Hauch
+ * @author John Verhaeg
*/
public interface PathFactory extends ValueFactory<Path> {
- /**
- * Create an absolute path with the supplied segment names, in order. If no segments
are provided, the result will be the root
- * path.
- * @param segmentNames the names of the segments
- * @return the new path
- * @throws IllegalArgumentException if at least one segment name is provided and if
any of the supplied segment names are null
- */
- Path create( Name... segmentNames );
+ /**
+ * Create an absolute root path. Subsequent calls will always return the same instance.
+ *
+ * @return the new path
+ */
+ Path create();
- /**
- * Create an absolute path with the supplied segments, in order. If no segments are
provided, the result will be the root
- * path.
- * @param segments the segments
- * @return the new path
- * @throws IllegalArgumentException if at least one segment is provided and if any of
the supplied segments are null
- */
- Path create( Path.Segment... segments );
+ /**
+ * Create an absolute path with the supplied segment names, in order. If no segments are
provided, the result will be the root
+ * path.
+ *
+ * @param segmentNames the names of the segments
+ * @return the new path
+ * @throws IllegalArgumentException if at least one segment name is provided and if any
of the supplied segment names are null
+ */
+ Path create( Name... segmentNames );
- /**
- * Create a relative path with the supplied segment names, in order. If no segments
are provided, the result will be the root
- * path.
- * @param segmentNames the names of the segments
- * @return the new path
- * @throws IllegalArgumentException if at least one segment name is provided and if
any of the supplied segment names are null
- */
- Path createRelativePath( Name... segmentNames );
+ /**
+ * Create an absolute path with the supplied segments, in order. If no segments are
provided, the result will be the root
+ * path.
+ *
+ * @param segments the segments
+ * @return the new path
+ * @throws IllegalArgumentException if at least one segment is provided and if any of
the supplied segments are null
+ */
+ Path create( Path.Segment... segments );
- /**
- * Create a relative path with the supplied segments, in order. If no segments are
provided, the result will be the root path.
- * @param segments the segments
- * @return the new path
- * @throws IllegalArgumentException if at least one segment is provided and if any of
the supplied segments are null
- */
- Path createRelativePath( Path.Segment... segments );
+ /**
+ * Create an empty relative path (i.e., equivalent to {@link
#createRelativePath(Path.Segment...) createRelativePath}({@link Path#SELF_SEGMENT})).
+ * Subsequent calls will always return the same instance.
+ *
+ * @return the new path
+ */
+ Path createRelativePath();
- /**
- * Create a path by appending the supplied names to the parent path.
- * @param parentPath the path that is to provide the basis for the new path
- * @param segmentName the name of the segment to be appended to the parent path
- * @param index the index for the new segment
- * @return the new path
- * @throws IllegalArgumentException if the parent path reference or the segment name
is null, or if the index is invalid
- */
- Path create( Path parentPath, Name segmentName, int index );
+ /**
+ * Create a relative path with the supplied segment names, in order. If no segments are
provided, the result will be the root
+ * path.
+ *
+ * @param segmentNames the names of the segments
+ * @return the new path
+ * @throws IllegalArgumentException if at least one segment name is provided and if any
of the supplied segment names are null
+ */
+ Path createRelativePath( Name... segmentNames );
- /**
- * Create a path by appending the supplied names to the parent path. If no names are
appended, the parent path is returned.
- * @param parentPath the path that is to provide the basis for the new path
- * @param segmentNames the names of the segments that are to be appended, in order,
to the parent path
- * @return the new path
- * @throws IllegalArgumentException if the parent path reference is null, or if at
least one segment name is provided and if
- * any of the supplied segment names are null
- */
- Path create( Path parentPath, Name... segmentNames );
+ /**
+ * Create a relative path with the supplied segments, in order. If no segments are
provided, the result will be the root path.
+ *
+ * @param segments the segments
+ * @return the new path
+ * @throws IllegalArgumentException if at least one segment is provided and if any of
the supplied segments are null
+ */
+ Path createRelativePath( Path.Segment... segments );
- /**
- * Create a path by appending the supplied names to the parent path. If no names are
appended, the parent path is returned.
- * @param parentPath the path that is to provide the basis for the new path
- * @param segments the segments that are to be appended, in order, to the parent
path
- * @return the new path
- * @throws IllegalArgumentException if the parent path reference is null, or if at
least one segment name is provided and if
- * any of the supplied segment names are null
- */
- Path create( Path parentPath, Path.Segment... segments );
+ /**
+ * Create a path by appending the supplied names to the parent path.
+ *
+ * @param parentPath the path that is to provide the basis for the new path
+ * @param segmentName the name of the segment to be appended to the parent path
+ * @param index the index for the new segment
+ * @return the new path
+ * @throws IllegalArgumentException if the parent path reference or the segment name is
null, or if the index is invalid
+ */
+ Path create( Path parentPath,
+ Name segmentName,
+ int index );
- /**
- * Create a path segment given the supplied segment name. The resulting segment will
have no index.
- * @param segmentName the name of the segment
- * @return the segment
- * @throws IllegalArgumentException if the segment name reference is null
- */
- Path.Segment createSegment( String segmentName );
+ /**
+ * Create a path by appending the supplied names to the parent path. If no names are
appended, the parent path is returned.
+ *
+ * @param parentPath the path that is to provide the basis for the new path
+ * @param segmentNames the names of the segments that are to be appended, in order, to
the parent path
+ * @return the new path
+ * @throws IllegalArgumentException if the parent path reference is null, or if at least
one segment name is provided and if
+ * any of the supplied segment names are null
+ */
+ Path create( Path parentPath,
+ Name... segmentNames );
- /**
- * Create a path segment given the supplied segment name and index.
- * @param segmentName the name of the new segment
- * @param index the index of the new segment
- * @return the segment
- * @throws IllegalArgumentException if the segment name reference is null or if the
index is invalid
- */
- Path.Segment createSegment( String segmentName, int index );
+ /**
+ * Create a path by appending the supplied names to the parent path. If no names are
appended, the parent path is returned.
+ *
+ * @param parentPath the path that is to provide the basis for the new path
+ * @param segments the segments that are to be appended, in order, to the parent path
+ * @return the new path
+ * @throws IllegalArgumentException if the parent path reference is null, or if at least
one segment name is provided and if
+ * any of the supplied segment names are null
+ */
+ Path create( Path parentPath,
+ Path.Segment... segments );
- /**
- * Create a path segment given the supplied segment name. The resulting segment will
have no index.
- * @param segmentName the name of the segment
- * @return the segment
- * @throws IllegalArgumentException if the segment name reference is null
- */
- Path.Segment createSegment( Name segmentName );
+ /**
+ * Create a path segment given the supplied segment name. The resulting segment will
have no index.
+ *
+ * @param segmentName the name of the segment
+ * @return the segment
+ * @throws IllegalArgumentException if the segment name reference is
<code>null</code> or the value could not be created
+ * from the supplied string
+ */
+ Path.Segment createSegment( String segmentName );
- /**
- * Create a path segment given the supplied segment name and index.
- * @param segmentName the name of the new segment
- * @param index the index of the new segment
- * @return the segment
- * @throws IllegalArgumentException if the segment name reference is null or if the
index is invalid
- */
- Path.Segment createSegment( Name segmentName, int index );
+ /**
+ * Create a path segment given the supplied segment name. The resulting segment will
have no index.
+ *
+ * @param segmentName the name of the segment
+ * @param encoder the encoder that should be used to decode the qualified name
+ * @return the segment
+ * @throws IllegalArgumentException if the segment name reference is
<code>null</code> or the value could not be created
+ * from the supplied string
+ */
+ Path.Segment createSegment( String segmentName,
+ TextEncoder encoder );
+ /**
+ * Create a path segment given the supplied segment name and index.
+ *
+ * @param segmentName the name of the new segment
+ * @param index the index of the new segment
+ * @return the segment
+ * @throws IllegalArgumentException if the segment name reference is
<code>null</code> or if the index is invalid
+ */
+ Path.Segment createSegment( String segmentName,
+ int index );
+
+ /**
+ * Create a path segment given the supplied segment name. The resulting segment will
have no index.
+ *
+ * @param segmentName the name of the segment
+ * @return the segment
+ * @throws IllegalArgumentException if the segment name reference is null
+ */
+ Path.Segment createSegment( Name segmentName );
+
+ /**
+ * Create a path segment given the supplied segment name and index.
+ *
+ * @param segmentName the name of the new segment
+ * @param index the index of the new segment
+ * @return the segment
+ * @throws IllegalArgumentException if the segment name reference is null or if the
index is invalid
+ */
+ Path.Segment createSegment( Name segmentName,
+ int index );
+
}
Modified: trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/PathValueFactory.java
===================================================================
---
trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/PathValueFactory.java 2008-06-02
20:12:41 UTC (rev 233)
+++
trunk/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/PathValueFactory.java 2008-06-02
20:19:51 UTC (rev 234)
@@ -2,7 +2,7 @@
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
+ * distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
@@ -35,7 +35,6 @@
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.common.util.ArgCheck;
import org.jboss.dna.spi.SpiI18n;
-import org.jboss.dna.spi.graph.InvalidPathException;
import org.jboss.dna.spi.graph.Name;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.PathFactory;
@@ -47,373 +46,453 @@
/**
* The standard {@link ValueFactory} for {@link PropertyType#NAME} values.
+ *
* @author Randall Hauch
+ * @author John Verhaeg
*/
@Immutable
public class PathValueFactory extends AbstractValueFactory<Path> implements
PathFactory {
- protected static final char DELIMITER = Path.DELIMITER;
+ /**
+ * Regular expression used to identify the different segments in a path, using the
standard '/' delimiter. The expression is
+ * simply:
+ *
+ * <pre>
+ * /
+ * </pre>
+ */
+ protected static final Pattern DELIMITER_PATTERN = Pattern.compile("/");
- /**
- * Regular expression used to identify the different segments in a path, using the
standard '/' delimiter. The expression is
- * simply:
- *
- * <pre>
- * /
- * </pre>
- */
- protected static final Pattern DELIMITER_PATTERN = Pattern.compile("/");
+ /**
+ * Regular expression used to identify the different parts of a segment. The expression
is
+ *
+ * <pre>
+ * ([ˆ*:/\[\]|]+)(:([ˆ*:/\[\]|]+))?(\[(\d+)])?
+ * </pre>
+ *
+ * where the first part is accessed with group 1, the second part is accessed with group
3, and the index is accessed with
+ * group 5.
+ */
+ protected static final Pattern SEGMENT_PATTERN =
Pattern.compile("([^:/]+)(:([^/\\[\\]]+))?(\\[(\\d+)])?");
- /**
- * Regular expression used to identify the different parts of a segment. The
expression is
- *
- * <pre>
- * ([ˆ*:/\[\]|]+)(:([ˆ*:/\[\]|]+))?(\[(\d+)])?
- * </pre>
- *
- * where the first part is accessed with group 1, the second part is accessed with
group 3, and the index is accessed with
- * group 5.
- */
- protected static final Pattern SEGMENT_PATTERN =
Pattern.compile("([^:/]+)(:([^/\\[\\]]+))?(\\[(\\d+)])?");
+ private final ValueFactory<Name> nameValueFactory;
- private final ValueFactory<Name> nameValueFactory;
+ public PathValueFactory( TextEncoder encoder,
+ ValueFactory<String> stringValueFactory,
+ ValueFactory<Name> nameValueFactory ) {
+ super(PropertyType.PATH, encoder, stringValueFactory);
+ ArgCheck.isNotNull(nameValueFactory, "nameValueFactory");
+ this.nameValueFactory = nameValueFactory;
+ }
- public PathValueFactory( TextEncoder encoder, ValueFactory<String>
stringValueFactory, ValueFactory<Name> nameValueFactory ) {
- super(PropertyType.PATH, encoder, stringValueFactory);
- ArgCheck.isNotNull(nameValueFactory, "nameValueFactory");
- this.nameValueFactory = nameValueFactory;
- }
+ /**
+ * @return nameValueFactory
+ */
+ protected ValueFactory<Name> getNameValueFactory() {
+ return this.nameValueFactory;
+ }
- /**
- * @return nameValueFactory
- */
- protected ValueFactory<Name> getNameValueFactory() {
- return this.nameValueFactory;
- }
+ /**
+ * <p>
+ * {@inheritDoc}
+ * </p>
+ *
+ * @see org.jboss.dna.spi.graph.PathFactory#create()
+ */
+ public Path create() {
+ return BasicPath.ROOT;
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( String value ) {
- return create(value, getEncoder());
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( String value ) {
+ return create(value, getEncoder());
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( final String value, TextEncoder decoder ) throws
ValueFormatException {
- if (value == null) return null;
- if (decoder == null) decoder = getEncoder();
- String trimmedValue = value.trim();
- int length = trimmedValue.length();
- boolean absolute = false;
- if (length == 0) {
- return BasicPath.ROOT;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( final String value,
+ TextEncoder decoder ) throws ValueFormatException {
+ if (value == null) return null;
+ if (decoder == null) decoder = getEncoder();
+ String trimmedValue = value.trim();
+ int length = trimmedValue.length();
+ boolean absolute = false;
+ if (length == 0) {
+ return BasicPath.ROOT;
+ }
- // Remove the leading delimiter ...
- if (trimmedValue.charAt(0) == DELIMITER) {
- trimmedValue = length > 1 ? trimmedValue.substring(1) : "";
- --length;
- absolute = true;
- }
- // remove the trailing delimiter ...
- if (length > 0 && trimmedValue.charAt(length - 1) == DELIMITER) {
- trimmedValue = length > 1 ? trimmedValue.substring(0, length - 1) :
"";
- length = trimmedValue.length();
- }
- if (length == 0) {
- return BasicPath.ROOT;
- }
+ // Remove the leading delimiter ...
+ if (trimmedValue.charAt(0) == Path.DELIMITER) {
+ trimmedValue = length > 1 ? trimmedValue.substring(1) : "";
+ --length;
+ absolute = true;
+ }
+ // remove the trailing delimiter ...
+ if (length > 0 && trimmedValue.charAt(length - 1) == Path.DELIMITER) {
+ trimmedValue = length > 1 ? trimmedValue.substring(0, length - 1) : "";
+ length = trimmedValue.length();
+ }
+ if (length == 0) {
+ return BasicPath.ROOT;
+ }
- // Parse the path into its segments ...
- List<Segment> segments = new ArrayList<Segment>();
- String[] pathSegments = DELIMITER_PATTERN.split(trimmedValue);
- if (pathSegments.length == 0) {
- throw new
InvalidPathException(SpiI18n.validPathMayNotContainEmptySegment.text(value));
- }
- assert pathSegments.length != 0;
- for (String segment : pathSegments) {
- assert segment != null;
- segment = segment.trim();
- if (segment.length() == 0) {
- throw new
InvalidPathException(SpiI18n.validPathMayNotContainEmptySegment.text(value));
- }
- // Create the name and add a segment with it ...
- Name segmentName = this.getNameValueFactory().create(segment, decoder);
- segments.add(new BasicPathSegment(segmentName));
- }
+ // Parse the path into its segments ...
+ List<Segment> segments = new ArrayList<Segment>();
+ String[] pathSegments = DELIMITER_PATTERN.split(trimmedValue);
+ if (pathSegments.length == 0) {
+ throw new
ValueFormatException(SpiI18n.validPathMayNotContainEmptySegment.text(value));
+ }
+ assert pathSegments.length != 0;
+ for (String segment : pathSegments) {
+ assert segment != null;
+ segment = segment.trim();
+ if (segment.length() == 0) {
+ throw new
ValueFormatException(SpiI18n.validPathMayNotContainEmptySegment.text(value));
+ }
+ // Create the name and add a segment with it ...
+ segments.add(createSegment(segment, decoder));
+ }
- // Create a path constructed from the supplied segments ...
- return new BasicPath(segments, absolute);
- }
+ // Create a path constructed from the supplied segments ...
+ return new BasicPath(segments, absolute);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( int value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( int value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( long value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( long value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( boolean value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( boolean value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( float value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( float value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( double value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( double value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( BigDecimal value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( BigDecimal value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Calendar value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Calendar value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Date value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Date.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Date value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Date.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Name value ) {
- if (value == null) return null;
- List<Path.Segment> segments = new ArrayList<Path.Segment>(1);
- segments.add(new BasicPathSegment(value));
- return new BasicPath(segments, true);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Name value ) {
+ if (value == null) return null;
+ List<Path.Segment> segments = new ArrayList<Path.Segment>(1);
+ segments.add(new BasicPathSegment(value));
+ return new BasicPath(segments, true);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Path value ) throws ValueFormatException {
- return value;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Path value ) throws ValueFormatException {
+ return value;
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Name... segmentNames ) {
- if (segmentNames == null || segmentNames.length == 0) return BasicPath.ROOT;
- List<Segment> segments = new
ArrayList<Segment>(segmentNames.length);
- for (Name segmentName : segmentNames) {
- if (segmentName == null) {
- ArgCheck.containsNoNulls(segmentNames, "segment names");
- }
- segments.add(new BasicPathSegment(segmentName));
- }
- return new BasicPath(segments, true);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Name... segmentNames ) {
+ if (segmentNames == null || segmentNames.length == 0) return BasicPath.ROOT;
+ List<Segment> segments = new ArrayList<Segment>(segmentNames.length);
+ for (Name segmentName : segmentNames) {
+ if (segmentName == null) {
+ ArgCheck.containsNoNulls(segmentNames, "segment names");
+ }
+ segments.add(new BasicPathSegment(segmentName));
+ }
+ return new BasicPath(segments, true);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Segment... segments ) {
- if (segments == null || segments.length == 0) return BasicPath.ROOT;
- List<Segment> segmentsList = new
ArrayList<Segment>(segments.length);
- for (Segment segment : segments) {
- if (segment == null) {
- ArgCheck.containsNoNulls(segments, "segments");
- }
- segmentsList.add(segment);
- }
- return new BasicPath(segmentsList, true);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Segment... segments ) {
+ if (segments == null || segments.length == 0) return BasicPath.ROOT;
+ List<Segment> segmentsList = new ArrayList<Segment>(segments.length);
+ for (Segment segment : segments) {
+ if (segment == null) {
+ ArgCheck.containsNoNulls(segments, "segments");
+ }
+ segmentsList.add(segment);
+ }
+ return new BasicPath(segmentsList, true);
+ }
- /**
- * {@inheritDoc}
- */
- public Path createRelativePath( Name... segmentNames ) {
- if (segmentNames == null || segmentNames.length == 0) return BasicPath.ROOT;
- List<Segment> segments = new
ArrayList<Segment>(segmentNames.length);
- for (Name segmentName : segmentNames) {
- if (segmentName == null) {
- ArgCheck.containsNoNulls(segmentNames, "segment names");
- }
- segments.add(new BasicPathSegment(segmentName));
- }
- return new BasicPath(segments, false);
- }
+ /**
+ * <p>
+ * {@inheritDoc}
+ * </p>
+ *
+ * @see org.jboss.dna.spi.graph.PathFactory#createRelativePath()
+ */
+ public Path createRelativePath() {
+ return BasicPath.SELF_PATH;
+ }
- /**
- * {@inheritDoc}
- */
- public Path createRelativePath( Segment... segments ) {
- if (segments == null || segments.length == 0) return BasicPath.ROOT;
- List<Segment> segmentsList = new
ArrayList<Segment>(segments.length);
- for (Segment segment : segments) {
- if (segment == null) {
- ArgCheck.containsNoNulls(segments, "segments");
- }
- segmentsList.add(segment);
- }
- return new BasicPath(segmentsList, false);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path createRelativePath( Name... segmentNames ) {
+ if (segmentNames == null || segmentNames.length == 0) return BasicPath.ROOT;
+ List<Segment> segments = new ArrayList<Segment>(segmentNames.length);
+ for (Name segmentName : segmentNames) {
+ if (segmentName == null) {
+ ArgCheck.containsNoNulls(segmentNames, "segment names");
+ }
+ segments.add(new BasicPathSegment(segmentName));
+ }
+ return new BasicPath(segments, false);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Path parentPath, Name segmentName, int index ) {
- ArgCheck.isNotNull(parentPath, "parent path");
- ArgCheck.isNotNull(segmentName, "segment name");
- List<Segment> segments = new ArrayList<Segment>(parentPath.size() +
1);
- segments.addAll(parentPath.getSegmentsList());
- segments.add(new BasicPathSegment(segmentName, index));
- return new BasicPath(segments, parentPath.isAbsolute());
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path createRelativePath( Segment... segments ) {
+ if (segments == null || segments.length == 0) return BasicPath.ROOT;
+ List<Segment> segmentsList = new ArrayList<Segment>(segments.length);
+ for (Segment segment : segments) {
+ if (segment == null) {
+ ArgCheck.containsNoNulls(segments, "segments");
+ }
+ segmentsList.add(segment);
+ }
+ return new BasicPath(segmentsList, false);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Path parentPath, Name... segmentNames ) {
- ArgCheck.isNotNull(parentPath, "parent path");
- if (segmentNames == null || segmentNames.length == 0) return parentPath;
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Path parentPath,
+ Name segmentName,
+ int index ) {
+ ArgCheck.isNotNull(parentPath, "parent path");
+ ArgCheck.isNotNull(segmentName, "segment name");
+ List<Segment> segments = new ArrayList<Segment>(parentPath.size() + 1);
+ segments.addAll(parentPath.getSegmentsList());
+ segments.add(new BasicPathSegment(segmentName, index));
+ return new BasicPath(segments, parentPath.isAbsolute());
+ }
- List<Segment> segments = new ArrayList<Segment>(parentPath.size() +
1);
- segments.addAll(parentPath.getSegmentsList());
- for (Name segmentName : segmentNames) {
- if (segmentName == null) {
- ArgCheck.containsNoNulls(segmentNames, "segment names");
- }
- segments.add(new BasicPathSegment(segmentName));
- }
- return new BasicPath(segments, parentPath.isAbsolute());
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Path parentPath,
+ Name... segmentNames ) {
+ ArgCheck.isNotNull(parentPath, "parent path");
+ if (segmentNames == null || segmentNames.length == 0) return parentPath;
- /**
- * {@inheritDoc}
- */
- public Path create( Path parentPath, Segment... segments ) {
- ArgCheck.isNotNull(parentPath, "parent path");
- if (segments == null || segments.length == 0) return BasicPath.ROOT;
+ List<Segment> segments = new ArrayList<Segment>(parentPath.size() + 1);
+ segments.addAll(parentPath.getSegmentsList());
+ for (Name segmentName : segmentNames) {
+ if (segmentName == null) {
+ ArgCheck.containsNoNulls(segmentNames, "segment names");
+ }
+ segments.add(new BasicPathSegment(segmentName));
+ }
+ return new BasicPath(segments, parentPath.isAbsolute());
+ }
- List<Segment> segmentsList = new ArrayList<Segment>(parentPath.size()
+ 1);
- segmentsList.addAll(parentPath.getSegmentsList());
- for (Segment segment : segments) {
- if (segment == null) {
- ArgCheck.containsNoNulls(segments, "segments");
- }
- segmentsList.add(segment);
- }
- return new BasicPath(segmentsList, parentPath.isAbsolute());
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Path parentPath,
+ Segment... segments ) {
+ ArgCheck.isNotNull(parentPath, "parent path");
+ if (segments == null || segments.length == 0) return BasicPath.ROOT;
- /**
- * {@inheritDoc}
- */
- public Segment createSegment( Name segmentName ) {
- ArgCheck.isNotNull(segmentName, "segment name");
- if (Path.SELF_NAME.equals(segmentName)) return Path.SELF_SEGMENT;
- if (Path.PARENT_NAME.equals(segmentName)) return Path.PARENT_SEGMENT;
- return new BasicPathSegment(segmentName);
- }
+ List<Segment> segmentsList = new ArrayList<Segment>(parentPath.size() +
1);
+ segmentsList.addAll(parentPath.getSegmentsList());
+ for (Segment segment : segments) {
+ if (segment == null) {
+ ArgCheck.containsNoNulls(segments, "segments");
+ }
+ segmentsList.add(segment);
+ }
+ return new BasicPath(segmentsList, parentPath.isAbsolute());
+ }
- /**
- * {@inheritDoc}
- */
- public Segment createSegment( Name segmentName, int index ) {
- ArgCheck.isNotNull(segmentName, "segment name");
- if (Path.SELF_NAME.equals(segmentName)) return Path.SELF_SEGMENT;
- if (Path.PARENT_NAME.equals(segmentName)) return Path.PARENT_SEGMENT;
- return new BasicPathSegment(segmentName, index);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Segment createSegment( Name segmentName ) {
+ ArgCheck.isNotNull(segmentName, "segment name");
+ if (Path.SELF_NAME.equals(segmentName)) return Path.SELF_SEGMENT;
+ if (Path.PARENT_NAME.equals(segmentName)) return Path.PARENT_SEGMENT;
+ return new BasicPathSegment(segmentName);
+ }
- /**
- * {@inheritDoc}
- */
- public Segment createSegment( String segmentName ) {
- ArgCheck.isNotNull(segmentName, "segment name");
- if (Path.SELF.equals(segmentName)) return Path.SELF_SEGMENT;
- if (Path.PARENT.equals(segmentName)) return Path.PARENT_SEGMENT;
- return new BasicPathSegment(this.nameValueFactory.create(segmentName));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Segment createSegment( Name segmentName,
+ int index ) {
+ ArgCheck.isNotNull(segmentName, "segment name");
+ if (Path.SELF_NAME.equals(segmentName)) return Path.SELF_SEGMENT;
+ if (Path.PARENT_NAME.equals(segmentName)) return Path.PARENT_SEGMENT;
+ return new BasicPathSegment(segmentName, index);
+ }
- /**
- * {@inheritDoc}
- */
- public Segment createSegment( String segmentName, int index ) {
- ArgCheck.isNotNull(segmentName, "segment name");
- if (Path.SELF.equals(segmentName)) return Path.SELF_SEGMENT;
- if (Path.PARENT.equals(segmentName)) return Path.PARENT_SEGMENT;
- return new BasicPathSegment(this.nameValueFactory.create(segmentName), index);
- }
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.jboss.dna.spi.graph.PathFactory#createSegment(java.lang.String)
+ */
+ public Segment createSegment( String segmentName ) {
+ return createSegment(segmentName, getEncoder());
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Reference value ) throws ValueFormatException {
- throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
Reference.class.getSimpleName(), value));
- }
+ /**
+ * <p>
+ * {@inheritDoc}
+ * </p>
+ *
+ * @see org.jboss.dna.spi.graph.PathFactory#createSegment(java.lang.String,
org.jboss.dna.common.text.TextEncoder)
+ */
+ public Segment createSegment( String segmentName,
+ TextEncoder encoder ) {
+ ArgCheck.isNotNull(segmentName, "segment name");
+ if (Path.SELF.equals(segmentName)) return Path.SELF_SEGMENT;
+ if (Path.PARENT.equals(segmentName)) return Path.PARENT_SEGMENT;
+ int startBracketNdx = segmentName.indexOf('[');
+ if (startBracketNdx < 0) {
+ return new BasicPathSegment(this.nameValueFactory.create(segmentName));
+ }
+ int endBracketNdx = segmentName.indexOf(']', startBracketNdx);
+ if (endBracketNdx < 0) {
+ throw new
IllegalArgumentException(SpiI18n.missingEndBracketInSegmentName.text(segmentName));
+ }
+ String ndx = segmentName.substring(startBracketNdx + 1, endBracketNdx);
+ try {
+ return new BasicPathSegment(this.nameValueFactory.create(segmentName.substring(0,
startBracketNdx)),
+ Integer.parseInt(ndx));
+ } catch (NumberFormatException err) {
+ throw new IllegalArgumentException(SpiI18n.invalidIndexInSegmentName.text(ndx,
segmentName));
+ }
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( URI value ) throws ValueFormatException {
- if (value == null) return null;
- String asciiString = value.toASCIIString();
- // Remove any leading "./" ...
- if (asciiString.startsWith("./") && asciiString.length() >
2) {
- asciiString = asciiString.substring(2);
- }
- if (asciiString.indexOf('/') == -1) {
- return create(asciiString);
- }
- throw new
ValueFormatException(SpiI18n.errorCreatingValue.text(getPropertyType().getName(),
Path.class.getSimpleName(), value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Segment createSegment( String segmentName,
+ int index ) {
+ ArgCheck.isNotNull(segmentName, "segment name");
+ if (Path.SELF.equals(segmentName)) return Path.SELF_SEGMENT;
+ if (Path.PARENT.equals(segmentName)) return Path.PARENT_SEGMENT;
+ return new BasicPathSegment(this.nameValueFactory.create(segmentName), index);
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( byte[] value ) throws ValueFormatException {
- // First attempt to create a string from the value, then a long from the string
...
- return create(getStringValueFactory().create(value));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Reference value ) throws ValueFormatException {
+ throw new
ValueFormatException(SpiI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Reference.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( InputStream stream, int approximateLength ) throws IOException,
ValueFormatException {
- // First attempt to create a string from the value, then a double from the string
...
- return create(getStringValueFactory().create(stream, approximateLength));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( URI value ) throws ValueFormatException {
+ if (value == null) return null;
+ String asciiString = value.toASCIIString();
+ // Remove any leading "./" ...
+ if (asciiString.startsWith("./") && asciiString.length() > 2) {
+ asciiString = asciiString.substring(2);
+ }
+ if (asciiString.indexOf('/') == -1) {
+ return create(asciiString);
+ }
+ throw new
ValueFormatException(SpiI18n.errorCreatingValue.text(getPropertyType().getName(),
+
Path.class.getSimpleName(),
+ value));
+ }
- /**
- * {@inheritDoc}
- */
- public Path create( Reader reader, int approximateLength ) throws IOException,
ValueFormatException {
- // First attempt to create a string from the value, then a double from the string
...
- return create(getStringValueFactory().create(reader, approximateLength));
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( byte[] value ) throws ValueFormatException {
+ // First attempt to create a string from the value, then a long from the string ...
+ return create(getStringValueFactory().create(value));
+ }
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( InputStream stream,
+ int approximateLength ) throws IOException, ValueFormatException {
+ // First attempt to create a string from the value, then a double from the string ...
+ return create(getStringValueFactory().create(stream, approximateLength));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Path create( Reader reader,
+ int approximateLength ) throws IOException, ValueFormatException {
+ // First attempt to create a string from the value, then a double from the string ...
+ return create(getStringValueFactory().create(reader, approximateLength));
+ }
+
}
Modified: trunk/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicPathTest.java
===================================================================
--- trunk/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicPathTest.java 2008-06-02
20:12:41 UTC (rev 233)
+++ trunk/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicPathTest.java 2008-06-02
20:19:51 UTC (rev 234)
@@ -2,7 +2,7 @@
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
+ * distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
@@ -36,726 +36,741 @@
import org.jboss.dna.spi.graph.Name;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.PathNotFoundException;
+import org.jboss.dna.spi.graph.ValueFormatException;
import org.junit.Before;
import org.junit.Test;
/**
* @author Randall Hauch
+ * @author John Verhaeg
*/
public class BasicPathTest {
- public static final TextEncoder NO_OP_ENCODER = Path.NO_OP_ENCODER;
- public static final Path ROOT = BasicPath.ROOT;
+ public static final TextEncoder NO_OP_ENCODER = Path.NO_OP_ENCODER;
+ public static final Path ROOT = BasicPath.ROOT;
- private BasicNamespaceRegistry namespaceRegistry;
- private String validNamespaceUri;
- private Path path;
- private Path path2;
- private Path.Segment[] validSegments;
- private List<Path.Segment> validSegmentsList;
- private Name[] validSegmentNames;
- private String validNamespacePrefix;
- private PathValueFactory pathFactory;
+ private BasicNamespaceRegistry namespaceRegistry;
+ private String validNamespaceUri;
+ private Path path;
+ private Path path2;
+ private Path.Segment[] validSegments;
+ private List<Path.Segment> validSegmentsList;
+ private Name[] validSegmentNames;
+ private String validNamespacePrefix;
+ private PathValueFactory pathFactory;
- @Before
- public void beforeEach() throws Exception {
- validNamespacePrefix = "dna";
- validNamespaceUri = "http://www.jboss.org/dna";
- validSegmentNames = new Name[] {new BasicName(validNamespaceUri, "a"),
new BasicName(validNamespaceUri, "b"), new BasicName(validNamespaceUri,
"c")};
- validSegments = new Path.Segment[] {new BasicPathSegment(validSegmentNames[0]),
new BasicPathSegment(validSegmentNames[1]), new BasicPathSegment(validSegmentNames[1])};
- validSegmentsList = new ArrayList<Path.Segment>();
- for (Path.Segment segment : validSegments) {
- validSegmentsList.add(segment);
- }
- path = new BasicPath(validSegmentsList, true);
- namespaceRegistry = new BasicNamespaceRegistry();
- namespaceRegistry.register(validNamespacePrefix, validNamespaceUri);
- StringValueFactory stringValueFactory = new
StringValueFactory(Path.DEFAULT_ENCODER);
- NameValueFactory nameValueFactory = new NameValueFactory(namespaceRegistry,
Path.DEFAULT_ENCODER, stringValueFactory);
- pathFactory = new PathValueFactory(Path.DEFAULT_ENCODER, stringValueFactory,
nameValueFactory);
- }
+ @Before
+ public void beforeEach() throws Exception {
+ validNamespacePrefix = "dna";
+ validNamespaceUri = "http://www.jboss.org/dna";
+ validSegmentNames = new Name[] {new BasicName(validNamespaceUri, "a"), new
BasicName(validNamespaceUri, "b"),
+ new BasicName(validNamespaceUri, "c")};
+ validSegments = new Path.Segment[] {new BasicPathSegment(validSegmentNames[0]),
+ new BasicPathSegment(validSegmentNames[1]), new
BasicPathSegment(validSegmentNames[1])};
+ validSegmentsList = new ArrayList<Path.Segment>();
+ for (Path.Segment segment : validSegments) {
+ validSegmentsList.add(segment);
+ }
+ path = new BasicPath(validSegmentsList, true);
+ namespaceRegistry = new BasicNamespaceRegistry();
+ namespaceRegistry.register(validNamespacePrefix, validNamespaceUri);
+ StringValueFactory stringValueFactory = new StringValueFactory(Path.DEFAULT_ENCODER);
+ NameValueFactory nameValueFactory = new NameValueFactory(namespaceRegistry,
Path.DEFAULT_ENCODER, stringValueFactory);
+ pathFactory = new PathValueFactory(Path.DEFAULT_ENCODER, stringValueFactory,
nameValueFactory);
+ }
- @Test
- public void shouldCreateAbsolutePathFromListOfValidSegments() {
- path = new BasicPath(validSegmentsList, true);
- assertThat(path.isAbsolute(), is(true));
- assertThat(path.isNormalized(), is(true));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateAbsolutePathFromListOfValidSegments() {
+ path = new BasicPath(validSegmentsList, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(true));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreateRelativePathFromListOfValidSegments() {
- path = new BasicPath(validSegmentsList, false);
- assertThat(path.isAbsolute(), is(false));
- assertThat(path.isNormalized(), is(true));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateRelativePathFromListOfValidSegments() {
+ path = new BasicPath(validSegmentsList, false);
+ assertThat(path.isAbsolute(), is(false));
+ assertThat(path.isNormalized(), is(true));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreateAbsolutePathWithParentSegment() {
- validSegmentsList.add(Path.PARENT_SEGMENT);
- path = new BasicPath(validSegmentsList, true);
- assertThat(path.isAbsolute(), is(true));
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateAbsolutePathWithParentSegment() {
+ validSegmentsList.add(Path.PARENT_SEGMENT);
+ path = new BasicPath(validSegmentsList, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreateRelativePathWithParentSegment() {
- validSegmentsList.add(Path.PARENT_SEGMENT);
- path = new BasicPath(validSegmentsList, false);
- assertThat(path.isAbsolute(), is(false));
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateRelativePathWithParentSegment() {
+ validSegmentsList.add(Path.PARENT_SEGMENT);
+ path = new BasicPath(validSegmentsList, false);
+ assertThat(path.isAbsolute(), is(false));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreateAbsolutePathWithSelfSegment() {
- validSegmentsList.add(Path.SELF_SEGMENT);
- path = new BasicPath(validSegmentsList, true);
- assertThat(path.isAbsolute(), is(true));
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateAbsolutePathWithSelfSegment() {
+ validSegmentsList.add(Path.SELF_SEGMENT);
+ path = new BasicPath(validSegmentsList, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreateRelativePathWithSelfSegment() {
- validSegmentsList.add(Path.SELF_SEGMENT);
- path = new BasicPath(validSegmentsList, false);
- assertThat(path.isAbsolute(), is(false));
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getSegmentsList(), is(validSegmentsList));
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldCreateRelativePathWithSelfSegment() {
+ validSegmentsList.add(Path.SELF_SEGMENT);
+ path = new BasicPath(validSegmentsList, false);
+ assertThat(path.isAbsolute(), is(false));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(validSegmentsList));
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldCreatePathWithNoNamespacePrefixes() {
- path = pathFactory.create("/a/b/c/");
- assertThat(path.size(), is(3));
- assertThat(path, hasSegments(pathFactory, "a", "b",
"c"));
- }
+ @Test
+ public void shouldCreatePathWithNoNamespacePrefixes() {
+ path = pathFactory.create("/a/b/c/");
+ assertThat(path.size(), is(3));
+ assertThat(path, hasSegments(pathFactory, "a", "b",
"c"));
+ }
- @Test
- public void shouldConstructRelativePath() {
- assertThat(pathFactory.create("a/b/c").isAbsolute(), is(false));
- assertThat(pathFactory.create("a/b/c").isNormalized(), is(true));
- assertThat(pathFactory.create("a/b/c").size(), is(3));
- assertThat(pathFactory.create("a/b/c").getString(namespaceRegistry),
is("a/b/c"));
- }
+ @Test
+ public void shouldConstructRelativePath() {
+ assertThat(pathFactory.create("a/b/c").isAbsolute(), is(false));
+ assertThat(pathFactory.create("a/b/c").isNormalized(), is(true));
+ assertThat(pathFactory.create("a/b/c").size(), is(3));
+ assertThat(pathFactory.create("a/b/c").getString(namespaceRegistry),
is("a/b/c"));
+ }
- @Test
- public void shouldConstructRelativePathToSelf() {
- assertThat(pathFactory.create(".").isAbsolute(), is(false));
- assertThat(pathFactory.create(".").size(), is(1));
- assertThat(pathFactory.create("."), hasSegments(pathFactory,
Path.SELF));
+ @Test
+ public void shouldConstructRelativePathToSelf() {
+ assertThat(pathFactory.create(".").isAbsolute(), is(false));
+ assertThat(pathFactory.create(".").size(), is(1));
+ assertThat(pathFactory.create("."), hasSegments(pathFactory, Path.SELF));
- assertThat(pathFactory.create("./").isAbsolute(), is(false));
- assertThat(pathFactory.create("./").size(), is(1));
- assertThat(pathFactory.create("./"), hasSegments(pathFactory,
Path.SELF));
- }
+ assertThat(pathFactory.create("./").isAbsolute(), is(false));
+ assertThat(pathFactory.create("./").size(), is(1));
+ assertThat(pathFactory.create("./"), hasSegments(pathFactory, Path.SELF));
+ }
- @Test
- public void shouldConstructRelativePathToParent() {
- assertThat(pathFactory.create("..").isAbsolute(), is(false));
- assertThat(pathFactory.create("..").size(), is(1));
- assertThat(pathFactory.create(".."), hasSegments(pathFactory,
Path.PARENT));
+ @Test
+ public void shouldConstructRelativePathToParent() {
+ assertThat(pathFactory.create("..").isAbsolute(), is(false));
+ assertThat(pathFactory.create("..").size(), is(1));
+ assertThat(pathFactory.create(".."), hasSegments(pathFactory, Path.PARENT));
- assertThat(pathFactory.create("../").isAbsolute(), is(false));
- assertThat(pathFactory.create("../").size(), is(1));
- assertThat(pathFactory.create("../"), hasSegments(pathFactory,
Path.PARENT));
- }
+ assertThat(pathFactory.create("../").isAbsolute(), is(false));
+ assertThat(pathFactory.create("../").size(), is(1));
+ assertThat(pathFactory.create("../"), hasSegments(pathFactory,
Path.PARENT));
+ }
- @Test
- public void shouldConstructRootPathFromStringWithSingleDelimiter() {
- assertThat(pathFactory.create("/"), is(ROOT));
- assertThat(pathFactory.create("/").isRoot(), is(true));
- }
+ @Test
+ public void shouldConstructRootPathFromStringWithSingleDelimiter() {
+ assertThat(pathFactory.create("/"), is(ROOT));
+ assertThat(pathFactory.create("/").isRoot(), is(true));
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotConstructPathWithSuccessiveDelimiters() {
- pathFactory.create("///a/b///c//d//");
- }
+ @Test( expected = ValueFormatException.class )
+ public void shouldNotConstructPathWithSuccessiveDelimiters() {
+ pathFactory.create("///a/b///c//d//");
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotConstructPathWithOnlyDelimiters() {
- pathFactory.create("///");
- }
+ @Test( expected = ValueFormatException.class )
+ public void shouldNotConstructPathWithOnlyDelimiters() {
+ pathFactory.create("///");
+ }
- @Test( expected = IllegalArgumentException.class )
- public void shouldNotConstructPathFromNullList() {
- new BasicPath(null, true);
- }
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotConstructPathFromNullList() {
+ new BasicPath(null, true);
+ }
- @Test
- public void
shouldConstructPathFromStringAndShouldIgnoreLeadingAndTrailingWhitespace() {
- assertThat(pathFactory.create(" \t / \t").toString(),
is("/"));
- }
+ @Test
+ public void shouldConstructPathFromStringAndShouldIgnoreLeadingAndTrailingWhitespace()
{
+ assertThat(pathFactory.create(" \t / \t").toString(), is("/"));
+ }
- @Test
- public void shouldConstructRelativePathIfSuppliedPathHasNoLeadingDelimiter() {
- assertThat(pathFactory.create("a"), hasSegments(pathFactory,
"a"));
- }
+ @Test
+ public void shouldConstructRelativePathIfSuppliedPathHasNoLeadingDelimiter() {
+ assertThat(pathFactory.create("a"), hasSegments(pathFactory,
"a"));
+ }
- @Test
- public void shouldHaveSizeThatReflectsNumberOfSegments() {
- assertThat(path.size(), is(validSegmentsList.size()));
- }
+ @Test
+ public void shouldHaveSizeThatReflectsNumberOfSegments() {
+ assertThat(path.size(), is(validSegmentsList.size()));
+ }
- @Test
- public void shouldIterateOverAllSegmentsReturnedByList() {
- Iterator<Path.Segment> expectedIter = validSegmentsList.iterator();
- for (Path.Segment segment : path) {
- assertThat(segment, is(expectedIter.next()));
- }
+ @Test
+ public void shouldIterateOverAllSegmentsReturnedByList() {
+ Iterator<Path.Segment> expectedIter = validSegmentsList.iterator();
+ for (Path.Segment segment : path) {
+ assertThat(segment, is(expectedIter.next()));
+ }
- expectedIter = path.getSegmentsList().iterator();
- for (Path.Segment segment : path) {
- assertThat(segment, is(expectedIter.next()));
- }
- }
+ expectedIter = path.getSegmentsList().iterator();
+ for (Path.Segment segment : path) {
+ assertThat(segment, is(expectedIter.next()));
+ }
+ }
- @Test
- public void shouldReturnRootForAncestorOfRoot() {
- assertThat(BasicPath.ROOT.getAncestor(), is(ROOT));
- }
+ @Test
+ public void shouldReturnRootForAncestorOfRoot() {
+ assertThat(BasicPath.ROOT.getAncestor(), is(ROOT));
+ }
- @Test
- public void shouldReturnAncestorForNodeOtherThanRoot() {
- assertThat(path.getAncestor(),
is(pathFactory.create("/dna:a/dna:b")));
- assertThat(path.getAncestor().getAncestor(),
is(pathFactory.create("/dna:a")));
- assertThat(path.getAncestor().getAncestor().getAncestor(), is(ROOT));
- }
+ @Test
+ public void shouldReturnAncestorForNodeOtherThanRoot() {
+ assertThat(path.getAncestor(), is(pathFactory.create("/dna:a/dna:b")));
+ assertThat(path.getAncestor().getAncestor(),
is(pathFactory.create("/dna:a")));
+ assertThat(path.getAncestor().getAncestor().getAncestor(), is(ROOT));
+ }
- @Test
- public void shouldReturnNthDegreeAncestor() {
- assertThat(path.getAncestor(1),
is(pathFactory.create("/dna:a/dna:b")));
- assertThat(path.getAncestor(2), is(pathFactory.create("/dna:a")));
- assertThat(path.getAncestor(3), is(ROOT));
- }
+ @Test
+ public void shouldReturnNthDegreeAncestor() {
+ assertThat(path.getAncestor(1), is(pathFactory.create("/dna:a/dna:b")));
+ assertThat(path.getAncestor(2), is(pathFactory.create("/dna:a")));
+ assertThat(path.getAncestor(3), is(ROOT));
+ }
- @Test( expected = PathNotFoundException.class )
- public void shouldNotAllowAncestorDegreeLargerThanSize() {
- path.getAncestor(path.size() + 1);
- }
+ @Test( expected = PathNotFoundException.class )
+ public void shouldNotAllowAncestorDegreeLargerThanSize() {
+ path.getAncestor(path.size() + 1);
+ }
- @Test( expected = IllegalArgumentException.class )
- public void shouldNotAllowNegativeAncestorDegree() {
- path.getAncestor(-1);
- }
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotAllowNegativeAncestorDegree() {
+ path.getAncestor(-1);
+ }
- @Test
- public void shouldReturnRootForAnyAncestorExactDegreeFromRoot() {
- assertThat(path.getAncestor(path.size()), is(ROOT));
- assertThat(ROOT.getAncestor(0), is(ROOT));
- }
+ @Test
+ public void shouldReturnRootForAnyAncestorExactDegreeFromRoot() {
+ assertThat(path.getAncestor(path.size()), is(ROOT));
+ assertThat(ROOT.getAncestor(0), is(ROOT));
+ }
- @Test
- public void shouldConsiderRootTheLowestCommonAncestorOfAnyNodeAndRoot() {
- assertThat(path.getCommonAncestor(ROOT), is(ROOT));
- assertThat(ROOT.getCommonAncestor(path), is(ROOT));
- }
+ @Test
+ public void shouldConsiderRootTheLowestCommonAncestorOfAnyNodeAndRoot() {
+ assertThat(path.getCommonAncestor(ROOT), is(ROOT));
+ assertThat(ROOT.getCommonAncestor(path), is(ROOT));
+ }
- @Test
- public void shouldReturnNullForLowestCommonAncestorWithNullPath() {
- assertThat(path.getCommonAncestor(null), is(nullValue()));
- }
+ @Test
+ public void shouldReturnNullForLowestCommonAncestorWithNullPath() {
+ assertThat(path.getCommonAncestor(null), is(nullValue()));
+ }
- @Test
- public void shouldFindLowestCommonAncestorBetweenTwoNonRootNodesOnCommonBranch() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path common = pathFactory.create("/a");
- assertThat(path1.getCommonAncestor(path2), is(common));
+ @Test
+ public void shouldFindLowestCommonAncestorBetweenTwoNonRootNodesOnCommonBranch() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path common = pathFactory.create("/a");
+ assertThat(path1.getCommonAncestor(path2), is(common));
- path1 = pathFactory.create("/a/b/c");
- path2 = pathFactory.create("/a/b/c/d");
- common = path1;
- assertThat(path1.getCommonAncestor(path2), is(common));
+ path1 = pathFactory.create("/a/b/c");
+ path2 = pathFactory.create("/a/b/c/d");
+ common = path1;
+ assertThat(path1.getCommonAncestor(path2), is(common));
- path1 = pathFactory.create("/a/b/c/x/y/");
- path2 = pathFactory.create("/a/b/c/d/e/f/");
- common = pathFactory.create("/a/b/c");
- assertThat(path1.getCommonAncestor(path2), is(common));
- }
+ path1 = pathFactory.create("/a/b/c/x/y/");
+ path2 = pathFactory.create("/a/b/c/d/e/f/");
+ common = pathFactory.create("/a/b/c");
+ assertThat(path1.getCommonAncestor(path2), is(common));
+ }
- @Test
- public void shouldConsiderRootTheLowestCommonAncestorOfAnyNodesOnSeparateBrances() {
- Path path1 = pathFactory.create("/x/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path common = ROOT;
- assertThat(path1.getCommonAncestor(path2), is(common));
- }
+ @Test
+ public void shouldConsiderRootTheLowestCommonAncestorOfAnyNodesOnSeparateBrances() {
+ Path path1 = pathFactory.create("/x/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path common = ROOT;
+ assertThat(path1.getCommonAncestor(path2), is(common));
+ }
- @Test
- public void shouldConsiderNodeToBeAncestorOfEveryDecendantNode() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z");
- Path common = pathFactory.create("/a");
- assertThat(common.isAncestorOf(path1), is(true));
- assertThat(common.isAncestorOf(path2), is(true));
- assertThat(common.isAncestorOf(path3), is(false));
+ @Test
+ public void shouldConsiderNodeToBeAncestorOfEveryDecendantNode() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z");
+ Path common = pathFactory.create("/a");
+ assertThat(common.isAncestorOf(path1), is(true));
+ assertThat(common.isAncestorOf(path2), is(true));
+ assertThat(common.isAncestorOf(path3), is(false));
- assertThat(path1.getAncestor().isAncestorOf(path1), is(true));
- for (int i = 1; i < path1.size(); ++i) {
- assertThat(path1.getAncestor(i).isAncestorOf(path1), is(true));
- }
- for (int i = 1; i < path2.size(); ++i) {
- assertThat(path2.getAncestor(i).isAncestorOf(path2), is(true));
- }
- for (int i = 1; i < path3.size(); ++i) {
- assertThat(path3.getAncestor(i).isAncestorOf(path3), is(true));
- }
- for (int i = 1; i < path4.size(); ++i) {
- assertThat(path4.getAncestor(i).isAncestorOf(path4), is(true));
- }
- }
+ assertThat(path1.getAncestor().isAncestorOf(path1), is(true));
+ for (int i = 1; i < path1.size(); ++i) {
+ assertThat(path1.getAncestor(i).isAncestorOf(path1), is(true));
+ }
+ for (int i = 1; i < path2.size(); ++i) {
+ assertThat(path2.getAncestor(i).isAncestorOf(path2), is(true));
+ }
+ for (int i = 1; i < path3.size(); ++i) {
+ assertThat(path3.getAncestor(i).isAncestorOf(path3), is(true));
+ }
+ for (int i = 1; i < path4.size(); ++i) {
+ assertThat(path4.getAncestor(i).isAncestorOf(path4), is(true));
+ }
+ }
- @Test
- public void shouldConsiderNodeToBeDecendantOfEveryAncestorNode() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z");
- Path common = pathFactory.create("/a");
- assertThat(path1.isDecendantOf(common), is(true));
- assertThat(path2.isDecendantOf(common), is(true));
- assertThat(path3.isDecendantOf(common), is(false));
+ @Test
+ public void shouldConsiderNodeToBeDecendantOfEveryAncestorNode() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z");
+ Path common = pathFactory.create("/a");
+ assertThat(path1.isDecendantOf(common), is(true));
+ assertThat(path2.isDecendantOf(common), is(true));
+ assertThat(path3.isDecendantOf(common), is(false));
- assertThat(path1.getAncestor().isAncestorOf(path1), is(true));
- for (int i = 1; i < path1.size(); ++i) {
- assertThat(path1.isDecendantOf(path1.getAncestor(i)), is(true));
- }
- for (int i = 1; i < path2.size(); ++i) {
- assertThat(path2.isDecendantOf(path2.getAncestor(i)), is(true));
- }
- for (int i = 1; i < path3.size(); ++i) {
- assertThat(path3.isDecendantOf(path3.getAncestor(i)), is(true));
- }
- for (int i = 1; i < path4.size(); ++i) {
- assertThat(path4.isDecendantOf(path4.getAncestor(i)), is(true));
- }
- }
+ assertThat(path1.getAncestor().isAncestorOf(path1), is(true));
+ for (int i = 1; i < path1.size(); ++i) {
+ assertThat(path1.isDecendantOf(path1.getAncestor(i)), is(true));
+ }
+ for (int i = 1; i < path2.size(); ++i) {
+ assertThat(path2.isDecendantOf(path2.getAncestor(i)), is(true));
+ }
+ for (int i = 1; i < path3.size(); ++i) {
+ assertThat(path3.isDecendantOf(path3.getAncestor(i)), is(true));
+ }
+ for (int i = 1; i < path4.size(); ++i) {
+ assertThat(path4.isDecendantOf(path4.getAncestor(i)), is(true));
+ }
+ }
- @Test
- public void shouldNotConsiderNodeToBeAncestorOfItself() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- assertThat(path1.isAncestorOf(path1), is(false));
- assertThat(path2.isAncestorOf(path2), is(false));
- assertThat(path3.isAncestorOf(path3), is(false));
- assertThat(ROOT.isAncestorOf(ROOT), is(false));
- }
+ @Test
+ public void shouldNotConsiderNodeToBeAncestorOfItself() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ assertThat(path1.isAncestorOf(path1), is(false));
+ assertThat(path2.isAncestorOf(path2), is(false));
+ assertThat(path3.isAncestorOf(path3), is(false));
+ assertThat(ROOT.isAncestorOf(ROOT), is(false));
+ }
- @Test
- public void shouldNotConsiderNodeToBeDecendantOfItself() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- assertThat(path1.isDecendantOf(path1), is(false));
- assertThat(path2.isDecendantOf(path2), is(false));
- assertThat(path3.isDecendantOf(path3), is(false));
- assertThat(ROOT.isDecendantOf(ROOT), is(false));
- }
+ @Test
+ public void shouldNotConsiderNodeToBeDecendantOfItself() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ assertThat(path1.isDecendantOf(path1), is(false));
+ assertThat(path2.isDecendantOf(path2), is(false));
+ assertThat(path3.isDecendantOf(path3), is(false));
+ assertThat(ROOT.isDecendantOf(ROOT), is(false));
+ }
- @Test
- public void shouldNotConsiderRootToBeDecendantOfAnyNode() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- Path common = pathFactory.create("/a");
- assertThat(ROOT.isDecendantOf(path1), is(false));
- assertThat(ROOT.isDecendantOf(path2), is(false));
- assertThat(ROOT.isDecendantOf(path3), is(false));
- assertThat(ROOT.isDecendantOf(common), is(false));
- }
+ @Test
+ public void shouldNotConsiderRootToBeDecendantOfAnyNode() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ Path common = pathFactory.create("/a");
+ assertThat(ROOT.isDecendantOf(path1), is(false));
+ assertThat(ROOT.isDecendantOf(path2), is(false));
+ assertThat(ROOT.isDecendantOf(path3), is(false));
+ assertThat(ROOT.isDecendantOf(common), is(false));
+ }
- @Test
- public void shouldConsiderRootToBeAncestorOfAnyNode() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- Path common = pathFactory.create("/a");
- assertThat(ROOT.isAncestorOf(path1), is(true));
- assertThat(ROOT.isAncestorOf(path2), is(true));
- assertThat(ROOT.isAncestorOf(path3), is(true));
- assertThat(ROOT.isAncestorOf(common), is(true));
- }
+ @Test
+ public void shouldConsiderRootToBeAncestorOfAnyNode() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ Path common = pathFactory.create("/a");
+ assertThat(ROOT.isAncestorOf(path1), is(true));
+ assertThat(ROOT.isAncestorOf(path2), is(true));
+ assertThat(ROOT.isAncestorOf(path3), is(true));
+ assertThat(ROOT.isAncestorOf(common), is(true));
+ }
- @Test
- public void shouldNotConsiderRootToBeAncestorOfItself() {
- assertThat(ROOT.isAncestorOf(ROOT), is(false));
- }
+ @Test
+ public void shouldNotConsiderRootToBeAncestorOfItself() {
+ assertThat(ROOT.isAncestorOf(ROOT), is(false));
+ }
- @Test
- public void shouldNotConsiderRootToBeDecendantOfItself() {
- assertThat(ROOT.isDecendantOf(ROOT), is(false));
- }
+ @Test
+ public void shouldNotConsiderRootToBeDecendantOfItself() {
+ assertThat(ROOT.isDecendantOf(ROOT), is(false));
+ }
- @Test
- public void shouldNeverBeDecendantOfNullPath() {
- assertThat(path.isDecendantOf(null), is(false));
- assertThat(ROOT.isDecendantOf(null), is(false));
- }
+ @Test
+ public void shouldNeverBeDecendantOfNullPath() {
+ assertThat(path.isDecendantOf(null), is(false));
+ assertThat(ROOT.isDecendantOf(null), is(false));
+ }
- @Test
- public void shouldReturnNullForLastSegmentOfRoot() {
- assertThat(ROOT.getLastSegment(), is(nullValue()));
- }
+ @Test
+ public void shouldReturnNullForLastSegmentOfRoot() {
+ assertThat(ROOT.getLastSegment(), is(nullValue()));
+ }
- @Test
- public void shouldReturnLastSegmentOfNonRootPath() {
- Path path1 = pathFactory.create("/a/y/z");
- Path path2 = pathFactory.create("/a/b/c");
- Path path3 = pathFactory.create("/x/b/c");
- Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x");
- assertThat(path1.getLastSegment().getName().getLocalName(), is("z"));
- assertThat(path2.getLastSegment().getName().getLocalName(), is("c"));
- assertThat(path3.getLastSegment().getName().getLocalName(), is("c"));
- assertThat(path4.getLastSegment().getName().getLocalName(), is("x"));
- }
+ @Test
+ public void shouldReturnLastSegmentOfNonRootPath() {
+ Path path1 = pathFactory.create("/a/y/z");
+ Path path2 = pathFactory.create("/a/b/c");
+ Path path3 = pathFactory.create("/x/b/c");
+ Path path4 =
pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x");
+ assertThat(path1.getLastSegment().getName().getLocalName(), is("z"));
+ assertThat(path2.getLastSegment().getName().getLocalName(), is("c"));
+ assertThat(path3.getLastSegment().getName().getLocalName(), is("c"));
+ assertThat(path4.getLastSegment().getName().getLocalName(), is("x"));
+ }
- @Test
- public void shouldNormalizePathWithSelfAndParentReferences() {
- path = pathFactory.create("/a/b/c/../d/./e/../..");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
- assertThat(path.getNormalizedPath().isAbsolute(), is(true));
- assertThat(path.getNormalizedPath().isNormalized(), is(true));
+ @Test
+ public void shouldNormalizePathWithSelfAndParentReferences() {
+ path = pathFactory.create("/a/b/c/../d/./e/../..");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
+ assertThat(path.getNormalizedPath().isAbsolute(), is(true));
+ assertThat(path.getNormalizedPath().isNormalized(), is(true));
- path = pathFactory.create("a/b/c/../d/./e/../..");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
- assertThat(path.getNormalizedPath().isAbsolute(), is(false));
- assertThat(path.getNormalizedPath().isNormalized(), is(true));
- }
+ path = pathFactory.create("a/b/c/../d/./e/../..");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
+ assertThat(path.getNormalizedPath().isAbsolute(), is(false));
+ assertThat(path.getNormalizedPath().isNormalized(), is(true));
+ }
- @Test
- public void shouldAlreadyBeNormalizedIfPathContainsNoParentOrSelfReferences() {
- assertThat(pathFactory.create("/a/b/c/d/e").isNormalized(), is(true));
- assertThat(pathFactory.create("a/b/c/d/e").isNormalized(), is(true));
- assertThat(pathFactory.create("a").isNormalized(), is(true));
- assertThat(pathFactory.create("/a").isNormalized(), is(true));
- assertThat(ROOT.isNormalized(), is(true));
- }
+ @Test
+ public void shouldAlreadyBeNormalizedIfPathContainsNoParentOrSelfReferences() {
+ assertThat(pathFactory.create("/a/b/c/d/e").isNormalized(), is(true));
+ assertThat(pathFactory.create("a/b/c/d/e").isNormalized(), is(true));
+ assertThat(pathFactory.create("a").isNormalized(), is(true));
+ assertThat(pathFactory.create("/a").isNormalized(), is(true));
+ assertThat(ROOT.isNormalized(), is(true));
+ }
- @Test
- public void shouldNotBeNormalizedIfPathContainsParentOrSelfReferences() {
- assertThat(pathFactory.create("/a/b/c/../d/./e/../..").isNormalized(),
is(false));
- assertThat(pathFactory.create("a/b/c/../d/./e/../..").isNormalized(),
is(false));
- assertThat(pathFactory.create("a/b/c/./d").isNormalized(), is(false));
- assertThat(pathFactory.create("/a/b/c/../d").isNormalized(),
is(false));
- assertThat(pathFactory.create(".").isNormalized(), is(false));
- assertThat(pathFactory.create("/.").isNormalized(), is(false));
- }
+ @Test
+ public void shouldNotBeNormalizedIfPathContainsParentOrSelfReferences() {
+ assertThat(pathFactory.create("/a/b/c/../d/./e/../..").isNormalized(),
is(false));
+ assertThat(pathFactory.create("a/b/c/../d/./e/../..").isNormalized(),
is(false));
+ assertThat(pathFactory.create("a/b/c/./d").isNormalized(), is(false));
+ assertThat(pathFactory.create("/a/b/c/../d").isNormalized(), is(false));
+ assertThat(pathFactory.create(".").isNormalized(), is(false));
+ assertThat(pathFactory.create("/.").isNormalized(), is(false));
+ }
- @Test( expected = InvalidPathException.class )
- public void
shouldFailToReturnNormalizedPathIfPathContainsReferencesToParentsAboveRoot() {
- path = pathFactory.create("/a/../../../..");
- assertThat(path.isNormalized(), is(false));
- path.getNormalizedPath();
- }
+ @Test( expected = InvalidPathException.class )
+ public void shouldFailToReturnNormalizedPathIfPathContainsReferencesToParentsAboveRoot()
{
+ path = pathFactory.create("/a/../../../..");
+ assertThat(path.isNormalized(), is(false));
+ path.getNormalizedPath();
+ }
- @Test
- public void
shouldReturnRootPathAsTheNormalizedPathForAnAbsolutePathWithZeroSegmentsAfterParentAndSelfReferencesRemoved()
{
- // "/a/../b/../c/.." => "/"
- path = pathFactory.create("/a/../b/../c/../");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath(), is(ROOT));
- }
+ @Test
+ public void
shouldReturnRootPathAsTheNormalizedPathForAnAbsolutePathWithZeroSegmentsAfterParentAndSelfReferencesRemoved()
{
+ // "/a/../b/../c/.." => "/"
+ path = pathFactory.create("/a/../b/../c/../");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath(), is(ROOT));
+ }
- @Test
- public void
shouldReturnSelfPathAsTheNormalizedPathForARelativePathWithZeroSegmentsAfterParentAndSelfReferencesRemoved()
{
- // "a/../b/../c/.." => "."
- path = pathFactory.create("a/../b/../c/../");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath().size(), is(1));
- assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "."));
- }
+ @Test
+ public void
shouldReturnSelfPathAsTheNormalizedPathForARelativePathWithZeroSegmentsAfterParentAndSelfReferencesRemoved()
{
+ // "a/../b/../c/.." => "."
+ path = pathFactory.create("a/../b/../c/../");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath().size(), is(1));
+ assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "."));
+ }
- @Test
- public void
shouldNotHaveAnyParentOrSelfReferencesInTheNormalizedPathOfAnAbsolutePath() {
- path = pathFactory.create("/a/b/c/../d/./e/../..");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
- assertThat(path.getNormalizedPath().isAbsolute(), is(true));
- assertThat(path.getNormalizedPath().isNormalized(), is(true));
- }
+ @Test
+ public void shouldNotHaveAnyParentOrSelfReferencesInTheNormalizedPathOfAnAbsolutePath()
{
+ path = pathFactory.create("/a/b/c/../d/./e/../..");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
+ assertThat(path.getNormalizedPath().isAbsolute(), is(true));
+ assertThat(path.getNormalizedPath().isNormalized(), is(true));
+ }
- @Test
- public void shouldNotHaveAnyParentReferencesInTheNormalizedPathOfARelativePath() {
- path = pathFactory.create("a/b/c/../d/./e/../..");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
- assertThat(path.getNormalizedPath().isAbsolute(), is(false));
- assertThat(path.getNormalizedPath().isNormalized(), is(true));
- }
+ @Test
+ public void shouldNotHaveAnyParentReferencesInTheNormalizedPathOfARelativePath() {
+ path = pathFactory.create("a/b/c/../d/./e/../..");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getNormalizedPath(), hasSegments(pathFactory, "a",
"b"));
+ assertThat(path.getNormalizedPath().isAbsolute(), is(false));
+ assertThat(path.getNormalizedPath().isNormalized(), is(true));
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotComputeCanonicalPathOfNodeThatIsNotAbsolute() {
- pathFactory.create("a/b/c/../d/./e/../..").getCanonicalPath();
- }
+ @Test( expected = InvalidPathException.class )
+ public void shouldNotComputeCanonicalPathOfNodeThatIsNotAbsolute() {
+ pathFactory.create("a/b/c/../d/./e/../..").getCanonicalPath();
+ }
- @Test
- public void shouldReturnNormalizedPathForTheCanonicalPathOfAbsolutePath() {
- path = pathFactory.create("/a/b/c/../d/./e/../..");
- assertThat(path.isNormalized(), is(false));
- assertThat(path.isAbsolute(), is(true));
- assertThat(path.getCanonicalPath(), hasSegments(pathFactory, "a",
"b"));
- assertThat(path.getCanonicalPath().isAbsolute(), is(true));
- assertThat(path.getCanonicalPath().isNormalized(), is(true));
- }
+ @Test
+ public void shouldReturnNormalizedPathForTheCanonicalPathOfAbsolutePath() {
+ path = pathFactory.create("/a/b/c/../d/./e/../..");
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.getCanonicalPath(), hasSegments(pathFactory, "a",
"b"));
+ assertThat(path.getCanonicalPath().isAbsolute(), is(true));
+ assertThat(path.getCanonicalPath().isNormalized(), is(true));
+ }
- @Test
- public void shouldReturnSameSegmentsInIteratorAndArrayAndList() {
- testSegmentsByIteratorAndListAndArray("/a/b/c/../d/./e/../..",
"a", "b", "c", "..", "d", ".",
"e", "..", "..");
- testSegmentsByIteratorAndListAndArray("/a/b/c", "a",
"b", "c");
- testSegmentsByIteratorAndListAndArray("a/b/c/../d/./e/../..",
"a", "b", "c", "..", "d", ".",
"e", "..", "..");
- testSegmentsByIteratorAndListAndArray("a/b/c", "a",
"b", "c");
- testSegmentsByIteratorAndListAndArray("");
- testSegmentsByIteratorAndListAndArray(ROOT.getString());
- }
+ @Test
+ public void shouldReturnSameSegmentsInIteratorAndArrayAndList() {
+ testSegmentsByIteratorAndListAndArray("/a/b/c/../d/./e/../..", "a",
"b", "c", "..", "d", ".", "e",
"..", "..");
+ testSegmentsByIteratorAndListAndArray("/a/b/c", "a", "b",
"c");
+ testSegmentsByIteratorAndListAndArray("a/b/c/../d/./e/../..", "a",
"b", "c", "..", "d", ".", "e",
"..", "..");
+ testSegmentsByIteratorAndListAndArray("a/b/c", "a", "b",
"c");
+ testSegmentsByIteratorAndListAndArray("");
+ testSegmentsByIteratorAndListAndArray(ROOT.getString());
+ }
- public void testSegmentsByIteratorAndListAndArray( String pathStr, String...
expectedSegmentStrings ) {
- path = pathFactory.create(pathStr);
- assertThat(expectedSegmentStrings.length, is(path.size()));
- Path.Segment[] segmentArray = path.getSegmentsArray();
- List<Path.Segment> segmentList = path.getSegmentsList();
- assertThat(segmentArray.length, is(path.size()));
- assertThat(segmentList.size(), is(path.size()));
- Iterator<Path.Segment> iter = path.iterator();
- Iterator<Path.Segment> listIter = segmentList.iterator();
- for (int i = 0; i != path.size(); ++i) {
- Path.Segment expected =
pathFactory.createSegment(expectedSegmentStrings[i]);
- assertThat(path.getSegment(i), is(expected));
- assertThat(segmentArray[i], is(expected));
- assertThat(segmentList.get(i), is(expected));
- assertThat(iter.next(), is(expected));
- assertThat(listIter.next(), is(expected));
- }
- assertThat(iter.hasNext(), is(false));
- assertThat(listIter.hasNext(), is(false));
- }
+ public void testSegmentsByIteratorAndListAndArray( String pathStr,
+ String... expectedSegmentStrings ) {
+ path = pathFactory.create(pathStr);
+ assertThat(expectedSegmentStrings.length, is(path.size()));
+ Path.Segment[] segmentArray = path.getSegmentsArray();
+ List<Path.Segment> segmentList = path.getSegmentsList();
+ assertThat(segmentArray.length, is(path.size()));
+ assertThat(segmentList.size(), is(path.size()));
+ Iterator<Path.Segment> iter = path.iterator();
+ Iterator<Path.Segment> listIter = segmentList.iterator();
+ for (int i = 0; i != path.size(); ++i) {
+ Path.Segment expected = pathFactory.createSegment(expectedSegmentStrings[i]);
+ assertThat(path.getSegment(i), is(expected));
+ assertThat(segmentArray[i], is(expected));
+ assertThat(segmentList.get(i), is(expected));
+ assertThat(iter.next(), is(expected));
+ assertThat(listIter.next(), is(expected));
+ }
+ assertThat(iter.hasNext(), is(false));
+ assertThat(listIter.hasNext(), is(false));
+ }
- @Test
- public void shouldGetStringWithNamespaceUrisIfNoNamespaceRegistryIsProvided() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.getString(NO_OP_ENCODER),
is("/{http://www.jboss.org/dna}a/{}b/{http://www.jboss.org/dna}c/../...);
- }
+ @Test
+ public void shouldGetStringWithNamespaceUrisIfNoNamespaceRegistryIsProvided() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.getString(NO_OP_ENCODER),
+
is("/{http://www.jboss.org/dna}a/{}b/{http://www.jboss.org/dna}c/../...);
+ }
- @Test
- public void
shouldGetStringWithNamespacePrefixesForAllNamesIfNamespaceRegistryIsProvided() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.getString(namespaceRegistry, NO_OP_ENCODER),
is("/dna:a/b/dna:c/../d/./dna:e/../.."));
- namespaceRegistry.register("dna2", validNamespaceUri);
- assertThat(path.getString(namespaceRegistry, NO_OP_ENCODER),
is("/dna2:a/b/dna2:c/../d/./dna2:e/../.."));
- }
+ @Test
+ public void
shouldGetStringWithNamespacePrefixesForAllNamesIfNamespaceRegistryIsProvided() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.getString(namespaceRegistry, NO_OP_ENCODER),
is("/dna:a/b/dna:c/../d/./dna:e/../.."));
+ namespaceRegistry.register("dna2", validNamespaceUri);
+ assertThat(path.getString(namespaceRegistry, NO_OP_ENCODER),
is("/dna2:a/b/dna2:c/../d/./dna2:e/../.."));
+ }
- @Test( expected = IndexOutOfBoundsException.class )
- public void shouldFailToReturnSubpathIfStartingIndexIsNegative() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- path.subpath(-1);
- }
+ @Test( expected = IndexOutOfBoundsException.class )
+ public void shouldFailToReturnSubpathIfStartingIndexIsNegative() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ path.subpath(-1);
+ }
- @Test( expected = IndexOutOfBoundsException.class )
- public void
shouldFailToReturnSubpathWithoutEndingIndexIfStartingIndexIsEqualToOrLargerThanSize() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- path.subpath(path.size() + 1);
- }
+ @Test( expected = IndexOutOfBoundsException.class )
+ public void
shouldFailToReturnSubpathWithoutEndingIndexIfStartingIndexIsEqualToOrLargerThanSize() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ path.subpath(path.size() + 1);
+ }
- @Test( expected = IndexOutOfBoundsException.class )
- public void
shouldFailToReturnSubpathWithEndingIndexIfStartingIndexIsEqualToOrLargerThanSize() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- path.subpath(path.size() + 1, path.size() + 2);
- }
+ @Test( expected = IndexOutOfBoundsException.class )
+ public void
shouldFailToReturnSubpathWithEndingIndexIfStartingIndexIsEqualToOrLargerThanSize() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ path.subpath(path.size() + 1, path.size() + 2);
+ }
- @Test( expected = IndexOutOfBoundsException.class )
- public void shouldFailToReturnSubpathIfEndingIndexIsSmallerThanStartingIndex() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- path.subpath(2, 1);
- }
+ @Test( expected = IndexOutOfBoundsException.class )
+ public void shouldFailToReturnSubpathIfEndingIndexIsSmallerThanStartingIndex() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ path.subpath(2, 1);
+ }
- @Test( expected = IndexOutOfBoundsException.class )
- public void shouldFailToReturnSubpathIfEndingIndexIsEqualToOrLargerThanSize() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- path.subpath(2, path.size() + 1);
- }
+ @Test( expected = IndexOutOfBoundsException.class )
+ public void shouldFailToReturnSubpathIfEndingIndexIsEqualToOrLargerThanSize() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ path.subpath(2, path.size() + 1);
+ }
- @Test
- public void shouldReturnRootAsSubpathIfStartingIndexAndEndingIndexAreBothZero() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.subpath(0, 0), is(ROOT));
- }
+ @Test
+ public void shouldReturnRootAsSubpathIfStartingIndexAndEndingIndexAreBothZero() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.subpath(0, 0), is(ROOT));
+ }
- @Test
- public void shouldReturnSubpathIfValidStartingIndexAndNoEndingIndexAreProvided() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.subpath(0), hasSegments(pathFactory, "dna:a",
"b", "dna:c", "..", "d", ".",
"dna:e", "..", ".."));
- assertThat(path.subpath(0), is(path));
- assertThat(path.subpath(0), is(sameInstance(path)));
- assertThat(path.subpath(1), hasSegments(pathFactory, "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
- assertThat(path.subpath(2), hasSegments(pathFactory, "dna:c",
"..", "d", ".", "dna:e", "..",
".."));
- assertThat(path.subpath(3), hasSegments(pathFactory, "..",
"d", ".", "dna:e", "..", ".."));
- assertThat(path.subpath(4), hasSegments(pathFactory, "d",
".", "dna:e", "..", ".."));
- assertThat(path.subpath(5), hasSegments(pathFactory, ".",
"dna:e", "..", ".."));
- assertThat(path.subpath(6), hasSegments(pathFactory, "dna:e",
"..", ".."));
- assertThat(path.subpath(7), hasSegments(pathFactory, "..",
".."));
- assertThat(path.subpath(8), hasSegments(pathFactory, ".."));
+ @Test
+ public void shouldReturnSubpathIfValidStartingIndexAndNoEndingIndexAreProvided() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.subpath(0), hasSegments(pathFactory, "dna:a", "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
+ assertThat(path.subpath(0), is(path));
+ assertThat(path.subpath(0), is(sameInstance(path)));
+ assertThat(path.subpath(1), hasSegments(pathFactory, "b", "dna:c",
"..", "d", ".", "dna:e", "..",
".."));
+ assertThat(path.subpath(2), hasSegments(pathFactory, "dna:c", "..",
"d", ".", "dna:e", "..", ".."));
+ assertThat(path.subpath(3), hasSegments(pathFactory, "..", "d",
".", "dna:e", "..", ".."));
+ assertThat(path.subpath(4), hasSegments(pathFactory, "d", ".",
"dna:e", "..", ".."));
+ assertThat(path.subpath(5), hasSegments(pathFactory, ".", "dna:e",
"..", ".."));
+ assertThat(path.subpath(6), hasSegments(pathFactory, "dna:e", "..",
".."));
+ assertThat(path.subpath(7), hasSegments(pathFactory, "..", ".."));
+ assertThat(path.subpath(8), hasSegments(pathFactory, ".."));
- path = pathFactory.create("dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.subpath(0), hasSegments(pathFactory, "dna:a",
"b", "dna:c", "..", "d", ".",
"dna:e", "..", ".."));
- assertThat(path.subpath(0), is(path));
- assertThat(path.subpath(0), is(sameInstance(path)));
- assertThat(path.subpath(1), hasSegments(pathFactory, "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
- assertThat(path.subpath(2), hasSegments(pathFactory, "dna:c",
"..", "d", ".", "dna:e", "..",
".."));
- assertThat(path.subpath(3), hasSegments(pathFactory, "..",
"d", ".", "dna:e", "..", ".."));
- assertThat(path.subpath(4), hasSegments(pathFactory, "d",
".", "dna:e", "..", ".."));
- assertThat(path.subpath(5), hasSegments(pathFactory, ".",
"dna:e", "..", ".."));
- assertThat(path.subpath(6), hasSegments(pathFactory, "dna:e",
"..", ".."));
- assertThat(path.subpath(7), hasSegments(pathFactory, "..",
".."));
- assertThat(path.subpath(8), hasSegments(pathFactory, ".."));
- }
+ path = pathFactory.create("dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.subpath(0), hasSegments(pathFactory, "dna:a", "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
+ assertThat(path.subpath(0), is(path));
+ assertThat(path.subpath(0), is(sameInstance(path)));
+ assertThat(path.subpath(1), hasSegments(pathFactory, "b", "dna:c",
"..", "d", ".", "dna:e", "..",
".."));
+ assertThat(path.subpath(2), hasSegments(pathFactory, "dna:c", "..",
"d", ".", "dna:e", "..", ".."));
+ assertThat(path.subpath(3), hasSegments(pathFactory, "..", "d",
".", "dna:e", "..", ".."));
+ assertThat(path.subpath(4), hasSegments(pathFactory, "d", ".",
"dna:e", "..", ".."));
+ assertThat(path.subpath(5), hasSegments(pathFactory, ".", "dna:e",
"..", ".."));
+ assertThat(path.subpath(6), hasSegments(pathFactory, "dna:e", "..",
".."));
+ assertThat(path.subpath(7), hasSegments(pathFactory, "..", ".."));
+ assertThat(path.subpath(8), hasSegments(pathFactory, ".."));
+ }
- @Test
- public void shouldReturnSubpathIfValidStartingIndexAndEndingIndexAreProvided() {
- path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
- assertThat(path.subpath(0, path.size()), hasSegments(pathFactory,
"dna:a", "b", "dna:c", "..", "d",
".", "dna:e", "..", ".."));
- assertThat(path.subpath(0, path.size()), is(path));
- assertThat(path.subpath(0, path.size()), is(sameInstance(path)));
- assertThat(path.subpath(1, path.size()), hasSegments(pathFactory, "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
- assertThat(path.subpath(2, path.size()), hasSegments(pathFactory,
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
- assertThat(path.subpath(3, path.size()), hasSegments(pathFactory, "..",
"d", ".", "dna:e", "..", ".."));
- assertThat(path.subpath(4, path.size()), hasSegments(pathFactory, "d",
".", "dna:e", "..", ".."));
- assertThat(path.subpath(5, path.size()), hasSegments(pathFactory, ".",
"dna:e", "..", ".."));
- assertThat(path.subpath(6, path.size()), hasSegments(pathFactory,
"dna:e", "..", ".."));
- assertThat(path.subpath(7, path.size()), hasSegments(pathFactory, "..",
".."));
- assertThat(path.subpath(8, path.size()), hasSegments(pathFactory,
".."));
+ @Test
+ public void shouldReturnSubpathIfValidStartingIndexAndEndingIndexAreProvided() {
+ path = pathFactory.create("/dna:a/b/dna:c/../d/./dna:e/../..");
+ assertThat(path.subpath(0, path.size()), hasSegments(pathFactory,
+ "dna:a",
+ "b",
+ "dna:c",
+ "..",
+ "d",
+ ".",
+ "dna:e",
+ "..",
+ ".."));
+ assertThat(path.subpath(0, path.size()), is(path));
+ assertThat(path.subpath(0, path.size()), is(sameInstance(path)));
+ assertThat(path.subpath(1, path.size()), hasSegments(pathFactory, "b",
"dna:c", "..", "d", ".", "dna:e",
"..", ".."));
+ assertThat(path.subpath(2, path.size()), hasSegments(pathFactory, "dna:c",
"..", "d", ".", "dna:e", "..",
".."));
+ assertThat(path.subpath(3, path.size()), hasSegments(pathFactory, "..",
"d", ".", "dna:e", "..", ".."));
+ assertThat(path.subpath(4, path.size()), hasSegments(pathFactory, "d",
".", "dna:e", "..", ".."));
+ assertThat(path.subpath(5, path.size()), hasSegments(pathFactory, ".",
"dna:e", "..", ".."));
+ assertThat(path.subpath(6, path.size()), hasSegments(pathFactory, "dna:e",
"..", ".."));
+ assertThat(path.subpath(7, path.size()), hasSegments(pathFactory, "..",
".."));
+ assertThat(path.subpath(8, path.size()), hasSegments(pathFactory, ".."));
- assertThat(path.subpath(0, 2), hasSegments(pathFactory, "dna:a",
"b"));
- assertThat(path.subpath(1, 2), hasSegments(pathFactory, "b"));
- assertThat(path.subpath(1, 5), hasSegments(pathFactory, "b",
"dna:c", "..", "d"));
- assertThat(path.subpath(2, 5), hasSegments(pathFactory, "dna:c",
"..", "d"));
- assertThat(path.subpath(3, 5), hasSegments(pathFactory, "..",
"d"));
- }
+ assertThat(path.subpath(0, 2), hasSegments(pathFactory, "dna:a",
"b"));
+ assertThat(path.subpath(1, 2), hasSegments(pathFactory, "b"));
+ assertThat(path.subpath(1, 5), hasSegments(pathFactory, "b",
"dna:c", "..", "d"));
+ assertThat(path.subpath(2, 5), hasSegments(pathFactory, "dna:c",
"..", "d"));
+ assertThat(path.subpath(3, 5), hasSegments(pathFactory, "..",
"d"));
+ }
- @Test
- public void shouldFindRelativePaths() {
- path = pathFactory.create("/a/b/c/d");
- assertThat(path.relativeTo(pathFactory.create("/a/e/f")),
is(pathFactory.create("../../b/c/d")));
- assertThat(path.relativeTo(pathFactory.create("/e/f")),
is(pathFactory.create("../../a/b/c/d")));
+ @Test
+ public void shouldFindRelativePaths() {
+ path = pathFactory.create("/a/b/c/d");
+ assertThat(path.relativeTo(pathFactory.create("/a/e/f")),
is(pathFactory.create("../../b/c/d")));
+ assertThat(path.relativeTo(pathFactory.create("/e/f")),
is(pathFactory.create("../../a/b/c/d")));
- }
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotAllowFindingRelativePathsFromRelativePaths() {
- path = pathFactory.create("a/b/c/d");
- path.relativeTo(pathFactory.create("/e/f"));
+ @Test( expected = InvalidPathException.class )
+ public void shouldNotAllowFindingRelativePathsFromRelativePaths() {
+ path = pathFactory.create("a/b/c/d");
+ path.relativeTo(pathFactory.create("/e/f"));
- }
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotResolveRelativePathToAnotherRelativePath() {
- path = pathFactory.create("/a/b/c/d");
- path.relativeTo(pathFactory.create("e/f"));
+ @Test( expected = InvalidPathException.class )
+ public void shouldNotResolveRelativePathToAnotherRelativePath() {
+ path = pathFactory.create("/a/b/c/d");
+ path.relativeTo(pathFactory.create("e/f"));
- }
+ }
- @Test( expected = InvalidPathException.class )
- public void shouldNotResolveRelativePathUsingAnAbsolutePath() {
- path = pathFactory.create("/a/b/c/d");
- path.resolve(pathFactory.create("/e/f"));
- }
+ @Test( expected = InvalidPathException.class )
+ public void shouldNotResolveRelativePathUsingAnAbsolutePath() {
+ path = pathFactory.create("/a/b/c/d");
+ path.resolve(pathFactory.create("/e/f"));
+ }
- @Test
- public void shouldResolveRelativePathToAbsolutePath() {
- path = pathFactory.create("/a/b/c/d");
- path2 = path.resolve(pathFactory.create("../../e/f"));
- assertThat(path2, is(pathFactory.create("/a/b/e/f")));
- assertThat(path2.isAbsolute(), is(true));
- assertThat(path2.isNormalized(), is(true));
- }
+ @Test
+ public void shouldResolveRelativePathToAbsolutePath() {
+ path = pathFactory.create("/a/b/c/d");
+ path2 = path.resolve(pathFactory.create("../../e/f"));
+ assertThat(path2, is(pathFactory.create("/a/b/e/f")));
+ assertThat(path2.isAbsolute(), is(true));
+ assertThat(path2.isNormalized(), is(true));
+ }
- @Test
- public void shouldOrderPathsCorrectly() {
- List<Path> paths = new ArrayList<Path>();
- paths.add(pathFactory.create("/a"));
- paths.add(pathFactory.create("/a/b"));
- paths.add(pathFactory.create("/a/b/alpha"));
- paths.add(pathFactory.create("/a/b/beta"));
- paths.add(pathFactory.create("/a/b/dna:mixinTypes"));
- paths.add(pathFactory.create("/a/b/dna:name"));
- paths.add(pathFactory.create("/a/b/dna:primaryType"));
- paths.add(pathFactory.create("/a/c[1]"));
- paths.add(pathFactory.create("/a/c[1]/alpha"));
- paths.add(pathFactory.create("/a/c[1]/beta"));
- paths.add(pathFactory.create("/a/c[1]/dna:mixinTypes"));
- paths.add(pathFactory.create("/a/c[1]/dna:name"));
- paths.add(pathFactory.create("/a/c[1]/dna:primaryType"));
- paths.add(pathFactory.create("/a/c[2]"));
- paths.add(pathFactory.create("/a/c[2]/alpha"));
- paths.add(pathFactory.create("/a/c[2]/beta"));
- paths.add(pathFactory.create("/a/c[2]/dna:mixinTypes"));
- paths.add(pathFactory.create("/a/c[2]/dna:name"));
- paths.add(pathFactory.create("/a/c[2]/dna:primaryType"));
+ @Test
+ public void shouldOrderPathsCorrectly() {
+ List<Path> paths = new ArrayList<Path>();
+ paths.add(pathFactory.create("/a"));
+ paths.add(pathFactory.create("/a/b"));
+ paths.add(pathFactory.create("/a/b/alpha"));
+ paths.add(pathFactory.create("/a/b/beta"));
+ paths.add(pathFactory.create("/a/b/dna:mixinTypes"));
+ paths.add(pathFactory.create("/a/b/dna:name"));
+ paths.add(pathFactory.create("/a/b/dna:primaryType"));
+ paths.add(pathFactory.create("/a/c[1]"));
+ paths.add(pathFactory.create("/a/c[1]/alpha"));
+ paths.add(pathFactory.create("/a/c[1]/beta"));
+ paths.add(pathFactory.create("/a/c[1]/dna:mixinTypes"));
+ paths.add(pathFactory.create("/a/c[1]/dna:name"));
+ paths.add(pathFactory.create("/a/c[1]/dna:primaryType"));
+ paths.add(pathFactory.create("/a/c[2]"));
+ paths.add(pathFactory.create("/a/c[2]/alpha"));
+ paths.add(pathFactory.create("/a/c[2]/beta"));
+ paths.add(pathFactory.create("/a/c[2]/dna:mixinTypes"));
+ paths.add(pathFactory.create("/a/c[2]/dna:name"));
+ paths.add(pathFactory.create("/a/c[2]/dna:primaryType"));
- // Randomize the list of paths, so we have something to sort ...
- List<Path> randomizedPaths = new ArrayList<Path>(paths);
- Collections.shuffle(randomizedPaths);
- assertThat(randomizedPaths, is(not(paths)));
+ // Randomize the list of paths, so we have something to sort ...
+ List<Path> randomizedPaths = new ArrayList<Path>(paths);
+ Collections.shuffle(randomizedPaths);
+ assertThat(randomizedPaths, is(not(paths)));
- // Sort ...
- Collections.sort(randomizedPaths);
- assertThat(randomizedPaths, is(paths));
- }
+ // Sort ...
+ Collections.sort(randomizedPaths);
+ assertThat(randomizedPaths, is(paths));
+ }
- @Test
- public void shouldGetNormalizedPathOfSelfShouldBeSame() {
- assertThat(pathFactory.create(".").getNormalizedPath(),
is(pathFactory.create(".")));
- assertThat(pathFactory.create("./").getNormalizedPath(),
is(pathFactory.create(".")));
- assertThat(pathFactory.create("./././").getNormalizedPath(),
is(pathFactory.create(".")));
- }
+ @Test
+ public void shouldGetNormalizedPathOfSelfShouldBeSame() {
+ assertThat(pathFactory.create(".").getNormalizedPath(),
is(pathFactory.create(".")));
+ assertThat(pathFactory.create("./").getNormalizedPath(),
is(pathFactory.create(".")));
+ assertThat(pathFactory.create("./././").getNormalizedPath(),
is(pathFactory.create(".")));
+ }
- @Test
- public void shouldGetNormalizedPathWithParentReferences() {
- assertThat(pathFactory.create("..").getNormalizedPath(),
is(pathFactory.create("..")));
- assertThat(pathFactory.create("../").getNormalizedPath(),
is(pathFactory.create("../")));
- assertThat(pathFactory.create("../../../../../..").getNormalizedPath(),
is(pathFactory.create("../../../../../..")));
- }
+ @Test
+ public void shouldGetNormalizedPathWithParentReferences() {
+ assertThat(pathFactory.create("..").getNormalizedPath(),
is(pathFactory.create("..")));
+ assertThat(pathFactory.create("../").getNormalizedPath(),
is(pathFactory.create("../")));
+ assertThat(pathFactory.create("../../../../../..").getNormalizedPath(),
is(pathFactory.create("../../../../../..")));
+ }
- @Test
- public void shouldGetRelativePathUsingSelf() {
- path = pathFactory.create("/a/b/c/d/e/f");
- assertThat(path.resolve(pathFactory.create(".")),
is(sameInstance(path)));
- assertThat(path.resolve(pathFactory.create("././.")),
is(sameInstance(path)));
- }
+ @Test
+ public void shouldGetRelativePathUsingSelf() {
+ path = pathFactory.create("/a/b/c/d/e/f");
+ assertThat(path.resolve(pathFactory.create(".")), is(sameInstance(path)));
+ assertThat(path.resolve(pathFactory.create("././.")),
is(sameInstance(path)));
+ }
- @Test
- public void shouldResolveRelativePathToParent() {
- path = pathFactory.create("/a/b/c/d/e/f");
- assertThat(path.resolve(pathFactory.create("..")),
is(path.getAncestor()));
- assertThat(path.resolve(pathFactory.create("..")),
hasSegments(pathFactory, "a", "b", "c", "d",
"e"));
- }
+ @Test
+ public void shouldResolveRelativePathToParent() {
+ path = pathFactory.create("/a/b/c/d/e/f");
+ assertThat(path.resolve(pathFactory.create("..")), is(path.getAncestor()));
+ assertThat(path.resolve(pathFactory.create("..")), hasSegments(pathFactory,
"a", "b", "c", "d", "e"));
+ }
- @Test
- public void shouldResolveRelativePaths() {
- path = pathFactory.create("/a/b/c/d/e/f");
- assertThat(path.resolve(pathFactory.create("../../../../../..")),
is(sameInstance(ROOT)));
- assertThat(path.resolve(pathFactory.create("../..")),
is(path.getAncestor().getAncestor()));
- assertThat(path.resolve(pathFactory.create("../..")),
hasSegments(pathFactory, "a", "b", "c", "d"));
- assertThat(path.resolve(pathFactory.create("../x/../y/../z/..")),
is(path.getAncestor()));
- assertThat(path.resolve(pathFactory.create("../x/../y/../z/..")),
hasSegments(pathFactory, "a", "b", "c", "d",
"e"));
- assertThat(path.resolve(pathFactory.create("../x")),
hasSegments(pathFactory, "a", "b", "c", "d",
"e", "x"));
- }
+ @Test
+ public void shouldResolveRelativePaths() {
+ path = pathFactory.create("/a/b/c/d/e/f");
+ assertThat(path.resolve(pathFactory.create("../../../../../..")),
is(sameInstance(ROOT)));
+ assertThat(path.resolve(pathFactory.create("../..")),
is(path.getAncestor().getAncestor()));
+ assertThat(path.resolve(pathFactory.create("../..")),
hasSegments(pathFactory, "a", "b", "c", "d"));
+ assertThat(path.resolve(pathFactory.create("../x/../y/../z/..")),
is(path.getAncestor()));
+ assertThat(path.resolve(pathFactory.create("../x/../y/../z/..")),
hasSegments(pathFactory, "a", "b", "c", "d",
"e"));
+ assertThat(path.resolve(pathFactory.create("../x")), hasSegments(pathFactory,
"a", "b", "c", "d", "e",
"x"));
+ }
- public void shouldResolveNonAbsolutePaths() {
- path = pathFactory.create("a/b/c");
- assertThat(path, hasSegments(pathFactory, "a", "b",
"c"));
- }
+ public void shouldResolveNonAbsolutePaths() {
+ path = pathFactory.create("a/b/c");
+ assertThat(path, hasSegments(pathFactory, "a", "b",
"c"));
+ }
}