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.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  
49          final String[] expected = {
50              "4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
51                  "java.io.File.createTempFile"),
52              "6: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Button"),
53              "8: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
54              "12: " + getCheckMessage(clazz, MSG_LEX, "java.io.File", "javax.swing.JTable"),
55              "13: " + getCheckMessage(clazz, MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
56              "14: " + getCheckMessage(clazz, MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
57              "15: " + getCheckMessage(clazz, MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
58              "17: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.base.Ascii",
59                  "javax.swing.JTable"),
60          };
61  
62          final Configuration checkConfig = getModuleConfig("CustomImportOrder");
63          final String filePath = getPath("InputCustomImportOrder1.java");
64  
65          final Integer[] warnList = getLinesWithWarn(filePath);
66          verify(checkConfig, filePath, expected, warnList);
67      }
68  
69      @Test
70      public void testCustomImport2() throws Exception {
71  
72          final String[] expected = {
73              "4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
74                  "java.io.File.createTempFile"),
75              "9: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
76              "11: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
77                  "java.util.concurrent.AbstractExecutorService"),
78              "13: " + getCheckMessage(clazz, MSG_LEX,
79                  "com.google.checkstyle.test.chapter2filebasic.rule21filename.*",
80                  "java.util.concurrent.AbstractExecutorService"),
81              "14: " + getCheckMessage(clazz, MSG_LEX, "com.sun.xml.internal.xsom.impl.scd.Iterators",
82                  "java.util.concurrent.AbstractExecutorService"),
83              "16: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
84                  "java.util.concurrent.AbstractExecutorService"),
85          };
86  
87          final Configuration checkConfig = getModuleConfig("CustomImportOrder");
88          final String filePath = getPath("InputCustomImportOrder2.java");
89  
90          final Integer[] warnList = getLinesWithWarn(filePath);
91          verify(checkConfig, filePath, expected, warnList);
92      }
93  
94      @Test
95      public void testCustomImport3() throws Exception {
96  
97          final String[] expected = {
98              "4: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Dialog"),
99              "5: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
100                 "javax.swing.WindowConstants.*"),
101             "7: " + getCheckMessage(clazz, MSG_LEX,
102                 "com.google.checkstyle.test.chapter2filebasic.rule21filename.*", "java.awt.Dialog"),
103             "8: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
104                 "java.awt.Dialog"),
105             "9: " + getCheckMessage(clazz, MSG_LEX, "com.sun.xml.internal.xsom.impl.scd.Iterators",
106                 "java.awt.Dialog"),
107             "11: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
108                 "java.io.File.createTempFile"),
109             "13: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
110             "15: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
111                 "java.util.concurrent.AbstractExecutorService"),
112         };
113 
114         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
115         final String filePath = getPath("InputCustomImportOrder3.java");
116 
117         final Integer[] warnList = getLinesWithWarn(filePath);
118         verify(checkConfig, filePath, expected, warnList);
119     }
120 
121     @Test
122     public void testValid() throws Exception {
123 
124         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
125 
126         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
127         final String filePath = getPath("InputCustomImportOrderValid.java");
128 
129         final Integer[] warnList = getLinesWithWarn(filePath);
130         verify(checkConfig, filePath, expected, warnList);
131     }
132 
133     @Test
134     public void testValidGoogleStyleOrderOfImports() throws Exception {
135         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
136 
137         final Configuration checkConfig = getModuleConfig("CustomImportOrder");
138         final String filePath = getPath("InputCustomImportOrderNoImports.java");
139 
140         final Integer[] warnList = getLinesWithWarn(filePath);
141         verify(checkConfig, filePath, expected, warnList);
142     }
143 }