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 DescendantIteratorTest {
30  
31      @Test
32      public void testIncludeSelf() {
33          final NodeInfo startNode = findNode("CLASS_DEF");
34  
35          try (DescendantIterator iterator = new DescendantIterator(startNode,
36                  DescendantIterator.StartWith.CURRENT_NODE)) {
37              assertWithMessage("Invalid node")
38                      .that(iterator.next())
39                      .isEqualTo(startNode);
40              assertWithMessage("Invalid node")
41                      .that(iterator.next())
42                      .isEqualTo(findNode("OBJBLOCK"));
43              assertWithMessage("Invalid node")
44                      .that(iterator.next())
45                      .isEqualTo(findNode("LCURLY"));
46              assertWithMessage("Invalid node")
47                      .that(iterator.next())
48                      .isEqualTo(findNode("METHOD_DEF"));
49              assertWithMessage("Invalid node")
50                      .that(iterator.next())
51                      .isEqualTo(findNode("RCURLY"));
52              assertWithMessage("Invalid node")
53                      .that(iterator.next())
54                      .isEqualTo(findNode("PARAMETERS"));
55              assertWithMessage("Invalid node")
56                      .that(iterator.next())
57                      .isEqualTo(findNode("SLIST"));
58              assertWithMessage("Node should be null")
59                      .that(iterator.next())
60                      .isNull();
61              assertWithMessage("Node should be null")
62                      .that(iterator.next())
63                      .isNull();
64          }
65      }
66  
67      @Test
68      public void testWithoutSelf() {
69          final NodeInfo startNode = findNode("CLASS_DEF");
70  
71          try (DescendantIterator iterator = new DescendantIterator(startNode,
72                  DescendantIterator.StartWith.CHILDREN)) {
73              assertWithMessage("Invalid node")
74                      .that(iterator.next())
75                      .isEqualTo(findNode("OBJBLOCK"));
76              assertWithMessage("Invalid node")
77                      .that(iterator.next())
78                      .isEqualTo(findNode("LCURLY"));
79              assertWithMessage("Invalid node")
80                      .that(iterator.next())
81                      .isEqualTo(findNode("METHOD_DEF"));
82              assertWithMessage("Invalid node")
83                      .that(iterator.next())
84                      .isEqualTo(findNode("RCURLY"));
85              assertWithMessage("Invalid node")
86                      .that(iterator.next())
87                      .isEqualTo(findNode("PARAMETERS"));
88              assertWithMessage("Invalid node")
89                      .that(iterator.next())
90                      .isEqualTo(findNode("SLIST"));
91              assertWithMessage("Node should be null")
92                      .that(iterator.next())
93                      .isNull();
94          }
95      }
96  
97      @Test
98      public void testWithNull() {
99          final NodeInfo startNode = findNode("CLASS_DEF");
100 
101         try (DescendantIterator iterator = new DescendantIterator(startNode, null)) {
102             assertWithMessage("Node should be null")
103                     .that(iterator.next())
104                     .isNull();
105         }
106     }
107 }