[dna-commits] DNA SVN: r769 - in trunk: dna-graph/src/main/java/org/jboss/dna/graph/property/basic and 3 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Tue Mar 10 15:43:15 EDT 2009


Author: rhauch
Date: 2009-03-10 15:43:15 -0400 (Tue, 10 Mar 2009)
New Revision: 769

Modified:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/Path.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/PathFactory.java
   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/PathValueFactory.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/RootPath.java
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/AbstractPathTest.java
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/RootPathTest.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrChildNodeIterator.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSingleValuePropertyTest.java
Log:
DNA-194 Implement update JCR capability

Small refactoring to make the loading of nodes more efficient.  This includes the addition of a new method on Path to obtain an Iterator<Path> from the root node to that instance.

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/Path.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/Path.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/Path.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -495,6 +495,14 @@
     public Iterator<Segment> iterator();
 
     /**
+     * Return an iterator that walks the paths from the root path down to this path. This method always returns at least one path
+     * (the root returns an iterator containing itself).
+     * 
+     * @return the path iterator; never null
+     */
+    public Iterator<Path> pathsFromRoot();
+
+    /**
      * Obtain a copy of the segments in this path. None of the segments are encoded.
      * 
      * @return the array of segments as a copy

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/PathFactory.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/PathFactory.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/PathFactory.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -133,6 +133,19 @@
                  int index );
 
     /**
+     * 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,
+                 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

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-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/AbstractPath.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -29,6 +29,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.NoSuchElementException;
+import net.jcip.annotations.NotThreadSafe;
 import org.jboss.dna.common.CommonI18n;
 import org.jboss.dna.common.text.TextEncoder;
 import org.jboss.dna.common.util.CheckArg;
@@ -83,6 +84,45 @@
         }
     };
 
+    @NotThreadSafe
+    protected static class SingleIterator<T> implements Iterator<T> {
+        private T value;
+
+        protected SingleIterator( T value ) {
+            this.value = value;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Iterator#hasNext()
+         */
+        public boolean hasNext() {
+            return value != null;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Iterator#next()
+         */
+        public T next() {
+            if (value == null) throw new NoSuchElementException();
+            T next = value;
+            value = null;
+            return next;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Iterator#remove()
+         */
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
     private transient int hc = 0;
 
     protected boolean isNormalized( List<Segment> segments ) {
@@ -368,7 +408,23 @@
 
     /**
      * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.property.Path#pathsFromRoot()
      */
+    public Iterator<Path> pathsFromRoot() {
+        LinkedList<Path> paths = new LinkedList<Path>();
+        Path path = this;
+        while (path != null) {
+            paths.addFirst(path);
+            if (path.isRoot()) break;
+            path = path.getParent();
+        }
+        return paths.iterator();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public Path relativeTo( Path startingPath ) {
         CheckArg.isNotNull(startingPath, "to");
         if (!this.isAbsolute()) {

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/PathValueFactory.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/PathValueFactory.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/PathValueFactory.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -401,8 +401,7 @@
     /**
      * {@inheritDoc}
      * 
-     * @see org.jboss.dna.graph.property.PathFactory#create(org.jboss.dna.graph.property.Path,
-     *      org.jboss.dna.graph.property.Path)
+     * @see org.jboss.dna.graph.property.PathFactory#create(org.jboss.dna.graph.property.Path, org.jboss.dna.graph.property.Path)
      */
     public Path create( Path parentPath,
                         Path childPath ) {
@@ -438,8 +437,19 @@
 
     /**
      * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.property.PathFactory#create(org.jboss.dna.graph.property.Path, java.lang.String, int)
      */
     public Path create( Path parentPath,
+                        String segmentName,
+                        int index ) {
+        return create(parentPath, nameValueFactory.create(segmentName), index);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Path create( Path parentPath,
                         Name... segmentNames ) {
         CheckArg.isNotNull(parentPath, "parent path");
         if (segmentNames == null || segmentNames.length == 0) return parentPath;

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/RootPath.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/RootPath.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/property/basic/RootPath.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -342,6 +342,16 @@
     /**
      * {@inheritDoc}
      * 
+     * @see org.jboss.dna.graph.property.Path#pathsFromRoot()
+     */
+    @Override
+    public Iterator<Path> pathsFromRoot() {
+        return new SingleIterator<Path>(this);
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
      * @see org.jboss.dna.graph.property.Path#size()
      */
     public int size() {

Modified: trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/AbstractPathTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/AbstractPathTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/AbstractPathTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -30,7 +30,9 @@
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.stub;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import org.jboss.dna.common.text.TextEncoder;
 import org.jboss.dna.graph.property.InvalidPathException;
 import org.jboss.dna.graph.property.NamespaceRegistry;
@@ -231,4 +233,18 @@
         assertThat(path.toString(), is(notNullValue()));
     }
 
+    @Test
+    public void shouldGetPathsFromRoot() {
+        Iterator<Path> iter = path.pathsFromRoot();
+        List<Path.Segment> segments = path.getSegmentsList();
+        List<Path.Segment> lastSegments = new ArrayList<Path.Segment>();
+        while (iter.hasNext()) {
+            Path next = iter.next();
+            assertThat(next, is(notNullValue()));
+            if (!next.isRoot()) lastSegments.add(next.getLastSegment());
+        }
+        assertThat(lastSegments.size(), is(path.size()));
+        assertThat(lastSegments, is(segments));
+    }
+
 }

Modified: trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/RootPathTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/RootPathTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/property/basic/RootPathTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -30,15 +30,12 @@
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.stub;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import org.jboss.dna.common.text.TextEncoder;
 import org.jboss.dna.graph.property.InvalidPathException;
 import org.jboss.dna.graph.property.NamespaceRegistry;
 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.RootPath;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -253,4 +250,12 @@
         assertThat(root.equals(root), is(true));
     }
 
+    @Test
+    public void shouldReturnIteratorWithRootPathFromPathsFromRoot() {
+        Iterator<Path> iter = root.pathsFromRoot();
+        assertThat(iter.hasNext(), is(true));
+        assertThat(iter.next(), is(root));
+        assertThat(iter.hasNext(), is(false));
+    }
+
 }

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-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -58,7 +58,6 @@
 import org.jboss.dna.graph.property.NamespaceRegistry;
 import org.jboss.dna.graph.property.Path;
 import org.jboss.dna.graph.property.ValueFormatException;
-import org.jboss.dna.graph.property.Path.Segment;
 
 /**
  * @author jverhaeg
@@ -72,7 +71,7 @@
     private final NodeDefinition definition;
     protected Location location;
     private Map<Name, Property> properties;
-    private List<Path.Segment> children;
+    private List<Location> children;
 
     AbstractJcrNode( JcrSession session,
                      Location location,
@@ -114,7 +113,7 @@
         location = location.with(uuid);
     }
 
-    final void setChildren( List<Segment> children ) {
+    final void setChildren( List<Location> children ) {
         assert children != null;
         this.children = children;
     }
@@ -418,8 +417,9 @@
                                               .getValueFactories()
                                               .getPathFactory()
                                               .createSegment(relativePath);
-                for (Path.Segment child : children) {
-                    if (child.equals(segment)) return true;
+                for (Location child : children) {
+                    Path.Segment childSegment = child.getPath().getLastSegment();
+                    if (childSegment.equals(segment)) return true;
                 }
             } catch (ValueFormatException e) {
                 throw new RepositoryException(JcrI18n.invalidRelativePath.text(relativePath));
@@ -462,7 +462,7 @@
         if (children == null) {
             return new JcrEmptyNodeIterator();
         }
-        return new JcrChildNodeIterator(this, namespaces(), children);
+        return new JcrChildNodeIterator(this, children);
     }
 
     /**
@@ -479,11 +479,11 @@
         List<Object> patterns = createPatternsFor(namePattern);
 
         // Implementing exact-matching only for now to prototype types as properties
-        List<Path.Segment> matchingChildren = new LinkedList<Path.Segment>();
+        List<Location> matchingChildren = new LinkedList<Location>();
         NamespaceRegistry registry = namespaces();
         boolean foundMatch = false;
-        for (Path.Segment child : children) {
-            String childName = child.getName().getString(registry);
+        for (Location child : children) {
+            String childName = child.getPath().getLastSegment().getName().getString(registry);
             for (Object patternOrMatch : patterns) {
                 if (patternOrMatch instanceof Pattern) {
                     Pattern pattern = (Pattern)patternOrMatch;
@@ -499,7 +499,7 @@
                 }
             }
         }
-        return new JcrChildNodeIterator(this, registry, matchingChildren);
+        return new JcrChildNodeIterator(this, matchingChildren);
     }
 
     /**

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrChildNodeIterator.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrChildNodeIterator.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrChildNodeIterator.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -30,8 +30,7 @@
 import javax.jcr.RepositoryException;
 import net.jcip.annotations.Immutable;
 import org.jboss.dna.common.util.CheckArg;
-import org.jboss.dna.graph.property.NamespaceRegistry;
-import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.Location;
 
 /**
  * @author jverhaeg
@@ -39,20 +38,18 @@
 @Immutable
 final class JcrChildNodeIterator implements NodeIterator {
 
-    private final NamespaceRegistry registry;
-    private final Node parent;
-    private final Iterator<Path.Segment> iterator;
+    private final AbstractJcrNode parent;
+    private final Iterator<Location> iterator;
+    private final JcrSession session;
     private int ndx;
     private int size;
 
-    JcrChildNodeIterator( Node parent,
-                          NamespaceRegistry registry,
-                          List<Path.Segment> children ) {
+    JcrChildNodeIterator( AbstractJcrNode parent,
+                          List<Location> children ) {
         assert parent != null;
-        assert registry != null;
         assert children != null;
-        this.registry = registry;
         this.parent = parent;
+        this.session = parent.session();
         iterator = children.iterator();
         size = children.size();
     }
@@ -99,11 +96,10 @@
      * @see javax.jcr.NodeIterator#nextNode()
      */
     public Node nextNode() {
-        Path.Segment childSegment = iterator.next();
+        Location location = iterator.next();
         ndx++;
-        String childName = childSegment.getString(registry);
         try {
-            return parent.getNode(childName);
+            return session.getChild(parent, location);
         } catch (RepositoryException error) {
             throw new RuntimeException(error);
         }

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -123,10 +123,10 @@
      */
     private final Map<String, Object> sessionAttributes;
 
-    private final ReferenceMap<UUID, Node> nodesByUuid;
-    private final ReferenceMap<String, Node> nodesByJcrUuid;
+    private final ReferenceMap<UUID, AbstractJcrNode> nodesByUuid;
+    private final ReferenceMap<String, AbstractJcrNode> nodesByJcrUuid;
     private boolean isLive;
-    private AbstractJcrNode rootNode;
+    private JcrRootNode rootNode;
     private PropertyDefinition anyMultiplePropertyDefinition;
     private transient org.jboss.dna.graph.property.Property defaultPrimaryType;
 
@@ -153,8 +153,8 @@
                                   this.repository.getConnectionFactory(),
                                   this.executionContext);
 
-        this.nodesByUuid = new ReferenceMap<UUID, Node>(ReferenceType.STRONG, ReferenceType.SOFT);
-        this.nodesByJcrUuid = new ReferenceMap<String, Node>(ReferenceType.STRONG, ReferenceType.SOFT);
+        this.nodesByUuid = new ReferenceMap<UUID, AbstractJcrNode>(ReferenceType.STRONG, ReferenceType.SOFT);
+        this.nodesByJcrUuid = new ReferenceMap<String, AbstractJcrNode>(ReferenceType.STRONG, ReferenceType.SOFT);
         this.isLive = true;
 
         assert this.repository != null;
@@ -182,7 +182,7 @@
      * 
      * @return nodesByUuid
      */
-    Map<UUID, Node> getNodesByUuid() {
+    Map<UUID, AbstractJcrNode> getNodesByUuid() {
         return Collections.unmodifiableMap(nodesByUuid);
     }
 
@@ -394,26 +394,16 @@
         }
         // Since we don't know whether path refers to a node or a property, look to see if we can tell it's a node ...
         if (path.getLastSegment().hasIndex()) {
-            try {
-                return getNode(path);
-            } catch (org.jboss.dna.graph.property.PathNotFoundException e) {
-                // If the node isn't found, throw a PathNotFoundException
-                throw new PathNotFoundException(JcrI18n.pathNotFound.text(path));
-            }
+            return getNode(path);
         }
         // We can't tell from the name, so try a node first ...
         try {
             return getNode(path);
-        } catch (org.jboss.dna.graph.property.PathNotFoundException e) {
-            // A node was not found, so treat look for a node using the parent as the node's path ...
+        } catch (PathNotFoundException e) {
+            // A node was not found, so look for a node using the parent as the node's path ...
             Path parentPath = path.getParent();
             Name propertyName = path.getLastSegment().getName();
-            try {
-                return getNode(parentPath).getProperty(propertyName.getString(namespaces()));
-            } catch (org.jboss.dna.graph.property.PathNotFoundException e2) {
-                // If the node isn't found, throw a PathNotFoundException
-                throw new PathNotFoundException(JcrI18n.pathNotFound.text(path));
-            }
+            return getNode(parentPath).getProperty(propertyName.getString(namespaces()));
         }
     }
 
@@ -427,19 +417,65 @@
         throw new UnsupportedOperationException();
     }
 
+    Node getChild( AbstractJcrNode parent,
+                   Location location ) throws RepositoryException {
+        Node child = null;
+        UUID uuid = location.getUuid();
+        if (uuid != null) {
+            // The location has a UUID, so look up the node by this UUID in the session ...
+            child = getNode(uuid);
+        }
+        if (child == null) {
+            // The child was not found in the session's cache, so create the node using its path ...
+            child = loadNode(parent, location.getPath());
+        }
+        return child;
+    }
+
     /**
      * Find or create a JCR Node for the given path. This method works for the root node, too.
      * 
      * @param path the path; may not be null
      * @return the JCR node instance for the given path; never null
+     * @throws PathNotFoundException if the path could not be found
      * @throws RepositoryException if there is a problem
      */
-    private Node getNode( Path path ) throws RepositoryException {
+    Node getNode( Path path ) throws RepositoryException, PathNotFoundException {
+        if (path.isRoot()) return rootNode();
+
+        // Start at the root and walk down the path ...
+        AbstractJcrNode parent = rootNode();
+        AbstractJcrNode node = null;
+        Iterator<Path> pathIter = path.pathsFromRoot();
+        while (pathIter.hasNext()) {
+            node = loadNode(parent, pathIter.next()); // should throw PathNotFoundException if not there
+            parent = node;
+        }
+        return node;
+    }
+
+    /**
+     * Find or create a JCR Node for the given path. This method works for the root node, too.
+     * 
+     * @param parent the parent of the node, if known; null if the parent is not known
+     * @param path the path; may not be null
+     * @return the JCR node instance for the given path; never null
+     * @throws PathNotFoundException if the path could not be found
+     * @throws RepositoryException if there is a problem
+     */
+    private AbstractJcrNode loadNode( AbstractJcrNode parent,
+                                      Path path ) throws RepositoryException, PathNotFoundException {
         boolean isRoot = path.isRoot();
         if (isRoot && rootNode != null) return rootNode;
 
         // Get node from source and get it's UUID ...
-        org.jboss.dna.graph.Node graphNode = graph.getNodeAt(path);
+        org.jboss.dna.graph.Node graphNode = null;
+        try {
+            graphNode = graph.getNodeAt(path);
+        } catch (org.jboss.dna.graph.property.PathNotFoundException e) {
+            // If the node isn't found, throw a PathNotFoundException
+            throw new PathNotFoundException(JcrI18n.pathNotFound.text(path));
+        }
 
         // Now get the DNA node's UUID ...
         Location location = graphNode.getLocation();
@@ -447,6 +483,10 @@
         UUID uuid = location.getUuid();
         org.jboss.dna.graph.property.Property uuidProperty = null;
         if (uuid != null) {
+            // Check for an existing node at this UUID ...
+            AbstractJcrNode existing = nodesByUuid.get(uuid);
+            if (existing != null) return existing;
+
             // Considered an identification property ...
             uuidProperty = location.getIdProperty(JcrLexicon.UUID);
             if (uuidProperty == null) uuidProperty = location.getIdProperty(DnaLexicon.UUID);
@@ -485,7 +525,7 @@
 
         // See if there is already a JCR node object for this UUID ...
         if (uuid != null && !isRoot) {
-            Node node = getNode(uuid);
+            AbstractJcrNode node = getNode(uuid);
             if (node != null) return node;
         }
 
@@ -538,7 +578,10 @@
             node = new JcrRootNode(this, location, definition);
         } else {
             // Find the parent ...
-            AbstractJcrNode parent = (AbstractJcrNode)getNode(path.getParent());
+            if (parent == null) {
+                parent = (AbstractJcrNode)getNode(path.getParent());
+            }
+            assert parent != null;
 
             // Find the node definition for this node ...
             if (definition == null) {
@@ -558,11 +601,11 @@
 
         // Now populate the node and add to the cache ...
         populateNode(node, graphNode, uuid, uuidProperty, primaryTypeProperty);
-        if (isRoot) rootNode = node;
+        if (isRoot) rootNode = (JcrRootNode)node;
         return node;
     }
 
-    Node getNode( UUID uuid ) {
+    AbstractJcrNode getNode( UUID uuid ) {
         return nodesByUuid.get(uuid);
     }
 
@@ -586,17 +629,24 @@
         return result;
     }
 
+    JcrRootNode rootNode() throws RepositoryException {
+        // Return cached root node if available
+        if (rootNode != null) {
+            return rootNode;
+        }
+        Path rootPath = executionContext.getValueFactories().getPathFactory().createRootPath();
+        loadNode(null, rootPath); // sets the root node
+        assert rootNode != null;
+        return rootNode;
+    }
+
     /**
      * {@inheritDoc}
      * 
      * @see javax.jcr.Session#getRootNode()
      */
     public Node getRootNode() throws RepositoryException {
-        // Return cached root node if available
-        if (rootNode != null) {
-            return rootNode;
-        }
-        return getNode(executionContext.getValueFactories().getPathFactory().createRootPath());
+        return rootNode();
     }
 
     /**
@@ -806,7 +856,7 @@
         // --------------------------------------------------
         // Create JCR children for corresponding DNA children
         // --------------------------------------------------
-        node.setChildren(graphNode.getChildrenSegments());
+        node.setChildren(graphNode.getChildren());
 
         // ------------------------------------------------------
         // Create JCR properties for corresponding DNA properties

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-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -52,7 +52,7 @@
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Location;
 import org.jboss.dna.graph.property.Name;
-import org.jboss.dna.graph.property.Path.Segment;
+import org.jboss.dna.graph.property.Path;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -67,13 +67,18 @@
     static MockAbstractJcrNode createChild( JcrSession session,
                                             String name,
                                             int index,
-                                            List<Segment> children,
-                                            Node parent ) throws Exception {
+                                            List<Location> children,
+                                            AbstractJcrNode parent ) throws Exception {
         MockAbstractJcrNode child = new MockAbstractJcrNode(session, name, parent);
-        Segment seg = session.getExecutionContext().getValueFactories().getPathFactory().createSegment(name, index);
-        children.add(seg);
+        String parentPath = parent.getPath();
+        String childPath = parentPath + "/" + name + "[" + index + "]";
+        Path path = session.getExecutionContext().getValueFactories().getPathFactory().create(childPath);
+        Location location = Location.create(path, UUID.randomUUID());
+        children.add(location);
         // Stub the session to return this node ...
-        String absolutePath = parent.getPath() + "/" + seg.getString(session.getExecutionContext().getNamespaceRegistry());
+        String absolutePath = path.getString(session.getExecutionContext().getNamespaceRegistry());
+        stub(session.getChild(parent, location)).toReturn(child);
+        stub(session.getNode(location.getUuid())).toReturn(child);
         stub(session.getItem(absolutePath)).toReturn(child);
         return child;
     }
@@ -120,7 +125,7 @@
     private Workspace workspace;
     @Mock
     private Repository repository;
-    private List<Segment> children;
+    private List<Location> children;
     private Map<Name, Property> properties;
     private ExecutionContext context;
 
@@ -129,7 +134,7 @@
         MockitoAnnotations.initMocks(this);
         context = new ExecutionContext();
         stub(session.getExecutionContext()).toReturn(context);
-        children = new ArrayList<Segment>();
+        children = new ArrayList<Location>();
         properties = new HashMap<Name, Property>();
         node = new MockAbstractJcrNode(session, "node", null);
         node.setProperties(properties);
@@ -397,8 +402,8 @@
         stub(prop.getName()).toReturn("prop");
         properties.put(name(prop.getName()), prop);
         assertThat(node.hasNode("prop"), is(false));
-        Node child = createChild(session, "child", 1, children, node);
-        Node child2 = createChild(session, "child2", 1, children, child);
+        AbstractJcrNode child = createChild(session, "child", 1, children, node);
+        AbstractJcrNode child2 = createChild(session, "child2", 1, children, child);
         node.setChildren(children);
         assertThat(node.hasNode("child"), is(true));
         stub(session.getItem("/node/child/{}child2")).toReturn(child2);

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -32,7 +32,7 @@
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.property.Path.Segment;
+import org.jboss.dna.graph.Location;
 import org.jboss.dna.jcr.AbstractJcrNodeTest.MockAbstractJcrNode;
 import org.junit.Before;
 import org.junit.Test;
@@ -47,14 +47,14 @@
     private AbstractJcrNode node;
     @Mock
     private JcrSession session;
-    private List<Segment> children;
+    private List<Location> children;
 
     @Before
     public void before() throws Exception {
         MockitoAnnotations.initMocks(this);
         ExecutionContext context = new ExecutionContext();
         stub(session.getExecutionContext()).toReturn(context);
-        children = new ArrayList<Segment>();
+        children = new ArrayList<Location>();
         node = new MockAbstractJcrNode(session, "node", null);
     }
 

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -30,14 +30,12 @@
 import java.util.HashMap;
 import java.util.UUID;
 import javax.jcr.ItemNotFoundException;
-import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.nodetype.NodeDefinition;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Location;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.Path;
-import org.jboss.dna.graph.property.Path.Segment;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.MockitoAnnotations;
@@ -49,7 +47,7 @@
 public class JcrNodeTest {
 
     private JcrNode node;
-    private Node root;
+    private AbstractJcrNode root;
     @Mock
     private JcrSession session;
     @Mock
@@ -73,7 +71,7 @@
         stub(session.getNode(rootUuid)).toReturn(root);
         stub(session.getNode(uuid)).toReturn(node);
         node.setProperties(new HashMap<Name, Property>());
-        node.setChildren(new ArrayList<Segment>());
+        node.setChildren(new ArrayList<Location>());
     }
 
     @Test( expected = ItemNotFoundException.class )

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -281,7 +281,7 @@
 
     @Test
     public void shouldProvideRootNode() throws Exception {
-        Map<UUID, Node> nodesByUuid = session.getNodesByUuid();
+        Map<UUID, AbstractJcrNode> nodesByUuid = session.getNodesByUuid();
         assertThat(nodesByUuid.isEmpty(), is(true));
         Node root = session.getRootNode();
         assertThat(root, notNullValue());

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSingleValuePropertyTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSingleValuePropertyTest.java	2009-03-10 16:51:50 UTC (rev 768)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSingleValuePropertyTest.java	2009-03-10 19:43:15 UTC (rev 769)
@@ -111,10 +111,10 @@
     public void shouldProvideNode() throws Exception {
         UUID referencedUuid = UUID.randomUUID();
         dnaProperty = executionContext.getPropertyFactory().create(JcrLexicon.MIMETYPE, referencedUuid);
-        Node referencedNode = mock(Node.class);
+        AbstractJcrNode referencedNode = mock(AbstractJcrNode.class);
         stub(session.getNode(referencedUuid)).toReturn(referencedNode);
         prop = new JcrSingleValueProperty(node, definition, PropertyType.REFERENCE, dnaProperty);
-        assertThat(prop.getNode(), is(referencedNode));
+        assertThat(prop.getNode(), is((Node)referencedNode));
         assertThat(prop.getType(), is(PropertyType.REFERENCE));
         assertThat(prop.getName(), is(dnaProperty.getName().getString(executionContext.getNamespaceRegistry())));
     }




More information about the dna-commits mailing list