Author: jverhaeg(a)redhat.com
Date: 2008-09-09 14:56:21 -0400 (Tue, 09 Sep 2008)
New Revision: 512
Added:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNodeIterator.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrPropertyIterator.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyIteratorTest.java
Modified:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java
Log:
DNA-178: Extracted node and property iterators out to top-level classes so that we can
document implementation.
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 2008-09-09 16:06:09
UTC (rev 511)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrNode.java 2008-09-09 18:56:21
UTC (rev 512)
@@ -24,9 +24,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
-import java.util.Iterator;
import java.util.List;
-import java.util.NoSuchElementException;
import java.util.Set;
import java.util.UUID;
import javax.jcr.Item;
@@ -250,63 +248,7 @@
* @see javax.jcr.Node#getNodes()
*/
public final NodeIterator getNodes() {
- return new NodeIterator() {
-
- private final Iterator<Name> childIterator = (children == null ? null :
children.iterator());
- private final Iterator<Integer> childNameCountIterator =
(childNameCounts == null ? null : childNameCounts.iterator());
- private Name child;
- private int childNameCount;
- private int childNdx = 1;
- private int ndx;
- private Node node;
-
- public long getPosition() {
- return ndx;
- }
-
- public long getSize() {
- return -1;
- }
-
- public boolean hasNext() {
- return ((childIterator != null && childIterator.hasNext()) ||
(child != null && childNdx <= childNameCount));
- }
-
- public Object next() {
- return nextNode();
- }
-
- public Node nextNode() {
- if (childIterator == null) {
- throw new NoSuchElementException();
- }
- if (child == null || childNdx > childNameCount) {
- child = childIterator.next();
- childNameCount = childNameCountIterator.next();
- childNdx = 1;
- }
- try {
- node = getNode(child.getString() + '[' + childNdx +
']');
- childNdx++;
- ndx++;
- return node;
- } catch (RepositoryException error) {
- // TODO: Change to DnaException once DNA-180 is addressed
- throw new RuntimeException(error);
- }
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- public void skip( long count ) {
- ArgCheck.isNonNegative(count, "count");
- while (--count >= 0) {
- nextNode();
- }
- }
- };
+ return new JcrNodeIterator(this, children, childNameCounts);
}
/**
@@ -374,45 +316,7 @@
* @see javax.jcr.Node#getProperties()
*/
public final PropertyIterator getProperties() {
- assert properties != null;
- return new PropertyIterator() {
-
- private final Iterator<Property> iterator = properties.iterator();
- private int ndx;
-
- public long getPosition() {
- return ndx;
- }
-
- public long getSize() {
- return -1;
- }
-
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- public Object next() {
- return nextProperty();
- }
-
- public Property nextProperty() {
- Property property = iterator.next();
- ndx++;
- return property;
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- public void skip( long count ) {
- ArgCheck.isNonNegative(count, "count");
- while (--count >= 0) {
- nextProperty();
- }
- }
- };
+ return new JcrPropertyIterator(properties);
}
/**
Added: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNodeIterator.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNodeIterator.java
(rev 0)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNodeIterator.java 2008-09-09 18:56:21
UTC (rev 512)
@@ -0,0 +1,140 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * 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.
+ *
+ * This software 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 java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import org.jboss.dna.common.util.ArgCheck;
+import org.jboss.dna.spi.graph.Name;
+
+/**
+ * @author jverhaeg
+ */
+final class JcrNodeIterator implements NodeIterator {
+
+ private final Node parent;
+ private final Iterator<Name> childIterator;
+ private final Iterator<Integer> childNameCountIterator;
+ private transient Name child;
+ private transient int childNameCount;
+ private transient int childNdx = 1;
+ private transient int ndx;
+ private transient Node node;
+
+ JcrNodeIterator( Node parent,
+ List<Name> children,
+ List<Integer> childNameCounts ) {
+ assert parent != null;
+ this.parent = parent;
+ childIterator = (children == null ? null : children.iterator());
+ childNameCountIterator = (childNameCounts == null ? null :
childNameCounts.iterator());
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.RangeIterator#getPosition()
+ */
+ public long getPosition() {
+ return ndx;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return -1L
+ * @see javax.jcr.RangeIterator#getSize()
+ */
+ public long getSize() {
+ return -1L;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Iterator#hasNext()
+ */
+ public boolean hasNext() {
+ return ((childIterator != null && childIterator.hasNext()) || (child !=
null && childNdx <= childNameCount));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Iterator#next()
+ */
+ public Object next() {
+ return nextNode();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.NodeIterator#nextNode()
+ */
+ public Node nextNode() {
+ if (childIterator == null) {
+ throw new NoSuchElementException();
+ }
+ if (child == null || childNdx > childNameCount) {
+ child = childIterator.next();
+ childNameCount = childNameCountIterator.next();
+ childNdx = 1;
+ }
+ try {
+ node = parent.getNode(child.getString() + '[' + childNdx +
']');
+ childNdx++;
+ ndx++;
+ return node;
+ } catch (RepositoryException error) {
+ // TODO: Change to DnaException once DNA-180 is addressed
+ throw new RuntimeException(error);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws UnsupportedOperationException always
+ * @see java.util.Iterator#remove()
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws IllegalArgumentException if <code>count</code> is negative.
+ * @see javax.jcr.RangeIterator#skip(long)
+ */
+ public void skip( long count ) {
+ ArgCheck.isNonNegative(count, "count");
+ while (--count >= 0) {
+ nextNode();
+ }
+ }
+}
Property changes on: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNodeIterator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrPropertyIterator.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrPropertyIterator.java
(rev 0)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrPropertyIterator.java 2008-09-09
18:56:21 UTC (rev 512)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * 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.
+ *
+ * This software 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 java.util.Iterator;
+import java.util.Set;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import org.jboss.dna.common.util.ArgCheck;
+
+/**
+ * @author jverhaeg
+ */
+final class JcrPropertyIterator implements PropertyIterator {
+
+ private final Iterator<Property> iterator;
+ private int ndx;
+
+ JcrPropertyIterator( Set<Property> properties ) {
+ assert properties != null;
+ iterator = properties.iterator();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.RangeIterator#getPosition()
+ */
+ public long getPosition() {
+ return ndx;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return -1L
+ * @see javax.jcr.RangeIterator#getSize()
+ */
+ public long getSize() {
+ return -1L;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Iterator#hasNext()
+ */
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Iterator#next()
+ */
+ public Object next() {
+ return nextProperty();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.PropertyIterator#nextProperty()
+ */
+ public Property nextProperty() {
+ Property property = iterator.next();
+ ndx++;
+ return property;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws UnsupportedOperationException always
+ * @see java.util.Iterator#remove()
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws IllegalArgumentException if <code>count</code> is negative.
+ * @see javax.jcr.RangeIterator#skip(long)
+ */
+ public void skip( long count ) {
+ ArgCheck.isNonNegative(count, "count");
+ while (--count >= 0) {
+ nextProperty();
+ }
+ }
+}
Property changes on:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrPropertyIterator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
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 2008-09-09
16:06:09 UTC (rev 511)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrNodeTest.java 2008-09-09
18:56:21 UTC (rev 512)
@@ -36,10 +36,8 @@
import javax.jcr.ItemNotFoundException;
import javax.jcr.ItemVisitor;
import javax.jcr.Node;
-import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
-import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
@@ -59,6 +57,53 @@
*/
public class AbstractJcrNodeTest {
+ static MockAbstractJcrNode createChild( Session session,
+ String name,
+ int index,
+ List<Segment> children,
+ Node parent ) throws Exception {
+ MockAbstractJcrNode child = new MockAbstractJcrNode(session, name, parent);
+ Segment seg = Mockito.mock(Segment.class);
+ stub(seg.getName()).toReturn(new BasicName(null, name));
+ children.add(seg);
+ stub(session.getItem(parent.getPath() + "/{}" + name + '[' +
index + ']')).toReturn(child);
+ return child;
+ }
+
+ static class MockAbstractJcrNode extends AbstractJcrNode {
+
+ String name;
+ Node parent;
+
+ MockAbstractJcrNode( Session session,
+ String name,
+ Node parent ) {
+ super(session);
+ this.name = name;
+ this.parent = parent;
+ }
+
+ public int getDepth() {
+ return 0;
+ }
+
+ public int getIndex() {
+ return 0;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Node getParent() {
+ return parent;
+ }
+
+ public String getPath() throws RepositoryException {
+ return (parent == null ? '/' + getName() : parent.getPath() +
'/' + getName());
+ }
+ }
+
private AbstractJcrNode node;
@Mock
private Session session;
@@ -173,7 +218,7 @@
@Test
public void shouldProvideNode() throws Exception {
- Node child = createChild("child", 1, children, node);
+ Node child = createChild(session, "child", 1, children, node);
node.setChildren(children);
stub(session.getItem("/node/child")).toReturn(child);
assertThat(node.getNode("child"), is(child));
@@ -198,45 +243,9 @@
@Test
public void shouldProvideNodeIterator() throws Exception {
- Node child1 = createChild("child1", 1, children, node);
- Node child2_1 = createChild("child2", 1, children, node);
- Node child2_2 = createChild("child2", 2, children, node);
- createChild("child3", 1, children, node);
- createChild("child4", 1, children, node);
- Node child5 = createChild("child5", 1, children, node);
- node.setChildren(children);
- NodeIterator iter = node.getNodes();
- assertThat(iter, notNullValue());
- assertThat(iter.getSize(), is(-1L));
- assertThat(iter.getPosition(), is(0L));
- assertThat(iter.hasNext(), is(true));
- assertThat((Node)iter.next(), is(child1));
- assertThat(iter.getPosition(), is(1L));
- assertThat(iter.hasNext(), is(true));
- assertThat(iter.nextNode(), is(child2_1));
- assertThat(iter.getPosition(), is(2L));
- assertThat(iter.hasNext(), is(true));
- assertThat(iter.nextNode(), is(child2_2));
- assertThat(iter.getPosition(), is(3L));
- assertThat(iter.hasNext(), is(true));
- iter.skip(2);
- assertThat(iter.getPosition(), is(5L));
- assertThat(iter.hasNext(), is(true));
- assertThat(iter.nextNode(), is(child5));
- assertThat(iter.getPosition(), is(6L));
- assertThat(iter.hasNext(), is(false));
+ assertThat(node.getNodes(), notNullValue());
}
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowNodeIteratorRemove() throws Exception {
- node.getNodes().remove();
- }
-
- @Test( expected = IllegalArgumentException.class )
- public void shouldNotAllowNodeIteratorNegativeSkip() throws Exception {
- node.getNodes().skip(-1);
- }
-
@Test
public void shoudProvidePrimaryItem() throws Exception {
Property property = Mockito.mock(Property.class);
@@ -254,51 +263,19 @@
}
@Test
- public void shouldProvidePropertyIterator() throws Exception {
- properties.add(Mockito.mock(Property.class));
- properties.add(Mockito.mock(Property.class));
- properties.add(Mockito.mock(Property.class));
- properties.add(Mockito.mock(Property.class));
- PropertyIterator iter = node.getProperties();
- assertThat(iter, notNullValue());
- assertThat(iter.getSize(), is(-1L));
- assertThat(iter.getPosition(), is(0L));
- assertThat(iter.hasNext(), is(true));
- assertThat(iter.next(), notNullValue());
- assertThat(iter.getPosition(), is(1L));
- assertThat(iter.hasNext(), is(true));
- iter.skip(2);
- assertThat(iter.getPosition(), is(3L));
- assertThat(iter.hasNext(), is(true));
- assertThat(iter.nextProperty(), notNullValue());
- assertThat(iter.getPosition(), is(4L));
- assertThat(iter.hasNext(), is(false));
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowPropertyIteratorRemove() throws Exception {
- node.getProperties().remove();
- }
-
- @Test( expected = IllegalArgumentException.class )
- public void shouldNotAllowPropertyIteratorNegativeSkip() throws Exception {
- node.getProperties().skip(-1);
- }
-
- @Test
public void shouldProvideProperty() throws Exception {
Property prop1 = Mockito.mock(Property.class);
stub(prop1.getName()).toReturn("prop1");
properties.add(prop1);
assertThat(node.getProperty("prop1"), is(prop1));
- MockAbstractJcrNode child = createChild("child", 1, children, node);
+ MockAbstractJcrNode child = createChild(session, "child", 1, children,
node);
Set<Property> properties = new HashSet<Property>();
child.setProperties(properties);
Property prop2 = Mockito.mock(Property.class);
stub(prop2.getName()).toReturn("prop2");
stub(session.getItem("/node/child/prop2")).toReturn(prop2);
properties.add(prop2);
- MockAbstractJcrNode prop3Node = createChild("prop3", 1, children,
child);
+ MockAbstractJcrNode prop3Node = createChild(session, "prop3", 1,
children, child);
node.setChildren(children);
assertThat(node.getProperty("child/prop2"), is(prop2));
// Ensure we return a property even when a child exists with the same name
@@ -326,10 +303,10 @@
@Test( expected = PathNotFoundException.class )
public void shouldNotProvideDescendentPropertyIfNotAvailable() throws Exception {
- MockAbstractJcrNode child = createChild("child", 1, children, node);
+ MockAbstractJcrNode child = createChild(session, "child", 1, children,
node);
Set<Property> properties = new HashSet<Property>();
child.setProperties(properties);
- MockAbstractJcrNode propNode = createChild("prop", 1, children,
child);
+ MockAbstractJcrNode propNode = createChild(session, "prop", 1,
children, child);
node.setChildren(children);
stub(session.getItem("/node/child/prop")).toReturn(propNode);
node.getProperty("child/prop");
@@ -398,18 +375,14 @@
stub(prop.getName()).toReturn("prop");
properties.add(prop);
assertThat(node.hasNode("prop"), is(false));
- Node child = createChild("child", 1, children, node);
- Node child2 = createChild("child2", 1, children, child);
+ Node child = createChild(session, "child", 1, children, node);
+ Node child2 = createChild(session, "child2", 1, children, child);
node.setChildren(children);
assertThat(node.hasNode("{}child"), is(true));
stub(session.getItem("/node/child/{}child2")).toReturn(child2);
assertThat(node.hasNode("child/{}child2"), is(true));
}
- @Test
- public void shouldNotIndicateHasNodeIfPathIsChildProperty() throws Exception {
- }
-
@Test( expected = IllegalArgumentException.class )
public void shouldNotAllowHasNodeWithNoPath() throws Exception {
node.hasNode(null);
@@ -423,7 +396,7 @@
@Test
public void shouldProvideHasNodes() throws Exception {
assertThat(node.hasNodes(), is(false));
- createChild("child", 1, children, node);
+ createChild(session, "child", 1, children, node);
node.setChildren(children);
assertThat(node.hasNodes(), is(true));
}
@@ -438,7 +411,7 @@
@Test
public void shouldIndicateHasProperty() throws Exception {
assertThat(node.hasProperty("prop"), is(false));
- MockAbstractJcrNode child = createChild("child", 1, children, node);
+ MockAbstractJcrNode child = createChild(session, "child", 1, children,
node);
node.setChildren(children);
assertThat(node.hasProperty("child"), is(false));
Property prop = Mockito.mock(Property.class);
@@ -629,50 +602,4 @@
public void shouldNotAllowUpdate() throws Exception {
node.update(null);
}
-
- private MockAbstractJcrNode createChild( String name,
- int index,
- List<Segment> children,
- Node parent ) throws Exception {
- MockAbstractJcrNode child = new MockAbstractJcrNode(session, name, parent);
- Segment seg = Mockito.mock(Segment.class);
- stub(seg.getName()).toReturn(new BasicName(null, name));
- children.add(seg);
- stub(session.getItem(parent.getPath() + "/{}" + name + '[' +
index + ']')).toReturn(child);
- return child;
- }
-
- private class MockAbstractJcrNode extends AbstractJcrNode {
-
- String name;
- Node parent;
-
- MockAbstractJcrNode( Session session,
- String name,
- Node parent ) {
- super(session);
- this.name = name;
- this.parent = parent;
- }
-
- public int getDepth() {
- return 0;
- }
-
- public int getIndex() {
- return 0;
- }
-
- public String getName() {
- return name;
- }
-
- public Node getParent() {
- return parent;
- }
-
- public String getPath() throws RepositoryException {
- return (parent == null ? '/' + getName() : parent.getPath() +
'/' + getName());
- }
- }
}
Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
(rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java 2008-09-09
18:56:21 UTC (rev 512)
@@ -0,0 +1,96 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * 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.
+ *
+ * This software 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.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+import java.util.ArrayList;
+import java.util.List;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Session;
+import org.jboss.dna.jcr.AbstractJcrNodeTest.MockAbstractJcrNode;
+import org.jboss.dna.spi.graph.Path.Segment;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
+
+/**
+ * @author jverhaeg
+ */
+public class JcrNodeIteratorTest {
+
+ private AbstractJcrNode node;
+ @Mock
+ private Session session;
+ private List<Segment> children;
+
+ @Before
+ public void before() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ children = new ArrayList<Segment>();
+ node = new MockAbstractJcrNode(session, "node", null);
+ }
+
+ @Test
+ public void shouldProvideNodeIterator() throws Exception {
+ Node child1 = AbstractJcrNodeTest.createChild(session, "child1", 1,
children, node);
+ Node child2_1 = AbstractJcrNodeTest.createChild(session, "child2", 1,
children, node);
+ Node child2_2 = AbstractJcrNodeTest.createChild(session, "child2", 2,
children, node);
+ AbstractJcrNodeTest.createChild(session, "child3", 1, children, node);
+ AbstractJcrNodeTest.createChild(session, "child4", 1, children, node);
+ Node child5 = AbstractJcrNodeTest.createChild(session, "child5", 1,
children, node);
+ node.setChildren(children);
+ NodeIterator iter = node.getNodes();
+ assertThat(iter, notNullValue());
+ assertThat(iter.getSize(), is(-1L));
+ assertThat(iter.getPosition(), is(0L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat((Node)iter.next(), is(child1));
+ assertThat(iter.getPosition(), is(1L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat(iter.nextNode(), is(child2_1));
+ assertThat(iter.getPosition(), is(2L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat(iter.nextNode(), is(child2_2));
+ assertThat(iter.getPosition(), is(3L));
+ assertThat(iter.hasNext(), is(true));
+ iter.skip(2);
+ assertThat(iter.getPosition(), is(5L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat(iter.nextNode(), is(child5));
+ assertThat(iter.getPosition(), is(6L));
+ assertThat(iter.hasNext(), is(false));
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowNodeIteratorRemove() throws Exception {
+ node.getNodes().remove();
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotAllowNodeIteratorNegativeSkip() throws Exception {
+ node.getNodes().skip(-1);
+ }
+}
Property changes on:
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNodeIteratorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyIteratorTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyIteratorTest.java
(rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyIteratorTest.java 2008-09-09
18:56:21 UTC (rev 512)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * 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.
+ *
+ * This software 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.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+import java.util.HashSet;
+import java.util.Set;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.Session;
+import org.jboss.dna.jcr.AbstractJcrNodeTest.MockAbstractJcrNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
+
+/**
+ * @author jverhaeg
+ */
+public class JcrPropertyIteratorTest {
+
+ private AbstractJcrNode node;
+ @Mock
+ private Session session;
+ private Set<Property> properties;
+
+ @Before
+ public void before() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ properties = new HashSet<Property>();
+ node = new MockAbstractJcrNode(session, "node", null);
+ node.setProperties(properties);
+ }
+
+ @Test
+ public void shouldProvidePropertyIterator() throws Exception {
+ properties.add(Mockito.mock(Property.class));
+ properties.add(Mockito.mock(Property.class));
+ properties.add(Mockito.mock(Property.class));
+ properties.add(Mockito.mock(Property.class));
+ PropertyIterator iter = node.getProperties();
+ assertThat(iter, notNullValue());
+ assertThat(iter.getSize(), is(-1L));
+ assertThat(iter.getPosition(), is(0L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat(iter.next(), notNullValue());
+ assertThat(iter.getPosition(), is(1L));
+ assertThat(iter.hasNext(), is(true));
+ iter.skip(2);
+ assertThat(iter.getPosition(), is(3L));
+ assertThat(iter.hasNext(), is(true));
+ assertThat(iter.nextProperty(), notNullValue());
+ assertThat(iter.getPosition(), is(4L));
+ assertThat(iter.hasNext(), is(false));
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowPropertyIteratorRemove() throws Exception {
+ node.getProperties().remove();
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotAllowPropertyIteratorNegativeSkip() throws Exception {
+ node.getProperties().skip(-1);
+ }
+}
Property changes on:
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyIteratorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain