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