View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.nio.charset.StandardCharsets;
27  import java.util.Arrays;
28  import java.util.Collections;
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.api.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.FileText;
37  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
38  
39  public class XpathQueryGeneratorTest extends AbstractPathTestSupport {
40  
41      private static final int DEFAULT_TAB_WIDTH = 4;
42  
43      private static DetailAST rootAst;
44  
45      private static FileText fileText;
46  
47      @Override
48      protected String getPackageLocation() {
49          return "com/puppycrawl/tools/checkstyle/xpath/xpathquerygenerator";
50      }
51  
52      @Before
53      public void init() throws Exception {
54          final File file = new File(getPath("InputXpathQueryGenerator.java"));
55          fileText = new FileText(file,
56                  StandardCharsets.UTF_8.name());
57          rootAst = TestUtil.parseFile(file);
58      }
59  
60      @Test
61      public void testClassDef() {
62          final int lineNumber = 12;
63          final int columnNumber = 1;
64          final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
65                  columnNumber, fileText, DEFAULT_TAB_WIDTH);
66          final List<String> actual = queryGenerator.generate();
67          final List<String> expected = Arrays.asList(
68              "/CLASS_DEF[@text='InputXpathQueryGenerator']",
69              "/CLASS_DEF[@text='InputXpathQueryGenerator']/MODIFIERS",
70              "/CLASS_DEF[@text='InputXpathQueryGenerator']/MODIFIERS/LITERAL_PUBLIC");
71          assertEquals("Generated queries do not match expected ones", expected, actual);
72      }
73  
74      @Test
75      public void testMethodDef() {
76          final int lineNumber = 45;
77          final int columnNumber = 5;
78          final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
79                  columnNumber, fileText, DEFAULT_TAB_WIDTH);
80          final List<String> actual = queryGenerator.generate();
81          final List<String> expected = Arrays.asList(
82              "/CLASS_DEF[@text='InputXpathQueryGenerator']"
83                      + "/OBJBLOCK/METHOD_DEF[@text='callSomeMethod']",
84              "/CLASS_DEF[@text='InputXpathQueryGenerator']"
85                      + "/OBJBLOCK/METHOD_DEF[@text='callSomeMethod']/MODIFIERS",
86              "/CLASS_DEF[@text='InputXpathQueryGenerator']"
87                      + "/OBJBLOCK/METHOD_DEF[@text='callSomeMethod']/MODIFIERS/LITERAL_PUBLIC");
88          assertEquals("Generated queries do not match expected ones", expected, actual);
89      }
90  
91      @Test
92      public void testVariableDef() {
93          final int lineNumber = 53;
94          final int columnNumber = 13;
95          final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
96                  columnNumber, fileText, DEFAULT_TAB_WIDTH);
97          final List<String> actual = queryGenerator.generate();
98          final List<String> expected = Arrays.asList(
99              "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
100                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/LITERAL_FOR"
101                     + "/SLIST/VARIABLE_DEF[@text='d']",
102             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
103                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/LITERAL_FOR"
104                     + "/SLIST/VARIABLE_DEF[@text='d']/MODIFIERS",
105             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
106                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/LITERAL_FOR"
107                     + "/SLIST/VARIABLE_DEF[@text='d']/TYPE",
108             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
109                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/LITERAL_FOR"
110                     + "/SLIST/VARIABLE_DEF[@text='d']/TYPE/LITERAL_SHORT");
111         assertEquals("Generated queries do not match expected ones", expected, actual);
112     }
113 
114     @Test
115     public void testLcurly() {
116         final int lineNumber = 37;
117         final int columnNumber = 20;
118         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
119                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
120         final List<String> actual = queryGenerator.generate();
121         final List<String> expected = Collections.singletonList(
122             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK/METHOD_DEF[@text='Label']"
123                     + "/SLIST/LITERAL_SWITCH/LCURLY");
124         assertEquals("Generated queries do not match expected ones", expected, actual);
125     }
126 
127     @Test
128     public void testRcurly() {
129         final int lineNumber = 25;
130         final int columnNumber = 5;
131         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
132                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
133         final List<String> actual = queryGenerator.generate();
134         final List<String> expected = Collections.singletonList(
135             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK/INSTANCE_INIT"
136                     + "/SLIST/RCURLY");
137         assertEquals("Generated queries do not match expected ones", expected, actual);
138     }
139 
140     @Test
141     public void testExpr() {
142         final int lineNumber = 17;
143         final int columnNumber = 50;
144         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
145                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
146         final List<String> actual = queryGenerator.generate();
147         final List<String> expected = Arrays.asList(
148             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
149                     + "/VARIABLE_DEF[@text='mUse4']/ASSIGN/EXPR",
150             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
151                     + "/VARIABLE_DEF[@text='mUse4']/ASSIGN/EXPR/DOT");
152         assertEquals("Generated queries do not match expected ones", expected, actual);
153     }
154 
155     @Test
156     public void testLparen() {
157         final int lineNumber = 45;
158         final int columnNumber = 31;
159         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
160                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
161         final List<String> actual = queryGenerator.generate();
162         final List<String> expected = Collections.singletonList(
163             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
164                     + "/METHOD_DEF[@text='callSomeMethod']/LPAREN");
165         assertEquals("Generated queries do not match expected ones", expected, actual);
166     }
167 
168     @Test
169     public void testEmpty() {
170         final int lineNumber = 300;
171         final int columnNumber = 300;
172         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
173                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
174         final List<String> actual = queryGenerator.generate();
175         assertTrue("Result should be empty", actual.isEmpty());
176     }
177 
178     @Test
179     public void testPackage() {
180         final int lineNumber = 2;
181         final int columnNumber = 1;
182         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
183                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
184         final List<String> actual = queryGenerator.generate();
185         final List<String> expected = Collections.singletonList(
186             "/PACKAGE_DEF");
187         assertEquals("Generated queries do not match expected ones", expected, actual);
188     }
189 
190     @Test
191     public void testImport() {
192         final int lineNumber = 5;
193         final int columnNumber = 1;
194         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
195                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
196         final List<String> actual = queryGenerator.generate();
197         final List<String> expected = Collections.singletonList(
198             "/IMPORT[./DOT[@text='File']]");
199         assertEquals("Generated queries do not match expected ones", expected, actual);
200     }
201 
202     @Test
203     public void testMethodParams() {
204         final int lineNumber = 72;
205         final int columnNumber = 30;
206         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
207                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
208         final List<String> actual = queryGenerator.generate();
209         final List<String> expected = Arrays.asList(
210             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
211                     + "/METHOD_DEF[@text='saveUser']/PARAMETERS",
212             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
213                     + "/METHOD_DEF[@text='saveUser']/PARAMETERS/PARAMETER_DEF[@text='name']",
214             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
215                     + "/METHOD_DEF[@text='saveUser']/PARAMETERS/PARAMETER_DEF[@text='name']"
216                     + "/MODIFIERS",
217             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
218                     + "/METHOD_DEF[@text='saveUser']/PARAMETERS/PARAMETER_DEF[@text='name']"
219                     + "/TYPE[@text='String']");
220         assertEquals("Generated queries do not match expected ones", expected, actual);
221     }
222 
223     @Test
224     public void testSwitch() {
225         final int lineNumber = 37;
226         final int columnNumber = 9;
227         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
228                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
229         final List<String> actual = queryGenerator.generate();
230         final List<String> expected = Collections.singletonList(
231             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
232                     + "/METHOD_DEF[@text='Label']/SLIST/LITERAL_SWITCH");
233         assertEquals("Generated queries do not match expected ones", expected, actual);
234     }
235 
236     @Test
237     public void testSwitchCase() {
238         final int lineNumber = 38;
239         final int columnNumber = 13;
240         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
241                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
242         final List<String> actual = queryGenerator.generate();
243         final List<String> expected = Arrays.asList(
244             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK/METHOD_DEF[@text='Label']"
245                     + "/SLIST/LITERAL_SWITCH/CASE_GROUP",
246             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK/METHOD_DEF[@text='Label']"
247                     + "/SLIST/LITERAL_SWITCH/CASE_GROUP/LITERAL_DEFAULT");
248         assertEquals("Generated queries do not match expected ones", expected, actual);
249     }
250 
251     @Test
252     public void testVariableStringLiteral() {
253         final int lineNumber = 47;
254         final int columnNumber = 26;
255         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
256                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
257         final List<String> actual = queryGenerator.generate();
258         final List<String> expected = Arrays.asList(
259             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
260                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/VARIABLE_DEF[@text='another']"
261                     + "/ASSIGN/EXPR",
262             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
263                     + "/METHOD_DEF[@text='callSomeMethod']/SLIST/VARIABLE_DEF[@text='another']"
264                     + "/ASSIGN/EXPR/STRING_LITERAL");
265         assertEquals("Generated queries do not match expected ones", expected, actual);
266     }
267 
268     @Test
269     public void testComma() {
270         final int lineNumber = 66;
271         final int columnNumber = 36;
272         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
273                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
274         final List<String> actual = queryGenerator.generate();
275         final List<String> expected = Collections.singletonList(
276             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK/METHOD_DEF[@text='foo']"
277                     + "/SLIST/LITERAL_FOR/FOR_ITERATOR/ELIST/COMMA");
278         assertEquals("Generated queries do not match expected ones", expected, actual);
279     }
280 
281     @Test
282     public void testLiteralVoid() {
283         final int lineNumber = 65;
284         final int columnNumber = 12;
285         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
286                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
287         final List<String> actual = queryGenerator.generate();
288         final List<String> expected = Arrays.asList(
289             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
290                     + "/METHOD_DEF[@text='foo']/TYPE",
291             "/CLASS_DEF[@text='InputXpathQueryGenerator']/OBJBLOCK"
292                     + "/METHOD_DEF[@text='foo']/TYPE/LITERAL_VOID");
293         assertEquals("Generated queries do not match expected ones", expected, actual);
294     }
295 
296     @Test
297     public void testFirstImport() {
298         final int lineNumber = 4;
299         final int columnNumber = 1;
300         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
301                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
302         final List<String> actual = queryGenerator.generate();
303         final List<String> expected = Collections.singletonList(
304                 "/IMPORT[./DOT[@text='JToolBar']]");
305         assertEquals("Generated queries do not match expected ones", expected, actual);
306     }
307 
308     @Test
309     public void testLastImport() {
310         final int lineNumber = 8;
311         final int columnNumber = 1;
312         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, lineNumber,
313                 columnNumber, fileText, DEFAULT_TAB_WIDTH);
314         final List<String> actual = queryGenerator.generate();
315         final List<String> expected = Collections.singletonList(
316                 "/IMPORT[./DOT[@text='Iterator']]");
317         assertEquals("Generated queries do not match expected ones", expected, actual);
318     }
319 
320     @Test
321     public void testTabWidthBeforeMethodDef() throws Exception {
322         final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
323         final FileText testFileText = new FileText(testFile,
324                 StandardCharsets.UTF_8.name());
325         final DetailAST detailAst = TestUtil.parseFile(testFile);
326         final int lineNumber = 4;
327         final int columnNumber = 13;
328         final int tabWidth = 4;
329         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber,
330                 columnNumber, testFileText, tabWidth);
331         final List<String> actual = queryGenerator.generate();
332         final List<String> expected = Arrays.asList(
333                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
334                         + "/METHOD_DEF[@text='toString']",
335                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
336                         + "/METHOD_DEF[@text='toString']/MODIFIERS",
337                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
338                         + "/METHOD_DEF[@text='toString']/MODIFIERS/LITERAL_PUBLIC");
339         assertEquals("Generated queries do not match expected ones", expected, actual);
340     }
341 
342     @Test
343     public void testTabWidthAfterVoidLiteral() throws Exception {
344         final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
345         final FileText testFileText = new FileText(testFile,
346                 StandardCharsets.UTF_8.name());
347         final DetailAST detailAst = TestUtil.parseFile(testFile);
348         final int lineNumber = 8;
349         final int columnNumber = 41;
350         final int tabWidth = 8;
351         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber,
352                 columnNumber, testFileText, tabWidth);
353         final List<String> actual = queryGenerator.generate();
354         final List<String> expected = Arrays.asList(
355                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
356                         + "/METHOD_DEF[@text='getName']/TYPE",
357                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
358                         + "/METHOD_DEF[@text='getName']/TYPE/LITERAL_VOID");
359         assertEquals("Generated queries do not match expected ones", expected, actual);
360     }
361 
362     @Test
363     public void testTabWidthBeforeSlist() throws Exception {
364         final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
365         final FileText testFileText = new FileText(testFile,
366                 StandardCharsets.UTF_8.name());
367         final DetailAST detailAst = TestUtil.parseFile(testFile);
368         final int lineNumber = 12;
369         final int columnNumber = 57;
370         final int tabWidth = 8;
371         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber,
372                 columnNumber, testFileText, tabWidth);
373         final List<String> actual = queryGenerator.generate();
374         final List<String> expected = Collections.singletonList(
375                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
376                         + "/METHOD_DEF[@text='tabAfterMe']/SLIST");
377         assertEquals("Generated queries do not match expected ones", expected, actual);
378     }
379 
380     @Test
381     public void testTabWidthEndOfLine() throws Exception {
382         final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
383         final FileText testFileText = new FileText(testFile,
384                 StandardCharsets.UTF_8.name());
385         final DetailAST detailAst = TestUtil.parseFile(testFile);
386         final int lineNumber = 16;
387         final int columnNumber = 58;
388         final int tabWidth = 8;
389         final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber,
390                 columnNumber, testFileText, tabWidth);
391         final List<String> actual = queryGenerator.generate();
392         final List<String> expected = Collections.singletonList(
393                 "/CLASS_DEF[@text='InputXpathQueryGeneratorTabWidth']/OBJBLOCK"
394                         + "/VARIABLE_DEF[@text='endLineTab']/SEMI");
395         assertEquals("Generated queries do not match expected ones", expected, actual);
396     }
397 }