Author: rhauch
Date: 2009-03-12 15:19:34 -0400 (Thu, 12 Mar 2009)
New Revision: 773
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/AbstractPath.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/ChildPath.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/BasicPathTest.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/ChildPathTest.java
Log:
DNA-299 Path.isNormalized() is incorrect if the path begins with parent reference(s)
Added the tests to verify it was failing, then corrected the logic in all Path
implementations (although RootPath was correct, since it always considered itself
normalized).
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/AbstractPath.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/AbstractPath.java 2009-03-12
16:10:03 UTC (rev 772)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/AbstractPath.java 2009-03-12
19:19:34 UTC (rev 773)
@@ -126,8 +126,16 @@
private transient int hc = 0;
protected boolean isNormalized( List<Segment> segments ) {
+ boolean nonParentReference = false;
+ boolean first = isAbsolute(); // only care about first one when it's
absolute
for (Segment segment : segments) {
- if (segment.isSelfReference() || segment.isParentReference()) return false;
+ if (segment.isSelfReference()) return false;
+ if (segment.isParentReference()) {
+ if (nonParentReference || first) return false;
+ } else {
+ nonParentReference = true;
+ }
+ first = false;
}
return true;
}
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/ChildPath.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/ChildPath.java 2009-03-12
16:10:03 UTC (rev 772)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/ChildPath.java 2009-03-12
19:19:34 UTC (rev 773)
@@ -187,8 +187,15 @@
* @see org.jboss.dna.graph.property.Path#isNormalized()
*/
public boolean isNormalized() {
- if (child.isParentReference() || child.isSelfReference()) return false;
- return parent.isNormalized();
+ if (child.isSelfReference()) return false;
+ if (!parent.isNormalized()) return false;
+ // Otherwise, the parent is normalized, so this child will be normalized if this
child is not a parent reference ...
+ if (!child.isParentReference()) return true;
+ // The path ends with a parent reference. It is normalized only if all other path
segments are parent references ...
+ for (Path.Segment segment : parent) {
+ if (!segment.isParentReference()) return false;
+ }
+ return true;
}
/**
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/BasicPathTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/BasicPathTest.java 2009-03-12
16:10:03 UTC (rev 772)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/BasicPathTest.java 2009-03-12
19:19:34 UTC (rev 773)
@@ -102,7 +102,7 @@
}
@Test
- public void shouldCreateAbsolutePathWithParentSegment() {
+ public void shouldConsiderAsNotNormalizedAnAbsolutePathWithParentSegmentAtEnd() {
validSegmentsList.add(Path.PARENT_SEGMENT);
path = new BasicPath(validSegmentsList, true);
assertThat(path.isAbsolute(), is(true));
@@ -112,7 +112,7 @@
}
@Test
- public void shouldCreateRelativePathWithParentSegment() {
+ public void shouldConsiderAsNotNormalizedARelativePathWithParentSegmentAtEnd() {
validSegmentsList.add(Path.PARENT_SEGMENT);
path = new BasicPath(validSegmentsList, false);
assertThat(path.isAbsolute(), is(false));
@@ -122,6 +122,82 @@
}
@Test
+ public void shouldConsiderAsNotNormalizedAnAbsolutePathWithParentSegmentAtFront() {
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ segments.add(Path.PARENT_SEGMENT);
+ segments.addAll(validSegmentsList);
+ path = new BasicPath(segments, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(segments));
+ assertThat(path.size(), is(segments.size()));
+ }
+
+ @Test
+ public void shouldConsiderAsNormalizedARelativePathWithParentSegmentAtFront() {
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ segments.add(Path.PARENT_SEGMENT);
+ segments.addAll(validSegmentsList);
+ path = new BasicPath(segments, false);
+ assertThat(path.isAbsolute(), is(false));
+ assertThat(path.isNormalized(), is(true));
+ assertThat(path.getSegmentsList(), is(segments));
+ assertThat(path.size(), is(segments.size()));
+ }
+
+ @Test
+ public void shouldConsiderAsNotNormalizedAnAbsolutePathWithAllParentReferences() {
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ for (int i = 0; i != 10; ++i) {
+ segments.add(Path.PARENT_SEGMENT);
+ }
+ path = new BasicPath(segments, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(segments));
+ assertThat(path.size(), is(segments.size()));
+ }
+
+ @Test
+ public void shouldConsiderAsNormalizedARelativePathWithAllParentReferences() {
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ for (int i = 0; i != 10; ++i) {
+ segments.add(Path.PARENT_SEGMENT);
+ }
+ path = new BasicPath(segments, false);
+ assertThat(path.isAbsolute(), is(false));
+ assertThat(path.isNormalized(), is(true));
+ assertThat(path.getSegmentsList(), is(segments));
+ assertThat(path.size(), is(segments.size()));
+ }
+
+ @Test
+ public void
shouldConsiderAsNotNormalizedPathWithMostParentReferencesAndOneNonParentReferenceInMiddle()
{
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ segments.add(Path.PARENT_SEGMENT);
+ segments.add(Path.PARENT_SEGMENT);
+ segments.add(pathFactory.createSegment("nonParentSegment"));
+ segments.add(Path.PARENT_SEGMENT);
+ segments.add(Path.PARENT_SEGMENT);
+ path = new BasicPath(segments, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ assertThat(path.getSegmentsList(), is(segments));
+ assertThat(path.size(), is(segments.size()));
+ }
+
+ @Test
+ public void
shouldConsiderAsNotNormalizedAnAbsolutePathThatBeginsWithParentReference() {
+ List<Path.Segment> segments = new ArrayList<Path.Segment>();
+ segments.add(Path.PARENT_SEGMENT);
+ segments.add(pathFactory.createSegment("nonParentSegment"));
+ segments.add(pathFactory.createSegment("nonParentSegment2"));
+ path = new BasicPath(segments, true);
+ assertThat(path.isAbsolute(), is(true));
+ assertThat(path.isNormalized(), is(false));
+ }
+
+ @Test
public void shouldCreateAbsolutePathWithSelfSegment() {
validSegmentsList.add(Path.SELF_SEGMENT);
path = new BasicPath(validSegmentsList, true);
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/ChildPathTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/ChildPathTest.java 2009-03-12
16:10:03 UTC (rev 772)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/ChildPathTest.java 2009-03-12
19:19:34 UTC (rev 773)
@@ -35,11 +35,6 @@
import java.util.List;
import org.jboss.dna.graph.property.Name;
import org.jboss.dna.graph.property.Path;
-import org.jboss.dna.graph.property.basic.BasicName;
-import org.jboss.dna.graph.property.basic.BasicPath;
-import org.jboss.dna.graph.property.basic.BasicPathSegment;
-import org.jboss.dna.graph.property.basic.ChildPath;
-import org.jboss.dna.graph.property.basic.RootPath;
import org.junit.Before;
import org.junit.Test;
@@ -163,6 +158,41 @@
assertThat(path.getParent(), is(sameInstance(parent)));
}
+ @Test
+ public void shouldConsiderAsNotNormalizedAPathWithParentSegmentAtEnd() {
+ path = new ChildPath(parent, Path.PARENT_SEGMENT);
+ assertThat(path.isAbsolute(), is(parent.isAbsolute()));
+ assertThat(path.isNormalized(), is(false));
+ }
+
+ @Test
+ public void shouldConsiderAsNormalizedARelativePathWithParentSegmentAtFront() {
+ parent = path("../../a/b/c/d");
+ path = new ChildPath(parent, segment("e"));
+ assertThat(path.isNormalized(), is(true));
+ }
+
+ @Test
+ public void shouldConsiderAsNormalizedAnAbsolutePathWithParentSegmentAtFront() {
+ parent = path("/../a/b");
+ path = new ChildPath(parent, segment("c"));
+ assertThat(path.isNormalized(), is(false));
+ }
+
+ @Test
+ public void shouldConsiderAsNormalizedPathWithAllParentReferences() {
+ parent = path("../../../../..");
+ path = new ChildPath(parent, Path.PARENT_SEGMENT);
+ assertThat(path.isNormalized(), is(true));
+ }
+
+ @Test
+ public void
shouldConsiderAsNotNormalizedPathWithMostParentReferencesAndOneNonParentReferenceInMiddle()
{
+ parent = path("../../a/b/../..");
+ path = new ChildPath(parent, Path.PARENT_SEGMENT);
+ assertThat(path.isNormalized(), is(false));
+ }
+
// @Test
// public void shouldReturnRootForLowestCommonAncestorWithAnyNodePath() {
// Path other = mock(Path.class);