Author: rhauch
Date: 2009-03-03 15:34:42 -0500 (Tue, 03 Mar 2009)
New Revision: 748
Modified:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrItem.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrI18n.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNode.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRootNode.java
trunk/dna-jcr/src/main/resources/org/jboss/dna/jcr/JcrI18n.properties
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrItemTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java
Log:
DNA-287 AbstractJcrNode.getAncestor Does Not Match JCR 1.0 Specification
Applied the patch that corrects the behavior of get ancestor, which per the spec is
absolute and not relative (as was implemented).
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrItem.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrItem.java 2009-03-03 19:20:59
UTC (rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrItem.java 2009-03-03 20:34:42
UTC (rev 748)
@@ -24,6 +24,7 @@
package org.jboss.dna.jcr;
import javax.jcr.Item;
+import javax.jcr.ItemNotFoundException;
import javax.jcr.RepositoryException;
/**
@@ -74,6 +75,45 @@
/**
* {@inheritDoc}
*
+ * @see javax.jcr.Item#getAncestor(int)
+ */
+ public final Item getAncestor( int depth ) throws RepositoryException {
+ if (depth < 0) {
+ throw new ItemNotFoundException(JcrI18n.noNegativeDepth.text(depth));
+ }
+
+ /*
+ * depth argument is absolute depth from root of content graph, not relative
depth from current node.
+ * That is, if current node is at path /foo/bar/baz, getAncestor(1) returns node
at /foo, getAncestor(2)
+ * returns node at /foo/bar, getAncestor(3) returns node at /foo/bar/baz,
getAncestor(0) returns root node,
+ * and any other argument results in ItemNotFoundException.
+ * Next statement converts depth parameter from a relative depth to an absolute
depth.
+ */
+ depth = getDepth() - depth;
+
+ if (depth < 0) {
+ throw new ItemNotFoundException(JcrI18n.tooDeep.text(depth));
+ }
+
+ Item ancestor = this;
+ while (--depth >= 0) {
+ ancestor = ancestor.getParent();
+ }
+ return ancestor;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Item#getDepth()
+ */
+ public int getDepth() throws RepositoryException {
+ return getParent().getDepth() + 1;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @throws UnsupportedOperationException always
* @see javax.jcr.Item#refresh(boolean)
*/
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java 2009-03-03 19:20:59
UTC (rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java 2009-03-03 20:34:42
UTC (rev 748)
@@ -165,21 +165,6 @@
/**
* {@inheritDoc}
*
- * @throws IllegalArgumentException if <code>depth</code> is negative.
- * @see javax.jcr.Item#getAncestor(int)
- */
- public final Item getAncestor( int depth ) throws RepositoryException {
- CheckArg.isNonNegative(depth, "depth");
- Node ancestor = this;
- while (--depth >= 0) {
- ancestor = ancestor.getParent();
- }
- return ancestor;
- }
-
- /**
- * {@inheritDoc}
- *
* @throws UnsupportedRepositoryOperationException always
* @see javax.jcr.Node#getBaseVersion()
*/
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java 2009-03-03
19:20:59 UTC (rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java 2009-03-03
20:34:42 UTC (rev 748)
@@ -88,25 +88,6 @@
return new JcrValue<T>(valueFactories, propertyType,
valueClass.cast(value));
}
- /**
- * {@inheritDoc}
- *
- * @throws IllegalArgumentException if <code>depth</code> is negative.
- * @see javax.jcr.Item#getAncestor(int)
- */
- public final Item getAncestor( int depth ) throws RepositoryException {
- return (depth == 0 ? this : node.getAncestor(depth - 1));
- }
-
- /**
- * {@inheritDoc}
- *
- * @see javax.jcr.Item#getDepth()
- */
- public int getDepth() throws RepositoryException {
- return getParent().getDepth() + 1;
- }
-
final ExecutionContext getExecutionContext() {
return executionContext;
}
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrI18n.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrI18n.java 2009-03-03 19:20:59 UTC
(rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrI18n.java 2009-03-03 20:34:42 UTC
(rev 748)
@@ -61,7 +61,11 @@
public static I18n invalidPathParameter;
public static I18n typeNotFound;
-
+
+ // Used in AbstractJcrNode#getAncestor
+ public static I18n noNegativeDepth;
+ public static I18n tooDeep;
+
public static I18n REP_NAME_DESC;
public static I18n REP_VENDOR_DESC;
public static I18n SPEC_NAME_DESC;
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNode.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNode.java 2009-03-03 19:20:59 UTC
(rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNode.java 2009-03-03 20:34:42 UTC
(rev 748)
@@ -52,15 +52,6 @@
/**
* {@inheritDoc}
*
- * @see javax.jcr.Item#getDepth()
- */
- public int getDepth() throws RepositoryException {
- return getParent().getDepth() + 1;
- }
-
- /**
- * {@inheritDoc}
- *
* @see javax.jcr.Node#getIndex()
*/
public int getIndex() {
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRootNode.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRootNode.java 2009-03-03 19:20:59 UTC
(rev 747)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRootNode.java 2009-03-03 20:34:42 UTC
(rev 748)
@@ -43,6 +43,7 @@
* @return 0;
* @see javax.jcr.Item#getDepth()
*/
+ @Override
public int getDepth() {
return 0;
}
Modified: trunk/dna-jcr/src/main/resources/org/jboss/dna/jcr/JcrI18n.properties
===================================================================
--- trunk/dna-jcr/src/main/resources/org/jboss/dna/jcr/JcrI18n.properties 2009-03-03
19:20:59 UTC (rev 747)
+++ trunk/dna-jcr/src/main/resources/org/jboss/dna/jcr/JcrI18n.properties 2009-03-03
20:34:42 UTC (rev 748)
@@ -59,3 +59,6 @@
workspaceNameIsInvalid = "{1}" is not a valid workspace name for the
"{0}" repository
errorVerifyingWorkspaceName =Error validating the workspace name "{1}" for the
"{0}" repository\: {2}
typeNotFound=No type exists with name "{0}"
+
+noNegativeDepth=Depth parameter ({0}) cannot be negative
+tooDeep=Depth parameter ({0}) cannot be greater than the result of getDepth() for this
node
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrItemTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrItemTest.java 2009-03-03
19:20:59 UTC (rev 747)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrItemTest.java 2009-03-03
20:34:42 UTC (rev 748)
@@ -25,7 +25,6 @@
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-import javax.jcr.Item;
import javax.jcr.ItemVisitor;
import javax.jcr.Node;
import javax.jcr.Session;
@@ -46,14 +45,6 @@
public void accept( ItemVisitor visitor ) {
}
- public Item getAncestor( int depth ) {
- return null;
- }
-
- public int getDepth() {
- return 0;
- }
-
public String getName() {
return null;
}
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java 2009-03-03
19:20:59 UTC (rev 747)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java 2009-03-03
20:34:42 UTC (rev 748)
@@ -86,6 +86,7 @@
this.parent = parent;
}
+ @Override
public int getDepth() {
return 0;
}
@@ -143,7 +144,7 @@
new MockAbstractJcrNode(null, null, null);
}
- @Test( expected = IllegalArgumentException.class )
+ @Test( expected = ItemNotFoundException.class )
public void shouldNotAllowNegativeAncestorDepth() throws Exception {
node.getAncestor(-1);
}
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java 2009-03-03
19:20:59 UTC (rev 747)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java 2009-03-03
20:34:42 UTC (rev 748)
@@ -98,7 +98,7 @@
new MockAbstractJcrProperty(node, executionContext, null);
}
- @Test( expected = IllegalArgumentException.class )
+ @Test( expected = ItemNotFoundException.class )
public void shouldNotAllowNegativeAncestorDepth() throws Exception {
stub(node.getAncestor(-2)).toThrow(new IllegalArgumentException());
prop.getAncestor(-1);
@@ -106,9 +106,9 @@
@Test
public void shouldProvideAncestor() throws Exception {
- assertThat(prop.getAncestor(0), is((Item)prop));
- stub(node.getAncestor(0)).toReturn(node);
- assertThat(prop.getAncestor(1), is((Item)node));
+ assertThat(prop.getAncestor(prop.getDepth()), is((Item)prop));
+ stub(node.getAncestor(node.getDepth())).toReturn(node);
+ assertThat(prop.getAncestor(prop.getDepth() - 1), is((Item)node));
}
@Test( expected = ItemNotFoundException.class )