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.assertTrue;
25  
26  import java.io.File;
27  import java.util.List;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
33  import com.puppycrawl.tools.checkstyle.JavaParser;
34  import com.puppycrawl.tools.checkstyle.api.DetailAST;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import net.sf.saxon.om.Item;
37  import net.sf.saxon.om.NodeInfo;
38  
39  public class ElementNodeTest extends AbstractPathTestSupport {
40  
41      private static RootNode rootNode;
42  
43      @Override
44      protected String getPackageLocation() {
45          return "com/puppycrawl/tools/checkstyle/xpath/xpathmapper";
46      }
47  
48      @Before
49      public void init() throws Exception {
50          final File file = new File(getPath("InputXpathMapperAst.java"));
51          final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
52          rootNode = new RootNode(rootAst);
53      }
54  
55      @Test
56      public void testGetParent() throws Exception {
57          final String xpath = "//OBJBLOCK";
58          final List<Item> nodes = getXpathItems(xpath, rootNode);
59          assertEquals("Invalid number of nodes", 1, nodes.size());
60          final AbstractNode parent = (AbstractNode) ((NodeInfo) nodes.get(0)).getParent();
61          assertEquals("Invalid token type", TokenTypes.CLASS_DEF, parent.getTokenType());
62      }
63  
64      @Test
65      public void testRootOfElementNode() throws Exception {
66          final String xpath = "//OBJBLOCK";
67          final List<Item> nodes = getXpathItems(xpath, rootNode);
68          assertEquals("Invalid number of nodes", 1, nodes.size());
69          final AbstractNode root = (AbstractNode) ((NodeInfo) nodes.get(0)).getRoot();
70          assertEquals("Invalid token type", TokenTypes.EOF, root.getTokenType());
71          assertTrue("Should return true, because selected node is RootNode",
72                  root instanceof RootNode);
73      }
74  
75  }