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.checks.imports;
21  
22  import org.checkstyle.base.AbstractCheckstyleModuleTestSupport;
23  import org.junit.jupiter.api.Test;
24  
25  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
26  import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class ImportOrderTest extends AbstractCheckstyleModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "org/checkstyle/checks/imports/importorder";
34      }
35  
36      @Test
37      public void testAndroid() throws Exception {
38          final DefaultConfiguration checkConfig = createModuleConfig(ImportOrderCheck.class);
39          checkConfig.addProperty("groups",
40              "android,androidx,com.android,dalvik,com,gov,junit,libcore,net,org,java,javax");
41          checkConfig.addProperty("option", "top");
42          checkConfig.addProperty("ordered", "true");
43          checkConfig.addProperty("separated", "true");
44          checkConfig.addProperty("separatedStaticGroups", "true");
45          checkConfig.addProperty("staticGroups",
46              "android,androidx,com.android,dalvik,com,gov,junit,libcore,net,org,java,javax");
47  
48          final String filePath = getNonCompilablePath("InputFromAndroid.java");
49          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
50          final Integer[] warnList = getLinesWithWarn(filePath);
51          verify(checkConfig, filePath, expected, warnList);
52      }
53  
54      @Test
55      public void testSpotify() throws Exception {
56          final DefaultConfiguration checkConfig = createModuleConfig(ImportOrderCheck.class);
57          checkConfig.addProperty("groups", "android,com,net,junit,org,java,javax");
58          checkConfig.addProperty("option", "bottom");
59          checkConfig.addProperty("ordered", "true");
60          checkConfig.addProperty("separated", "true");
61          checkConfig.addProperty("separatedStaticGroups", "true");
62          checkConfig.addProperty("staticGroups", "android,com,net,junit,org,java,javax");
63  
64          final String filePath = getNonCompilablePath("InputFromSpotify.java");
65          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66          final Integer[] warnList = getLinesWithWarn(filePath);
67          verify(checkConfig, filePath, expected, warnList);
68      }
69  
70      @Test
71      public void testTwitter() throws Exception {
72          final DefaultConfiguration checkConfig = createModuleConfig(ImportOrderCheck.class);
73          checkConfig.addProperty("caseSensitive", "true");
74          checkConfig.addProperty("groups", "android,com.twitter,com,junit,net,org,java,javax");
75          checkConfig.addProperty("option", "bottom");
76          checkConfig.addProperty("ordered", "true");
77          checkConfig.addProperty("separated", "true");
78          checkConfig.addProperty("separatedStaticGroups", "true");
79          checkConfig.addProperty("staticGroups",
80              "android,com.twitter,com,junit,net,org,java,javax");
81  
82          final String filePath = getNonCompilablePath("InputFromTwitter.java");
83          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
84          final Integer[] warnList = getLinesWithWarn(filePath);
85          verify(checkConfig, filePath, expected, warnList);
86      }
87  
88  }