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  import static com.puppycrawl.tools.checkstyle.internal.utils.XpathIteratorUtil.findNode;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import net.sf.saxon.om.NodeInfo;
28  
29  public class PrecedingIteratorTest {
30  
31      @Test
32      public void testPrecedingNodes() {
33          final NodeInfo startNode = findNode("SLIST");
34  
35          try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
36              assertWithMessage("Invalid node")
37                      .that(iterator.next())
38                      .isEqualTo(findNode("PARAMETERS"));
39              assertWithMessage("Invalid node")
40                      .that(iterator.next())
41                      .isEqualTo(findNode("METHOD_DEF"));
42              assertWithMessage("Invalid node")
43                      .that(iterator.next())
44                      .isEqualTo(findNode("LCURLY"));
45              assertWithMessage("Invalid node")
46                      .that(iterator.next())
47                      .isEqualTo(findNode("OBJBLOCK"));
48              assertWithMessage("Invalid node")
49                      .that(iterator.next())
50                      .isEqualTo(findNode("CLASS_DEF"));
51              assertWithMessage("Invalid node")
52                      .that(iterator.next())
53                      .isEqualTo(findNode("ANNOTATIONS"));
54              assertWithMessage("Invalid node")
55                      .that(iterator.next())
56                      .isEqualTo(findNode("ANNOTATION_DEF"));
57              assertWithMessage("Invalid node")
58                      .that(iterator.next())
59                      .isEqualTo(findNode("PACKAGE_DEF"));
60              assertWithMessage("Invalid node")
61                      .that(iterator.next())
62                      .isEqualTo(findNode("ROOT"));
63              assertWithMessage("Node should be null")
64                      .that(iterator.next())
65                      .isNull();
66              assertWithMessage("Node should be null")
67                      .that(iterator.next())
68                      .isNull();
69          }
70      }
71  
72      @Test
73      public void testNoParent() {
74          final NodeInfo startNode = findNode("ROOT");
75  
76          try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
77              assertWithMessage("Node should be null")
78                      .that(iterator.next())
79                      .isNull();
80          }
81      }
82  
83      @Test
84      public void testReverseOrderOfDescendants() {
85          final NodeInfo startNode = findNode("RCURLY");
86  
87          try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
88              assertWithMessage("Invalid node")
89                      .that(iterator.next())
90                      .isEqualTo(findNode("METHOD_DEF"));
91              assertWithMessage("Invalid node")
92                      .that(iterator.next())
93                      .isEqualTo(findNode("SLIST"));
94              assertWithMessage("Invalid node")
95                      .that(iterator.next())
96                      .isEqualTo(findNode("PARAMETERS"));
97              assertWithMessage("Invalid node")
98                      .that(iterator.next())
99                      .isEqualTo(findNode("LCURLY"));
100             assertWithMessage("Invalid node")
101                     .that(iterator.next())
102                     .isEqualTo(findNode("OBJBLOCK"));
103             assertWithMessage("Invalid node")
104                     .that(iterator.next())
105                     .isEqualTo(findNode("CLASS_DEF"));
106             assertWithMessage("Invalid node")
107                     .that(iterator.next())
108                     .isEqualTo(findNode("ANNOTATIONS"));
109             assertWithMessage("Invalid node")
110                     .that(iterator.next())
111                     .isEqualTo(findNode("ANNOTATION_DEF"));
112             assertWithMessage("Invalid node")
113                     .that(iterator.next())
114                     .isEqualTo(findNode("PACKAGE_DEF"));
115             assertWithMessage("Invalid node")
116                     .that(iterator.next())
117                     .isEqualTo(findNode("ROOT"));
118             assertWithMessage("Node should be null")
119                     .that(iterator.next())
120                     .isNull();
121         }
122     }
123 
124     @Test
125     public void testNoSibling() {
126         final NodeInfo startNode = findNode("ANNOTATIONS");
127 
128         try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
129             assertWithMessage("Invalid node")
130                     .that(iterator.next())
131                     .isEqualTo(findNode("PACKAGE_DEF"));
132             assertWithMessage("Invalid node")
133                     .that(iterator.next())
134                     .isEqualTo(findNode("ROOT"));
135             assertWithMessage("Node should be null")
136                     .that(iterator.next())
137                     .isNull();
138         }
139     }
140 }