View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class ImportControlTest {
29      private final ImportControl icRoot = new ImportControl("com.kazgroup.courtlink", false);
30      private final ImportControl icCommon = new ImportControl(icRoot, "common", false);
31  
32      private final ImportControl icRootRegexpChild = new ImportControl("com.kazgroup.courtlink",
33              false);
34      private final ImportControl icCommonRegexpChild = new ImportControl(icRootRegexpChild,
35              "common", false);
36  
37      private final ImportControl icRootRegexpParent = new ImportControl("com\\.[^.]+\\.courtlink",
38              true);
39      private final ImportControl icCommonRegexpParen = new ImportControl(icRootRegexpParent,
40              "com+on", true);
41  
42      @Before
43      public void setUp() {
44          icRoot.addChild(icCommon);
45          icRoot.addImportRule(
46              new PkgImportRule(false, false, "org.springframework", false, false));
47          icRoot.addImportRule(
48              new PkgImportRule(false, false, "org.hibernate", false, false));
49          icRoot.addImportRule(
50              new PkgImportRule(true, false, "org.apache.commons", false, false));
51  
52          icRootRegexpChild.addChild(icCommonRegexpChild);
53          icRootRegexpChild.addImportRule(
54              new PkgImportRule(false, false, ".*\\.(spring|lui)framework", false, true));
55          icRootRegexpChild.addImportRule(
56              new PkgImportRule(false, false, "org\\.hibernate", false, true));
57          icRootRegexpChild.addImportRule(
58              new PkgImportRule(true, false, "org\\.(apache|lui)\\.commons", false, true));
59  
60          icCommon.addImportRule(
61              new PkgImportRule(true, false, "org.hibernate", false, false));
62  
63          icCommonRegexpChild.addImportRule(
64              new PkgImportRule(true, false, "org\\.h.*", false, true));
65  
66          icRootRegexpParent.addChild(icCommonRegexpParen);
67      }
68  
69      @Test
70      public void testLocateFinest() {
71          assertEquals("Unexpected response", icRoot, icRoot
72                  .locateFinest("com.kazgroup.courtlink.domain"));
73          assertEquals("Unexpected response", icCommon, icRoot
74                  .locateFinest("com.kazgroup.courtlink.common.api"));
75          assertNull("Unexpected response", icRoot.locateFinest("com"));
76      }
77  
78      @Test
79      public void testEnsureTrailingDot() {
80          assertNull("Unexpected response", icRoot.locateFinest("com.kazgroup.courtlinkkk"));
81          assertNull("Unexpected response",
82                  icRoot.locateFinest("com.kazgroup.courtlink/common.api"));
83      }
84  
85      @Test
86      public void testCheckAccess() {
87          assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
88                  "com.kazgroup.courtlink.common",
89                  "org.springframework.something"));
90          assertEquals("Unexpected access result", AccessResult.ALLOWED, icCommon
91                  .checkAccess("com.kazgroup.courtlink.common",
92                          "org.apache.commons.something"));
93          assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
94                  "com.kazgroup.courtlink.common", "org.apache.commons"));
95          assertEquals("Unexpected access result", AccessResult.ALLOWED, icCommon.checkAccess(
96                  "com.kazgroup.courtlink.common", "org.hibernate.something"));
97          assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
98                  "com.kazgroup.courtlink.common", "com.badpackage.something"));
99          assertEquals("Unexpected access result", AccessResult.DISALLOWED, icRoot.checkAccess(
100                 "com.kazgroup.courtlink", "org.hibernate.something"));
101     }
102 
103     @Test
104     public void testUnknownPkg() {
105         assertNull("Unexpected response", icRoot.locateFinest("net.another"));
106     }
107 
108     @Test
109     public void testRegExpChildLocateFinest() {
110         assertEquals("Unexpected response", icRootRegexpChild, icRootRegexpChild
111                 .locateFinest("com.kazgroup.courtlink.domain"));
112         assertEquals("Unexpected response", icCommonRegexpChild, icRootRegexpChild
113                 .locateFinest("com.kazgroup.courtlink.common.api"));
114         assertNull("Unexpected response", icRootRegexpChild.locateFinest("com"));
115     }
116 
117     @Test
118     public void testRegExpChildCheckAccess() {
119         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
120                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
121                         "org.springframework.something"));
122         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
123                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
124                         "org.luiframework.something"));
125         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
126                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
127                         "de.springframework.something"));
128         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
129                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
130                 "de.luiframework.something"));
131         assertEquals("Unexpected access result", AccessResult.ALLOWED,
132                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
133                         "org.apache.commons.something"));
134         assertEquals("Unexpected access result", AccessResult.ALLOWED,
135                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
136                         "org.lui.commons.something"));
137         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
138                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
139                         "org.apache.commons"));
140         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
141                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
142                         "org.lui.commons"));
143         assertEquals("Unexpected access result", AccessResult.ALLOWED,
144                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
145                         "org.hibernate.something"));
146         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
147                 icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
148                         "com.badpackage.something"));
149         assertEquals("Unexpected access result", AccessResult.DISALLOWED,
150                 icRootRegexpChild.checkAccess("com.kazgroup.courtlink",
151                         "org.hibernate.something"));
152     }
153 
154     @Test
155     public void testRegExpChildUnknownPkg() {
156         assertNull("Unexpected response", icRootRegexpChild.locateFinest("net.another"));
157     }
158 
159     @Test
160     public void testRegExpParentInRootIsConsidered() {
161         assertNull("Package should not be null", icRootRegexpParent.locateFinest("com"));
162         assertNull("Package should not be null",
163                 icRootRegexpParent.locateFinest("com/hurz/courtlink"));
164         assertNull("Package should not be null",
165                 icRootRegexpParent.locateFinest("com.hurz.hurz.courtlink"));
166         assertEquals("Invalid package", icRootRegexpParent,
167                 icRootRegexpParent.locateFinest("com.hurz.courtlink.domain"));
168         assertEquals("Invalid package", icRootRegexpParent, icRootRegexpParent
169                 .locateFinest("com.kazgroup.courtlink.domain"));
170     }
171 
172     @Test
173     public void testRegExpParentInSubpackageIsConsidered() {
174         assertEquals("Invalid package", icCommonRegexpParen, icRootRegexpParent
175                 .locateFinest("com.kazgroup.courtlink.common.api"));
176         assertEquals("Invalid package", icCommonRegexpParen, icRootRegexpParent
177                 .locateFinest("com.kazgroup.courtlink.comon.api"));
178     }
179 
180     @Test
181     public void testRegExpParentEnsureTrailingDot() {
182         assertNull("Invalid package", icRootRegexpParent.locateFinest("com.kazgroup.courtlinkkk"));
183         assertNull("Invalid package",
184                 icRootRegexpParent.locateFinest("com.kazgroup.courtlink/common.api"));
185     }
186 
187     @Test
188     public void testRegExpParentAlternationInParentIsHandledCorrectly() {
189         // the regular expression has to be adjusted to (com\.foo|com\.bar)
190         final ImportControl root = new ImportControl("com\\.foo|com\\.bar", true);
191         final ImportControl common = new ImportControl(root, "common", false);
192         root.addChild(common);
193         assertEquals("Invalid package", root, root.locateFinest("com.foo"));
194         assertEquals("Invalid package", common, root.locateFinest("com.foo.common"));
195         assertEquals("Invalid package", root, root.locateFinest("com.bar"));
196         assertEquals("Invalid package", common, root.locateFinest("com.bar.common"));
197     }
198 
199     @Test
200     public void testRegExpParentAlternationInParentIfUserCaresForIt() {
201         // the regular expression has to be adjusted to (com\.foo|com\.bar)
202         final ImportControl root = new ImportControl("(com\\.foo|com\\.bar)", true);
203         final ImportControl common = new ImportControl(root, "common", false);
204         root.addChild(common);
205         assertEquals("Invalid package", root, root.locateFinest("com.foo"));
206         assertEquals("Invalid package", common, root.locateFinest("com.foo.common"));
207         assertEquals("Invalid package", root, root.locateFinest("com.bar"));
208         assertEquals("Invalid package", common, root.locateFinest("com.bar.common"));
209     }
210 
211     @Test
212     public void testRegExpParentAlternationInSubpackageIsHandledCorrectly() {
213         final ImportControl root = new ImportControl("org.somewhere", false);
214         // the regular expression has to be adjusted to (foo|bar)
215         final ImportControl subpackages = new ImportControl(root, "foo|bar", true);
216         root.addChild(subpackages);
217         assertEquals("Invalid package", root, root.locateFinest("org.somewhere"));
218         assertEquals("Invalid package", subpackages, root.locateFinest("org.somewhere.foo"));
219         assertEquals("Invalid package", subpackages, root.locateFinest("org.somewhere.bar"));
220     }
221 
222     @Test
223     public void testRegExpParentUnknownPkg() {
224         assertNull("Package should not be null", icRootRegexpParent.locateFinest("net.another"));
225     }
226 }