Update builder tests

This commit is contained in:
Paul Campbell 2018-01-03 20:35:56 +00:00
parent 372b8e0f3c
commit 87358bfe92
6 changed files with 83 additions and 20 deletions

View file

@ -22,7 +22,6 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.speedment.common.mapstream.MapStream; import com.speedment.common.mapstream.MapStream;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import lombok.val; import lombok.val;
@ -58,13 +57,13 @@ class CheckstyleWriter implements CommandLineRunner {
private final RuleClassLocator ruleClassLocator; private final RuleClassLocator ruleClassLocator;
@Override @Override
public void run(final String... args) throws Exception { public void run(final String... args) {
Stream.of(RuleLevel.values()) Stream.of(RuleLevel.values())
.filter(level -> !level.equals(RuleLevel.UNSPECIFIED)) .filter(level -> !level.equals(RuleLevel.UNSPECIFIED))
.forEach(this::writeCheckstyleFile); .forEach(this::writeCheckstyleFile);
} }
private void writeCheckstyleFile(@NonNull final RuleLevel ruleLevel) { private void writeCheckstyleFile(final RuleLevel ruleLevel) {
val xmlFile = outputProperties.getDirectory() val xmlFile = outputProperties.getDirectory()
.resolve(outputProperties.getRulesetFiles() .resolve(outputProperties.getRulesetFiles()
.get(ruleLevel)); .get(ruleLevel));

View file

@ -28,6 +28,7 @@ import javax.annotation.PostConstruct;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -62,6 +63,7 @@ public class DefaultRuleClassLocator implements RuleClassLocator {
} }
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) return checkClasses.get(source)
.stream() .stream()
.sorted() .sorted()

View file

@ -7,8 +7,6 @@ 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 java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; 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.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link CheckstyleWriter}. * Tests for {@link CheckstyleWriter}.
@ -58,12 +57,10 @@ public class CheckstyleWriterTest {
private Path outputDirectory; private Path outputDirectory;
@Mock private RuleClassLocator ruleClassLocator = mock(RuleClassLocator.class);
private RuleClassLocator ruleClassLocator;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
ruleName = "RegexpOnFilename"; ruleName = "RegexpOnFilename";
ruleClassname = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck"; ruleClassname = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck";
outputProperties = new OutputProperties(); outputProperties = new OutputProperties();
@ -90,7 +87,7 @@ public class CheckstyleWriterTest {
given(ruleClassLocator.apply(any())).willReturn(ruleClassname); 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()); final String xmlFile = String.format("checkstyle-%s.xml", level.toString());
return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile); return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile);
} }
@ -202,7 +199,7 @@ public class CheckstyleWriterTest {
// throw RTE if template not found // throw RTE if template not found
@Test @Test
public void throwRteIfTemplateNotFound() throws Exception { public void throwRteIfTemplateNotFound() {
//given //given
templateProperties.setCheckstyleXml(Paths.get("garbage")); templateProperties.setCheckstyleXml(Paths.get("garbage"));
//when //when
@ -214,7 +211,7 @@ public class CheckstyleWriterTest {
// throw RTE if error writing file // throw RTE if error writing file
@Test @Test
public void throwRteIfErrorWritingFile() throws Exception { public void throwRteIfErrorWritingFile() {
//given //given
final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary"); final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary");
outputProperties.setDirectory(Paths.get(imaginary)); outputProperties.setDirectory(Paths.get(imaginary));

View file

@ -2,14 +2,15 @@ package net.kemitix.checkstyle.ruleset.builder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; 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.BDDMockito.given;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link DefaultRuleClassLocator}. * Tests for {@link DefaultRuleClassLocator}.
@ -20,17 +21,15 @@ public class DefaultRuleClassLocatorTest {
private DefaultRuleClassLocator subject; private DefaultRuleClassLocator subject;
@Mock private PackageScanner packageScanner = mock(PackageScanner.class);
private PackageScanner packageScanner;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
subject = new DefaultRuleClassLocator(packageScanner); subject = new DefaultRuleClassLocator(packageScanner);
} }
@Test @Test
public void canLookupRule() { public void canLookupRuleWithClassNameEndingInCheck() {
//given //given
final String rulename = "RegexpOnFilename"; final String rulename = "RegexpOnFilename";
final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck"; final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck";
@ -46,6 +45,47 @@ public class DefaultRuleClassLocatorTest {
assertThat(result).isEqualTo(expected); 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) { private Rule createCheckstyleRule(final String rulename) {
final Rule rule = new Rule(); final Rule rule = new Rule();
rule.setSource(RuleSource.CHECKSTYLE); rule.setSource(RuleSource.CHECKSTYLE);

View file

@ -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());
}
}

View file

@ -15,16 +15,16 @@ public class RulesPropertiesTest {
private RulesProperties rulesProperties; private RulesProperties rulesProperties;
@Before @Before
public void setUp() throws Exception { public void setUp() {
rulesProperties = new RulesProperties(); rulesProperties = new RulesProperties();
} }
@Test @Test
public void getEmpty() throws Exception { public void getEmpty() {
assertThat(rulesProperties.getRules()).isEmpty(); assertThat(rulesProperties.getRules()).isEmpty();
} }
@Test @Test
public void getContent() throws Exception { public void getContent() {
//given //given
final Rule rule = new Rule(); final Rule rule = new Rule();
//when //when