builder: refactored and improved test coverage

This commit is contained in:
Paul Campbell 2017-05-30 12:32:29 +01:00
parent d870720ed6
commit 2e8e26c351
17 changed files with 393 additions and 74 deletions

View file

@ -0,0 +1,49 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/**
* Configuration for Builder.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
@Configuration
public class BuilderConfiguration {
/**
* Create the ClassPath used to scan packages.
*
* @return the ClassPath
*
* @throws IOException if there is an error
*/
@Bean
public ClassPath classPath() throws IOException {
return ClassPath.from(getClass().getClassLoader());
}
}

View file

@ -55,6 +55,8 @@ class CheckstyleWriter implements CommandLineRunner {
private final RulesProperties rulesProperties; private final RulesProperties rulesProperties;
private final RuleClassLocator ruleClassLocator;
@Override @Override
public void run(final String... args) throws Exception { public void run(final String... args) throws Exception {
Stream.of(RuleLevel.values()) Stream.of(RuleLevel.values())
@ -101,9 +103,9 @@ class CheckstyleWriter implements CommandLineRunner {
private String formatRuleAsModule(final Rule rule) { private String formatRuleAsModule(final Rule rule) {
if (rule.getProperties() if (rule.getProperties()
.isEmpty()) { .isEmpty()) {
return String.format("<module name=\"%s\"/>", rule.getCanonicalClassName()); return String.format("<module name=\"%s\"/>", ruleClassLocator.apply(rule));
} }
return String.format("<module name=\"%s\">%n %s%n</module>", rule.getCanonicalClassName(), return String.format("<module name=\"%s\">%n %s%n</module>", ruleClassLocator.apply(rule),
formatProperties(rule.getProperties()) formatProperties(rule.getProperties())
); );
} }

View file

@ -0,0 +1,47 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.stream.Stream;
/**
* Default implementation of {@link PackageScanner}.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
@Service
@RequiredArgsConstructor
public class DefaultPackageScanner implements PackageScanner {
private final ClassPath classPath;
@Override
public final Stream<String> apply(final RuleSource ruleSource) {
return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage())
.stream()
.map(ClassPath.ClassInfo::getName);
}
}

View file

@ -0,0 +1,77 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.builder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Default implementation of {@link RuleClassLocator}.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
@Service
@RequiredArgsConstructor
public class DefaultRuleClassLocator implements RuleClassLocator {
private final PackageScanner packageScanner;
private Map<RuleSource, List<String>> checkClasses;
/**
* Initialise class lists for {@link RuleSource}s.
*/
@PostConstruct
public final void init() {
checkClasses = Stream.of(RuleSource.values())
.map(source -> new AbstractMap.SimpleEntry<>(
source, packageScanner.apply(source)
.collect(Collectors.toList())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
@Override
public final String apply(final Rule rule) {
return getCanonicalClassName(rule.getSource(), rule.getName());
}
private String getCanonicalClassName(final RuleSource source, final String name) {
return checkClasses.get(source)
.stream()
.sorted()
.filter(classname -> byRuleName(classname, name))
.findFirst()
.orElseThrow(() -> new CheckstyleClassNotFoundException(name));
}
private boolean byRuleName(final String classname, final String name) {
final String classNameWithDelimiter = "." + name;
return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check");
}
}

View file

@ -0,0 +1,34 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.builder;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Scans a package for all classes.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
interface PackageScanner extends Function<RuleSource, Stream<String>> {
}

View file

@ -103,22 +103,4 @@ public class Rule {
private String getLowerCaseRuleName() { private String getLowerCaseRuleName() {
return getName().toLowerCase(LOCALE); return getName().toLowerCase(LOCALE);
} }
/**
* Returns the canonical name of the class that implements this rule.
*
* @return the canonical name of the implementing class
*/
public String getCanonicalClassName() {
return source.getCheckClasses()
.filter(this::byRuleName)
.findFirst()
.orElseThrow(() -> new CheckstyleClassNotFoundException(name));
}
private boolean byRuleName(final String classname) {
final String classNameWithDelimiter = "." + name;
return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check");
}
} }

View file

@ -0,0 +1,33 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.builder;
import java.util.function.Function;
/**
* Returns the canonical name of the class that implements a {@link Rule}.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
interface RuleClassLocator extends Function<Rule, String> {
}

View file

@ -21,14 +21,8 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import lombok.Getter; import lombok.Getter;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* The origin of the rule. * The origin of the rule.
* *
@ -42,7 +36,6 @@ public enum RuleSource {
@Getter @Getter
private final String basePackage; private final String basePackage;
private final List<String> checkClasses;
/** /**
* Constructor. * Constructor.
@ -51,19 +44,5 @@ public enum RuleSource {
*/ */
RuleSource(final String basePackage) { RuleSource(final String basePackage) {
this.basePackage = basePackage; this.basePackage = basePackage;
try {
checkClasses = ClassPath.from(getClass().getClassLoader())
.getTopLevelClassesRecursive(basePackage)
.stream()
.map(ClassPath.ClassInfo::getName)
.collect(Collectors.toList());
} catch (IOException e) {
throw new CheckstyleSourceLoadException(basePackage, e);
}
}
public Stream<String> getCheckClasses() {
return checkClasses.stream()
.sorted();
} }
} }

View file

@ -6,6 +6,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import java.io.IOException; import java.io.IOException;
@ -19,6 +20,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
/** /**
* Tests for {@link CheckstyleWriter}. * Tests for {@link CheckstyleWriter}.
@ -31,6 +34,12 @@ public class CheckstyleWriterTest {
private static final String FILE_SEPARATOR = System.getProperty("file.separator"); private static final String FILE_SEPARATOR = System.getProperty("file.separator");
@org.junit.Rule
public ExpectedException exception = ExpectedException.none();
@org.junit.Rule
public TemporaryFolder folder = new TemporaryFolder();
private CheckstyleWriter checkstyleWriter; private CheckstyleWriter checkstyleWriter;
private OutputProperties outputProperties; private OutputProperties outputProperties;
@ -49,11 +58,8 @@ public class CheckstyleWriterTest {
private Path checkstyleTemplate; private Path checkstyleTemplate;
@org.junit.Rule @Mock
public ExpectedException exception = ExpectedException.none(); private RuleClassLocator ruleClassLocator;
@org.junit.Rule
public TemporaryFolder folder = new TemporaryFolder();
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -78,7 +84,14 @@ public class CheckstyleWriterTest {
checkstyleTemplate, TEMPLATE.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING); checkstyleTemplate, TEMPLATE.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
templateProperties.setCheckstyleXml(checkstyleTemplate); templateProperties.setCheckstyleXml(checkstyleTemplate);
rulesProperties = new RulesProperties(); rulesProperties = new RulesProperties();
checkstyleWriter = new CheckstyleWriter(outputProperties, templateProperties, rulesProperties); checkstyleWriter =
new CheckstyleWriter(outputProperties, templateProperties, rulesProperties, ruleClassLocator);
given(ruleClassLocator.apply(any())).willReturn(ruleClassname);
}
private Map.Entry<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException {
final String xmlFile = String.format("checkstyle-%s.xml", level.toString());
return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile);
} }
// write rule that matches current level // write rule that matches current level
@ -95,6 +108,23 @@ public class CheckstyleWriterTest {
assertThat(lines).containsExactly("C:", String.format("TW:<module name=\"%s\"/>", ruleClassname)); assertThat(lines).containsExactly("C:", String.format("TW:<module name=\"%s\"/>", ruleClassname));
} }
private List<String> loadOutputFile(final RuleLevel level) throws IOException {
val path = outputDirectory.resolve(outputFiles.get(level));
assertThat(path).as("Output path exists")
.exists();
return Files.readAllLines(path, StandardCharsets.UTF_8);
}
private Rule enabledRule(final RuleLevel level, final RuleParent parent) {
val rule = new Rule();
rule.setName(ruleName);
rule.setSource(RuleSource.CHECKSTYLE);
rule.setEnabled(true);
rule.setLevel(level);
rule.setParent(parent);
return rule;
}
// write rule that is below current level // write rule that is below current level
@Test @Test
public void writeRuleThatIsBelowCurrentLevel() throws Exception { public void writeRuleThatIsBelowCurrentLevel() throws Exception {
@ -192,26 +222,4 @@ public class CheckstyleWriterTest {
//when //when
checkstyleWriter.run(); checkstyleWriter.run();
} }
private Map.Entry<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException {
final String xmlFile = String.format("checkstyle-%s.xml", level.toString());
return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile);
}
private List<String> loadOutputFile(final RuleLevel level) throws IOException {
val path = outputDirectory.resolve(outputFiles.get(level));
assertThat(path).as("Output path exists")
.exists();
return Files.readAllLines(path, StandardCharsets.UTF_8);
}
private Rule enabledRule(final RuleLevel level, final RuleParent parent) {
val rule = new Rule();
rule.setName(ruleName);
rule.setSource(RuleSource.CHECKSTYLE);
rule.setEnabled(true);
rule.setLevel(level);
rule.setParent(parent);
return rule;
}
} }

View file

@ -0,0 +1,46 @@
package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DefaultPackageScanner}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DefaultPackageScannerTest {
private ClassPath classPath;
private DefaultPackageScanner scanner;
@Before
public void setUp() throws Exception {
classPath = ClassPath.from(getClass().getClassLoader());
scanner = new DefaultPackageScanner(classPath);
}
@Test
public void canScanCheckstylePackage() throws IOException {
//when
final Stream<String> result = scanner.apply(RuleSource.CHECKSTYLE);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage()))
.contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck");
}
@Test
public void canScanSevntuPackage() throws IOException {
//when
final Stream<String> result = scanner.apply(RuleSource.SEVNTU);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage()))
.contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck");
}
}

View file

@ -0,0 +1,55 @@
package net.kemitix.checkstyle.ruleset.builder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
/**
* Tests for {@link DefaultRuleClassLocator}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DefaultRuleClassLocatorTest {
private DefaultRuleClassLocator subject;
@Mock
private PackageScanner packageScanner;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
subject = new DefaultRuleClassLocator(packageScanner);
}
@Test
public void canLookupRule() {
//given
final String rulename = "RegexpOnFilename";
final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck";
final List<String> checkstyleClasses = Collections.singletonList(expected);
final List<String> sevntuClasses = Collections.emptyList();
given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(checkstyleClasses.stream());
given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(sevntuClasses.stream());
subject.init();
final Rule rule = createCheckstyleRule(rulename);
//when
final String result = subject.apply(rule);
//then
assertThat(result).isEqualTo(expected);
}
private Rule createCheckstyleRule(final String rulename) {
final Rule rule = new Rule();
rule.setSource(RuleSource.CHECKSTYLE);
rule.setName(rulename);
return rule;
}
}

View file

@ -30,4 +30,16 @@ public class RuleSourceTest {
//then //then
assertThat(values).containsExactlyElementsOf(expected); assertThat(values).containsExactlyElementsOf(expected);
} }
@Test
public void basePackages() {
//given
final String puppycrawl = "puppycrawl";
final String sevntu = "sevntu";
//then
assertThat(RuleSource.CHECKSTYLE.getBasePackage()).contains(puppycrawl)
.doesNotContain(sevntu);
assertThat(RuleSource.SEVNTU.getBasePackage()).contains(sevntu)
.doesNotContain(puppycrawl);
}
} }

View file

@ -63,4 +63,3 @@
</module><!-- /TreeWalker --> </module><!-- /TreeWalker -->
</module><!-- /Checker --> </module><!-- /Checker -->

View file

@ -95,4 +95,3 @@
</module><!-- /TreeWalker --> </module><!-- /TreeWalker -->
</module><!-- /Checker --> </module><!-- /Checker -->

View file

@ -121,4 +121,3 @@
</module><!-- /TreeWalker --> </module><!-- /TreeWalker -->
</module><!-- /Checker --> </module><!-- /Checker -->

View file

@ -180,4 +180,3 @@
</module><!-- /TreeWalker --> </module><!-- /TreeWalker -->
</module><!-- /Checker --> </module><!-- /Checker -->

View file

@ -233,4 +233,3 @@
</module><!-- /TreeWalker --> </module><!-- /TreeWalker -->
</module><!-- /Checker --> </module><!-- /Checker -->