CodeNarcProfileExporter { public void exportProfile(RulesProfile profile) { try { generateXML(profile.getActiveRulesByRepository(CodeNarcRulesDefinition.REPOSITORY_KEY)); } catch (IOException e) { throw new IllegalStateException("Fail to export CodeNarc profile : " + profile, e); } } CodeNarcProfileExporter(Writer writer); void exportProfile(RulesProfile profile);  }
@Test public void shouldExportProfile() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.basic.AddEmptyStringRule", "Add Empty String"); profile.activateRule(rule, RulePriority.MAJOR); rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.size.ClassSizeRule", "Class Size"); profile.activateRule(rule, RulePriority.MAJOR); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportProfile.xml"), writer.toString()); }
@Test public void shouldFailToExport() throws IOException { Writer writer = Mockito.mock(Writer.class); Mockito.when(writer.append(Matchers.any(CharSequence.class))).thenThrow(new IOException()); exporter = new CodeNarcProfileExporter(writer); try { exporter.exportProfile(profile); Fail.fail("Should have failed"); } catch(IllegalStateException e) { assertThat(e.getMessage()).contains("Fail to export CodeNarc profile"); } }
@Test public void shouldExportParameters() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.size.ClassSizeRule", "Class Size"); rule.createParameter("maxLines"); profile.activateRule(rule, RulePriority.MAJOR).setParameter("maxLines", "20"); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportParameters.xml"), writer.toString()); }
@Test public void shouldNotExportUnsetParameters() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.size.ClassSizeRule", "Class Size"); rule.createParameter("maxLines"); profile.activateRule(rule, RulePriority.MAJOR).setParameter("maxLines", null); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportNullParameters.xml"), writer.toString()); }
@Test public void shouldExportFixedRulesCorrectly() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.design.PrivateFieldCouldBeFinalRule.fixed", "Private Field Could Be Final"); profile.activateRule(rule, RulePriority.MAJOR); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportFixedRules.xml"), writer.toString()); }
@Test public void shouldNotExportParametersWithDefaultValue() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.size.ClassSizeRule", "Class Size"); rule.createParameter("maxLines").setDefaultValue("20"); profile.activateRule(rule, RulePriority.MAJOR).setParameter("maxLines", "20"); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportNullParameters.xml"), writer.toString()); }
@Test public void shouldEscapeExportedParameters() throws Exception { Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.naming.ClassNameRule", "Class Name"); rule.createParameter("regex").setDefaultValue("([A-Z]\\w*\\$?)*"); profile.activateRule(rule, RulePriority.MAJOR).setParameter("regex", "[A-Z]+[a-z&&[^bc]]"); exporter.exportProfile(profile); assertSimilarXml( TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportEscapedParameters.xml"), writer.toString()); }