1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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.api;
21  
22  import java.io.File;
23  import java.nio.charset.StandardCharsets;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.JavaParser;
30  
31  public class FullIdentTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/api/fullident/";
36      }
37  
38      @Test
39      public void testToString() {
40          final DetailAST ast = new DetailAST();
41          ast.setType(TokenTypes.LITERAL_NEW);
42          ast.setColumnNo(14);
43          ast.setLineNo(15);
44          ast.setText("MyTest");
45  
46          final FullIdent indent = FullIdent.createFullIdent(ast);
47          Assert.assertEquals("Invalid full indent", "MyTest[15x14]", indent.toString());
48          Assert.assertEquals("Invalid text", "MyTest", indent.getText());
49          Assert.assertEquals("Invalid line", 15, indent.getLineNo());
50          Assert.assertEquals("Invalid column", 14, indent.getColumnNo());
51      }
52  
53      @Test
54      public void testCreateFullIdentBelow() {
55          final DetailAST ast = new DetailAST();
56  
57          final FullIdent indent = FullIdent.createFullIdentBelow(ast);
58          Assert.assertEquals("Invalid full indent", "", indent.getText());
59      }
60  
61      @Test
62      public void testGetDetailAst() throws Exception {
63          final FileText testFileText = new FileText(
64                  new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(),
65                  System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
66          final DetailAST packageDefinitionNode = JavaParser.parse(new FileContents(testFileText));
67          final DetailAST packageName = packageDefinitionNode.getFirstChild().getNextSibling();
68          final FullIdent ident = FullIdent.createFullIdent(packageName);
69          Assert.assertEquals("Invalid full indent", "com[1x8]", ident.getDetailAst().toString());
70      }
71  
72      @Test
73      public void testNonValidCoordinatesWithNegative() {
74          final FullIdent fullIdent = prepareFullIdentWithCoordinates(14, 15);
75          Assert.assertEquals("Invalid full indent", "MyTest.MyTestik[15x14]", fullIdent.toString());
76      }
77  
78      @Test
79      public void testNonValidCoordinatesWithZero() {
80          final FullIdent fullIdent = prepareFullIdentWithCoordinates(0, 0);
81          Assert.assertEquals("Invalid full indent", "MyTest.MyTestik[15x14]", fullIdent.toString());
82      }
83  
84      @Test
85      public void testWithArrayCreateFullIdentWithArrayDeclare() throws Exception {
86          final FileText testFileText = new FileText(
87                  new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(),
88                  System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
89          final DetailAST packageDefinitionNode = JavaParser.parse(new FileContents(testFileText));
90          final DetailAST arrayDeclarator = packageDefinitionNode.getNextSibling()
91                  .findFirstToken(TokenTypes.OBJBLOCK)
92                  .findFirstToken(TokenTypes.VARIABLE_DEF)
93                  .findFirstToken(TokenTypes.TYPE)
94                  .getFirstChild();
95          final FullIdent ident = FullIdent.createFullIdent(arrayDeclarator);
96          Assert.assertEquals("Invalid full indent", "int[][][5x12]", ident.toString());
97      }
98  
99      private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) {
100         final DetailAST ast = new DetailAST();
101         ast.setType(TokenTypes.DOT);
102         ast.setColumnNo(1);
103         ast.setLineNo(2);
104         ast.setText("Root");
105 
106         final DetailAST ast2 = new DetailAST();
107         ast2.setType(TokenTypes.LE);
108         ast2.setColumnNo(columnNo);
109         ast2.setLineNo(lineNo);
110         ast2.setText("MyTestik");
111 
112         final DetailAST ast1 = new DetailAST();
113         ast1.setType(TokenTypes.LITERAL_NEW);
114         ast1.setColumnNo(14);
115         ast1.setLineNo(15);
116         ast1.setText("MyTest");
117 
118         ast.addChild(ast1);
119         ast.addChild(ast2);
120 
121         return FullIdent.createFullIdent(ast);
122     }
123 
124 }