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 com.puppycrawl.tools.checkstyle.checks.imports;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  public class PkgImportControlTest {
28  
29      private final PkgImportControl icRoot = new PkgImportControl(
30              "com.kazgroup.courtlink", false, MismatchStrategy.DISALLOWED);
31      private final PkgImportControl icCommon = new PkgImportControl(icRoot,
32              "common", false, MismatchStrategy.DELEGATE_TO_PARENT);
33      private final PkgImportControl icUncommon = new PkgImportControl(icRoot,
34              "uncommon", true, MismatchStrategy.DELEGATE_TO_PARENT);
35  
36      private final PkgImportControl icRootRegexpChild = new PkgImportControl(
37              "com.kazgroup.courtlink", false, MismatchStrategy.DELEGATE_TO_PARENT);
38      private final PkgImportControl icCommonRegexpChild = new PkgImportControl(icRootRegexpChild,
39              "common", false, MismatchStrategy.DELEGATE_TO_PARENT);
40  
41      private final PkgImportControl icRootRegexpParent = new PkgImportControl(
42              "com\\.[^.]+\\.courtlink", true, MismatchStrategy.DELEGATE_TO_PARENT);
43      private final PkgImportControl icBootRegexpParen = new PkgImportControl(icRootRegexpParent,
44              "bo+t", true, MismatchStrategy.DELEGATE_TO_PARENT);
45  
46      @BeforeEach
47      public void setUp() {
48          icRoot.addChild(icCommon);
49          icRoot.addImportRule(
50              new PkgImportRule(false, false, "org.springframework", false, false));
51          icRoot.addImportRule(
52              new PkgImportRule(false, false, "org.hibernate", false, false));
53          icRoot.addImportRule(
54              new PkgImportRule(true, false, "org.apache.commons", false, false));
55  
56          icRootRegexpChild.addChild(icCommonRegexpChild);
57          icRootRegexpChild.addImportRule(
58              new PkgImportRule(false, false, ".*\\.(spring|lui)framework", false, true));
59          icRootRegexpChild.addImportRule(
60              new PkgImportRule(false, false, "org\\.hibernate", false, true));
61          icRootRegexpChild.addImportRule(
62              new PkgImportRule(true, false, "org\\.(apache|lui)\\.commons", false, true));
63  
64          icCommon.addImportRule(
65              new PkgImportRule(true, false, "org.hibernate", false, false));
66  
67          icCommonRegexpChild.addImportRule(
68              new PkgImportRule(true, false, "org\\.h.*", false, true));
69  
70          icRootRegexpParent.addChild(icBootRegexpParen);
71      }
72  
73      @Test
74      public void testDotMetaCharacter() {
75          assertWithMessage("Unexpected response")
76              .that(icUncommon.locateFinest("com-kazgroup.courtlink.uncommon.regexp", "MyClass"))
77              .isNull();
78      }
79  
80      @Test
81      public void testLocateFinest() {
82          assertWithMessage("Unexpected response")
83              .that(icRoot.locateFinest("com.kazgroup.courtlink.domain", "MyClass"))
84              .isEqualTo(icRoot);
85          assertWithMessage("Unexpected response")
86              .that(icRoot.locateFinest("com.kazgroup.courtlink.common.api", "MyClass"))
87              .isEqualTo(icCommon);
88          assertWithMessage("Unexpected response")
89              .that(icRoot.locateFinest("com", "MyClass"))
90              .isNull();
91      }
92  
93      @Test
94      public void testEnsureTrailingDot() {
95          assertWithMessage("Unexpected response")
96              .that(icRoot.locateFinest("com.kazgroup.courtlinkkk", "MyClass"))
97              .isNull();
98          assertWithMessage("Unexpected response")
99              .that(icRoot.locateFinest("com.kazgroup.courtlink/common.api", "MyClass"))
100             .isNull();
101     }
102 
103     @Test
104     public void testCheckAccess() {
105         assertWithMessage("Unexpected access result")
106             .that(icCommon.checkAccess(
107                 "com.kazgroup.courtlink.common", "MyClass",
108                 "org.springframework.something"))
109             .isEqualTo(AccessResult.DISALLOWED);
110         assertWithMessage("Unexpected access result")
111             .that(icCommon
112                 .checkAccess("com.kazgroup.courtlink.common", "MyClass",
113                         "org.apache.commons.something"))
114             .isEqualTo(AccessResult.ALLOWED);
115         assertWithMessage("Unexpected access result")
116             .that(icCommon.checkAccess(
117                 "com.kazgroup.courtlink.common", "MyClass",
118                 "org.apache.commons"))
119             .isEqualTo(AccessResult.DISALLOWED);
120         assertWithMessage("Unexpected access result")
121             .that(icCommon.checkAccess(
122                 "com.kazgroup.courtlink.common", "MyClass",
123                 "org.hibernate.something"))
124             .isEqualTo(AccessResult.ALLOWED);
125         assertWithMessage("Unexpected access result")
126             .that(icCommon.checkAccess(
127                 "com.kazgroup.courtlink.common", "MyClass",
128                 "com.badpackage.something"))
129             .isEqualTo(AccessResult.DISALLOWED);
130         assertWithMessage("Unexpected access result")
131             .that(icRoot.checkAccess(
132                 "com.kazgroup.courtlink", "MyClass",
133                 "org.hibernate.something"))
134             .isEqualTo(AccessResult.DISALLOWED);
135     }
136 
137     @Test
138     public void testUnknownPkg() {
139         assertWithMessage("Unexpected response")
140             .that(icRoot.locateFinest("net.another", "MyClass"))
141             .isNull();
142     }
143 
144     @Test
145     public void testRegExpChildLocateFinest() {
146         assertWithMessage("Unexpected response")
147             .that(icRootRegexpChild.locateFinest("com.kazgroup.courtlink.domain", "MyClass"))
148             .isEqualTo(icRootRegexpChild);
149         assertWithMessage("Unexpected response")
150             .that(icRootRegexpChild.locateFinest("com.kazgroup.courtlink.common.api", "MyClass"))
151             .isEqualTo(icCommonRegexpChild);
152         assertWithMessage("Unexpected response")
153             .that(icRootRegexpChild.locateFinest("com", "MyClass"))
154             .isNull();
155     }
156 
157     @Test
158     public void testRegExpChildCheckAccess() {
159         assertWithMessage("Unexpected access result")
160             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
161                         "org.springframework.something"))
162             .isEqualTo(AccessResult.DISALLOWED);
163         assertWithMessage("Unexpected access result")
164             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
165                         "org.luiframework.something"))
166             .isEqualTo(AccessResult.DISALLOWED);
167         assertWithMessage("Unexpected access result")
168             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
169                         "de.springframework.something"))
170             .isEqualTo(AccessResult.DISALLOWED);
171         assertWithMessage("Unexpected access result")
172             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
173                 "de.luiframework.something"))
174             .isEqualTo(AccessResult.DISALLOWED);
175         assertWithMessage("Unexpected access result")
176             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
177                         "org.apache.commons.something"))
178             .isEqualTo(AccessResult.ALLOWED);
179         assertWithMessage("Unexpected access result")
180             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
181                         "org.lui.commons.something"))
182             .isEqualTo(AccessResult.ALLOWED);
183         assertWithMessage("Unexpected access result")
184             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
185                         "org.apache.commons"))
186             .isEqualTo(AccessResult.DISALLOWED);
187         assertWithMessage("Unexpected access result")
188             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
189                         "org.lui.commons"))
190             .isEqualTo(AccessResult.DISALLOWED);
191         assertWithMessage("Unexpected access result")
192             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
193                         "org.hibernate.something"))
194             .isEqualTo(AccessResult.ALLOWED);
195         assertWithMessage("Unexpected access result")
196             .that(icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common", "MyClass",
197                         "com.badpackage.something"))
198             .isEqualTo(AccessResult.DISALLOWED);
199         assertWithMessage("Unexpected access result")
200             .that(icRootRegexpChild.checkAccess("com.kazgroup.courtlink", "MyClass",
201                         "org.hibernate.something"))
202             .isEqualTo(AccessResult.DISALLOWED);
203     }
204 
205     @Test
206     public void testRegExpChildUnknownPkg() {
207         assertWithMessage("Unexpected response")
208             .that(icRootRegexpChild.locateFinest("net.another", "MyClass"))
209             .isNull();
210     }
211 
212     @Test
213     public void testRegExpParentInRootIsConsidered() {
214         assertWithMessage("Package should not be null")
215             .that(icRootRegexpParent.locateFinest("com", "MyClass"))
216             .isNull();
217         assertWithMessage("Package should not be null")
218             .that(icRootRegexpParent.locateFinest("com/hurz/courtlink", "MyClass"))
219             .isNull();
220         assertWithMessage("Package should not be null")
221             .that(icRootRegexpParent.locateFinest("com.hurz.hurz.courtlink", "MyClass"))
222             .isNull();
223         assertWithMessage("Invalid package")
224             .that(icRootRegexpParent.locateFinest("com.hurz.courtlink.domain", "MyClass"))
225             .isEqualTo(icRootRegexpParent);
226         assertWithMessage("Invalid package")
227             .that(icRootRegexpParent.locateFinest("com.kazgroup.courtlink.domain", "MyClass"))
228             .isEqualTo(icRootRegexpParent);
229     }
230 
231     @Test
232     public void testRegExpParentInSubpackageIsConsidered() {
233         assertWithMessage("Invalid package")
234             .that(icRootRegexpParent
235                 .locateFinest("com.kazgroup.courtlink.boot.api", "MyClass"))
236             .isEqualTo(icBootRegexpParen);
237         assertWithMessage("Invalid package")
238             .that(icRootRegexpParent
239                 .locateFinest("com.kazgroup.courtlink.bot.api", "MyClass"))
240             .isEqualTo(icBootRegexpParen);
241     }
242 
243     @Test
244     public void testRegExpParentEnsureTrailingDot() {
245         assertWithMessage("Invalid package")
246             .that(icRootRegexpParent.locateFinest("com.kazgroup.courtlinkkk", "MyClass"))
247             .isNull();
248         assertWithMessage("Invalid package")
249             .that(icRootRegexpParent.locateFinest("com.kazgroup.courtlink/common.api", "MyClass"))
250             .isNull();
251     }
252 
253     @Test
254     public void testRegExpParentAlternationInParentIsHandledCorrectly() {
255         // the regular expression has to be adjusted to (com\.foo|com\.bar)
256         final PkgImportControl root = new PkgImportControl("com\\.foo|com\\.bar", true,
257                 MismatchStrategy.DISALLOWED);
258         final PkgImportControl common = new PkgImportControl(root, "common", false,
259                 MismatchStrategy.DELEGATE_TO_PARENT);
260         root.addChild(common);
261         assertWithMessage("Invalid package")
262             .that(root.locateFinest("com.foo", "MyClass"))
263             .isEqualTo(root);
264         assertWithMessage("Invalid package")
265             .that(root.locateFinest("com.foo.common", "MyClass"))
266             .isEqualTo(common);
267         assertWithMessage("Invalid package")
268             .that(root.locateFinest("com.bar", "MyClass"))
269             .isEqualTo(root);
270         assertWithMessage("Invalid package")
271             .that(root.locateFinest("com.bar.common", "MyClass"))
272             .isEqualTo(common);
273     }
274 
275     @Test
276     public void testRegExpParentAlternationInParentIfUserCaresForIt() {
277         // the regular expression has to be adjusted to (com\.foo|com\.bar)
278         final PkgImportControl root = new PkgImportControl("(com\\.foo|com\\.bar)", true,
279                 MismatchStrategy.DISALLOWED);
280         final PkgImportControl common = new PkgImportControl(root, "common", false,
281                 MismatchStrategy.DELEGATE_TO_PARENT);
282         root.addChild(common);
283         assertWithMessage("Invalid package")
284             .that(root.locateFinest("com.foo", "MyClass"))
285             .isEqualTo(root);
286         assertWithMessage("Invalid package")
287             .that(root.locateFinest("com.foo.common", "MyClass"))
288             .isEqualTo(common);
289         assertWithMessage("Invalid package")
290             .that(root.locateFinest("com.bar", "MyClass"))
291             .isEqualTo(root);
292         assertWithMessage("Invalid package")
293             .that(root.locateFinest("com.bar.common", "MyClass"))
294             .isEqualTo(common);
295     }
296 
297     @Test
298     public void testRegExpParentAlternationInSubpackageIsHandledCorrectly() {
299         final PkgImportControl root = new PkgImportControl("org.somewhere", false,
300                 MismatchStrategy.DISALLOWED);
301         // the regular expression has to be adjusted to (foo|bar)
302         final PkgImportControl subpackages = new PkgImportControl(root, "foo|bar", true,
303                 MismatchStrategy.DELEGATE_TO_PARENT);
304         root.addChild(subpackages);
305         assertWithMessage("Invalid package")
306             .that(root.locateFinest("org.somewhere", "MyClass"))
307             .isEqualTo(root);
308         assertWithMessage("Invalid package")
309             .that(root.locateFinest("org.somewhere.foo", "MyClass"))
310             .isEqualTo(subpackages);
311         assertWithMessage("Invalid package")
312             .that(root.locateFinest("org.somewhere.bar", "MyClass"))
313             .isEqualTo(subpackages);
314     }
315 
316     @Test
317     public void testRegExpParentUnknownPkg() {
318         assertWithMessage("Package should not be null")
319             .that(icRootRegexpParent.locateFinest("net.another", "MyClass"))
320             .isNull();
321     }
322 
323 }