From d496c187af0274a6c2b989455a926f1313ad7221 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 24 May 2022 10:45:23 +0100 Subject: [PATCH] Add JavadocPackageCheck to the default Spring checks Closes gh-328 --- .../src/main/java/simple/package-info.java | 1 + .../checkstyle/spring-checkstyle.xml | 1 + .../checkstyle/SpringChecksTests.java | 16 +++++++++--- .../SpringConfigurationLoaderTests.java | 12 ++++----- .../check/nopackageinfo/NoPackageInfo.txt | 1 + .../source/nopackageinfo/NoPackageInfo.java | 26 +++++++++++++++++++ .../test/resources/source/package-info.java | 0 7 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-ok/src/main/java/simple/package-info.java create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/nopackageinfo/NoPackageInfo.txt create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/nopackageinfo/NoPackageInfo.java create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/package-info.java diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-ok/src/main/java/simple/package-info.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-ok/src/main/java/simple/package-info.java new file mode 100644 index 00000000..35b32be0 --- /dev/null +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-ok/src/main/java/simple/package-info.java @@ -0,0 +1 @@ +package simple; \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml b/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml index ac2e3b31..894b78bb 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml +++ b/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml @@ -12,6 +12,7 @@ + diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringChecksTests.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringChecksTests.java index 6eb6f653..311d7f50 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringChecksTests.java +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringChecksTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -112,8 +113,14 @@ private void printDebugInfo(File file) throws CheckstyleException { } public static Collection paramaters() throws IOException { - return Arrays.stream(SOURCES_DIR.list((dir, name) -> !name.startsWith("."))).sorted().map(Parameter::new) - .collect(Collectors.toList()); + ArrayList parameters = Arrays.stream(SOURCES_DIR.listFiles(SpringChecksTests::sourceFile)).sorted() + .map(Parameter::new).collect(Collectors.toCollection(ArrayList::new)); + parameters.add(new Parameter(new File(SOURCES_DIR, "nopackageinfo/NoPackageInfo.java"))); + return parameters; + } + + private static boolean sourceFile(File file) { + return file.isFile() && !file.getName().startsWith(".") && !file.getName().equals("package-info.java"); } private static class Parameter { @@ -126,7 +133,8 @@ private static class Parameter { private final File configFile; - Parameter(String sourceName) { + Parameter(File sourceFile) { + String sourceName = sourceFile.getAbsolutePath().substring(SOURCES_DIR.getAbsolutePath().length() + 1); this.name = sourceName.replace(".java", ""); this.sourceFile = new File(SOURCES_DIR, sourceName); File configFile = new File(CONFIGS_DIR, this.name + ".xml"); diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java index 7415277f..bb42d4f0 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,8 +45,8 @@ public class SpringConfigurationLoaderTests { @Test public void loadShouldLoadChecks() { Collection checks = load(null); - assertThat(checks).hasSize(3); - TreeWalker treeWalker = (TreeWalker) checks.toArray()[2]; + assertThat(checks).hasSize(4); + TreeWalker treeWalker = (TreeWalker) checks.toArray()[3]; Set ordinaryChecks = (Set) Extractors.byName("ordinaryChecks").extract(treeWalker); assertThat(ordinaryChecks).hasSize(60); } @@ -56,8 +56,8 @@ public void loadWithExcludeShouldExcludeChecks() { Set excludes = Collections .singleton("com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck"); Collection checks = load(excludes); - assertThat(checks).hasSize(3); - TreeWalker treeWalker = (TreeWalker) checks.toArray()[2]; + assertThat(checks).hasSize(4); + TreeWalker treeWalker = (TreeWalker) checks.toArray()[3]; Set ordinaryChecks = (Set) Extractors.byName("ordinaryChecks").extract(treeWalker); assertThat(ordinaryChecks).hasSize(59); } @@ -66,7 +66,7 @@ public void loadWithExcludeShouldExcludeChecks() { public void loadWithExcludeHeaderShouldExcludeChecks() { Set excludes = Collections.singleton("io.spring.javaformat.checkstyle.check.SpringHeaderCheck"); Object[] checks = load(excludes).stream().toArray(); - assertThat(checks).hasSize(2); + assertThat(checks).hasSize(3); } private Collection load(Set excludes) { diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/nopackageinfo/NoPackageInfo.txt b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/nopackageinfo/NoPackageInfo.txt new file mode 100644 index 00000000..a19759c0 --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/nopackageinfo/NoPackageInfo.txt @@ -0,0 +1 @@ ++Missing package-info.java file \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/nopackageinfo/NoPackageInfo.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/nopackageinfo/NoPackageInfo.java new file mode 100644 index 00000000..a6f8b3ae --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/nopackageinfo/NoPackageInfo.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package nopackageinfo; + +/** + * A class in a package with no {@code package-info.java} file. + * + * @author Andy Wilkinson + */ +class NoPackageInfo { + +} diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/package-info.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/package-info.java new file mode 100644 index 00000000..e69de29b