[dna-commits] DNA SVN: r1426 - in trunk/dna-jcr/src: test/java/org/jboss/dna/jcr and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Fri Dec 11 15:08:47 EST 2009


Author: blafond
Date: 2009-12-11 15:08:47 -0500 (Fri, 11 Dec 2009)
New Revision: 1426

Added:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrToolsTest.java
Modified:
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrTools.java
Log:
DNA-49 Added JcrToolsTest. Cleaned up/removed property access methods. Removed notion of managing "Problems" on nodes.

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrTools.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrTools.java	2009-12-11 16:55:05 UTC (rev 1425)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrTools.java	2009-12-11 20:08:47 UTC (rev 1426)
@@ -23,158 +23,19 @@
  */
 package org.jboss.dna.jcr;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.PathNotFoundException;
-import javax.jcr.Property;
-import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
-import javax.jcr.Value;
-import javax.jcr.ValueFormatException;
-import org.jboss.dna.common.collection.Problem;
-import org.jboss.dna.common.collection.Problems;
 import org.jboss.dna.common.util.CheckArg;
-import org.jboss.dna.common.util.IoUtil;
-import org.jboss.dna.common.util.Logger;
-import org.jboss.dna.common.util.StringUtil;
-import org.jboss.dna.repository.RepositoryI18n;
 
 /**
- * Utility methods for working with JCR nodes and properties.
+ * Utility methods for working with JCR nodes.
  */
 public class JcrTools {
 
     /**
-     * Create a map of properties for a given node's nodes 
-     * 
-     * @param propertyContainer the node and its children that may contain problems. may be null
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null
-     * @return the map of loaded properties
-     * @throws IllegalArgumentException if the problems argument is null
-     */
-    public Map<String, Object> loadProperties( Node propertyContainer,
-                                               Problems problems ) {
-        CheckArg.isNotNull(problems, "problems");
-        return loadProperties(propertyContainer, null, problems);
-    }
-
-    /**
-     * Create a map of properties for a given node's nodes
-     * 
-     * @param propertyContainer the node and its children that may contain problems. may be null
-     * @param properties the existing properties map to append to. may be null
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null
-     * @return the map of loaded properties
-     * @throws IllegalArgumentException if the problems argument is null
-     */
-    public Map<String, Object> loadProperties( Node propertyContainer,
-                                               Map<String, Object> properties,
-                                               Problems problems ) {
-        CheckArg.isNotNull(problems, "problems");
-        if (properties == null) properties = new HashMap<String, Object>();
-        if (propertyContainer != null) {
-            try {
-                NodeIterator iter = propertyContainer.getNodes();
-                while (iter.hasNext()) {
-                    Node propertyNode = iter.nextNode();
-                    if (propertyNode != null && propertyNode.getPrimaryNodeType().isNodeType("dna:property")) {
-                        String propertyName = propertyNode.getName();
-                        Object propertyValue = getPropertyValue(propertyNode, "dna:propertyValue", true, problems);
-                        properties.put(propertyName, propertyValue);
-                    }
-                }
-            } catch (RepositoryException e) {
-                problems.addError(e, RepositoryI18n.errorReadingPropertiesFromContainerNode, getReadable(propertyContainer));
-            }
-        }
-
-        return properties;
-    }
-
-    /**
-     * Removes problems attached to a given node
-     * 
-     * @param parent the parent node
-     * @return true if problems existed and are removed else return false.
-     * @throws RepositoryException
-     * @throws IllegalArgumentException if the parent argument is null
-     */
-    public boolean removeProblems( Node parent ) throws RepositoryException {
-        CheckArg.isNotNull(parent, "parent");
-        Node problemsNode = null;
-        if (parent.hasNode("dna:problems")) {
-            problemsNode = parent.getNode("dna:problems");
-            problemsNode.remove();
-            return true;
-        }
-        return false;
-    }
-
-    /**
-     * Add problems to a specified node.
-     * 
-     * @param parent the parent node
-     * @param problems the list of problems to add to node
-     * @return true if problems were added else return false.
-     * @throws RepositoryException
-     * @throws IllegalArgumentException if the parent or problems argument is null
-     */
-    public boolean storeProblems( Node parent,
-                                  Problems problems ) throws RepositoryException {
-        CheckArg.isNotNull(parent, "parent");
-        CheckArg.isNotNull(problems, "problems");
-        Node problemsNode = null;
-        if (parent.hasNode("dna:problems")) {
-            problemsNode = parent.getNode("dna:problems");
-            // Delete all problems ...
-            removeAllChildren(problemsNode);
-        }
-        if (problems.isEmpty()) {
-            return false;
-        }
-        if (problemsNode == null) {
-            problemsNode = parent.addNode("dna:problems"); // primary type dictated by child definition
-        }
-
-        // Add a child for each problem ...
-        for (Problem problem : problems) {
-            Node problemNode = problemsNode.addNode("problem", "dna:problem");
-            // - dna:status (string) mandatory copy
-            // < 'ERROR', 'WARNING', 'INFO'
-            // - dna:message (string) mandatory copy
-            // - dna:code (string) copy
-            // - dna:type (string) copy
-            // - dna:resource (string) copy
-            // - dna:location (string) copy
-            // - dna:trace (string) copy
-            problemNode.setProperty("dna:status", problem.getStatus().name());
-            problemNode.setProperty("dna:message", problem.getMessageString());
-            if (problem.getCode() != Problem.DEFAULT_CODE) {
-                problemNode.setProperty("dna:code", Integer.toString(problem.getCode()));
-            }
-            String resource = problem.getResource();
-            if (resource != null) {
-                problemNode.setProperty("dna:resource", resource);
-            }
-            String location = problem.getLocation();
-            if (location != null) {
-                problemNode.setProperty("dna:location", location);
-            }
-            Throwable t = problem.getThrowable();
-            if (t != null) {
-                String trace = StringUtil.getStackTrace(t);
-                problemNode.setProperty("dna:trace", trace);
-            }
-        }
-        return true;
-    }
-
-    /**
      * Remove all children from the specified node
      * 
      * @param node
@@ -195,221 +56,30 @@
     }
 
     /**
-     * Get the string property value for a given node and property name.
+     * Get the node under a specified node at a location defined by the specified relative path. If node is required, then a problem
+     * is created and added to the Problems list.
      * 
-     * @param node the node containing the property. may not be null
-     * @param propertyName the name of the property attached to the node. may not be null
-     * @param required true if property is required to exist for the given node.
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null 
-     * @return the property string
-     * @throws IllegalArgumentException if the node, propertyName or problems argument is null
-     */
-    public String getPropertyAsString( Node node,
-                                       String propertyName,
-                                       boolean required,
-                                       Problems problems ) {
-        return getPropertyAsString(node, propertyName, required, null);
-    }
-
-    /**
-     * Get the string property value for a given node and property name.
-     * 
-     * @param node the node containing the property. may not be null
-     * @param propertyName the name of the property attached to the node. may not be null
-     * @param required true if property is required to exist for the given node.
-     * @param defaultValue the default property. may be null
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null 
-     * @return the property string
-     * @throws IllegalArgumentException if the node, propertyName or problems argument is null
-     */
-    public String getPropertyAsString( Node node,
-                                       String propertyName,
-                                       boolean required,
-                                       String defaultValue,
-                                       Problems problems ) {
-        CheckArg.isNotNull(node, "node");
-        CheckArg.isNotNull(propertyName, "propertyName");
-        CheckArg.isNotNull(problems, "problems");
-        try {
-            Property property = node.getProperty(propertyName);
-            return property.getString();
-        } catch (ValueFormatException e) {
-            if (required) {
-                problems.addError(e,
-                                  RepositoryI18n.requiredPropertyOnNodeWasExpectedToBeStringValue,
-                                  propertyName,
-                                  getReadable(node));
-            } else {
-                problems.addError(e,
-                                  RepositoryI18n.optionalPropertyOnNodeWasExpectedToBeStringValue,
-                                  propertyName,
-                                  getReadable(node));
-            }
-        } catch (PathNotFoundException e) {
-            if (required) {
-                problems.addError(e, RepositoryI18n.requiredPropertyIsMissingFromNode, propertyName, getReadable(node));
-            }
-            if (!required) return defaultValue;
-        } catch (RepositoryException err) {
-            if (required) {
-                problems.addError(err, RepositoryI18n.errorGettingRequiredPropertyFromNode, propertyName, getReadable(node));
-            } else {
-                problems.addError(err, RepositoryI18n.errorGettingOptionalPropertyFromNode, propertyName, getReadable(node));
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Get the property value for specified node and property name on <code>Object</code> format.
-     * 
-     * @param node the node containing the property. may not be null
-     * @param propertyName the name of the property attached to the node. may not be null
-     * @param required true if property is required to exist for the given node.
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null
-     * @return the property value
-     * @throws IllegalArgumentException if the node, propertyName or problems argument is null
-     */
-    public Object getPropertyValue( Node node,
-                                    String propertyName,
-                                    boolean required,
-                                    Problems problems ) {
-        CheckArg.isNotNull(node, "node");
-        CheckArg.isNotNull(propertyName, "propertyName");
-        CheckArg.isNotNull(problems, "problems");
-        try {
-            Property property = node.getProperty(propertyName);
-            switch (property.getType()) {
-                case PropertyType.BINARY: {
-                    InputStream stream = property.getStream();
-                    try {
-                        stream = property.getStream();
-                        return IoUtil.readBytes(stream);
-                    } finally {
-                        if (stream != null) {
-                            try {
-                                stream.close();
-                            } catch (IOException e) {
-                                // Log ...
-                                Logger.getLogger(this.getClass())
-                                      .error(e,
-                                             RepositoryI18n.errorClosingBinaryStreamForPropertyFromNode,
-                                             propertyName,
-                                             node.getPath());
-                            }
-                        }
-                    }
-                }
-                default: {
-                    return property.getString();
-                }
-            }
-        } catch (IOException e) {
-            if (required) {
-                problems.addError(e, RepositoryI18n.requiredPropertyOnNodeCouldNotBeRead, propertyName, getReadable(node));
-            } else {
-                problems.addError(e, RepositoryI18n.optionalPropertyOnNodeCouldNotBeRead, propertyName, getReadable(node));
-            }
-        } catch (PathNotFoundException e) {
-            if (required) {
-                problems.addError(e, RepositoryI18n.requiredPropertyIsMissingFromNode, propertyName, getReadable(node));
-            }
-        } catch (RepositoryException err) {
-            if (required) {
-                problems.addError(err, RepositoryI18n.errorGettingRequiredPropertyFromNode, propertyName, getReadable(node));
-            } else {
-                problems.addError(err, RepositoryI18n.errorGettingOptionalPropertyFromNode, propertyName, getReadable(node));
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Get the property value for a specified node and property name in string array format.
-     * 
-     * @param node the node containing the property. may not be null
-     * @param propertyName the name of the property attached to the node. may not be null
-     * @param required true if property is required to exist for the given node.
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null
-     * @param defaultValues
-     * @return the array of string properties
-     * @throws IllegalArgumentException if the node, propertyName or problems argument is null
-     */
-    public String[] getPropertyAsStringArray( Node node,
-                                              String propertyName,
-                                              boolean required,
-                                              Problems problems,
-                                              String... defaultValues ) {
-        CheckArg.isNotNull(node, "node");
-        CheckArg.isNotNull(propertyName, "propertyName");
-        CheckArg.isNotNull(problems, "problems");
-        String[] result = defaultValues;
-        try {
-            Property property = node.getProperty(propertyName);
-            if (property.getDefinition().isMultiple()) {
-                Value[] values = property.getValues();
-                result = new String[values.length];
-                int i = 0;
-                for (Value value : values) {
-                    result[i++] = value.getString();
-                }
-            } else {
-                result = new String[] {property.getString()};
-            }
-        } catch (ValueFormatException e) {
-            if (required) {
-                problems.addError(e,
-                                  RepositoryI18n.requiredPropertyOnNodeWasExpectedToBeStringArrayValue,
-                                  propertyName,
-                                  getReadable(node));
-            } else {
-                problems.addError(e,
-                                  RepositoryI18n.optionalPropertyOnNodeWasExpectedToBeStringArrayValue,
-                                  propertyName,
-                                  getReadable(node));
-            }
-        } catch (PathNotFoundException e) {
-            if (required) {
-                problems.addError(e, RepositoryI18n.requiredPropertyIsMissingFromNode, propertyName, getReadable(node));
-            }
-        } catch (RepositoryException err) {
-            if (required) {
-                problems.addError(err, RepositoryI18n.errorGettingRequiredPropertyFromNode, propertyName, getReadable(node));
-            } else {
-                problems.addError(err, RepositoryI18n.errorGettingOptionalPropertyFromNode, propertyName, getReadable(node));
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Get the node under a specified node at a location defined by the specified relative path.
-     * 
      * @param node a parent node from which to obtain a node relative to. may not be null
      * @param relativePath the path of the desired node. may not be null
      * @param required true if node is required to exist under the given node.
-     * @param problems the list of problems to add to if problems encountered loading properties. may not be null
      * @return the node located relative the the input node
+     * @throws RepositoryException 
      * @throws IllegalArgumentException if the node, relativePath or problems argument is null
      */
     public Node getNode( Node node,
                          String relativePath,
-                         boolean required,
-                         Problems problems ) {
+                         boolean required) throws RepositoryException {
         CheckArg.isNotNull(node, "node");
         CheckArg.isNotNull(relativePath, "relativePath");
-        CheckArg.isNotNull(problems, "problems");
         Node result = null;
         try {
             result = node.getNode(relativePath);
         } catch (PathNotFoundException e) {
-            if (required) problems.addError(e,
-                                            RepositoryI18n.requiredNodeDoesNotExistRelativeToNode,
-                                            relativePath,
-                                            getReadable(node));
-        } catch (RepositoryException err) {
-            problems.addError(err, RepositoryI18n.errorGettingNodeRelativeToNode, relativePath, getReadable(node));
+            if (required) {
+                throw e;
+            }
         }
+        
         return result;
     }
 

Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrToolsTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrToolsTest.java	                        (rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrToolsTest.java	2009-12-11 20:08:47 UTC (rev 1426)
@@ -0,0 +1,409 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.jcr;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import org.jboss.dna.common.collection.Problems;
+import org.jboss.dna.common.collection.SimpleProblems;
+import org.jboss.dna.graph.SecurityContext;
+import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class JcrToolsTest {
+    private JcrEngine engine;
+    private Session session;
+    private JcrTools tools;
+    private Node personNode;
+    private Node addressNode;
+    private Problems problems;
+    private Node NULL_NODE;
+    private String NULL_STRING;
+    
+    private static final String DEF_TYPE = "nt:unstructured";
+    
+    @Before
+    public void before()  throws Exception {
+        tools = new JcrTools();
+        
+        String repositoryName = "ddlRepository";
+        String workspaceName = "default";
+        String repositorySource = "ddlRepositorySource";
+                
+        JcrConfiguration config = new JcrConfiguration();
+        // Set up the in-memory source where we'll upload the content and where the sequenced output will be stored ...
+        config.repositorySource(repositorySource)
+              .usingClass(InMemoryRepositorySource.class)
+              .setDescription("The repository for our content")
+              .setProperty("defaultWorkspaceName", workspaceName);
+        // Set up the JCR repository to use the source ...
+        config.repository(repositoryName).setSource(repositorySource);
+
+        config.save();
+        this.engine = config.build();
+        this.engine.start();
+
+        this.session = this.engine.getRepository(repositoryName)
+                                  .login(new SecurityContextCredentials(new MyCustomSecurityContext()), workspaceName);
+        
+        Node rootNode = session.getRootNode();
+        
+        personNode = rootNode.addNode("Person");
+        personNode.setProperty("First Name", "Ryan");
+        personNode.setProperty("Middle Name", "Joseph");
+        personNode.setProperty("Last Name", "Franklin");
+        personNode.setProperty("Age", 37);
+        personNode.setProperty("Children", new String[] {"Sally", "Brent", "Michael"} );
+        
+        
+        addressNode = personNode.addNode("Address");
+        addressNode.setProperty("Street", "Frost Avenue");
+        addressNode.setProperty("House Number", 166);
+        addressNode.setProperty("City", "Flagstaff");
+        addressNode.setProperty("State", "AZ");
+        addressNode.setProperty("Country", "US");
+        addressNode.setProperty("Zip Code", 77777);
+
+        problems = new SimpleProblems();
+    }
+    
+    @After
+    public void afterEach() throws Exception {
+        if (this.session != null) {
+            this.session.logout();
+        }
+        if (this.engine != null) {
+            this.engine.shutdown();
+        }
+    }
+    
+    protected class MyCustomSecurityContext implements SecurityContext {
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.jboss.dna.graph.SecurityContext#getUserName()
+         */
+        public String getUserName() {
+            return "Fred";
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.jboss.dna.graph.SecurityContext#hasRole(java.lang.String)
+         */
+        public boolean hasRole( String roleName ) {
+            return true;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.jboss.dna.graph.SecurityContext#logout()
+         */
+        public void logout() {
+            // do something
+        }
+    }
+
+
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#getNode(javax.jcr.Node, java.lang.String, boolean)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void shouldGetNode() throws RepositoryException {
+        Node node = tools.getNode(session.getRootNode(), "Person", true);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Person"));
+        assertThat(problems.size(), is(0));
+    }
+    
+    @Test
+    public void shouldFailGetNodeWithNoDoesntExist() {
+        try {
+            tools.getNode(session.getRootNode(), "Animal", true);
+        } catch (Exception e) {
+            assertTrue(e instanceof PathNotFoundException);
+        }
+    }
+    
+    @Test
+    public void shouldFailGetNodeNullParent() {
+        try {
+            tools.getNode(NULL_NODE, "Person", true);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailGetNodeNullPath() {
+        try {
+            tools.getNode(personNode, NULL_STRING, true);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#getReadable(javax.jcr.Node)}.
+     */
+    @Test
+    public void testGetReadable() {
+        String personStr = tools.getReadable(personNode);
+        assertThat(personStr, is("/Person"));
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateNode(javax.jcr.Session, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void testCreateNodeSessionPath() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Hobby");
+        assertNotNull(node);
+        assertThat(node.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void testFindNodeSessionPath() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Person");
+        assertNotNull(node);
+        assertThat(node.getName(), is("Person"));
+    }
+
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathNullSession() {
+        Session sess = null;
+        try {
+            tools.findOrCreateNode(sess, "Person");
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathNullPath() {
+        try {
+            tools.findOrCreateNode(session, NULL_STRING);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateNode(javax.jcr.Session, java.lang.String, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void testCreateNodeSessionPathType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Hobby", DEF_TYPE);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void testFindNodeSessionPathType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Person", DEF_TYPE);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Person"));
+    }
+
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathTypeNullSession() {
+        Session sess = null;
+        try {
+            tools.findOrCreateNode(sess, "Person", DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathTypeNullPath() {
+        try {
+            tools.findOrCreateNode(session, NULL_STRING, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateNode(javax.jcr.Session, java.lang.String, java.lang.String, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void testCreateNodeSessionPathTypeType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Hobby", DEF_TYPE, DEF_TYPE);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void testFindNodeSessionPathTypeType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(session, "Person", DEF_TYPE, DEF_TYPE);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Person"));
+    }
+
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathTypeTypeNullSession() {
+        Session sess = null;
+        try {
+            tools.findOrCreateNode(sess, "Person", DEF_TYPE, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateNodeSessionPathTypeTypeNullPath() {
+        try {
+            tools.findOrCreateNode(session, NULL_STRING, DEF_TYPE, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateNode(javax.jcr.Node, java.lang.String, java.lang.String, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void shouldCreateNodeNodePathTypeType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(personNode, "Hobby", DEF_TYPE, "nt:unstructured");
+        assertNotNull(node);
+        assertThat(node.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void shouldFindNodeParentPathTypeType() throws RepositoryException {
+        Node node = tools.findOrCreateNode(personNode, "Address", DEF_TYPE, DEF_TYPE);
+        assertNotNull(node);
+        assertThat(node.getName(), is("Address"));
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateNodeNodePathTypeTypeNullNode() {
+        try {
+            tools.findOrCreateNode(NULL_NODE, "/topNode", DEF_TYPE, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateNodeNodePathTypeTypeNullPath() {
+        try {
+            tools.findOrCreateNode(personNode, NULL_STRING, DEF_TYPE, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateChild(javax.jcr.Node, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void shouldCreateChildNodeWithParentName() throws RepositoryException {
+        Node childNode = tools.findOrCreateChild(personNode, "Hobby");
+        assertNotNull(childNode);
+        assertThat(childNode.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void shouldFindChildNodeWithParentName() throws RepositoryException {
+        Node childNode = tools.findOrCreateChild(personNode, "Address");
+        assertNotNull(childNode);
+        assertThat(childNode.getName(), is("Address"));
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateChildNodeStringNullNode() {
+        try {
+            tools.findOrCreateChild(NULL_NODE, "childNode");
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateChildNodeStringNullPath() {
+        try {
+            tools.findOrCreateChild(addressNode, NULL_STRING);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+
+    /**
+     * Test method for {@link org.jboss.dna.jcr.JcrTools#findOrCreateChild(javax.jcr.Node, java.lang.String, java.lang.String)}.
+     * @throws RepositoryException 
+     */
+    @Test
+    public void testCreateChildNodeWithParentNameType() throws RepositoryException {
+        Node childNode = tools.findOrCreateChild(personNode, "Hobby", DEF_TYPE);
+        assertNotNull(childNode);
+        assertThat(childNode.getName(), is("Hobby"));
+    }
+    
+    @Test
+    public void testFindChildNodeWithParentNameType() throws RepositoryException {
+        Node childNode = tools.findOrCreateChild(personNode, "Address", DEF_TYPE);
+        assertNotNull(childNode);
+        assertThat(childNode.getName(), is("Address"));
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateChildWithNullParentNameType() {
+        try {
+            tools.findOrCreateChild(NULL_NODE, "childNode", DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void shouldFailFindOrCreateChildNodeStringStringNullPath() {
+        try {
+            tools.findOrCreateChild(addressNode, NULL_STRING, DEF_TYPE);
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+}


Property changes on: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrToolsTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the dna-commits mailing list