diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-10-08 14:38:32 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-10-08 14:38:32 +0000 |
commit | 7ca495d67492c6b3dbb653102e345f8fc43f09ae (patch) | |
tree | 2dd4d54faaad9c4c74f172fba50bdfb5f3b734c8 /sonar-plugin-api | |
parent | 9185222d18826c0648a8bf3cf8e2e491537b472e (diff) | |
download | sonarqube-7ca495d67492c6b3dbb653102e345f8fc43f09ae.tar.gz sonarqube-7ca495d67492c6b3dbb653102e345f8fc43f09ae.zip |
API: improve the pattern to define quality profiles. The extension point is org.sonar.api.profiles.ProfileDefinition, whereas XMLProfileParser and AnnotationProfileParser are components that can be used but not extended.
Diffstat (limited to 'sonar-plugin-api')
18 files changed, 234 insertions, 306 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileParser.java index a5cbf14f06f..5fc713eecf1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileParser.java @@ -19,6 +19,8 @@ */ package org.sonar.api.profiles; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.ServerComponent; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleAnnotationUtils; import org.sonar.api.rules.RuleFinder; @@ -31,45 +33,34 @@ import java.util.Collection; /** * @since 2.3 */ -public abstract class AnnotationProfileDefinition extends ProfileDefinition { +public final class AnnotationProfileParser implements ServerComponent { - private String name; - private String language; - private String repositoryKey; - private Collection<Class> annotatedClasses; private RuleFinder ruleFinder; - protected AnnotationProfileDefinition(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses, RuleFinder ruleFinder) { - this.name = profileName; - this.language = language; - this.repositoryKey = repositoryKey; - this.annotatedClasses = annotatedClasses; + public AnnotationProfileParser(RuleFinder ruleFinder) { this.ruleFinder = ruleFinder; } - @Override - public RulesProfile createProfile(ValidationMessages validation) { - RulesProfile profile = RulesProfile.create(name, language); - if (annotatedClasses != null) { - for (Class aClass : annotatedClasses) { - BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class); - registerRule(aClass, belongsToProfile, profile, validation); - } + public RulesProfile parse(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses, ValidationMessages messages) { + RulesProfile profile = RulesProfile.create(profileName, language); + for (Class aClass : annotatedClasses) { + BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class); + addRule(aClass, belongsToProfile, profile, repositoryKey, messages); } return profile; } - private void registerRule(Class aClass, BelongsToProfile belongsToProfile, RulesProfile profile, ValidationMessages validation) { - if (belongsToProfile != null) { + private void addRule(Class aClass, BelongsToProfile annotation, RulesProfile profile, String repositoryKey, ValidationMessages messages) { + if (annotation != null && StringUtils.equals(annotation.title(), profile.getName())) { String ruleKey = RuleAnnotationUtils.getRuleKey(aClass); Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey); if (rule == null) { - validation.addErrorText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]"); + messages.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]"); } else { RulePriority priority = null; - if (belongsToProfile.priority() != null) { - priority = RulePriority.fromCheckPriority(belongsToProfile.priority()); + if (annotation.priority() != null) { + priority = RulePriority.fromCheckPriority(annotation.priority()); } profile.activateRule(rule, priority); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java index e37343a9abf..69aa3c99b73 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java @@ -23,6 +23,9 @@ import org.sonar.api.ServerExtension; import org.sonar.api.utils.ValidationMessages; /** + * Define a profile which is automatically registered during sonar startup. + * The components <code>AnnotationProfileParser</code> and <code>XMLProfileParser</code> can be used to help implementing the method create(). + * * @since 2.3 */ public abstract class ProfileDefinition implements ServerExtension { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileDefinition.java deleted file mode 100644 index 9cbbe4eaa1b..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileDefinition.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.profiles; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.CharEncoding; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.utils.SonarException; -import org.sonar.api.utils.ValidationMessages; - -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.util.List; - -/** - * @since 2.3 - */ -public abstract class XMLProfileDefinition extends ProfileDefinition { - - private RuleFinder ruleFinder; - private ClassLoader classloader; - private String xmlClassPath; - - protected XMLProfileDefinition(ClassLoader classloader, String xmlClassPath, RuleFinder ruleFinder) { - this.ruleFinder = ruleFinder; - this.classloader = classloader; - this.xmlClassPath = xmlClassPath; - } - - @Override - public final RulesProfile createProfile(ValidationMessages validation) { - Reader reader = new InputStreamReader(classloader.getResourceAsStream(xmlClassPath), Charset.forName(CharEncoding.UTF_8)); - try { - return XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); - - } finally { - IOUtils.closeQuietly(reader); - } - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java index 3a13d82b8b0..571e2fd6d9b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java @@ -19,11 +19,14 @@ */ package org.sonar.api.profiles; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.StringUtils; import org.codehaus.stax2.XMLInputFactory2; import org.codehaus.staxmate.SMInputFactory; import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; +import org.sonar.api.ServerComponent; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; @@ -32,26 +35,34 @@ import org.sonar.api.utils.ValidationMessages; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; +import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; /** * @since 2.3 */ -public final class XMLProfileImporter { +public final class XMLProfileParser implements ServerComponent { private RuleFinder ruleFinder; - private XMLProfileImporter(RuleFinder ruleFinder) { + public XMLProfileParser(RuleFinder ruleFinder) { this.ruleFinder = ruleFinder; } - public static XMLProfileImporter create(RuleFinder ruleFinder) { - return new XMLProfileImporter(ruleFinder); + public RulesProfile parseResource(ClassLoader classloader, String xmlClassPath, ValidationMessages messages) { + Reader reader = new InputStreamReader(classloader.getResourceAsStream(xmlClassPath), Charset.forName(CharEncoding.UTF_8)); + try { + return parse(reader, messages); + + } finally { + IOUtils.closeQuietly(reader); + } } - public RulesProfile importProfile(Reader reader, ValidationMessages messages) { + public RulesProfile parse(Reader reader, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); SMInputFactory inputFactory = initStax(); try { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileSerializer.java index ee78f365cbe..e670002e968 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileSerializer.java @@ -21,6 +21,7 @@ package org.sonar.api.profiles; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; +import org.sonar.api.ServerComponent; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.utils.SonarException; @@ -31,13 +32,9 @@ import java.io.Writer; /** * @since 2.3 */ -public final class XMLProfileExporter { +public final class XMLProfileSerializer implements ServerComponent { - public static XMLProfileExporter create() { - return new XMLProfileExporter(); - } - - public void exportProfile(RulesProfile profile, Writer writer) { + public void write(RulesProfile profile, Writer writer) { try { appendHeader(profile, writer); appendRules(profile, writer); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/ModelDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/ModelDefinition.java index 416059c0cad..6a8eb7aaf7c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/ModelDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/ModelDefinition.java @@ -41,7 +41,7 @@ public abstract class ModelDefinition implements ServerExtension { return name; } - public abstract Model create(); + public abstract Model createModel(); @Override public final boolean equals(Object o) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java deleted file mode 100644 index 45e6bee0be0..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.profiles; - -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RulePriority; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.check.BelongsToProfile; -import org.sonar.check.Check; -import org.sonar.check.IsoCategory; -import org.sonar.check.Priority; - -import java.util.Arrays; -import java.util.Collection; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class AnnotationProfileDefinitionTest { - - @Test - public void importProfile() { - RuleFinder ruleFinder = mock(RuleFinder.class); - when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>(){ - public Rule answer(InvocationOnMock iom) throws Throwable { - return Rule.create((String)iom.getArguments()[0], (String)iom.getArguments()[1], (String)iom.getArguments()[1]); - } - }); - - ProfileDefinition definition = new FakeDefinition(ruleFinder); - ValidationMessages validation = ValidationMessages.create(); - RulesProfile profile = definition.createProfile(validation); - assertThat(profile.getActiveRule("squid", "fake").getPriority(), is(RulePriority.BLOCKER)); - assertThat(validation.hasErrors(), is(false)); - } -} - -@BelongsToProfile(title = "not used !", priority = Priority.BLOCKER) -@Check(key = "fake", isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) -class FakeRule { - -} - - -class FakeDefinition extends AnnotationProfileDefinition { - - public FakeDefinition(RuleFinder ruleFinder) { - super("squid", "sonar way", "java", Arrays.<Class>asList(FakeRule.class), ruleFinder); - } -}
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileParserTest.java new file mode 100644 index 00000000000..a6d0c6a0a90 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileParserTest.java @@ -0,0 +1,85 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.profiles; + +import com.google.common.collect.Lists; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.check.BelongsToProfile; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AnnotationProfileParserTest { + + @Test + public void shouldParseAnnotatedClasses() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + } + }); + + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class), messages); + + assertThat(profile.getName(), is("Foo way")); + assertThat(profile.getLanguage(), is("java")); + assertThat(profile.getActiveRule("squid", "fake").getPriority(), is(RulePriority.BLOCKER)); + assertThat(messages.hasErrors(), is(false)); + } + + @Test + public void shouldParseOnlyWantedProfile() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + } + }); + + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class, RuleOnOtherProfile.class), messages); + + assertNotNull(profile.getActiveRule("squid", "fake")); + assertNull(profile.getActiveRule("squid", "other")); + } +} + +@BelongsToProfile(title = "Other profile", priority = Priority.BLOCKER) +@org.sonar.check.Rule(key = "other", isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class RuleOnOtherProfile { +} + +@BelongsToProfile(title = "Foo way", priority = Priority.BLOCKER) +@org.sonar.check.Rule(key = "fake", isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class FakeRule { +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java deleted file mode 100644 index 6358f1d08c3..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.profiles; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.CharEncoding; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RulePriority; -import org.sonar.api.utils.ValidationMessages; - -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; - -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.internal.matchers.StringContains.containsString; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class XMLProfileImporterTest { - - @Test - public void importProfile() throws UnsupportedEncodingException { - Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfile.xml"), CharEncoding.UTF_8); - try { - ValidationMessages validation = ValidationMessages.create(); - RuleFinder ruleFinder = newRuleFinder(); - RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); - - assertThat(profile.getLanguage(), is("java")); - assertThat(profile.getName(), is("sonar way")); - assertThat(validation.hasErrors(), is(false)); - assertNotNull(profile); - assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getPriority(), is(RulePriority.CRITICAL)); - - } finally { - IOUtils.closeQuietly(reader); - } - } - - @Test - public void nameAndLanguageShouldBeMandatory() throws UnsupportedEncodingException { - Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml"), CharEncoding.UTF_8); - try { - ValidationMessages validation = ValidationMessages.create(); - RuleFinder ruleFinder = newRuleFinder(); - RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); - - assertThat(validation.getErrors().size(), is(2)); - assertThat(validation.getErrors().get(0) , containsString("")); - - } finally { - IOUtils.closeQuietly(reader); - } - } - - @Test - public void importProfileWithRuleParameters() throws UnsupportedEncodingException { - Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithRuleParameters.xml"), CharEncoding.UTF_8); - try { - ValidationMessages validation = ValidationMessages.create(); - RuleFinder ruleFinder = newRuleFinder(); - RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); - - assertThat(validation.hasErrors(), is(false)); - assertThat(validation.hasWarnings(), is(false)); - ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); - assertThat(rule.getParameter("format"), is("foo")); - assertThat(rule.getParameter("message"), is("with special characters < > &")); - - } finally { - IOUtils.closeQuietly(reader); - } - } - - @Test - public void importProfileWithUnknownRuleParameter() throws UnsupportedEncodingException { - Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithUnknownRuleParameter.xml"), CharEncoding.UTF_8); - try { - ValidationMessages validation = ValidationMessages.create(); - RuleFinder ruleFinder = newRuleFinder(); - RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); - - assertThat(validation.getWarnings().size(), is(1)); - ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); - assertThat(rule.getParameter("unknown"), nullValue()); - - } finally { - IOUtils.closeQuietly(reader); - } - } - - private RuleFinder newRuleFinder() { - RuleFinder ruleFinder = mock(RuleFinder.class); - when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>(){ - public Rule answer(InvocationOnMock iom) throws Throwable { - Rule rule = Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); - rule.createParameter("format"); - rule.createParameter("message"); - return rule; - } - }); - return ruleFinder; - } -}
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java new file mode 100644 index 00000000000..3167cb941b4 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java @@ -0,0 +1,104 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.profiles; + +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.ValidationMessages; + +import java.io.UnsupportedEncodingException; + +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.StringContains.containsString; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class XMLProfileParserTest { + + @Test + public void importProfile() throws UnsupportedEncodingException { + ValidationMessages validation = ValidationMessages.create(); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = new XMLProfileParser(ruleFinder).parseResource(getClass().getClassLoader(), "org/sonar/api/profiles/XMLProfileParserTest/importProfile.xml", validation); + + assertThat(profile.getLanguage(), is("java")); + assertThat(profile.getName(), is("sonar way")); + assertThat(validation.hasErrors(), is(false)); + assertNotNull(profile); + assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getPriority(), is(RulePriority.CRITICAL)); + } + + @Test + public void nameAndLanguageShouldBeMandatory() throws UnsupportedEncodingException { + ValidationMessages validation = ValidationMessages.create(); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = new XMLProfileParser(ruleFinder).parseResource(getClass().getClassLoader(), "org/sonar/api/profiles/XMLProfileParserTest/nameAndLanguageShouldBeMandatory.xml", validation); + + assertThat(validation.getErrors().size(), is(2)); + assertThat(validation.getErrors().get(0), containsString("")); + + } + + @Test + public void importProfileWithRuleParameters() throws UnsupportedEncodingException { + ValidationMessages validation = ValidationMessages.create(); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = new XMLProfileParser(ruleFinder).parseResource(getClass().getClassLoader(), "org/sonar/api/profiles/XMLProfileParserTest/importProfileWithRuleParameters.xml", validation); + + assertThat(validation.hasErrors(), is(false)); + assertThat(validation.hasWarnings(), is(false)); + ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); + assertThat(rule.getParameter("format"), is("foo")); + assertThat(rule.getParameter("message"), is("with special characters < > &")); + } + + @Test + public void importProfileWithUnknownRuleParameter() throws UnsupportedEncodingException { + ValidationMessages validation = ValidationMessages.create(); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = new XMLProfileParser(ruleFinder).parseResource(getClass().getClassLoader(), "org/sonar/api/profiles/XMLProfileParserTest/importProfileWithUnknownRuleParameter.xml", validation); + + assertThat(validation.getWarnings().size(), is(1)); + ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); + assertThat(rule.getParameter("unknown"), nullValue()); + } + + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + Rule rule = Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + rule.createParameter("format"); + rule.createParameter("message"); + return rule; + } + }); + return ruleFinder; + } +}
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileExporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileSerializerTest.java index e43ce06c329..90169f10dab 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileExporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileSerializerTest.java @@ -36,15 +36,15 @@ import java.io.Writer; import static org.junit.Assert.assertTrue; -public class XMLProfileExporterTest { +public class XMLProfileSerializerTest { @Test public void exportEmptyProfile() throws IOException, SAXException { Writer writer = new StringWriter(); RulesProfile profile = RulesProfile.create("sonar way", "java"); - XMLProfileExporter.create().exportProfile(profile, writer); + new XMLProfileSerializer().write(profile, writer); - assertSimilarXml("/org/sonar/api/profiles/XMLProfileExporterTest/exportEmptyProfile.xml", writer.toString()); + assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportEmptyProfile.xml", writer.toString()); } @Test @@ -52,9 +52,9 @@ public class XMLProfileExporterTest { Writer writer = new StringWriter(); RulesProfile profile = RulesProfile.create("sonar way", "java"); profile.activateRule(Rule.create("checkstyle", "IllegalRegexp", "illegal regexp"), RulePriority.BLOCKER); - XMLProfileExporter.create().exportProfile(profile, writer); + new XMLProfileSerializer().write(profile, writer); - assertSimilarXml("/org/sonar/api/profiles/XMLProfileExporterTest/exportProfile.xml", writer.toString()); + assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportProfile.xml", writer.toString()); } @Test @@ -70,13 +70,13 @@ public class XMLProfileExporterTest { activeRule.setParameter("format", "foo"); activeRule.setParameter("message", "with special characters < > &"); // the tokens parameter is not set - XMLProfileExporter.create().exportProfile(profile, writer); + new XMLProfileSerializer().write(profile, writer); - assertSimilarXml("/org/sonar/api/profiles/XMLProfileExporterTest/exportRuleParameters.xml", writer.toString()); + assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportRuleParameters.xml", writer.toString()); } public static void assertSimilarXml(String pathToExpectedXml, String xml) throws IOException, SAXException { - InputStream stream = XMLProfileExporterTest.class.getResourceAsStream(pathToExpectedXml); + InputStream stream = XMLProfileSerializerTest.class.getResourceAsStream(pathToExpectedXml); try { Diff diff = isSimilarXml(IOUtils.toString(stream), xml); String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml; diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfile.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfile.xml index 047c4c7a844..047c4c7a844 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfile.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfile.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithRuleParameters.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithRuleParameters.xml index f3e2758b931..f3e2758b931 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithRuleParameters.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithRuleParameters.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithUnknownRuleParameter.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithUnknownRuleParameter.xml index eb4a460fcf1..eb4a460fcf1 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithUnknownRuleParameter.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithUnknownRuleParameter.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/nameAndLanguageShouldBeMandatory.xml index 303494eb8bb..303494eb8bb 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/nameAndLanguageShouldBeMandatory.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportEmptyProfile.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportEmptyProfile.xml index 3bdb8fc1c76..3bdb8fc1c76 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportEmptyProfile.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportEmptyProfile.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportProfile.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportProfile.xml index 76779cd4f33..76779cd4f33 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportProfile.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportProfile.xml diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportRuleParameters.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportRuleParameters.xml index f3e2758b931..f3e2758b931 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileExporterTest/exportRuleParameters.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileSerializerTest/exportRuleParameters.xml |