Package org.apache.sling.bgservlets.impl

Source Code of org.apache.sling.bgservlets.impl.DeepNodeCreatorTest

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sling.bgservlets.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

/** Test the DeepNodeCreator class
* TOO replace with the version of JCR-2687 once that's released.
* */
public class DeepNodeCreatorTest {

    @Test
    public void testExistingNode() throws Exception {
        final Mockery mockery = new Mockery();
        final DeepNodeCreator c = new DeepNodeCreator();
        final String path = "/foo/bar";
        final Session s = mockery.mock(Session.class);
        final Node n = mockery.mock(Node.class);
       
        mockery.checking(new Expectations() {{
            allowing(s).itemExists(path);
            will(returnValue(true));

            allowing(s).getItem(path);
            will(returnValue(n));
           
            allowing(n).isNode();
            will(returnValue(true));
        }});

        final Node result = c.deepCreateNode(path, s, null);
        assertTrue("Expecting deepCreate to return existing node", result == n);
    }
   
    public void testCreateFromRoot() throws Exception {
        final Mockery mockery = new Mockery();
        final DeepNodeCreator c = new DeepNodeCreator();
        final String rootPath = "/";
        final String fooPath = "/foo";
        final String barPath = "/foo/bar";
        final Session s = mockery.mock(Session.class);
        final Node root = mockery.mock(Node.class, rootPath);
        final Node foo = mockery.mock(Node.class, fooPath);
        final Node bar = mockery.mock(Node.class, barPath);
        final String testNodeType = "NT_TEST";
       
        mockery.checking(new Expectations() {{
            allowing(s).itemExists(barPath);
            will(returnValue(false));

            allowing(s).itemExists(fooPath);
            will(returnValue(false));

            allowing(s).itemExists(rootPath);
            will(returnValue(true));

            allowing(s).getItem(rootPath);
            will(returnValue(root));
           
            allowing(root).isNode();
            will(returnValue(true));
           
            allowing(root).addNode("foo", testNodeType);
            will(returnValue(foo));
           
            allowing(foo).addNode("bar", testNodeType);
            will(returnValue(bar));
           
            allowing(s).save();
        }});
       
        final Node result = c.deepCreateNode(barPath, s, testNodeType);
        assertTrue("Expecting deepCreate to return create node", result == bar);
    }
   
    public void testCreateWithVariousTypes() throws Exception {
        final Mockery mockery = new Mockery();
       
        final String fooPath = "/foo";
        final String barPath = "/foo/bar";
        final String wiiPath = "/foo/bar/wii";
        final Session s = mockery.mock(Session.class);
        final Node foo = mockery.mock(Node.class, fooPath);
        final Node bar = mockery.mock(Node.class, barPath);
        final Node wii = mockery.mock(Node.class, wiiPath);
       
        mockery.checking(new Expectations() {{
            allowing(s).itemExists(wiiPath);
            will(returnValue(false));

            allowing(s).itemExists(barPath);
            will(returnValue(false));

            allowing(s).itemExists(fooPath);
            will(returnValue(true));

            allowing(s).getItem(fooPath);
            will(returnValue(foo));
           
            allowing(foo).isNode();
            will(returnValue(true));
           
            allowing(foo).getPath();
            will(returnValue(fooPath));
           
            allowing(foo).addNode("bar", "NT_/foo.bar");
            will(returnValue(bar));
           
            allowing(bar).getPath();
            will(returnValue(barPath));
           
            allowing(bar).addNode("wii", "NT_/foo/bar.wii");
            will(returnValue(wii));
           
            allowing(s).save();
        }});
       
        final AtomicInteger counter = new AtomicInteger();
        final DeepNodeCreator c = new DeepNodeCreator() {

            @Override
            protected String getNodeType(Node parent, String childPath, String suggestedNodeType)
            throws RepositoryException {
                return "NT_" + parent.getPath() + "." + childPath;
            }

            @Override
            protected void nodeCreated(Node n) throws RepositoryException {
                counter.addAndGet(1);
            }
        };
        final Node result = c.deepCreateNode(wiiPath, s, null);
        assertTrue("Expecting deepCreate to return created node", result == wii);
        assertEquals("Expecting correct count of nodeCreated calls", 2, counter.get());
    }
}
TOP

Related Classes of org.apache.sling.bgservlets.impl.DeepNodeCreatorTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.