1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }