aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlTest.java
blob: 7a92cc5e5cc29d275e90b0dda10d1f20e20af665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.checks.imports;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;

public class ImportControlTest {
    private final ImportControl icRoot = new ImportControl("com.kazgroup.courtlink", false);
    private final ImportControl icCommon = new ImportControl(icRoot, "common", false);

    private final ImportControl icRootRegexpChild = new ImportControl("com.kazgroup.courtlink",
            false);
    private final ImportControl icCommonRegexpChild = new ImportControl(icRootRegexpChild,
            "common", false);

    private final ImportControl icRootRegexpParent = new ImportControl("com\\.[^.]+\\.courtlink",
            true);
    private final ImportControl icCommonRegexpParen = new ImportControl(icRootRegexpParent,
            "com+on", true);

    @Before
    public void setUp() {
        icRoot.addChild(icCommon);
        icRoot.addImportRule(
            new PkgImportRule(false, false, "org.springframework", false, false));
        icRoot.addImportRule(
            new PkgImportRule(false, false, "org.hibernate", false, false));
        icRoot.addImportRule(
            new PkgImportRule(true, false, "org.apache.commons", false, false));

        icRootRegexpChild.addChild(icCommonRegexpChild);
        icRootRegexpChild.addImportRule(
            new PkgImportRule(false, false, ".*\\.(spring|lui)framework", false, true));
        icRootRegexpChild.addImportRule(
            new PkgImportRule(false, false, "org\\.hibernate", false, true));
        icRootRegexpChild.addImportRule(
            new PkgImportRule(true, false, "org\\.(apache|lui)\\.commons", false, true));

        icCommon.addImportRule(
            new PkgImportRule(true, false, "org.hibernate", false, false));

        icCommonRegexpChild.addImportRule(
            new PkgImportRule(true, false, "org\\.h.*", false, true));

        icRootRegexpParent.addChild(icCommonRegexpParen);
    }

    @Test
    public void testLocateFinest() {
        assertEquals("Unexpected response", icRoot, icRoot
                .locateFinest("com.kazgroup.courtlink.domain"));
        assertEquals("Unexpected response", icCommon, icRoot
                .locateFinest("com.kazgroup.courtlink.common.api"));
        assertNull("Unexpected response", icRoot.locateFinest("com"));
    }

    @Test
    public void testEnsureTrailingDot() {
        assertNull("Unexpected response", icRoot.locateFinest("com.kazgroup.courtlinkkk"));
        assertNull("Unexpected response",
                icRoot.locateFinest("com.kazgroup.courtlink/common.api"));
    }

