Skip to content

Commit

Permalink
Issue #45: resolve LineLength issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Dec 27, 2016
1 parent 6be73c4 commit 6039316
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 91 deletions.
1 change: 0 additions & 1 deletion checkstyle-sonar-plugin/config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<suppress checks="MemberName" files=".*[\/]src[\/]test[\/]"/>
<suppress checks="RegexpMultiline" files=".*[\/]src[\/]test[\/]"/>
<suppress checks="AnonInnerLength" files=".*[\/]src[\/]test[\/]"/>
<suppress checks="LineLength" files=".*[\/]src[\/]test[\/]"/>
<suppress checks="TrailingComment" files=".*[\/]src[\/]test[\/]"/>
<suppress checks="EmptyLineSeparator" files=".*[\/]src[\/]test[\/]"/>
<!-- END of legacy code -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public class CheckstyleAuditListenerTest {

private File file = new File("file1");
private AuditEvent event =
new AuditEvent(this, file.getAbsolutePath(), new LocalizedMessage(42, "", "", null, "", CheckstyleAuditListenerTest.class, "msg"));
new AuditEvent(this, file.getAbsolutePath(),
new LocalizedMessage(42, "", "", null, "",
CheckstyleAuditListenerTest.class, "msg"));
private DefaultFileSystem fs = new DefaultFileSystem(new File(""));
private RuleFinder ruleFinder = mock(RuleFinder.class);
private DefaultInputFile inputFile = new DefaultInputFile("", file.getPath());
Expand All @@ -62,15 +64,21 @@ public void before() {
public void testUtilityMethods() {
AuditEvent event;

event = new AuditEvent(this, "", new LocalizedMessage(0, "", "", null, "", CheckstyleAuditListenerTest.class, "msg"));
event = new AuditEvent(this, "",
new LocalizedMessage(0, "", "", null, "",
CheckstyleAuditListenerTest.class, "msg"));
assertThat(CheckstyleAuditListener.getLineId(event)).isNull();
assertThat(CheckstyleAuditListener.getMessage(event)).isEqualTo("msg");
assertThat(CheckstyleAuditListener.getRuleKey(event)).isEqualTo(CheckstyleAuditListenerTest.class.getName());
assertThat(CheckstyleAuditListener.getRuleKey(event))
.isEqualTo(CheckstyleAuditListenerTest.class.getName());

event = new AuditEvent(this, "", new LocalizedMessage(1, "", "", null, "", CheckstyleAuditListenerTest.class, "msg"));
event = new AuditEvent(this, "",
new LocalizedMessage(1, "", "", null, "",
CheckstyleAuditListenerTest.class, "msg"));
assertThat(CheckstyleAuditListener.getLineId(event)).isEqualTo(1);
assertThat(CheckstyleAuditListener.getMessage(event)).isEqualTo("msg");
assertThat(CheckstyleAuditListener.getRuleKey(event)).isEqualTo(CheckstyleAuditListenerTest.class.getName());
assertThat(CheckstyleAuditListener.getRuleKey(event))
.isEqualTo(CheckstyleAuditListenerTest.class.getName());

event = new AuditEvent(this);
assertThat(CheckstyleAuditListener.getLineId(event)).isNull();
Expand Down Expand Up @@ -124,7 +132,8 @@ private CheckstyleAuditListener addErrorToListener() {
private Rule setupRule(String repo, String key) {
Rule rule = mock(Rule.class);
when(rule.ruleKey()).thenReturn(RuleKey.of(repo, key));
when(ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, CheckstyleAuditListenerTest.class.getCanonicalName()))
when(ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY,
CheckstyleAuditListenerTest.class.getCanonicalName()))
.thenReturn(rule);
return rule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,30 @@ public void beforeClass() {
@Test
public void getSourceFiles() {
CheckstyleProfileExporter exporter = new FakeExporter();
CheckstyleConfiguration configuration = new CheckstyleConfiguration(null, exporter, null, fileSystem);
CheckstyleConfiguration configuration = new CheckstyleConfiguration(null, exporter,
null, fileSystem);
assertThat(configuration.getSourceFiles()).hasSize(1);
assertThat(configuration.getSourceFiles().iterator().next().toString()).contains("mainFile");
}

@Test
public void getTargetXmlReport() {
Settings conf = new Settings();
CheckstyleConfiguration configuration = new CheckstyleConfiguration(conf, null, null, fileSystem);
CheckstyleConfiguration configuration = new CheckstyleConfiguration(conf, null,
null, fileSystem);
assertThat(configuration.getTargetXmlReport()).isNull();

conf.setProperty(CheckstyleConfiguration.PROPERTY_GENERATE_XML, "true");
configuration = new CheckstyleConfiguration(conf, null, null, fileSystem);
assertThat(configuration.getTargetXmlReport()).isEqualTo(new File(fileSystem.workDir(), "checkstyle-result.xml"));
assertThat(configuration.getTargetXmlReport()).isEqualTo(new File(fileSystem.workDir(),
"checkstyle-result.xml"));
}

@Test
public void writeConfigurationToWorkingDir() throws IOException {
CheckstyleProfileExporter exporter = new FakeExporter();
CheckstyleConfiguration configuration = new CheckstyleConfiguration(null, exporter, null, fileSystem);
CheckstyleConfiguration configuration = new CheckstyleConfiguration(null, exporter,
null, fileSystem);
File xmlFile = configuration.getXmlDefinitionFile();

assertThat(xmlFile.exists()).isTrue();
Expand All @@ -91,16 +95,19 @@ public void writeConfigurationToWorkingDir() throws IOException {
@Test
public void getCheckstyleConfiguration() throws IOException, CheckstyleException {
fileSystem.setEncoding(StandardCharsets.UTF_8);
Settings settings = new Settings(new PropertyDefinitions(new CheckstylePlugin().getExtensions()));
settings.setProperty(CheckstyleConstants.FILTERS_KEY, CheckstyleConstants.FILTERS_DEFAULT_VALUE);
Settings settings =
new Settings(new PropertyDefinitions(new CheckstylePlugin().getExtensions()));
settings.setProperty(CheckstyleConstants.FILTERS_KEY,
CheckstyleConstants.FILTERS_DEFAULT_VALUE);

RulesProfile profile = RulesProfile.create("sonar way", "java");

Rule rule = Rule.create("checkstyle", "CheckStyleRule1", "checkstyle rule one");
rule.setConfigKey("checkstyle/rule1");
profile.activateRule(rule, null);

CheckstyleConfiguration configuration = new CheckstyleConfiguration(settings, new CheckstyleProfileExporter(settings), profile, fileSystem);
CheckstyleConfiguration configuration = new CheckstyleConfiguration(settings,
new CheckstyleProfileExporter(settings), profile, fileSystem);
Configuration checkstyleConfiguration = configuration.getCheckstyleConfiguration();
assertThat(checkstyleConfiguration).isNotNull();
assertThat(checkstyleConfiguration.getAttribute("charset")).isEqualTo("UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class CheckstyleConstantsTest {

@Test
public void privateConstructor() throws ReflectiveOperationException {
Constructor<CheckstyleConstants> constructor = CheckstyleConstants.class.getDeclaredConstructor();
Constructor<CheckstyleConstants> constructor =
CheckstyleConstants.class.getDeclaredConstructor();
assertThat(constructor.isAccessible()).isFalse();
constructor.setAccessible(true);
constructor.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public class CheckstyleExecutorTest {
public void execute() throws CheckstyleException {
CheckstyleConfiguration conf = mockConf();
CheckstyleAuditListener listener = mockListener();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, createJavaResourceLocator());
CheckstyleExecutor executor =
new CheckstyleExecutor(conf, listener, createJavaResourceLocator());
executor.execute();

verify(listener, times(1)).auditStarted(any(AuditEvent.class));
Expand All @@ -80,23 +81,27 @@ public void execute() throws CheckstyleException {
verify(listener, atLeast(1)).addError(captor.capture());
AuditEvent event = captor.getValue();
assertThat(event.getFileName()).matches(".*Hello.java");
assertThat(event.getSourceName()).matches("com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck");
assertThat(event.getSourceName())
.matches("com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck");
}

@Test
public void executeException() throws CheckstyleException {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Can not execute Checkstyle");
CheckstyleConfiguration conf = mockConf();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, null, createJavaResourceLocator());
CheckstyleExecutor executor =
new CheckstyleExecutor(conf, null, createJavaResourceLocator());
executor.execute();
}

@Test
public void getUrlException() throws URISyntaxException {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Fail to create the project classloader. Classpath element is invalid: htp://aa");
CheckstyleExecutor executor = new CheckstyleExecutor(null, null, createJavaResourceLocator());
thrown.expectMessage("Fail to create the project classloader. "
+ "Classpath element is invalid: htp://aa");
CheckstyleExecutor executor =
new CheckstyleExecutor(null, null, createJavaResourceLocator());
executor.getUrl(new URI("htp://aa"));
}

Expand All @@ -116,7 +121,8 @@ public void canGenerateXmlReportInEnglish() throws CheckstyleException, IOExcept
File report = new File("target/test-tmp/checkstyle-report.xml");
when(conf.getTargetXmlReport()).thenReturn(report);
CheckstyleAuditListener listener = mockListener();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, createJavaResourceLocator());
CheckstyleExecutor executor =
new CheckstyleExecutor(conf, listener, createJavaResourceLocator());
executor.execute();

assertThat(report.exists(), is(true));
Expand All @@ -137,8 +143,14 @@ private static CheckstyleAuditListener mockListener() {
private static CheckstyleConfiguration mockConf() throws CheckstyleException {
CheckstyleConfiguration conf = mock(CheckstyleConfiguration.class);
when(conf.getCharset()).thenReturn(Charset.defaultCharset());
when(conf.getCheckstyleConfiguration()).thenReturn(CheckstyleConfiguration.toCheckstyleConfiguration(new File("test-resources/checkstyle-conf.xml")));
when(conf.getSourceFiles()).thenReturn(Arrays.asList(new File("test-resources/Hello.java"), new File("test-resources/World.java")));
when(conf.getCheckstyleConfiguration()).thenReturn(
CheckstyleConfiguration.toCheckstyleConfiguration(
new File("test-resources/checkstyle-conf.xml"))
);
when(conf.getSourceFiles()).thenReturn(
Arrays.asList(new File("test-resources/Hello.java"),
new File("test-resources/World.java"))
);
return conf;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void alwaysSetFileContentsHolderAndSuppressionCommentFilter() {
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/alwaysSetFileContentsHolderAndSuppressionCommentFilter.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "alwaysSetFileContentsHolderAndSuppressionCommentFilter.xml",
sanitizeForTests(writer.toString()));
}

Expand All @@ -62,7 +63,8 @@ public void noCheckstyleRulesToExport() {
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/noCheckstyleRulesToExport.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "noCheckstyleRulesToExport.xml",
sanitizeForTests(writer.toString()));
}

Expand All @@ -71,29 +73,40 @@ public void singleCheckstyleRulesToExport() {
RulesProfile profile = RulesProfile.create("sonar way", "java");
profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
profile.activateRule(
Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc").setConfigKey("Checker/JavadocPackage"),
Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
.setConfigKey("Checker/JavadocPackage"),
RulePriority.MAJOR
);
profile.activateRule(Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck", "Line Length").setConfigKey("Checker/TreeWalker/LineLength"),
profile.activateRule(Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck", "Line Length")
.setConfigKey("Checker/TreeWalker/LineLength"),
RulePriority.CRITICAL);
profile.activateRule(
Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck", "Local Variable").setConfigKey(
"Checker/TreeWalker/Checker/TreeWalker/LocalFinalVariableName"),
Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck",
"Local Variable")
.setConfigKey("Checker/TreeWalker/Checker/TreeWalker/LocalFinalVariableName"),
RulePriority.MINOR);

StringWriter writer = new StringWriter();
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/singleCheckstyleRulesToExport.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "singleCheckstyleRulesToExport.xml",
sanitizeForTests(writer.toString()));
}

@Test
public void addTheIdPropertyWhenManyInstancesWithTheSameConfigKey() {
RulesProfile profile = RulesProfile.create("sonar way", "java");
Rule rule1 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc").setConfigKey("Checker/JavadocPackage");
Rule rule2 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345", "Javadoc").setConfigKey("Checker/JavadocPackage")
Rule rule1 = Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
.setConfigKey("Checker/JavadocPackage");
Rule rule2 = Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345", "Javadoc")
.setConfigKey("Checker/JavadocPackage")
.setParent(rule1);

profile.activateRule(rule1, RulePriority.MAJOR);
Expand All @@ -103,17 +116,20 @@ public void addTheIdPropertyWhenManyInstancesWithTheSameConfigKey() {
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml",
sanitizeForTests(writer.toString()));
}

@Test
public void exportParameters() {
RulesProfile profile = RulesProfile.create("sonar way", "java");
Rule rule = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
Rule rule = Rule.create("checkstyle",
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
.setConfigKey("Checker/JavadocPackage");
rule.createParameter("format");
rule.createParameter("message"); // not set in the profile and no default value => not exported in checkstyle
// not set in the profile and no default value => not exported in checkstyle
rule.createParameter("message");
rule.createParameter("ignore");

profile.activateRule(rule, RulePriority.MAJOR)
Expand All @@ -123,7 +139,8 @@ public void exportParameters() {
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/exportParameters.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "exportParameters.xml",
sanitizeForTests(writer.toString()));
}

Expand All @@ -134,7 +151,8 @@ public void addCustomFilters() {
+ "<property name=\"offCommentFormat\" value=\"BEGIN GENERATED CODE\"/>"
+ "<property name=\"onCommentFormat\" value=\"END GENERATED CODE\"/>" + "</module>"
+ "<module name=\"SuppressWithNearbyCommentFilter\">"
+ "<property name=\"commentFormat\" value=\"CHECKSTYLE IGNORE (\\w+) FOR NEXT (\\d+) LINES\"/>"
+ "<property name=\"commentFormat\""
+ " value=\"CHECKSTYLE IGNORE (\\w+) FOR NEXT (\\d+) LINES\"/>"
+ "<property name=\"checkFormat\" value=\"$1\"/>"
+ "<property name=\"messageFormat\" value=\"$2\"/>"
+ "</module>");
Expand All @@ -144,7 +162,8 @@ public void addCustomFilters() {
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addCustomFilters.xml",
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
+ "addCustomFilters.xml",
sanitizeForTests(writer.toString()));
}

Expand Down
Loading

0 comments on commit 6039316

Please sign in to comment.