View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
24  
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  import net.sf.saxon.om.AxisInfo;
29  import net.sf.saxon.om.NamespaceUri;
30  import net.sf.saxon.tree.iter.AxisIterator;
31  
32  public class AttributeNodeTest {
33  
34      private static AttributeNode attributeNode;
35  
36      @BeforeEach
37      public void init() {
38          attributeNode = new AttributeNode("name", "value");
39      }
40  
41      @Test
42      public void testGetNamespaceUri() {
43          assertWithMessage("Attribute node should have default namespace URI")
44              .that(attributeNode.getNamespaceUri())
45              .isEqualTo(NamespaceUri.NULL);
46      }
47  
48      @Test
49      public void testGetUri() {
50          assertWithMessage("Attribute node should have blank URI")
51              .that(attributeNode.getURI())
52              .isEqualTo("");
53      }
54  
55      @Test
56      public void testCompareOrder() {
57          final UnsupportedOperationException exc =
58                  getExpectedThrowable(UnsupportedOperationException.class, () -> {
59                      attributeNode.compareOrder(null);
60                  });
61          assertWithMessage("Invalid exception message")
62              .that(exc.getMessage())
63              .isEqualTo("Operation is not supported");
64      }
65  
66      @Test
67      public void testGetDepth() {
68          final UnsupportedOperationException exception =
69              getExpectedThrowable(UnsupportedOperationException.class, attributeNode::getDepth);
70          assertWithMessage("Invalid exception message")
71              .that(exception)
72              .hasMessageThat()
73                  .isEqualTo("Operation is not supported");
74      }
75  
76      @Test
77      public void testHasChildNodes() {
78          assertWithMessage("Attribute node shouldn't have children")
79              .that(attributeNode.hasChildNodes())
80              .isFalse();
81      }
82  
83      @Test
84      public void testGetAttributeValue() {
85          final UnsupportedOperationException exc =
86                  getExpectedThrowable(UnsupportedOperationException.class, () -> {
87                      attributeNode.getAttributeValue("", "");
88                  });
89          assertWithMessage("Invalid exception message")
90              .that(exc.getMessage())
91              .isEqualTo("Operation is not supported");
92      }
93  
94      @Test
95      public void testGetChildren() {
96          final UnsupportedOperationException exception =
97              getExpectedThrowable(UnsupportedOperationException.class, attributeNode::getChildren);
98          assertWithMessage("Invalid exception message")
99              .that(exception)
100             .hasMessageThat()
101             .isEqualTo("Operation is not supported");
102     }
103 
104     @Test
105     public void testGetParent() {
106         final UnsupportedOperationException exc =
107                 getExpectedThrowable(UnsupportedOperationException.class, attributeNode::getParent);
108         assertWithMessage("Invalid exception message")
109             .that(exc.getMessage())
110             .isEqualTo("Operation is not supported");
111     }
112 
113     @Test
114     public void testGetRoot() {
115         final UnsupportedOperationException exc =
116                 getExpectedThrowable(UnsupportedOperationException.class, attributeNode::getRoot);
117         assertWithMessage("Invalid exception message")
118             .that(exc.getMessage())
119             .isEqualTo("Operation is not supported");
120     }
121 
122     @Test
123     public void testGetStringValue() {
124         assertWithMessage("Invalid string value")
125             .that(attributeNode.getStringValue())
126             .isEqualTo("value");
127     }
128 
129     @Test
130     public void testIterate() {
131         final UnsupportedOperationException exc =
132                 getExpectedThrowable(UnsupportedOperationException.class,
133                         () -> callIterateAxis(attributeNode));
134         assertWithMessage("Invalid exception message")
135             .that(exc.getMessage())
136             .isEqualTo("Operation is not supported");
137     }
138 
139     private static void callIterateAxis(AttributeNode node) {
140         try (AxisIterator ignored = node.iterateAxis(AxisInfo.SELF)) {
141             assertWithMessage("Exception is expected")
142                     .that(ignored)
143                     .isNotNull();
144         }
145     }
146 
147     @Test
148     public void testGetLineNumber() {
149         final UnsupportedOperationException exc =
150                 getExpectedThrowable(UnsupportedOperationException.class,
151                         attributeNode::getLineNumber);
152         assertWithMessage("Invalid exception message")
153             .that(exc.getMessage())
154             .isEqualTo("Operation is not supported");
155     }
156 
157     @Test
158     public void testGetColumnNumber() {
159         final UnsupportedOperationException exc =
160                 getExpectedThrowable(UnsupportedOperationException.class,
161                         attributeNode::getColumnNumber);
162         assertWithMessage("Invalid exception message")
163             .that(exc.getMessage())
164             .isEqualTo("Operation is not supported");
165     }
166 
167     @Test
168     public void testGetTokenType() {
169         final UnsupportedOperationException exc =
170                 getExpectedThrowable(UnsupportedOperationException.class,
171                         attributeNode::getTokenType);
172         assertWithMessage("Invalid exception message")
173             .that(exc.getMessage())
174             .isEqualTo("Operation is not supported");
175     }
176 
177     @Test
178     public void testGetUnderlyingNode() {
179         final UnsupportedOperationException exc =
180                 getExpectedThrowable(UnsupportedOperationException.class,
181                         attributeNode::getUnderlyingNode);
182         assertWithMessage("Invalid exception message")
183             .that(exc.getMessage())
184             .isEqualTo("Operation is not supported");
185     }
186 
187     @Test
188     public void testGetAllNamespaces() {
189         final UnsupportedOperationException exc =
190                 getExpectedThrowable(UnsupportedOperationException.class,
191                         attributeNode::getAllNamespaces);
192         assertWithMessage("Invalid exception message")
193             .that(exc.getMessage())
194             .isEqualTo("Operation is not supported");
195     }
196 }