    @Test
    public void testCheckAccess() {
        assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
                "com.kazgroup.courtlink.common",
                "org.springframework.something"));
        assertEquals("Unexpected access result", AccessResult.ALLOWED, icCommon
                .checkAccess("com.kazgroup.courtlink.common",
                        "org.apache.commons.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
                "com.kazgroup.courtlink.common", "org.apache.commons"));
        assertEquals("Unexpected access result", AccessResult.ALLOWED, icCommon.checkAccess(
                "com.kazgroup.courtlink.common", "org.hibernate.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED, icCommon.checkAccess(
                "com.kazgroup.courtlink.common", "com.badpackage.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED, icRoot.checkAccess(
                "com.kazgroup.courtlink", "org.hibernate.something"));
    }

    @Test
    public void testUnknownPkg() {
        assertNull("Unexpected response", icRoot.locateFinest("net.another"));
    }

    @Test
    public void testRegExpChildLocateFinest() {
        assertEquals("Unexpected response", icRootRegexpChild, icRootRegexpChild
                .locateFinest("com.kazgroup.courtlink.domain"));
        assertEquals("Unexpected response", icCommonRegexpChild, icRootRegexpChild
                .locateFinest("com.kazgroup.courtlink.common.api"));
        assertNull("Unexpected response", icRootRegexpChild.locateFinest("com"));
    }

    @Test
    public void testRegExpChildCheckAccess() {
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.springframework.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.luiframework.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "de.springframework.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                "de.luiframework.something"));
        assertEquals("Unexpected access result", AccessResult.ALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.apache.commons.something"));
        assertEquals("Unexpected access result", AccessResult.ALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.lui.commons.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.apache.commons"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.lui.commons"));
        assertEquals("Unexpected access result", AccessResult.ALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "org.hibernate.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icCommonRegexpChild.checkAccess("com.kazgroup.courtlink.common",
                        "com.badpackage.something"));
        assertEquals("Unexpected access result", AccessResult.DISALLOWED,
                icRootRegexpChild.checkAccess("com.kazgroup.courtlink",
                        "org.hibernate.something"));
    }

    @Test
    public void testRegExpChildUnknownPkg() {
        assertNull("Unexpected response", icRootRegexpChild.locateFinest("net.another"));
    }

    @Test
    public void testRegExpParentInRootIsConsidered() {
        assertNull("Package should not be null", icRootRegexpParent.locateFinest("com"));
        assertNull("Package should not be null",
                icRootRegexpParent.locateFinest("com/hurz/courtlink"));
        assertNull("Package should not be null",
                icRootRegexpParent.locateFinest("com.hurz.hurz.courtlink"));
        assertEquals("Invalid package", icRootRegexpParent,
                icRootRegexpParent.locateFinest("com.hurz.courtlink.domain"));
        assertEquals("Invalid package", icRootRegexpParent, icRootRegexpParent
                .locateFinest("com.kazgroup.courtlink.domain"));
    }

    @Test
    public void testRegExpParentInSubpackageIsConsidered() {
        assertEquals("Invalid package", icCommonRegexpParen, icRootRegexpParent
                .locateFinest("com.kazgroup.courtlink.common.api"));
        assertEquals("Invalid package", icCommonRegexpParen, icRootRegexpParent
                .locateFinest("com.kazgroup.courtlink.comon.api"));
    }

    @Test
    public void testRegExpParentEnsureTrailingDot() {
        assertNull("Invalid package", icRootRegexpParent.locateFinest("com.kazgroup.courtlinkkk"));
        assertNull("Invalid package",
                icRootRegexpParent.locateFinest("com.kazgroup.courtlink/common.api"));
    }

    @Test
    public void testRegExpParentAlternationInParentIsHandledCorrectly() {
        // the regular expression has to be adjusted to (com\.foo|com\.bar)
        final ImportControl root = new ImportControl("com\\.foo|com\\.bar", true);
        final ImportControl common = new ImportControl(root, "common", false);
        root.addChild(common);
        assertEquals("Invalid package", root, root.locateFinest("com.foo"));
        assertEquals("Invalid package", common, root.locateFinest("com.foo.common"));
        assertEquals("Invalid package", root, root.locateFinest("com.bar"));
        assertEquals("Invalid package", common, root.locateFinest("com.bar.common"));
    }

    @Test
    public void testRegExpParentAlternationInParentIfUserCaresForIt() {
        // the regular expression has to be adjusted to (com\.foo|com\.bar)
        final ImportControl root = new ImportControl("(com\\.foo|com\\.bar)", true);
        final ImportControl common = new ImportControl(root, "common", false);
        root.addChild(common);
        assertEquals("Invalid package", root, root.locateFinest("com.foo"));
        assertEquals("Invalid package", common, root.locateFinest("com.foo.common"));
        assertEquals("Invalid package", root, root.locateFinest("com.bar"));
        assertEquals("Invalid package", common, root.locateFinest("com.bar.common"));
    }

    @Test
    public void testRegExpParentAlternationInSubpackageIsHandledCorrectly() {
        final ImportControl root = new ImportControl("org.somewhere", false);
        // the regular expression has to be adjusted to (foo|bar)
        final ImportControl subpackages = new ImportControl(root, "foo|bar", true);
        root.addChild(subpackages);
        assertEquals("Invalid package", root, root.locateFinest("org.somewhere"));
        assertEquals("Invalid package", subpackages, root.locateFinest("org.somewhere.foo"));
        assertEquals("Invalid package", subpackages, root.locateFinest("org.somewhere.bar"));
    }

    @Test
    public void testRegExpParentUnknownPkg() {
        assertNull("Package should not be null", icRootRegexpParent.locateFinest("net.another"));
    }
}