Remove use of @PostContruct
This commit is contained in:
parent
9be6ca763a
commit
d3f86fc318
3 changed files with 61 additions and 49 deletions
|
@ -26,6 +26,12 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Configuration for Builder.
|
||||
|
@ -46,4 +52,35 @@ public class BuilderConfiguration {
|
|||
public ClassPath classPath() throws IOException {
|
||||
return ClassPath.from(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* A Map of rules for each RuleSource.
|
||||
*
|
||||
* @param packageScanner the PackageScanner
|
||||
*
|
||||
* @return a Map with a list of check classes for each rule source
|
||||
*/
|
||||
@Bean
|
||||
public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) {
|
||||
return Stream.of(RuleSource.values())
|
||||
.map(toRuleSourceEntry(packageScanner))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
private static Function<RuleSource, AbstractMap.SimpleEntry<RuleSource, List<String>>> toRuleSourceEntry(
|
||||
final PackageScanner packageScanner
|
||||
) {
|
||||
return source -> new AbstractMap.SimpleEntry<>(
|
||||
source,
|
||||
classesInSource(source, packageScanner)
|
||||
);
|
||||
}
|
||||
|
||||
private static List<String> classesInSource(
|
||||
final RuleSource source,
|
||||
final PackageScanner packageScanner
|
||||
) {
|
||||
return packageScanner.apply(source)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,13 +24,9 @@ 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.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link RuleClassLocator}.
|
||||
|
@ -41,38 +37,30 @@ import java.util.stream.Stream;
|
|||
@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));
|
||||
}
|
||||
private final Map<RuleSource, List<String>> checkClasses;
|
||||
|
||||
@Override
|
||||
public final String apply(final Rule rule) {
|
||||
return getCanonicalClassName(rule.getSource(), rule.getName());
|
||||
}
|
||||
|
||||
private String getCanonicalClassName(final RuleSource source, final String name) {
|
||||
private String getCanonicalClassName(
|
||||
final RuleSource source,
|
||||
final String name
|
||||
) {
|
||||
Objects.requireNonNull(checkClasses, "init() method not called");
|
||||
return checkClasses.get(source)
|
||||
.stream()
|
||||
.sorted()
|
||||
.filter(classname -> byRuleName(classname, name))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new CheckstyleClassNotFoundException(name));
|
||||
.stream()
|
||||
.sorted()
|
||||
.filter(classname -> byRuleName(classname, name))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new CheckstyleClassNotFoundException(name));
|
||||
}
|
||||
|
||||
private boolean byRuleName(final String classname, final String name) {
|
||||
private boolean byRuleName(
|
||||
final String classname,
|
||||
final String name
|
||||
) {
|
||||
final String classNameWithDelimiter = "." + name;
|
||||
return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check");
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
|
||||
|
@ -19,14 +21,9 @@ import static org.mockito.Mockito.mock;
|
|||
*/
|
||||
public class DefaultRuleClassLocatorTest {
|
||||
|
||||
private DefaultRuleClassLocator subject;
|
||||
private final Map<RuleSource, List<String>> checkClasses = new HashMap<>();
|
||||
|
||||
private PackageScanner packageScanner = mock(PackageScanner.class);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
subject = new DefaultRuleClassLocator(packageScanner);
|
||||
}
|
||||
private final DefaultRuleClassLocator subject = new DefaultRuleClassLocator(checkClasses);
|
||||
|
||||
@Test
|
||||
public void canLookupRuleWithClassNameEndingInCheck() {
|
||||
|
@ -35,9 +32,8 @@ public class DefaultRuleClassLocatorTest {
|
|||
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();
|
||||
checkClasses.put(RuleSource.CHECKSTYLE, checkstyleClasses);
|
||||
checkClasses.put(RuleSource.SEVNTU, sevntuClasses);
|
||||
final Rule rule = createCheckstyleRule(rulename);
|
||||
//when
|
||||
final String result = subject.apply(rule);
|
||||
|
@ -52,9 +48,8 @@ public class DefaultRuleClassLocatorTest {
|
|||
final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilename";
|
||||
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();
|
||||
checkClasses.put(RuleSource.CHECKSTYLE, checkstyleClasses);
|
||||
checkClasses.put(RuleSource.SEVNTU, sevntuClasses);
|
||||
final Rule rule = createCheckstyleRule(rulename);
|
||||
//when
|
||||
final String result = subject.apply(rule);
|
||||
|
@ -69,9 +64,8 @@ public class DefaultRuleClassLocatorTest {
|
|||
final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameNoMatch";
|
||||
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();
|
||||
checkClasses.put(RuleSource.CHECKSTYLE, checkstyleClasses);
|
||||
checkClasses.put(RuleSource.SEVNTU, sevntuClasses);
|
||||
final Rule rule = createCheckstyleRule(rulename);
|
||||
//then
|
||||
assertThatThrownBy(() -> subject.apply(rule))
|
||||
|
@ -79,13 +73,6 @@ public class DefaultRuleClassLocatorTest {
|
|||
.hasMessage("No class found to implement RegexpOnFilename");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwsNullPointerExceptionWhenInitNotCalled() {
|
||||
assertThatNullPointerException()
|
||||
.isThrownBy(() -> subject.apply(new Rule()))
|
||||
.withMessage("init() method not called");
|
||||
}
|
||||
|
||||
private Rule createCheckstyleRule(final String rulename) {
|
||||
final Rule rule = new Rule();
|
||||
rule.setSource(RuleSource.CHECKSTYLE);
|
||||
|
|
Loading…
Reference in a new issue