View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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  
35  public class ReverseListIteratorTest {
36  
37      @Test
38      public void testCorrectOrder() {
39          final List<AbstractNode> nodes = Arrays.asList(new TestNode(), new TestNode(),
40                  new TestNode());
41  
42          try (ReverseListIterator iterator = new ReverseListIterator(nodes)) {
43              for (int i = nodes.size() - 1; i >= 0; i--) {
44                  assertWithMessage("Invalid node")
45                          .that(iterator.next())
46                          .isEqualTo(nodes.get(i));
47              }
48              assertWithMessage("Node should be null")
49                      .that(iterator.next())
50                      .isNull();
51          }
52      }
53  
54      @Test
55      public void testNullList() {
56          try (ReverseListIterator iterator = new ReverseListIterator(null)) {
57              assertWithMessage("Node should be null")
58                      .that(iterator.next())
59                      .isNull();
60          }
61      }
62  
63      private static final class TestNode extends AbstractNode {
64  
65          private TestNode() {
66              super(null);
67          }
68  
69          @Override
70          public int getTokenType() {
71              return 0;
72          }
73  
74          @Override
75          public DetailAST getUnderlyingNode() {
76              return null;
77          }
78  
79          @Override
80          public int getDepth() {
81              return 0;
82          }
83  
84          @Override
85          protected List<AbstractNode> createChildren() {
86              return new ArrayList<>();
87          }
88  
89          @Override
90          public int getNodeKind() {
91              return 0;
92          }
93  
94          @Override
95          public int compareOrder(NodeInfo other) {
96              return 0;
97          }
98  
99          @Override
100         public String getLocalPart() {
101             return null;
102         }
103 
104         @Override
105         public NodeInfo getParent() {
106             return null;
107         }
108 
109         @Override
110         public String getAttributeValue(NamespaceUri uri, String local) {
111             return null;
112         }
113 
114         @Override
115         public NodeInfo getRoot() {
116             return null;
117         }
118 
119         @Override
120         public boolean hasChildNodes() {
121             return false;
122         }
123     }
124 }