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 org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck;
30  
31  public class XpathRegressionRedundantImportTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = RedundantImportCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testInternal() throws Exception {
42          final File fileToProcess =
43                  new File(getPath("InputXpathRedundantImportInternal.java"));
44          final DefaultConfiguration moduleConfig =
45                  createModuleConfig(RedundantImportCheck.class);
46          final String[] expectedViolation = {
47              "3:1: " + getCheckMessage(RedundantImportCheck.class,
48                          RedundantImportCheck.MSG_SAME, "org.checkstyle.suppressionxpathfilter"
49                                  + ".redundantimport.InputXpathRedundantImportInternal"),
50          };
51          final List<String> expectedXpathQueries = Collections.singletonList(
52                  "/COMPILATION_UNIT/IMPORT");
53  
54          runVerifications(moduleConfig, fileToProcess, expectedViolation,
55                  expectedXpathQueries);
56      }
57  
58      @Test
59      public void testLibrary() throws Exception {
60          final File fileToProcess =
61                  new File(getPath("InputXpathRedundantImportLibrary.java"));
62          final DefaultConfiguration moduleConfig =
63                  createModuleConfig(RedundantImportCheck.class);
64          final String[] expectedViolation = {
65              "3:1: " + getCheckMessage(RedundantImportCheck.class,
66                          RedundantImportCheck.MSG_LANG, "java.lang.String"),
67          };
68          final List<String> expectedXpathQueries = Collections.singletonList(
69                  "/COMPILATION_UNIT/IMPORT");
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testDuplicate() throws Exception {
77          final File fileToProcess =
78                  new File(getPath("InputXpathRedundantImportDuplicate.java"));
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(RedundantImportCheck.class);
81          final String[] expectedViolation = {
82              "4:1: " + getCheckMessage(RedundantImportCheck.class,
83                          RedundantImportCheck.MSG_DUPLICATE, 3, "java.util.Scanner"),
84          };
85          final List<String> expectedXpathQueries = Collections.singletonList(
86                  "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='Scanner']]");
87  
88          runVerifications(moduleConfig, fileToProcess, expectedViolation,
89                  expectedXpathQueries);
90      }
91  
92  }