View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.xpath.iterators;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.api.DetailAST;
31  import com.puppycrawl.tools.checkstyle.xpath.AbstractNode;
32  import net.sf.saxon.om.NamespaceUri;
33  import net.sf.saxon.om.NodeInfo;
34  import net.sf.saxon.om.TreeInfo;
35  
36  public class ReverseListIteratorTest {
37  
38      @Test
39      public void testCorrectOrder() {
40          final List<AbstractNode> nodes = Arrays.asList(new TestNode(), new TestNode(),
41                  new TestNode());
42  
43          try (ReverseListIterator iterator = new ReverseListIterator(nodes)) {
44              for (int i = nodes.size() - 1; i >= 0; i--) {
45                  assertWithMessage("Invalid node")
46                          .that(iterator.next())
47                          .isEqualTo(nodes.get(i));
48              }
49              assertWithMessage("Node should be null")
50                      .that(iterator.next())
51                      .isNull();
52          }
53      }
54  
55      @Test
56      public void testNullList() {
57          try (ReverseListIterator iterator = new ReverseListIterator(null)) {
58              assertWithMessage("Node should be null")
59                      .that(iterator.next())
60                      .isNull();
61          }
62      }
63  
64      private static class TestNode extends AbstractNode {
65  
66          private TestNode() {
67              super(null);
68          }
69  
70          protected TestNode(TreeInfo treeInfo) {
71              super(treeInfo);
72          }
73  
74          @Override
75          public int getTokenType() {
76              return 0;
77          }
78  
79          @Override
80          public DetailAST getUnderlyingNode() {
81              return null;
82          }
83  
84          @Override
85          public int getDepth() {
86              return 0;
87          }
88  
89          @Override
90          protected List<AbstractNode> createChildren() {
91              return new ArrayList<>();
92          }
93  
94          @Override
95          public int getNodeKind() {
96              return 0;
97          }
98  
99          @Override
100         public int compareOrder(NodeInfo other) {
101             return 0;
102         }
103 
104         @Override
105         public String getLocalPart() {
106             return null;
107         }
108 
109         @Override
110         public NodeInfo getParent() {
111             return null;
112         }
113 
114         @Override
115         public String getAttributeValue(NamespaceUri uri, String local) {
116             return null;
117         }
118 
119         @Override
120         public NodeInfo getRoot() {
121             return null;
122         }
123 
124         @Override
125         public boolean hasChildNodes() {
126             return false;
127         }
128     }
129 }