View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.ImportOrderCheck.MSG_ORDERING_GROUP;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_ORDERING_LEX;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_ORDERING_STATIC;
25  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_SEPARATED_IN_GROUP;
26  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_SEPARATION;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
31  
32  public class ImportOrderCheckExamplesTest extends AbstractExamplesModuleTestSupport {
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/imports/importorder";
36      }
37  
38      @Test
39      public void testExample1() throws Exception {
40          final String[] expected = {
41              "15:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.IOException"),
42              "15:1: " + getCheckMessage(MSG_ORDERING_LEX,
43                      "java.io.IOException",
44                      "java.net.URL"),
45              "19:1: " + getCheckMessage(MSG_ORDERING_LEX,
46                      "java.util.Set",
47                      "org.w3c.dom.Document"),
48              "20:1: " + getCheckMessage(MSG_ORDERING_LEX,
49                      "java.util.Map",
50                      "java.util.Set"),
51              "21:1: " + getCheckMessage(MSG_ORDERING_LEX,
52                      "com.sun.security.auth.UserPrincipal",
53                      "java.util.Map"),
54          };
55  
56          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
57      }
58  
59      @Test
60      public void testExample2() throws Exception {
61          final String[] expected = {
62              "19:1: " + getCheckMessage(MSG_ORDERING_LEX,
63                      "java.lang.Math.PI",
64                      "java.lang.System.out"),
65              "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
66                      "java.net.URL"),
67              "27:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
68                      "javax.net.ssl.X509TrustManager"),
69          };
70  
71          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
72      }
73  
74      @Test
75      public void testExample3() throws Exception {
76          final String[] expected = {
77              "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
78                      "java.io.File"),
79              "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
80                      "java.io.IOException"),
81              "27:1: " + getCheckMessage(MSG_ORDERING_GROUP,
82                      "javax.swing.WindowConstants.EXIT_ON_CLOSE"),
83              "29:1: " + getCheckMessage(MSG_SEPARATION,
84                      "org.w3c.dom.Element"),
85          };
86  
87          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
88      }
89  
90      @Test
91      public void testExample4() throws Exception {
92          final String[] expected = {
93              "24:1: " + getCheckMessage(MSG_ORDERING_STATIC,
94                      "javax.swing.JComponent"),
95              "27:1: " + getCheckMessage(MSG_ORDERING_GROUP,
96                      "javax.swing.JComponent"),
97              "28:1: " + getCheckMessage(MSG_ORDERING_GROUP,
98                      "com.sun.source.tree.Tree"),
99          };
100 
101         verifyWithInlineConfigParser(getPath("Example4.java"), expected);
102     }
103 
104     @Test
105     public void testExample5() throws Exception {
106         final String[] expected = {
107             "17:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.swing.JComponent"),
108         };
109 
110         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
111     }
112 
113     @Test
114     public void testExample6() throws Exception {
115         final String[] expected = {
116             "19:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
117                     "java.util.Set"),
118             "20:1: " + getCheckMessage(MSG_ORDERING_STATIC,
119                     "java.lang.Math.sin"),
120         };
121 
122         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
123     }
124 
125     @Test
126     public void testExample7() throws Exception {
127         final String[] expected = {
128 
129         };
130 
131         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
132     }
133 
134     @Test
135     public void testExample8() throws Exception {
136         final String[] expected = {
137 
138         };
139 
140         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
141     }
142 
143     @Test
144     public void testExample9() throws Exception {
145         final String[] expected = {
146             "21:1: " + getCheckMessage(MSG_ORDERING_LEX,
147                     "java.lang.Character.UnicodeBlock.BASIC_LATIN",
148                     "java.lang.Character.valueOf"),
149         };
150 
151         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
152     }
153 
154     @Test
155     public void testExample10() throws Exception {
156         final String[] expected = {
157             "18:1: " + getCheckMessage(MSG_SEPARATION, "javax.swing.WindowConstants.*"),
158         };
159 
160         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
161     }
162 
163     @Test
164     public void testExample11() throws Exception {
165         final String[] expected = {
166 
167         };
168 
169         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
170     }
171 
172     @Test
173     public void testExample12() throws Exception {
174         final String[] expected = {
175             "17:1: " + getCheckMessage(MSG_ORDERING_GROUP, "java.io.File.createTempFile"),
176         };
177 
178         verifyWithInlineConfigParser(getPath("Example12.java"), expected);
179     }
180 }