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.google.checkstyle.test.chapter3filestructure.rule333orderingandspacing;
21  
22  import org.junit.Test;
23  
24  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
28  
29  public class CustomImportOrderTest extends AbstractModuleTestSupport {
30  
31      /** Shortcuts to make code more compact. */
32      private static final String MSG_LINE_SEPARATOR = CustomImportOrderCheck.MSG_LINE_SEPARATOR;
33      private static final String MSG_LEX = CustomImportOrderCheck.MSG_LEX;
34      private static final String MSG_NONGROUP_EXPECTED =
35          CustomImportOrderCheck.MSG_NONGROUP_EXPECTED;
36  
37      private static final String STATIC = CustomImportOrderCheck.STATIC_RULE_GROUP;
38  
39      private final Class<CustomImportOrderCheck> clazz = CustomImportOrderCheck.class;
40  
41      @Override
42      protected String getPackageLocation() {
43          return "com/google/checkstyle/test/chapter3filestructure/rule333orderingandspacing";
44      }
45  
46      @Test
47      public void testCustomImport1() throws Exception {
48          final String[] expected = {
49              "4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
50                  "java.io.File.createTempFile"),
51              "6: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Button"),
52              "8: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
53              "12: " + getCheckMessage(clazz, MSG_LEX, "java.io.File", "javax.swing.JTable"),
54              "13: " + getCheckMessage(clazz, MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
55              "14: " + getCheckMessage(clazz, MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
56              "15: " + getCheckMessage(clazz, MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
57              "17: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.base.Ascii",
58                  "javax.swing.JTable"),
59          };
60  
61          final Configuration checkConfig = getModuleConfig("CustomImportOrder");
62          final String filePath = getPath("InputCustomImportOrder1.java");
63  
64          final Integer[] warnList = getLinesWithWarn(filePath);
65          verify(checkConfig, filePath, expected, warnList);
66      }
67  
68      @Test
69      public void testCustomImport2() throws Exception {
70          final String[] expected = {
71              "4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
72                  "java.io.File.createTempFile"),
73              "9: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
74              "11: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
75                  "java.util.concurrent.AbstractExecutorService"),
76              "13: " + getCheckMessage(clazz, MSG_LEX,
77                  "com.google.checkstyle.test.chapter2filebasic.rule21filename.*",
78                  "java.util.concurrent.AbstractExecutorService"),
79              "14: " + getCheckMessage(clazz, MSG_LEX,
80                  "com.google.checkstyle.test.chapter3filestructure.rule3sourcefile.*",
81                  "java.util.concurrent.AbstractExecutorService"),
82              "16: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
83                  "java.util.concurrent.AbstractExecutorService"),
84          };
85  
86          final Configuration checkConfig = getModuleConfig("CustomImportOrder");
87          final String filePath = getPath("InputCustomImportOrder2.java");
88  
89          final Integer[] warnList = getLinesWithWarn(filePath);
90          verify(checkConfig, filePath, expected, warnList);
91      }
92  
93      @Test
94      public void testCustomImport3() throws Exception {
95          final String[] expected = {
96              "4: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Dialog"),
97              "5: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
98                  "javax.swing.WindowConstants.*"),
99              "7: " + getCheckMessage(clazz, MSG_LEX,
100                 "com.google.checkstyle.test.chapter2filebasic.rule21filename.*", "java.awt.Dialog"),
101             "8: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
102                 "java.awt.Dialog"),
103             "9: " + getCheckMessage(clazz, MSG_LEX,
104                 "com.google.checkstyle.test.chapter3filestructure.rule3sourcefile.*",
105                 "java.awt.Dialog"),
106             "11: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
107                 "java.io.File.createTempFile"),
108             "13: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
109             "15: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
110                 "java.util.concurrent.AbstractExecutorService"),
111         };
112 
113         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
114         final String filePath = getPath("InputCustomImportOrder3.java");
115 
116         final Integer[] warnList = getLinesWithWarn(filePath);
117         verify(checkConfig, filePath, expected, warnList);
118     }
119 
120     @Test
121     public void testValid() throws Exception {
122         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
123 
124         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
125         final String filePath = getPath("InputCustomImportOrderValid.java");
126 
127         final Integer[] warnList = getLinesWithWarn(filePath);
128         verify(checkConfig, filePath, expected, warnList);
129     }
130 
131     @Test
132     public void testValidGoogleStyleOrderOfImports() throws Exception {
133         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
134 
135         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
136         final String filePath = getPath("InputCustomImportOrderNoImports.java");
137 
138         final Integer[] warnList = getLinesWithWarn(filePath);
139         verify(checkConfig, filePath, expected, warnList);
140     }
141 
142 }