Skip to content

Commit 21580c9

Browse files
committed
Add tests and for SpringImportOrderCheck
Add tests for `SpringImportOrderCheck` and also allow it to be configured from `SpringChecks`. Closes gh-62
1 parent 69bc101 commit 21580c9

File tree

10 files changed

+119
-2
lines changed

10 files changed

+119
-2
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringChecks.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.puppycrawl.tools.checkstyle.filters.SuppressFilterElement;
3939

4040
import io.spring.javaformat.checkstyle.check.SpringHeaderCheck;
41+
import io.spring.javaformat.checkstyle.check.SpringImportOrderCheck;
4142

4243
/**
4344
* {@link FileSetCheck} that applies Spring checkstype rules.
@@ -62,6 +63,8 @@ public class SpringChecks extends AbstractFileSetCheck implements ExternalResour
6263

6364
private String headerFile;
6465

66+
private String projectRootPackage = SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE;
67+
6568
/**
6669
* Sets tab width.
6770
* @param tabWidth the distance between tab stops
@@ -97,6 +100,7 @@ public void finishLocalSetup() {
97100
put(properties, "headerType", this.headerType);
98101
put(properties, "headerCopyrightPattern", this.headerCopyrightPattern);
99102
put(properties, "headerFile", this.headerFile);
103+
put(properties, "projectRootPackage", this.projectRootPackage);
100104
this.checks = new SpringConfigurationLoader(context, this.moduleFactory)
101105
.load(new PropertiesExpander(properties));
102106
}
@@ -161,4 +165,8 @@ public void setHeaderFile(String headerFile) {
161165
this.headerFile = headerFile;
162166
}
163167

168+
public void setProjectRootPackage(String projectRootPackage) {
169+
this.projectRootPackage = projectRootPackage;
170+
}
171+
164172
}

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringImportOrderCheck.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
*/
2626
public class SpringImportOrderCheck extends ImportOrderCheck {
2727

28-
private static final String DEFAULT_PROJECT_ROOT_PACKAGE = "org.springframework";
28+
/**
29+
* The default root package.
30+
*/
31+
public static final String DEFAULT_PROJECT_ROOT_PACKAGE = "org.springframework";
2932

3033
public SpringImportOrderCheck() {
3134
setProjectRootPackage(DEFAULT_PROJECT_ROOT_PACKAGE);

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@
8181
<module name="com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck">
8282
<property name="processJavadoc" value="true" />
8383
</module>
84-
<module name="io.spring.javaformat.checkstyle.check.SpringImportOrderCheck"/>
84+
<module name="io.spring.javaformat.checkstyle.check.SpringImportOrderCheck">
85+
<property name="projectRootPackage" value="${projectRootPackage}"/>
86+
</module>
8587

8688
<!-- Javadoc Comments -->
8789
<module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck">

spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.Test;
2929

3030
import io.spring.javaformat.checkstyle.check.SpringHeaderCheck;
31+
import io.spring.javaformat.checkstyle.check.SpringImportOrderCheck;
3132

3233
import static org.assertj.core.api.Assertions.assertThat;
3334

@@ -56,6 +57,8 @@ private PropertyResolver getPropertyResolver() {
5657
properties.put("headerFile", "");
5758
properties.put("headerCopyrightPattern",
5859
SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN);
60+
properties.put("projectRootPackage",
61+
SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE);
5962
return new PropertiesExpander(properties);
6063
}
6164

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+ImportOrderCustomPackageInvalid.java:21: import.ordering [SpringImportOrder]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="com.puppycrawl.tools.checkstyle.Checker">
6+
<module name="io.spring.javaformat.checkstyle.SpringChecks">
7+
<property name="projectRootPackage" value="com.example"/>
8+
</module>
9+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="com.puppycrawl.tools.checkstyle.Checker">
6+
<module name="io.spring.javaformat.checkstyle.SpringChecks">
7+
<property name="projectRootPackage" value="com.example"/>
8+
</module>
9+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.example.util.MyUtils1;
20+
import com.example.util.MyUtils2;
21+
import java.util.Objects;
22+
23+
/**
24+
* Custom pacakge with valid imports.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
public final class ImportOrderCustomPackageInvalid {
29+
30+
private ImportOrderCustomPackageValid() {
31+
}
32+
33+
public static void main(String[] args) {
34+
if (Objects.nonNull(args)) {
35+
MyUtils1.sort(args);
36+
MyUtils2.find(args);
37+
}
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2017-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import java.util.Objects;
20+
21+
import com.example.util.MyUtils1;
22+
import com.example.util.MyUtils2;
23+
24+
/**
25+
* Custom pacakge with valid imports.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
public final class ImportOrderCustomPackageValid {
30+
31+
private ImportOrderCustomPackageValid() {
32+
}
33+
34+
public static void main(String[] args) {
35+
if (Objects.nonNull(args)) {
36+
MyUtils1.sort(args);
37+
MyUtils2.find(args);
38+
}
39+
}
40+
41+
}

0 commit comments

Comments
 (0)