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      /**
46       * Junit Constructor
47       */
48      public NodeListIteratorTest() {
49          super(NodeListIteratorTest.class.getSimpleName());
50      }
51  
52      @Override
53      public Iterator<Node> makeEmptyIterator() {
54          final NodeList emptyNodeList = new NodeList() {
55              @Override
56              public int getLength() {
57                  return 0;
58              }
59              @Override
60              public Node item(final int index) {
61                  throw new IndexOutOfBoundsException();
62              }
63          };
64  
65          if (createIteratorWithStandardConstr) {
66              return new NodeListIterator(emptyNodeList);
67          }
68          final Node parentNode = createMock(Node.class);
69          expect(parentNode.getChildNodes()).andStubReturn(emptyNodeList);
70          replay(parentNode);
71  
72          return new NodeListIterator(parentNode);
73      }
74  
75      @Override
76      public Iterator<Node> makeObject() {
77          final NodeList nodeList = new NodeList() {
78              @Override
79              public int getLength() {
80                  return nodes.length;
81              }
82              @Override
83              public Node item(final int index) {
84                  return nodes[index];
85              }
86          };
87  
88          return new NodeListIterator(nodeList);
89      }
90  
91      @BeforeEach
92      protected void setUp() throws Exception {
93  
94          // Default: use standard constr.
95          createIteratorWithStandardConstr = true;
96  
97          // create mocked Node Instances and fill Node[] to be used by test cases
98          final Node node1 = createMock(Element.class);
99          final Node node2 = createMock(Element.class);
100         final Node node3 = createMock(Text.class);
101         final Node node4 = createMock(Element.class);
102         nodes = new Node[] {node1, node2, node3, node4};
103 
104         replay(node1);
105         replay(node2);
106         replay(node3);
107         replay(node4);
108     }
109 
110     @Override
111     public boolean supportsRemove() {
112         return false;
113     }
114 
115     /**
116      * tests the convenience Constructor with parameter type org.w3c.Node
117      */
118     @Test
119     public void testEmptyIteratorWithNodeConstructor(){
120         createIteratorWithStandardConstr = false;
121         testEmptyIterator();
122     }
123 
124     /**
125      * tests the convenience Constructor with parameter type org.w3c.Node
126      */
127     @Test
128     public void testFullIteratorWithNodeConstructor(){
129         createIteratorWithStandardConstr = false;
130         testFullIterator();
131     }
132 
133     @Test
134     public void testNullConstructor() {
135         assertThrows(NullPointerException.class, () -> new NodeListIterator((Node) null));
136     }
137 
138 }