View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.iterators;
18  
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.replay;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.Iterator;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  import org.w3c.dom.Text;
32  
33  /**
34   * Tests the NodeListIterator.
35   */
36  public class NodeListIteratorTest extends AbstractIteratorTest<Node> {
37  
38      // Node array to be filled with mocked Node instances
39      private Node[] nodes;
40  
41      // NodeListIterator supports two constructors. This flag allows to
42      // control, which constructor to use in makeObject() and makeEmptyIterator
43      private boolean createIteratorWithStandardConstr = true;
44  
45      @Override
46      public Iterator<Node> makeEmptyIterator() {
47          final NodeList emptyNodeList = new NodeList() {
48              @Override
49              public int getLength() {
50                  return 0;
51              }
52  
53              @Override
54              public Node item(final int index) {
55                  throw new IndexOutOfBoundsException();
56              }
57          };
58  
59          if (createIteratorWithStandardConstr) {
60              return new NodeListIterator(emptyNodeList);
61          }
62          final Node parentNode = createMock(Node.class);
63          expect(parentNode.getChildNodes()).andStubReturn(emptyNodeList);
64          replay(parentNode);
65  
66          return new NodeListIterator(parentNode);
67      }
68  
69      @Override
70      public Iterator<Node> makeObject() {
71          final NodeList nodeList = new NodeList() {
72              @Override
73              public int getLength() {
74                  return nodes.length;
75              }
76  
77              @Override
78              public Node item(final int index) {
79                  return nodes[index];
80              }
81          };
82  
83          return new NodeListIterator(nodeList);
84      }
85  
86      @BeforeEach
87      protected void setUp() throws Exception {
88  
89          // Default: use standard constr.
90          createIteratorWithStandardConstr = true;
91  
92          // create mocked Node Instances and fill Node[] to be used by test cases
93          final Node node1 = createMock(Element.class);
94          final Node node2 = createMock(Element.class);
95          final Node node3 = createMock(Text.class);
96          final Node node4 = createMock(Element.class);
97          nodes = new Node[] { node1, node2, node3, node4 };
98  
99          replay(node1);
100         replay(node2);
101         replay(node3);
102         replay(node4);
103     }
104 
105     @Override
106     public boolean supportsRemove() {
107         return false;
108     }
109 
110     /**
111      * tests the convenience Constructor with parameter type org.w3c.Node
112      */
113     @Test
114     public void testEmptyIteratorWithNodeConstructor() {
115         createIteratorWithStandardConstr = false;
116         testEmptyIterator();
117     }
118 
119     /**
120      * tests the convenience Constructor with parameter type org.w3c.Node
121      */
122     @Test
123     public void testFullIteratorWithNodeConstructor() {
124         createIteratorWithStandardConstr = false;
125         testFullIterator();
126     }
127 
128     @Test
129     public void testNullConstructor() {
130         assertThrows(NullPointerException.class, () -> new NodeListIterator((Node) null));
131     }
132 
133 }