Author: bdelacretaz
Date: Mon Jun 15 09:06:48 2015
New Revision: 1685518

URL: http://svn.apache.org/r1685518
Log:
SLING-4151 - expand DefaultContentCreatorTest, contributed by Petr Shypila, 
thanks!

Modified:
    sling/trunk/bundles/jcr/contentloader/pom.xml
    
sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreatorTest.java

Modified: sling/trunk/bundles/jcr/contentloader/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/pom.xml?rev=1685518&r1=1685517&r2=1685518&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/contentloader/pom.xml (original)
+++ sling/trunk/bundles/jcr/contentloader/pom.xml Mon Jun 15 09:06:48 2015
@@ -241,6 +241,17 @@
         <version>2.10.1</version>
         <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.testing</artifactId>
+      <version>2.0.18</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit-addons</groupId>
+      <artifactId>junit-addons</artifactId>
+      <version>1.4</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
-
 </project>

Modified: 
sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreatorTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreatorTest.java?rev=1685518&r1=1685517&r2=1685518&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreatorTest.java
 (original)
+++ 
sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreatorTest.java
 Mon Jun 15 09:06:48 2015
@@ -17,41 +17,66 @@
 package org.apache.sling.jcr.contentloader.internal;
 
 import java.text.ParseException;
-import java.util.Calendar;
-import java.util.HashMap;
+import java.util.*;
 
 import javax.jcr.*;
+import javax.jcr.nodetype.NodeType;
 
+import junitx.util.PrivateAccessor;
+import org.apache.sling.commons.testing.jcr.RepositoryProvider;
+import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.contentloader.ContentImportListener;
 import org.apache.sling.jcr.contentloader.ContentReader;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
-import org.jmock.integration.junit4.JMock;
 import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.rules.ExpectedException;
 
 import static org.junit.Assert.*;
 
-@RunWith(JMock.class)
 public class DefaultContentCreatorTest {
-    
+
+    static final String DEFAULT_NAME = "default-name";
+    final Mockery mockery = new JUnit4Mockery();
     DefaultContentCreator contentCreator;
-    
-    Mockery mockery = new JUnit4Mockery();
-    
+
+    Session session;
     Node parentNode;
-    
     Property prop;
-    
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    @Before
+    public void setup() throws Exception {
+        final SlingRepository repo = 
RepositoryProvider.instance().getRepository();
+        session = repo.loginAdministrative(null);
+        contentCreator = new DefaultContentCreator(null);
+        contentCreator.init(U.createImportOptions(true, true, true, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        parentNode = 
session.getRootNode().addNode(getClass().getSimpleName()).addNode(uniqueId());
+    }
+
+    @After
+    public void cleanup() throws RepositoryException {
+        if(session != null) {
+            session.save();
+            session.logout();
+            session = null;
+        }
+    }
+
     @Test
     public void willRewriteUndefinedPropertyType() throws RepositoryException {
-        contentCreator = new DefaultContentCreator(null);
         parentNode = mockery.mock(Node.class);
         prop = mockery.mock(Property.class);
         contentCreator.init(U.createImportOptions(true, true, true, false, 
false),
                 new HashMap<String, ContentReader>(), null, null);
-        
+
         contentCreator.prepareParsing(parentNode, null);
         this.mockery.checking(new Expectations() {{
                allowing(parentNode).isNodeType("mix:versionable"); 
will(returnValue(Boolean.FALSE));
@@ -61,15 +86,14 @@ public class DefaultContentCreatorTest {
         }});
         contentCreator.createProperty("foo", PropertyType.UNDEFINED, "bar");
     }
-    
+
     @Test
     public void willNotRewriteUndefinedPropertyType() throws 
RepositoryException {
-        contentCreator = new DefaultContentCreator(null);
         parentNode = mockery.mock(Node.class);
         prop = mockery.mock(Property.class);
         contentCreator.init(U.createImportOptions(false, false, true, false, 
false),
                 new HashMap<String, ContentReader>(), null, null);
-        
+
         contentCreator.prepareParsing(parentNode, null);
         this.mockery.checking(new Expectations() {{
             oneOf (parentNode).hasProperty("foo"); 
will(returnValue(Boolean.TRUE));
@@ -90,8 +114,6 @@ public class DefaultContentCreatorTest {
             oneOf(parentNode).getProperty(propertyName); 
will(returnValue(prop));
             oneOf(prop).isNew(); will(returnValue(false));
         }});
-
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, null);
         contentCreator.prepareParsing(parentNode, null);
@@ -105,7 +127,7 @@ public class DefaultContentCreatorTest {
     public void testCreateReferenceProperty() throws RepositoryException {
         final String propertyName = "foo";
         final String propertyValue = "bar";
-        final String rootNodeName = "root";
+        final String rootNodeName = uniqueId();
         final String uuid = "1b8c88d37f0000020084433d3af4941f";
         final Session session = mockery.mock(Session.class);
         final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
@@ -127,7 +149,6 @@ public class DefaultContentCreatorTest {
             oneOf(listener).onCreate(with(any(String.class)));
         }});
 
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, listener);
         contentCreator.prepareParsing(parentNode,null);
@@ -144,7 +165,6 @@ public class DefaultContentCreatorTest {
             oneOf(parentNode).hasProperty(with(any(String.class)));
         }});
 
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, null);
         contentCreator.prepareParsing(parentNode, null);
@@ -164,7 +184,6 @@ public class DefaultContentCreatorTest {
             oneOf(parentNode).hasProperty(with(any(String.class)));
         }});
 
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, null);
         contentCreator.prepareParsing(parentNode, null);
@@ -192,7 +211,6 @@ public class DefaultContentCreatorTest {
             oneOf(listener).onCreate(with(any(String.class)));
         }});
 
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, listener);
         contentCreator.prepareParsing(parentNode, null);
@@ -218,7 +236,6 @@ public class DefaultContentCreatorTest {
             oneOf(listener).onCreate(with(any(String.class)));
         }});
 
-        contentCreator = new DefaultContentCreator(null);
         contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
                 new HashMap<String, ContentReader>(), null, listener);
         contentCreator.prepareParsing(parentNode, null);
@@ -226,4 +243,295 @@ public class DefaultContentCreatorTest {
         contentCreator.createProperty(propertyName, propertyType, 
propertyValue);
         mockery.assertIsSatisfied();
     }
+
+    //----- DefaultContentCreator#createNode(String name, String 
primaryNodeType, String[] mixinNodeTypes)-------//
+
+    @Test
+    public void createNodeWithoutNameAndTwoInStack() throws 
RepositoryException {
+        contentCreator.init(U.createImportOptions(true, true, true, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        //Making parentNodeStack.size() == 1
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+        //Making parentNodeStack.size() == 2
+        contentCreator.createNode(uniqueId(), null, null);
+
+        thrown.expect(RepositoryException.class);
+        contentCreator.createNode(null, null, null);
+    }
+
+    @Test
+    public void createNodeWithoutProvidedNames() throws RepositoryException, 
NoSuchFieldException {
+        Stack nodesStack = (Stack)PrivateAccessor.getField(contentCreator, 
"parentNodeStack");
+
+        contentCreator.init(U.createImportOptions(true, true, true, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+
+        contentCreator.prepareParsing(parentNode, null);
+        //Contains parentNode only
+        assertEquals(1, nodesStack.size());
+        contentCreator.createNode(null, null, null);
+        //Node has not been created since any name not provided
+        assertEquals(1, nodesStack.size());
+    }
+
+    @Test
+    public void createNodeWithOverwrite() throws RepositoryException {
+        final String newNodeName = uniqueId();
+        final String propertyName = uniqueId();
+        final String propertyValue = uniqueId();
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            oneOf(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(true, false, true, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+
+        Node nodeToOverwrite = parentNode.addNode(newNodeName);
+        nodeToOverwrite.setProperty(propertyName, propertyValue);
+
+        assertTrue(parentNode.getNode(newNodeName).hasProperty(propertyName));
+        contentCreator.createNode(newNodeName, null, null);
+        //If node was overwritten(as we expect) it will not contain this 
property
+        assertFalse(parentNode.getNode(newNodeName).hasProperty(propertyName));
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void addMixinsToExistingNode() throws RepositoryException, 
NoSuchFieldException {
+        final String newNodeName = uniqueId();
+        final String[] mixins = {"mix:versionable"};
+        final List<Node> versionables = (List<Node>) 
PrivateAccessor.getField(contentCreator, "versionables");
+
+        contentCreator.init(U.createImportOptions(false, false, false, true, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+
+        Node newNode = parentNode.addNode(newNodeName);
+        assertEquals(0, versionables.size());
+        assertFalse(newNode.isNodeType(mixins[0]));
+        contentCreator.createNode(newNodeName, null, mixins);
+        assertEquals(1, versionables.size());
+        assertTrue(newNode.isNodeType(mixins[0]));
+    }
+
+    @Test
+    public void createNodeWithPrimaryType() throws RepositoryException {
+        final String newNodeName = uniqueId();
+        final List<String> createdNodes = new ArrayList<String>();
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            oneOf(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(true, false, true, false, 
false),
+                new HashMap<String, ContentReader>(), createdNodes, listener);
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+
+        int createdNodesSize = createdNodes.size();
+        contentCreator.createNode(newNodeName, NodeType.NT_UNSTRUCTURED, null);
+        assertEquals(createdNodesSize + 1, createdNodes.size());
+        Node createdNode = parentNode.getNode(newNodeName);
+        assertNotNull(createdNode);
+        
assertTrue(createdNode.getPrimaryNodeType().isNodeType(NodeType.NT_UNSTRUCTURED));
+        mockery.assertIsSatisfied();
+    }
+
+    //----- DefaultContentCreator#createProperty(String name, int 
propertyType, String[] values)-------//
+
+    @Test
+    public void propertyDoesntOverwritten() throws RepositoryException {
+        final String newPropertyName = uniqueId();
+        final String newPropertyValue = uniqueId();
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+
+        parentNode.setProperty(newPropertyName, newPropertyValue);
+        session.save();
+        contentCreator.createProperty(newPropertyName, PropertyType.REFERENCE, 
new String[]{"bar1", "bar2"});
+        //Checking that property is old
+        assertTrue(!parentNode.getProperty(newPropertyName).isNew());
+        assertEquals(newPropertyValue, 
parentNode.getProperty(newPropertyName).getString());
+    }
+
+
+    @Test
+    public void createReferenceProperty() throws RepositoryException, 
NoSuchFieldException {
+        final String propName = uniqueId();
+        final String[] propValues = {uniqueId(), uniqueId()};
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+        final Map<String, String[]> delayedMultipleReferences =
+                (Map<String, 
String[]>)PrivateAccessor.getField(contentCreator, "delayedMultipleReferences");
+        this.mockery.checking(new Expectations(){{
+            oneOf(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
+
+        contentCreator.createProperty(propName, PropertyType.REFERENCE, 
propValues);
+        assertTrue(parentNode.hasProperty(propName));
+        assertTrue(parentNode.getProperty(propName).isNew());
+
+        String referencesKey = parentNode.getPath() + "/" + propName;
+        String[] uuidsOrPaths = delayedMultipleReferences.get(referencesKey);
+        assertNotNull(uuidsOrPaths);
+        assertEquals(propValues.length, uuidsOrPaths.length);
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void createDateProperty() throws RepositoryException, 
ParseException {
+        final String propName = "dateProp";
+        final String[] propValues = {"2012-10-01T09:45:00.000+02:00", 
"2011-02-13T09:45:00.000+02:00"};
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            oneOf(listener).onCreate(with(any(String.class)));
+        }});
+        parentNode.addMixin("mix:versionable");
+
+        contentCreator.init(U.createImportOptions(false, false, true, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, null);
+
+        assertFalse(parentNode.hasProperty(propName));
+        contentCreator.createProperty(propName, PropertyType.DATE, propValues);
+        assertTrue(parentNode.hasProperty(propName));
+
+        Property dateProp = parentNode.getProperty(propName);
+        assertTrue(dateProp.isNew());
+        assertEquals(propValues.length, dateProp.getValues().length);
+
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void createUndefinedProperty() throws RepositoryException {
+        final String propName = uniqueId();
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            oneOf(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, null);
+
+        assertFalse(parentNode.hasProperty(propName));
+        contentCreator.createProperty(propName, PropertyType.UNDEFINED, new 
String[]{});
+        assertTrue(parentNode.hasProperty(propName));
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void createOtherProperty() throws RepositoryException {
+        final String propName = uniqueId();
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        contentCreator.prepareParsing(parentNode, null);
+
+        assertFalse(parentNode.hasProperty(propName));
+        contentCreator.createProperty(propName, PropertyType.STRING, new 
String[]{});
+        assertTrue(parentNode.hasProperty(propName));
+    }
+
+    //------DefaultContentCreator#finishNode()------//
+
+    @Test
+    public void finishNodeWithMultipleProperty() throws RepositoryException, 
NoSuchFieldException {
+        final String propName = uniqueId();
+        final String underTestNodeName = uniqueId();
+        final Map<String, List<String>> delayedMultipleReferences =
+                (Map<String, List<String>>) 
PrivateAccessor.getField(contentCreator, "delayedMultipleReferences");
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            exactly(3).of(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, null);
+
+        contentCreator.createProperty(propName, PropertyType.REFERENCE, new 
String[]{underTestNodeName});
+        contentCreator.createNode(underTestNodeName, null, null);
+        assertEquals(1, delayedMultipleReferences.size());
+
+        Node underTest = parentNode.getNode(underTestNodeName);
+        underTest.addMixin("mix:referenceable");
+
+        contentCreator.finishNode();
+        assertEquals(0, delayedMultipleReferences.size());
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void finishNodeWithSingleProperty() throws RepositoryException, 
NoSuchFieldException {
+        final String propName = uniqueId();
+        final String underTestNodeName = uniqueId();
+        final ContentImportListener listener = 
mockery.mock(ContentImportListener.class);
+
+        this.mockery.checking(new Expectations(){{
+            exactly(2).of(listener).onCreate(with(any(String.class)));
+        }});
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, listener);
+        contentCreator.prepareParsing(parentNode, null);
+
+        contentCreator.createProperty(propName, PropertyType.REFERENCE, 
underTestNodeName);
+        contentCreator.createNode(underTestNodeName, null, null);
+
+        Node underTest = parentNode.getNode(underTestNodeName);
+        underTest.addMixin("mix:referenceable");
+
+        contentCreator.finishNode();
+        assertEquals(underTest.getUUID(), 
parentNode.getProperty(propName).getString());
+        mockery.assertIsSatisfied();
+    }
+
+    @Test
+    public void finishNodeWithoutProperties() throws RepositoryException, 
NoSuchFieldException {
+        final String propName = uniqueId();
+        final String underTestNodeName = uniqueId();
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        contentCreator.prepareParsing(parentNode, null);
+
+        contentCreator.createProperty(propName, PropertyType.UNDEFINED, 
underTestNodeName);
+        contentCreator.createNode(underTestNodeName, null, null);
+
+        contentCreator.finishNode();
+        assertEquals(underTestNodeName, 
parentNode.getProperty(propName).getString());
+    }
+
+    @Test
+    public void finishNotReferenceableNode() throws RepositoryException, 
NoSuchFieldException {
+        final String propName = uniqueId();
+        final String underTestNodeName = uniqueId();
+
+        contentCreator.init(U.createImportOptions(false, false, false, false, 
false),
+                new HashMap<String, ContentReader>(), null, null);
+        contentCreator.prepareParsing(parentNode, null);
+
+        contentCreator.createProperty(propName, PropertyType.REFERENCE, 
underTestNodeName);
+        contentCreator.createNode(underTestNodeName, null, null);
+
+        contentCreator.finishNode();
+        //False since it doesn't have referenceable mixin
+        assertFalse(parentNode.hasProperty(propName));
+    }
+
+    private final String uniqueId() {
+        return getClass().getSimpleName() + UUID.randomUUID();
+    }
 }


Reply via email to