From 603931673a7059be965470929a7c9d4d05621069 Mon Sep 17 00:00:00 2001 From: Roman Ivanov Date: Tue, 27 Dec 2016 15:53:36 -0800 Subject: [PATCH] Issue #45: resolve LineLength issues in tests --- .../config/suppressions.xml | 1 - .../CheckstyleAuditListenerTest.java | 21 ++-- .../CheckstyleConfigurationTest.java | 21 ++-- .../checkstyle/CheckstyleConstantsTest.java | 3 +- .../checkstyle/CheckstyleExecutorTest.java | 28 ++++-- .../CheckstyleProfileExporterTest.java | 49 +++++++--- .../CheckstyleProfileImporterTest.java | 97 +++++++++++++------ .../CheckstyleRulesDefinitionTest.java | 6 +- .../checkstyle/CheckstyleSensorTest.java | 6 +- .../CheckstyleSeverityUtilsTest.java | 15 ++- .../checkstyle/CheckstyleTestUtils.java | 3 +- .../checkstyle/internal/CheckUtil.java | 3 +- .../checkstyle/internal/ChecksTest.java | 34 ++++--- 13 files changed, 196 insertions(+), 91 deletions(-) diff --git a/checkstyle-sonar-plugin/config/suppressions.xml b/checkstyle-sonar-plugin/config/suppressions.xml index 381e1d10..df61d526 100644 --- a/checkstyle-sonar-plugin/config/suppressions.xml +++ b/checkstyle-sonar-plugin/config/suppressions.xml @@ -50,7 +50,6 @@ - diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java index 7f37e940..5e5e4bda 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java @@ -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()); @@ -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(); @@ -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; } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java index ce19ae40..57c772a2 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java @@ -61,7 +61,8 @@ 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"); } @@ -69,18 +70,21 @@ public void getSourceFiles() { @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(); @@ -91,8 +95,10 @@ 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"); @@ -100,7 +106,8 @@ public void getCheckstyleConfiguration() throws IOException, CheckstyleException 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"); diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConstantsTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConstantsTest.java index df981d93..37eb4dc2 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConstantsTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConstantsTest.java @@ -29,7 +29,8 @@ public class CheckstyleConstantsTest { @Test public void privateConstructor() throws ReflectiveOperationException { - Constructor constructor = CheckstyleConstants.class.getDeclaredConstructor(); + Constructor constructor = + CheckstyleConstants.class.getDeclaredConstructor(); assertThat(constructor.isAccessible()).isFalse(); constructor.setAccessible(true); constructor.newInstance(); diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java index 7036b387..9bb7deda 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java @@ -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)); @@ -80,7 +81,8 @@ 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 @@ -88,15 +90,18 @@ 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")); } @@ -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)); @@ -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; } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java index 2098b108..f6fa748c 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java @@ -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())); } @@ -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())); } @@ -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); @@ -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) @@ -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())); } @@ -134,7 +151,8 @@ public void addCustomFilters() { + "" + "" + "" + "" - + "" + + "" + "" + "" + ""); @@ -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())); } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java index 50322d81..b3d65985 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java @@ -63,21 +63,28 @@ public void before() { @Test public void importSimpleProfile() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent( + "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); RulesProfile profile = importer.importProfile(reader, messages); assertThat(profile.getActiveRules().size()).isEqualTo(2); - assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode")); - assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); + assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/TreeWalker/EqualsHashCode")); + assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage")); assertThat(messages.hasErrors()).isFalse(); } @Test public void importParameters() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent( + "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); RulesProfile profile = importer.importProfile(reader, messages); - ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage"); assertThat(javadocCheck.getActiveRuleParams()).hasSize(2); assertThat(javadocCheck.getParameter("format")).isEqualTo("abcde"); assertThat(javadocCheck.getParameter("ignore")).isEqualTo("true"); @@ -86,49 +93,68 @@ public void importParameters() { @Test public void propertiesShouldBeInherited() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/inheritance_of_properties.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent( + "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/" + + "inheritance_of_properties.xml")); RulesProfile profile = importer.importProfile(reader, messages); - ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/MissingOverride"); + ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle", + "Checker/TreeWalker/MissingOverride"); assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER); assertThat(activeRule.getParameter("javaFiveCompatibility")).isEqualTo("true"); } @Test public void importPriorities() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent( + "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); RulesProfile profile = importer.importProfile(reader, messages); - ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage"); assertThat(javadocCheck.getSeverity()).isEqualTo(RulePriority.BLOCKER); } @Test public void priorityIsOptional() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/" + + "CheckstyleProfileImporterTest/simple.xml")); RulesProfile profile = importer.importProfile(reader, messages); - ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"); - assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER); // reuse the rule default priority + ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle", + "Checker/TreeWalker/EqualsHashCode"); + // reuse the rule default priority + assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER); } @Test public void idPropertyShouldBeTheRuleKey() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idPropertyShouldBeTheRuleKey.xml")); + Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent( + "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/" + + "idPropertyShouldBeTheRuleKey.xml")); RulesProfile profile = importer.importProfile(reader, messages); - assertNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); + assertNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage")); assertThat(messages.getWarnings().size()).isEqualTo(1); } @Test public void shouldUseTheIdPropertyToFindRule() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/shouldUseTheIdPropertyToFindRule.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/" + + "CheckstyleProfileImporterTest/shouldUseTheIdPropertyToFindRule.xml")); RulesProfile profile = importer.importProfile(reader, messages); - assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); - assertThat(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage").getRule().getKey()) - .isEqualTo("com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345"); + assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage")); + assertThat(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/JavadocPackage").getRule().getKey()) + .isEqualTo("com.puppycrawl.tools.checkstyle.checks.javadoc." + + "JavadocPackageCheck_12345"); assertThat(messages.getWarnings().size()).isEqualTo(0); } @@ -141,11 +167,15 @@ public void testUnvalidXml() { @Test public void importingFiltersIsNotSupported() { - Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml")); + Reader reader = new StringReader( + CheckstyleTestUtils.getResourceContent("/org/sonar/plugins/checkstyle/" + + "CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml")); RulesProfile profile = importer.importProfile(reader, messages); - assertNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/SuppressionCommentFilter")); - assertNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/FileContentsHolder")); + assertNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/SuppressionCommentFilter")); + assertNull(profile.getActiveRuleByConfigKey("checkstyle", + "Checker/TreeWalker/FileContentsHolder")); assertThat(profile.getActiveRules().size()).isEqualTo(2); assertThat(messages.getWarnings().size()).isEqualTo(5); // no warning for FileContentsHolder } @@ -158,25 +188,36 @@ public Rule answer(InvocationOnMock iom) throws Throwable { RuleQuery query = (RuleQuery) iom.getArguments()[0]; Rule rule = null; if (StringUtils.equals(query.getConfigKey(), "Checker/JavadocPackage")) { - rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc Package") + rule = Rule.create(query.getRepositoryKey(), + "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", + "Javadoc Package") .setConfigKey("Checker/JavadocPackage") .setSeverity(RulePriority.MAJOR); rule.createParameter("format"); rule.createParameter("ignore"); - } else if (StringUtils.equals(query.getConfigKey(), "Checker/TreeWalker/EqualsHashCode")) { - rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck", "Equals HashCode") + } else if (StringUtils.equals(query.getConfigKey(), + "Checker/TreeWalker/EqualsHashCode")) { + rule = Rule.create(query.getRepositoryKey(), + "com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck", + "Equals HashCode") .setConfigKey("Checker/TreeWalker/EqualsHashCode") .setSeverity(RulePriority.BLOCKER); - } else if (StringUtils.equals(query.getKey(), "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345")) { - rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345", "Javadoc Package") + } else if (StringUtils.equals(query.getKey(), + "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345")) { + rule = Rule.create(query.getRepositoryKey(), + "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345", + "Javadoc Package") .setConfigKey("Checker/JavadocPackage") .setSeverity(RulePriority.MAJOR); rule.createParameter("format"); rule.createParameter("ignore"); - } else if (StringUtils.equals(query.getConfigKey(), "Checker/TreeWalker/MissingOverride")) { - rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck", "Missing Override") + } else if (StringUtils.equals(query.getConfigKey(), + "Checker/TreeWalker/MissingOverride")) { + rule = Rule.create(query.getRepositoryKey(), + "com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck", + "Missing Override") .setConfigKey("Checker/TreeWalker/MissingOverride") .setSeverity(RulePriority.MINOR); rule.createParameter("javaFiveCompatibility"); diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinitionTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinitionTest.java index bedbd8b5..40bd5518 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinitionTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinitionTest.java @@ -68,7 +68,8 @@ public void test() { for (RulesDefinition.Param param : rule.params()) { assertThat(param.name()).isNotNull(); assertThat(param.description()) - .overridingErrorMessage("Description is not set for parameter '" + param.name() + "' of rule '" + rule.key()) + .overridingErrorMessage("Description is not set for parameter '" + param.name() + + "' of rule '" + rule.key()) .isNotNull(); } @@ -81,7 +82,8 @@ public void test() { .isNull(); } else { assertThat(rule.debtRemediationFunction()) - .overridingErrorMessage("Sqale remediation function is not set for rule '" + rule.key()) + .overridingErrorMessage("Sqale remediation function is not set for rule '" + + rule.key()) .isNotNull(); assertThat(rule.debtSubCharacteristic()) .overridingErrorMessage("Sqale characteristic is not set for rule '" + rule.key()) diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSensorTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSensorTest.java index 85fc0fd8..07c0f536 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSensorTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSensorTest.java @@ -64,7 +64,8 @@ public void shouldExecuteOnProjectWithJavaFilesAndRules() { @Test public void testToString() { - assertThat(new CheckstyleSensor(null, null, null).toString()).isEqualTo("CheckstyleSensor"); + assertThat(new CheckstyleSensor(null, null, null).toString()) + .isEqualTo("CheckstyleSensor"); } private void addOneJavaFile() { @@ -74,7 +75,8 @@ private void addOneJavaFile() { } private void addOneActiveRule() { - when(profile.getActiveRulesByRepository("checkstyle")).thenReturn(ImmutableList.of(mock(ActiveRule.class))); + when(profile.getActiveRulesByRepository("checkstyle")) + .thenReturn(ImmutableList.of(mock(ActiveRule.class))); } } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java index ae47e56c..78dc960c 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java @@ -30,8 +30,10 @@ public class CheckstyleSeverityUtilsTest { @Test public void testToSeverity() { - assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.BLOCKER)).isEqualTo("error"); - assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.CRITICAL)).isEqualTo("error"); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.BLOCKER)) + .isEqualTo("error"); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.CRITICAL)) + .isEqualTo("error"); assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.MAJOR)).isEqualTo("warning"); assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.MINOR)).isEqualTo("info"); assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.INFO)).isEqualTo("info"); @@ -39,8 +41,10 @@ public void testToSeverity() { @Test public void testFromSeverity() { - assertThat(CheckstyleSeverityUtils.fromSeverity("error")).isEqualTo(RulePriority.BLOCKER); - assertThat(CheckstyleSeverityUtils.fromSeverity("warning")).isEqualTo(RulePriority.MAJOR); + assertThat(CheckstyleSeverityUtils.fromSeverity("error")) + .isEqualTo(RulePriority.BLOCKER); + assertThat(CheckstyleSeverityUtils.fromSeverity("warning")) + .isEqualTo(RulePriority.MAJOR); assertThat(CheckstyleSeverityUtils.fromSeverity("info")).isEqualTo(RulePriority.INFO); assertThat(CheckstyleSeverityUtils.fromSeverity("ignore")).isEqualTo(RulePriority.INFO); assertThat(CheckstyleSeverityUtils.fromSeverity("")).isNull(); @@ -48,7 +52,8 @@ public void testFromSeverity() { @Test public void privateConstructor() throws ReflectiveOperationException { - Constructor constructor = CheckstyleSeverityUtils.class.getDeclaredConstructor(); + Constructor constructor = + CheckstyleSeverityUtils.class.getDeclaredConstructor(); assertThat(constructor.isAccessible()).isFalse(); constructor.setAccessible(true); constructor.newInstance(); diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleTestUtils.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleTestUtils.java index c7b6390e..d272a38a 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleTestUtils.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleTestUtils.java @@ -39,7 +39,8 @@ private CheckstyleTestUtils() { public static String getResourceContent(String path) { try { - return Resources.toString(Resources.getResource(CheckstyleTestUtils.class, path), Charsets.UTF_8); + return Resources.toString(Resources.getResource(CheckstyleTestUtils.class, path), + Charsets.UTF_8); } catch (IOException e) { throw new IllegalArgumentException("Could not load resource " + path, e); } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java index 6f6d1eb3..b2531553 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java @@ -168,7 +168,8 @@ public static String getCheckMessage(Class module, String messageKey, catch (IOException ex) { return null; } - final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), Locale.ENGLISH); + final MessageFormat formatter = + new MessageFormat(pr.getProperty(messageKey), Locale.ENGLISH); return formatter.format(arguments); } } diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/ChecksTest.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/ChecksTest.java index f37032be..b83edde8 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/ChecksTest.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/ChecksTest.java @@ -50,11 +50,14 @@ import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck; public final class ChecksTest { - private static final String RULES_PATH = "src/main/resources/org/sonar/plugins/checkstyle/rules.xml"; - private static final String MODULE_PROPERTIES_PATH = "src/main/resources/org/sonar/l10n/checkstyle.properties"; + private static final String RULES_PATH = + "src/main/resources/org/sonar/plugins/checkstyle/rules.xml"; + private static final String MODULE_PROPERTIES_PATH = + "src/main/resources/org/sonar/l10n/checkstyle.properties"; private static final Set CHECK_PROPERTIES = getProperties(AbstractCheck.class); - private static final Set JAVADOC_CHECK_PROPERTIES = getProperties(AbstractJavadocCheck.class); + private static final Set JAVADOC_CHECK_PROPERTIES = + getProperties(AbstractJavadocCheck.class); private static final Set FILESET_PROPERTIES = getProperties(AbstractFileSetCheck.class); private static final List UNDOCUMENTED_PROPERTIES = Arrays.asList( @@ -81,7 +84,8 @@ public void verifyTestConfigurationFiles() throws Exception { validateSonarProperties(new HashSet<>(modules)); } - private static void validateSonarRules(Set> modules) throws ParserConfigurationException, IOException { + private static void validateSonarRules(Set> modules) + throws ParserConfigurationException, IOException { final File rulesFile = new File(RULES_PATH); Assert.assertTrue("'rules.xml' must exist", rulesFile.exists()); @@ -132,7 +136,8 @@ private static void validateSonarRules(Document document, Set> modules) expectedConfigKey = "Checker/" + moduleSimpleName.replaceAll("Check$", ""); } - Assert.assertNotNull(moduleName + " requires a configKey in sonar rules", configKey); + Assert.assertNotNull(moduleName + + " requires a configKey in sonar rules", configKey); Assert.assertEquals(moduleName + " requires a valid configKey in sonar rules", expectedConfigKey, configKey.getTextContent()); @@ -164,8 +169,8 @@ private static void validateSonarRuleProperties(Class module, Set param + " requires a valid key for unknown parameter in sonar rules", paramKey.isEmpty()); - Assert.assertTrue(moduleName + " has an unknown parameter in sonar rules: " + paramKey, - properties.remove(paramKey)); + Assert.assertTrue(moduleName + " has an unknown parameter in sonar rules: " + + paramKey, properties.remove(paramKey)); } for (String property : properties) { @@ -192,17 +197,17 @@ private static void validateSonarProperties(Properties properties, Set> for (Object key : new TreeSet<>(properties.keySet())) { final String keyName = key.toString(); - Assert.assertTrue("sonar properties must start with 'rule.checkstyle.': " + keyName, - keyName.startsWith("rule.checkstyle.")); + Assert.assertTrue("sonar properties must start with 'rule.checkstyle.': " + + keyName, keyName.startsWith("rule.checkstyle.")); final String keyValue = properties.get(keyName).toString(); Assert.assertFalse("sonar properties value must not be empty: " + keyName, keyValue.isEmpty()); - Assert.assertFalse("sonar properties value must not have single quote: " + keyName, - keyValue.matches(".*[^'{}]'[^'{}].*")); - Assert.assertFalse("sonar properties value must not have unescaped braces: " + keyName, - keyValue.matches(".*[^'](\\{|\\})[^'].*")); + Assert.assertFalse("sonar properties value must not have single quote: " + + keyName, keyValue.matches(".*[^'{}]'[^'{}].*")); + Assert.assertFalse("sonar properties value must not have unescaped braces: " + + keyName, keyValue.matches(".*[^'](\\{|\\})[^'].*")); final String moduleName; @@ -215,7 +220,8 @@ private static void validateSonarProperties(Properties properties, Set> final Class module = findModule(modules, moduleName); - Assert.assertNotNull("Unknown class found in sonar properties: " + moduleName, module); + Assert.assertNotNull("Unknown class found in sonar properties: " + moduleName, + module); if (CheckUtil.isFilterModule(module)) { Assert.fail("Module should not be in sonar properties: "