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.Test;
25  
26  public class ClassImportRuleTest {
27  
28      @Test
29      public void testClassImportRule() {
30          final ClassImportRule rule = new ClassImportRule(true, false, "pkg.a", false);
31          assertWithMessage("Class import rule should not be null")
32              .that(rule)
33              .isNotNull();
34          assertWithMessage("Invalid access result")
35              .that(rule.verifyImport("asda"))
36              .isEqualTo(AccessResult.UNKNOWN);
37          assertWithMessage("Invalid access result")
38              .that(rule.verifyImport("p"))
39              .isEqualTo(AccessResult.UNKNOWN);
40          assertWithMessage("Invalid access result")
41              .that(rule.verifyImport("pkga"))
42              .isEqualTo(AccessResult.UNKNOWN);
43          assertWithMessage("Invalid access result")
44              .that(rule.verifyImport("pkg.a"))
45              .isEqualTo(AccessResult.ALLOWED);
46          assertWithMessage("Invalid access result")
47              .that(rule.verifyImport("pkg.a.b"))
48              .isEqualTo(AccessResult.UNKNOWN);
49          assertWithMessage("Invalid access result")
50              .that(rule.verifyImport("pkg"))
51              .isEqualTo(AccessResult.UNKNOWN);
52      }
53  
54      @Test
55      public void testClassImportRuleRegexpSimple() {
56          final ClassImportRule rule = new ClassImportRule(true, false, "pkg.a", true);
57          assertWithMessage("Class import rule should not be null")
58              .that(rule)
59              .isNotNull();
60          assertWithMessage("Invalid access result")
61              .that(rule.verifyImport("asda"))
62              .isEqualTo(AccessResult.UNKNOWN);
63          assertWithMessage("Invalid access result")
64              .that(rule.verifyImport("p"))
65              .isEqualTo(AccessResult.UNKNOWN);
66          assertWithMessage("Invalid access result")
67              .that(rule.verifyImport("pkga"))
68              .isEqualTo(AccessResult.UNKNOWN);
69          assertWithMessage("Invalid access result")
70              .that(rule.verifyImport("pkg.a"))
71              .isEqualTo(AccessResult.ALLOWED);
72          assertWithMessage("Invalid access result")
73              .that(rule.verifyImport("pkg.a.b"))
74              .isEqualTo(AccessResult.UNKNOWN);
75          assertWithMessage("Invalid access result")
76              .that(rule.verifyImport("pkg"))
77              .isEqualTo(AccessResult.UNKNOWN);
78      }
79  
80      @Test
81      public void testClassImportRuleRegexp() {
82          final ClassImportRule rule = new ClassImportRule(true, false, "pk[gx]\\.a", true);
83          assertWithMessage("Class import rule should not be null")
84              .that(rule)
85              .isNotNull();
86          assertWithMessage("Invalid access result")
87              .that(rule.verifyImport("asda"))
88              .isEqualTo(AccessResult.UNKNOWN);
89          assertWithMessage("Invalid access result")
90              .that(rule.verifyImport("p"))
91              .isEqualTo(AccessResult.UNKNOWN);
92          assertWithMessage("Invalid access result")
93              .that(rule.verifyImport("pkga"))
94              .isEqualTo(AccessResult.UNKNOWN);
95          assertWithMessage("Invalid access result")
96              .that(rule.verifyImport("pkg.a"))
97              .isEqualTo(AccessResult.ALLOWED);
98          assertWithMessage("Invalid access result")
99              .that(rule.verifyImport("pkx.a"))
100             .isEqualTo(AccessResult.ALLOWED);
101         assertWithMessage("Invalid access result")
102             .that(rule.verifyImport("pkg.a.b"))
103             .isEqualTo(AccessResult.UNKNOWN);
104         assertWithMessage("Invalid access result")
105             .that(rule.verifyImport("pkg"))
106             .isEqualTo(AccessResult.UNKNOWN);
107     }
108 
109 }