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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
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          try {
58              attributeNode.compareOrder(null);
59              assertWithMessage("Exception is excepted").fail();
60          }
61          catch (UnsupportedOperationException ex) {
62              assertWithMessage("Invalid exception message")
63                  .that(ex.getMessage())
64                  .isEqualTo("Operation is not supported");
65          }
66      }
67  
68      @Test
69      public void testGetDepth() {
70          final UnsupportedOperationException exception =
71              assertThrows(UnsupportedOperationException.class, attributeNode::getDepth);
72          assertWithMessage("Invalid exception message")
73              .that(exception)
74              .hasMessageThat()
75                  .isEqualTo("Operation is not supported");
76      }
77  
78      @Test
79      public void testHasChildNodes() {
80          assertWithMessage("Attribute node shouldn't have children")
81              .that(attributeNode.hasChildNodes())
82              .isFalse();
83      }
84  
85      @Test
86      public void testGetAttributeValue() {
87          try {
88              attributeNode.getAttributeValue("", "");
89              assertWithMessage("Exception is excepted").fail();
90          }
91          catch (UnsupportedOperationException ex) {
92              assertWithMessage("Invalid exception message")
93                  .that(ex.getMessage())
94                  .isEqualTo("Operation is not supported");
95          }
96      }
97  
98      @Test
99      public void testGetChildren() {
100         final UnsupportedOperationException exception =
101             assertThrows(UnsupportedOperationException.class, attributeNode::getChildren);
102         assertWithMessage("Invalid exception message")
103             .that(exception)
104             .hasMessageThat()
105             .isEqualTo("Operation is not supported");
106     }
107 
108     @Test
109     public void testGetParent() {
110         try {
111             attributeNode.getParent();
112             assertWithMessage("Exception is excepted").fail();
113         }
114         catch (UnsupportedOperationException ex) {
115             assertWithMessage("Invalid exception message")
116                 .that(ex.getMessage())
117                 .isEqualTo("Operation is not supported");
118         }
119     }
120 
121     @Test
122     public void testGetRoot() {
123         try {
124             attributeNode.getRoot();
125             assertWithMessage("Exception is excepted").fail();
126         }
127         catch (UnsupportedOperationException ex) {
128             assertWithMessage("Invalid exception message")
129                 .that(ex.getMessage())
130                 .isEqualTo("Operation is not supported");
131         }
132     }
133 
134     @Test
135     public void testGetStringValue() {
136         assertWithMessage("Invalid string value")
137             .that(attributeNode.getStringValue())
138             .isEqualTo("value");
139     }
140 
141     @Test
142     public void testIterate() {
143         try (AxisIterator ignored = attributeNode.iterateAxis(AxisInfo.SELF)) {
144             assertWithMessage("Exception is excepted").fail();
145         }
146         catch (UnsupportedOperationException ex) {
147             assertWithMessage("Invalid exception message")
148                 .that(ex.getMessage())
149                 .isEqualTo("Operation is not supported");
150         }
151     }
152 
153     @Test
154     public void testGetLineNumber() {
155         try {
156             attributeNode.getLineNumber();
157             assertWithMessage("Exception is excepted").fail();
158         }
159         catch (UnsupportedOperationException ex) {
160             assertWithMessage("Invalid exception message")
161                 .that(ex.getMessage())
162                 .isEqualTo("Operation is not supported");
163         }
164     }
165 
166     @Test
167     public void testGetColumnNumber() {
168         try {
169             attributeNode.getColumnNumber();
170             assertWithMessage("Exception is excepted").fail();
171         }
172         catch (UnsupportedOperationException ex) {
173             assertWithMessage("Invalid exception message")
174                 .that(ex.getMessage())
175                 .isEqualTo("Operation is not supported");
176         }
177     }
178 
179     @Test
180     public void testGetTokenType() {
181         try {
182             attributeNode.getTokenType();
183             assertWithMessage("Exception is excepted").fail();
184         }
185         catch (UnsupportedOperationException ex) {
186             assertWithMessage("Invalid exception message")
187                 .that(ex.getMessage())
188                 .isEqualTo("Operation is not supported");
189         }
190     }
191 
192     @Test
193     public void testGetUnderlyingNode() {
194         try {
195             attributeNode.getUnderlyingNode();
196             assertWithMessage("Exception is excepted").fail();
197         }
198         catch (UnsupportedOperationException ex) {
199             assertWithMessage("Invalid exception message")
200                 .that(ex.getMessage())
201                 .isEqualTo("Operation is not supported");
202         }
203     }
204 
205     @Test
206     public void testGetAllNamespaces() {
207         try {
208             attributeNode.getAllNamespaces();
209             assertWithMessage("Exception is excepted").fail();
210         }
211         catch (UnsupportedOperationException ex) {
212             assertWithMessage("Invalid exception message")
213                 .that(ex.getMessage())
214                 .isEqualTo("Operation is not supported");
215         }
216     }
217 }