Search in sources :

Example 1 with MockNodeIterator

use of org.apache.sling.commons.testing.jcr.MockNodeIterator in project sling by apache.

the class JcrNodeResourceIteratorTest method testRoot.

public void testRoot() throws RepositoryException {
    String path = "/child";
    Node node = new MockNode(path);
    NodeIterator ni = new MockNodeIterator(new Node[] { node });
    JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, "/", null, ni, getHelperData(), null);
    assertTrue(ri.hasNext());
    Resource res = ri.next();
    assertEquals(path, res.getPath());
    assertEquals(node.getPrimaryNodeType().getName(), res.getResourceType());
    assertFalse(ri.hasNext());
    try {
        ri.next();
        fail("Expected no element in the iterator");
    } catch (NoSuchElementException nsee) {
    // expected
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNode(org.apache.sling.commons.testing.jcr.MockNode) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) MockNode(org.apache.sling.commons.testing.jcr.MockNode) JcrNodeResourceIterator(org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with MockNodeIterator

use of org.apache.sling.commons.testing.jcr.MockNodeIterator in project sling by apache.

the class JcrNodeResourceIteratorTest method testMulti.

public void testMulti() throws RepositoryException {
    int numNodes = 10;
    String pathBase = "/parent/path/node/";
    Node[] nodes = new Node[numNodes];
    for (int i = 0; i < nodes.length; i++) {
        nodes[i] = new MockNode(pathBase + i, "some:type" + i);
    }
    NodeIterator ni = new MockNodeIterator(nodes);
    JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, null, null, ni, getHelperData(), null);
    for (int i = 0; i < nodes.length; i++) {
        assertTrue(ri.hasNext());
        Resource res = ri.next();
        assertEquals(pathBase + i, res.getPath());
        assertEquals(nodes[i].getPrimaryNodeType().getName(), res.getResourceType());
    }
    assertFalse(ri.hasNext());
    try {
        ri.next();
        fail("Expected no element in the iterator");
    } catch (NoSuchElementException nsee) {
    // expected
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNode(org.apache.sling.commons.testing.jcr.MockNode) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) MockNode(org.apache.sling.commons.testing.jcr.MockNode) JcrNodeResourceIterator(org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with MockNodeIterator

use of org.apache.sling.commons.testing.jcr.MockNodeIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method generateBucketNodeChain.

private Node[] generateBucketNodeChain(Node rootNode, boolean isEmpty) throws RepositoryException {
    final Node[] bucketNodeChain = new Node[settings.bucketDepth];
    Node currentParentNode = rootNode;
    for (int i = 0; i < settings.bucketDepth; i++) {
        final Node node = mockStandardNode("bucketnode" + (isEmpty ? "-empty" : "") + "-level-" + (i + 1));
        bucketNodeChain[i] = node;
        when(node.getParent()).thenReturn(currentParentNode);
        when(node.getProperties()).thenReturn(new MockPropertyIterator(IteratorUtils.EMPTY_ITERATOR));
        when(node.hasProperty(JCRHttpCacheStoreConstants.PN_ISCACHEENTRYNODE)).thenReturn(false);
        when(node.hasProperty(JCRHttpCacheStoreConstants.PN_ISBUCKETNODE)).thenReturn(true);
        currentParentNode = node;
    }
    for (int i = 0; i < settings.bucketDepth; i++) {
        if (i < settings.bucketDepth) {
            final Node node = bucketNodeChain[i];
            final Node childNode = bucketNodeChain[i];
            final AtomicInteger deleteCounter = new AtomicInteger();
            doAnswer(new Answer<Object>() {

                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    deleteCounter.getAndIncrement();
                    return null;
                }
            }).when(node).remove();
            when(node.getParent().getNodes()).thenAnswer(new Answer<NodeIterator>() {

                @Override
                public NodeIterator answer(InvocationOnMock invocationOnMock) throws Throwable {
                    if (deleteCounter.get() > 0) {
                        return new MockNodeIterator();
                    } else {
                        return new MockNodeIterator(new Node[] { childNode });
                    }
                }
            });
        }
    }
    return bucketNodeChain;
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node) MockPropertyIterator(org.apache.sling.commons.testing.jcr.MockPropertyIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 4 with MockNodeIterator

use of org.apache.sling.commons.testing.jcr.MockNodeIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method generateEntryNodes.

private NodeIterator generateEntryNodes(final Node parentNode) throws RepositoryException, IOException {
    int totalNodeCount = settings.entryNodeCount + settings.expiredEntryNodeCount;
    final Node[] nodes = new AbstractNode[totalNodeCount];
    int i = 0;
    for (; i < settings.entryNodeCount; i++) {
        final Node randomNode = mockEntryNode(parentNode, i, false);
        nodes[i] = randomNode;
    }
    for (; i < totalNodeCount; i++) {
        final Node randomNode = mockEntryNode(parentNode, i, true);
        nodes[i] = randomNode;
    }
    return new MockNodeIterator(nodes);
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node)

Example 5 with MockNodeIterator

use of org.apache.sling.commons.testing.jcr.MockNodeIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method build.

public Node build() throws RepositoryException, IOException {
    final Node rootNode = mockStandardNode("rootnode");
    when(rootNode.getPath()).thenReturn(ROOT_PATH);
    Node[] bucketNodeChain = generateBucketNodeChain(rootNode, false);
    Node parentNode = bucketNodeChain[bucketNodeChain.length - 1];
    final NodeIterator entryNodes = generateEntryNodes(parentNode);
    when(parentNode.getNodes()).thenReturn(entryNodes);
    Node[] level1nodes = new Node[1 + settings.emptyBucketNodeChainCount];
    level1nodes[0] = bucketNodeChain[0];
    for (int i = 0; i < settings.emptyBucketNodeChainCount; i++) {
        Node[] emptyBucketNodeChain = generateBucketNodeChain(rootNode, true);
        when(emptyBucketNodeChain[emptyBucketNodeChain.length - 1].getNodes()).thenReturn(new MockNodeIterator());
        level1nodes[i + 1] = emptyBucketNodeChain[0];
    }
    when(rootNode.getNodes()).thenReturn(new MockNodeIterator(level1nodes));
    when(rootNode.getProperties()).thenReturn(new MockPropertyIterator(IteratorUtils.EMPTY_ITERATOR));
    return rootNode;
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node) MockPropertyIterator(org.apache.sling.commons.testing.jcr.MockPropertyIterator)

Aggregations

MockNodeIterator (org.apache.sling.commons.testing.jcr.MockNodeIterator)8 Node (javax.jcr.Node)7 NodeIterator (javax.jcr.NodeIterator)6 NoSuchElementException (java.util.NoSuchElementException)4 AbstractNode (org.apache.jackrabbit.commons.AbstractNode)4 JcrNodeResourceIterator (org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator)4 Resource (org.apache.sling.api.resource.Resource)3 MockNode (org.apache.sling.commons.testing.jcr.MockNode)3 MockPropertyIterator (org.apache.sling.commons.testing.jcr.MockPropertyIterator)3 Calendar (java.util.Calendar)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MockProperty (org.apache.sling.commons.testing.jcr.MockProperty)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1