Update builder tests
This commit is contained in:
parent
372b8e0f3c
commit
87358bfe92
6 changed files with 83 additions and 20 deletions
|
@ -22,7 +22,6 @@
|
|||
package net.kemitix.checkstyle.ruleset.builder;
|
||||
|
||||
import com.speedment.common.mapstream.MapStream;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.val;
|
||||
|
@ -58,13 +57,13 @@ class CheckstyleWriter implements CommandLineRunner {
|
|||
private final RuleClassLocator ruleClassLocator;
|
||||
|
||||
@Override
|
||||
public void run(final String... args) throws Exception {
|
||||
public void run(final String... args) {
|
||||
Stream.of(RuleLevel.values())
|
||||
.filter(level -> !level.equals(RuleLevel.UNSPECIFIED))
|
||||
.forEach(this::writeCheckstyleFile);
|
||||
}
|
||||
|
||||
private void writeCheckstyleFile(@NonNull final RuleLevel ruleLevel) {
|
||||
private void writeCheckstyleFile(final RuleLevel ruleLevel) {
|
||||
val xmlFile = outputProperties.getDirectory()
|
||||
.resolve(outputProperties.getRulesetFiles()
|
||||
.get(ruleLevel));
|
||||
|
|
|
@ -28,6 +28,7 @@ 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;
|
||||
|
||||
|
@ -62,6 +63,7 @@ public class DefaultRuleClassLocator implements RuleClassLocator {
|
|||
}
|
||||
|
||||
private String getCanonicalClassName(final RuleSource source, final String name) {
|
||||
Objects.requireNonNull(checkClasses, "init() method not called");
|
||||
return checkClasses.get(source)
|
||||
.stream()
|
||||
.sorted()
|
||||
|
|
|
@ -7,8 +7,6 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -24,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link CheckstyleWriter}.
|
||||
|
@ -58,12 +57,10 @@ public class CheckstyleWriterTest {
|
|||
|
||||
private Path outputDirectory;
|
||||
|
||||
@Mock
|
||||
private RuleClassLocator ruleClassLocator;
|
||||
private RuleClassLocator ruleClassLocator = mock(RuleClassLocator.class);
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ruleName = "RegexpOnFilename";
|
||||
ruleClassname = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck";
|
||||
outputProperties = new OutputProperties();
|
||||
|
@ -90,7 +87,7 @@ public class CheckstyleWriterTest {
|
|||
given(ruleClassLocator.apply(any())).willReturn(ruleClassname);
|
||||
}
|
||||
|
||||
private Map.Entry<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException {
|
||||
private Map.Entry<RuleLevel, String> getOutputFile(final RuleLevel level) {
|
||||
final String xmlFile = String.format("checkstyle-%s.xml", level.toString());
|
||||
return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile);
|
||||
}
|
||||
|
@ -202,7 +199,7 @@ public class CheckstyleWriterTest {
|
|||
|
||||
// throw RTE if template not found
|
||||
@Test
|
||||
public void throwRteIfTemplateNotFound() throws Exception {
|
||||
public void throwRteIfTemplateNotFound() {
|
||||
//given
|
||||
templateProperties.setCheckstyleXml(Paths.get("garbage"));
|
||||
//when
|
||||
|
@ -214,7 +211,7 @@ public class CheckstyleWriterTest {
|
|||
|
||||
// throw RTE if error writing file
|
||||
@Test
|
||||
public void throwRteIfErrorWritingFile() throws Exception {
|
||||
public void throwRteIfErrorWritingFile() {
|
||||
//given
|
||||
final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary");
|
||||
outputProperties.setDirectory(Paths.get(imaginary));
|
||||
|
|
|
@ -2,14 +2,15 @@ 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.assertj.core.api.Assertions.assertThatNullPointerException;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultRuleClassLocator}.
|
||||
|
@ -20,17 +21,15 @@ public class DefaultRuleClassLocatorTest {
|
|||
|
||||
private DefaultRuleClassLocator subject;
|
||||
|
||||
@Mock
|
||||
private PackageScanner packageScanner;
|
||||
private PackageScanner packageScanner = mock(PackageScanner.class);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
subject = new DefaultRuleClassLocator(packageScanner);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canLookupRule() {
|
||||
public void canLookupRuleWithClassNameEndingInCheck() {
|
||||
//given
|
||||
final String rulename = "RegexpOnFilename";
|
||||
final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck";
|
||||
|
@ -46,6 +45,47 @@ public class DefaultRuleClassLocatorTest {
|
|||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canLookupRuleWithClassNameMatchingRuleName() {
|
||||
//given
|
||||
final String rulename = "RegexpOnFilename";
|
||||
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();
|
||||
final Rule rule = createCheckstyleRule(rulename);
|
||||
//when
|
||||
final String result = subject.apply(rule);
|
||||
//then
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwsExceptionWhenCheckstyleClassNotFound() {
|
||||
//given
|
||||
final String rulename = "RegexpOnFilename";
|
||||
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();
|
||||
final Rule rule = createCheckstyleRule(rulename);
|
||||
//then
|
||||
assertThatThrownBy(() -> subject.apply(rule))
|
||||
.isInstanceOf(CheckstyleClassNotFoundException.class)
|
||||
.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);
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package net.kemitix.checkstyle.ruleset.builder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RuleLevelTest {
|
||||
|
||||
private final String[] ruleLevelStrings =
|
||||
{"DISABLED", "LAYOUT", "NAMING", "JAVADOC", "TWEAKS", "COMPLEXITY", "UNSPECIFIED"};
|
||||
|
||||
@Test
|
||||
public void valuesToString() {
|
||||
assertThat(Stream.of(RuleLevel.values())
|
||||
.map(RuleLevel::toString)).containsExactly(ruleLevelStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringValueOf() {
|
||||
assertThat(Stream.of(ruleLevelStrings)
|
||||
.map(RuleLevel::valueOf)).containsExactly(RuleLevel.values());
|
||||
}
|
||||
}
|
|
@ -15,16 +15,16 @@ public class RulesPropertiesTest {
|
|||
private RulesProperties rulesProperties;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
rulesProperties = new RulesProperties();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEmpty() throws Exception {
|
||||
public void getEmpty() {
|
||||
assertThat(rulesProperties.getRules()).isEmpty();
|
||||
}
|
||||
@Test
|
||||
public void getContent() throws Exception {
|
||||
public void getContent() {
|
||||
//given
|
||||
final Rule rule = new Rule();
|
||||
//when
|
||||
|
|
Loading…
Reference in a new issue