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.checks.imports;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_DUPLICATE;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_LANG;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_SAME;
25  import static org.junit.Assert.assertArrayEquals;
26  
27  import java.io.File;
28  import java.util.Arrays;
29  import java.util.List;
30  
31  import org.junit.Test;
32  
33  import com.google.common.collect.ImmutableMap;
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
38  
39  public class RedundantImportCheckTest
40      extends AbstractModuleTestSupport {
41  
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/imports/redundantimport";
45      }
46  
47      @Test
48      public void testGetRequiredTokens() {
49          final RedundantImportCheck checkObj = new RedundantImportCheck();
50          final int[] expected = {
51              TokenTypes.IMPORT,
52              TokenTypes.STATIC_IMPORT,
53              TokenTypes.PACKAGE_DEF,
54          };
55          assertArrayEquals("Default required tokens are invalid",
56              expected, checkObj.getRequiredTokens());
57      }
58  
59      @Test
60      public void testStateIsClearedOnBeginTree1()
61              throws Exception {
62          final DefaultConfiguration checkConfig = createModuleConfig(RedundantImportCheck.class);
63          final String inputWithWarnings = getPath("InputRedundantImportCheckClearState.java");
64          final String inputWithoutWarnings = getPath("InputRedundantImportWithoutWarnings.java");
65          final List<String> expectedFirstInput = Arrays.asList(
66              "4:1: " + getCheckMessage(MSG_DUPLICATE, 3, "java.util.Arrays.asList"),
67              "7:1: " + getCheckMessage(MSG_DUPLICATE, 6, "java.util.List")
68          );
69          final List<String> expectedSecondInput = Arrays.asList(CommonUtils.EMPTY_STRING_ARRAY);
70          final File[] inputs = {new File(inputWithWarnings), new File(inputWithoutWarnings)};
71  
72          verify(createChecker(checkConfig), inputs, ImmutableMap.of(
73              inputWithWarnings, expectedFirstInput,
74              inputWithoutWarnings, expectedSecondInput));
75      }
76  
77      @Test
78      public void testWithChecker()
79              throws Exception {
80          final DefaultConfiguration checkConfig =
81              createModuleConfig(RedundantImportCheck.class);
82          final String[] expected = {
83              "7:1: " + getCheckMessage(MSG_SAME,
84                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*"),
85              "8:1: " + getCheckMessage(MSG_SAME,
86                  "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport."
87                          + "InputRedundantImportBug"),
88              "10:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
89              "11:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
90              "14:1: " + getCheckMessage(MSG_DUPLICATE, 13, "java.util.List"),
91              "26:1: " + getCheckMessage(MSG_DUPLICATE, 25, "javax.swing.WindowConstants.*"),
92          };
93          verify(checkConfig, getPath("InputRedundantImportWithChecker.java"), expected);
94      }
95  
96      @Test
97      public void testUnnamedPackage()
98              throws Exception {
99          final DefaultConfiguration checkConfig =
100             createModuleConfig(RedundantImportCheck.class);
101         final String[] expected = {
102             "4:1: " + getCheckMessage(MSG_DUPLICATE, 3, "java.util.List"),
103             "6:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
104         };
105         verify(checkConfig, getNonCompilablePath("InputRedundantImport_UnnamedPackage.java"),
106             expected);
107     }
108 
109     @Test
110     public void testGetAcceptableTokens() {
111         final RedundantImportCheck testCheckObject =
112                 new RedundantImportCheck();
113         final int[] actual = testCheckObject.getAcceptableTokens();
114         final int[] expected = {
115             TokenTypes.IMPORT,
116             TokenTypes.STATIC_IMPORT,
117             TokenTypes.PACKAGE_DEF,
118         };
119 
120         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
121     }
122 
123 }