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