View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.internal.utils.XpathUtil.getXpathItems;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.File;
29  import java.util.List;
30  
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
35  import com.puppycrawl.tools.checkstyle.JavaParser;
36  import com.puppycrawl.tools.checkstyle.api.DetailAST;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import net.sf.saxon.om.AxisInfo;
39  import net.sf.saxon.om.Item;
40  import net.sf.saxon.om.NamespaceBinding;
41  import net.sf.saxon.tree.iter.EmptyIterator;
42  
43  public class RootNodeTest extends AbstractPathTestSupport {
44  
45      private static RootNode rootNode;
46  
47      @Override
48      protected String getPackageLocation() {
49          return "com/puppycrawl/tools/checkstyle/xpath/xpathmapper";
50      }
51  
52      @Before
53      public void init() throws Exception {
54          final File file = new File(getPath("InputXpathMapperAst.java"));
55          final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
56          rootNode = new RootNode(rootAst);
57      }
58  
59      @Test
60      public void testXpath() throws Exception {
61          final String xpath = "/";
62          final List<Item> nodes = getXpathItems(xpath, rootNode);
63          assertEquals("Invalid number of nodes", 1, nodes.size());
64          assertTrue("Should return true, because selected node is RootNode",
65                  nodes.get(0) instanceof RootNode);
66          assertEquals("Result node should have same reference as expected",
67                  nodes.get(0),
68                  rootNode);
69      }
70  
71      @Test
72      public void testGetTokenType() {
73          assertEquals("Invalid token type", TokenTypes.EOF, rootNode.getTokenType());
74      }
75  
76      @Test
77      public void testGetLineNumber() {
78          assertEquals("Invalid line number", 1, rootNode.getLineNumber());
79      }
80  
81      @Test
82      public void testGetColumnNumber() {
83          assertEquals("Invalid column number", 0, rootNode.getColumnNumber());
84      }
85  
86      @Test
87      public void testGetLocalPart() {
88          assertEquals("Invalid local part", "ROOT", rootNode.getLocalPart());
89      }
90  
91      @Test
92      public void testGetStringValue() {
93          assertEquals("Invalid string value", "ROOT", rootNode.getStringValue());
94      }
95  
96      @Test
97      public void testIterate() {
98          assertEquals("Result iterator does not match expected",
99                  EmptyIterator.OfNodes.THE_INSTANCE, rootNode.iterateAxis(AxisInfo.PARENT));
100         assertEquals("Result iterator does not match expected",
101                 EmptyIterator.OfNodes.THE_INSTANCE, rootNode.iterateAxis(AxisInfo.PARENT,
102                         null));
103     }
104 
105     @Test
106     public void testIterateWithoutArgument() {
107         try {
108             rootNode.iterate();
109             fail("Exception is excepted");
110         }
111         catch (UnsupportedOperationException ex) {
112             assertEquals("Invalid exception message",
113                     "Operation is not supported",
114                     ex.getMessage());
115         }
116     }
117 
118     @Test
119     public void testGetAttributeValue() {
120         try {
121             rootNode.getAttributeValue("", "");
122             fail("Exception is excepted");
123         }
124         catch (UnsupportedOperationException ex) {
125             assertEquals(
126                 "Invalid exception message",
127                 "Operation is not supported",
128                 ex.getMessage());
129         }
130     }
131 
132     @Test
133     public void testGetDeclaredNamespaces() {
134         try {
135             rootNode.getDeclaredNamespaces(
136                     new NamespaceBinding[] {new NamespaceBinding("prefix", "uri")});
137             fail("Exception is excepted");
138         }
139         catch (UnsupportedOperationException ex) {
140             assertEquals(
141                 "Invalid exception message",
142                 "Operation is not supported",
143                 ex.getMessage());
144         }
145     }
146 
147     @Test
148     public void testGetTreeInfo() {
149         try {
150             rootNode.getTreeInfo();
151             fail("Exception is excepted");
152         }
153         catch (UnsupportedOperationException ex) {
154             assertEquals(
155                 "Invalid exception message",
156                 "Operation is not supported",
157                 ex.getMessage());
158         }
159     }
160 
161     @Test
162     public void testIsId() {
163         try {
164             rootNode.isId();
165             fail("Exception is excepted");
166         }
167         catch (UnsupportedOperationException ex) {
168             assertEquals(
169                 "Invalid exception message",
170                 "Operation is not supported",
171                 ex.getMessage());
172         }
173     }
174 
175     @Test
176     public void testIsIdref() {
177         try {
178             rootNode.isIdref();
179             fail("Exception is excepted");
180         }
181         catch (UnsupportedOperationException ex) {
182             assertEquals(
183                 "Invalid exception message",
184                 "Operation is not supported",
185                 ex.getMessage());
186         }
187     }
188 
189     @Test
190     public void testIsNilled() {
191         try {
192             rootNode.isNilled();
193             fail("Exception is excepted");
194         }
195         catch (UnsupportedOperationException ex) {
196             assertEquals(
197                 "Invalid exception message",
198                 "Operation is not supported",
199                 ex.getMessage());
200         }
201     }
202 
203     @Test
204     public void testIsStreamed() {
205         try {
206             rootNode.isStreamed();
207             fail("Exception is excepted");
208         }
209         catch (UnsupportedOperationException ex) {
210             assertEquals(
211                 "Invalid exception message",
212                 "Operation is not supported",
213                 ex.getMessage());
214         }
215     }
216 
217     @Test
218     public void testGetConfiguration() {
219         try {
220             rootNode.getConfiguration();
221             fail("Exception is excepted");
222         }
223         catch (UnsupportedOperationException ex) {
224             assertEquals(
225                 "Invalid exception message",
226                 "Operation is not supported",
227                 ex.getMessage());
228         }
229     }
230 
231     @Test
232     public void testSetSystemId() {
233         try {
234             rootNode.setSystemId("1");
235             fail("Exception is excepted");
236         }
237         catch (UnsupportedOperationException ex) {
238             assertEquals(
239                 "Invalid exception message",
240                 "Operation is not supported",
241                 ex.getMessage());
242         }
243     }
244 
245     @Test
246     public void testGetSystemId() {
247         try {
248             rootNode.getSystemId();
249             fail("Exception is excepted");
250         }
251         catch (UnsupportedOperationException ex) {
252             assertEquals(
253                 "Invalid exception message",
254                 "Operation is not supported",
255                 ex.getMessage());
256         }
257     }
258 
259     @Test
260     public void testGetPublicId() {
261         try {
262             rootNode.getPublicId();
263             fail("Exception is excepted");
264         }
265         catch (UnsupportedOperationException ex) {
266             assertEquals(
267                 "Invalid exception message",
268                 "Operation is not supported",
269                 ex.getMessage());
270         }
271     }
272 
273     @Test
274     public void testBaseUri() {
275         try {
276             rootNode.getBaseURI();
277             fail("Exception is excepted");
278         }
279         catch (UnsupportedOperationException ex) {
280             assertEquals(
281                 "Invalid exception message",
282                 "Operation is not supported",
283                 ex.getMessage());
284         }
285     }
286 
287     @Test
288     public void testSaveLocation() {
289         try {
290             rootNode.saveLocation();
291             fail("Exception is excepted");
292         }
293         catch (UnsupportedOperationException ex) {
294             assertEquals(
295                 "Invalid exception message",
296                 "Operation is not supported",
297                 ex.getMessage());
298         }
299     }
300 
301     @Test
302     public void testComparePosition() {
303         try {
304             rootNode.comparePosition(null);
305             fail("Exception is excepted");
306         }
307         catch (UnsupportedOperationException ex) {
308             assertEquals(
309                 "Invalid exception message",
310                 "Operation is not supported",
311                 ex.getMessage());
312         }
313     }
314 
315     @Test
316     public void testHead() {
317         try {
318             rootNode.head();
319             fail("Exception is excepted");
320         }
321         catch (UnsupportedOperationException ex) {
322             assertEquals(
323                 "Invalid exception message",
324                 "Operation is not supported",
325                 ex.getMessage());
326         }
327     }
328 
329     @Test
330     public void testGetStringValueCs() {
331         try {
332             rootNode.getStringValueCS();
333             fail("Exception is excepted");
334         }
335         catch (UnsupportedOperationException ex) {
336             assertEquals(
337                 "Invalid exception message",
338                 "Operation is not supported",
339                 ex.getMessage());
340         }
341     }
342 
343     @Test
344     public void testFingerprint() {
345         try {
346             rootNode.getFingerprint();
347             fail("Exception is excepted");
348         }
349         catch (UnsupportedOperationException ex) {
350             assertEquals(
351                 "Invalid exception message",
352                 "Operation is not supported",
353                 ex.getMessage());
354         }
355     }
356 
357     @Test
358     public void testGetDisplayName() {
359         try {
360             rootNode.getDisplayName();
361             fail("Exception is excepted");
362         }
363         catch (UnsupportedOperationException ex) {
364             assertEquals(
365                 "Invalid exception message",
366                 "Operation is not supported",
367                 ex.getMessage());
368         }
369     }
370 
371     @Test
372     public void testGetPrefix() {
373         try {
374             rootNode.getPrefix();
375             fail("Exception is excepted");
376         }
377         catch (UnsupportedOperationException ex) {
378             assertEquals(
379                 "Invalid exception message",
380                 "Operation is not supported",
381                 ex.getMessage());
382         }
383     }
384 
385     @Test
386     public void testGetSchemaType() {
387         try {
388             rootNode.getSchemaType();
389             fail("Exception is excepted");
390         }
391         catch (UnsupportedOperationException ex) {
392             assertEquals(
393                 "Invalid exception message",
394                 "Operation is not supported",
395                 ex.getMessage());
396         }
397     }
398 
399     @Test
400     public void testAtomize() {
401         try {
402             rootNode.atomize();
403             fail("Exception is excepted");
404         }
405         catch (UnsupportedOperationException ex) {
406             assertEquals(
407                 "Invalid exception message",
408                 "Operation is not supported",
409                 ex.getMessage());
410         }
411     }
412 
413     @Test
414     public void testGenerateId() {
415         try {
416             rootNode.generateId(null);
417             fail("Exception is excepted");
418         }
419         catch (UnsupportedOperationException ex) {
420             assertEquals(
421                 "Invalid exception message",
422                 "Operation is not supported",
423                 ex.getMessage());
424         }
425     }
426 
427     @Test
428     public void testCopy() {
429         try {
430             rootNode.copy(null, -1, null);
431             fail("Exception is excepted");
432         }
433         catch (UnsupportedOperationException ex) {
434             assertEquals(
435                 "Invalid exception message",
436                 "Operation is not supported",
437                 ex.getMessage());
438         }
439     }
440 
441     @Test
442     public void testSameNodeInfo() {
443         assertTrue("Should return true, because object is being compared to itself",
444                 rootNode.isSameNodeInfo(rootNode));
445         assertFalse("Should return false, because object does not equal null",
446                 rootNode.isSameNodeInfo(null));
447     }
448 
449 }