summaryrefslogtreecommitdiffstats
path: root/sonar-deprecated/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-deprecated/src/test')
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/batch/AbstractViolationsStaxParserTest.java103
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheck.java27
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheckWithParameters.java44
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/RuleAnnotationUtilsTest.java64
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/RulePriorityTest.java44
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/RuleTest.java95
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java66
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/StandardProfileXmlParserTest.java172
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRuleXmlFormatTest.java91
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRulesXmlParserTest.java138
-rw-r--r--sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/deprecated.xml11
-rw-r--r--sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/rules.xml39
-rw-r--r--sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/utf8.xml11
-rw-r--r--sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile.xml7
-rw-r--r--sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile_name_null.xml6
15 files changed, 918 insertions, 0 deletions
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/batch/AbstractViolationsStaxParserTest.java b/sonar-deprecated/src/test/java/org/sonar/api/batch/AbstractViolationsStaxParserTest.java
new file mode 100644
index 00000000000..d1f91f1b4d3
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/batch/AbstractViolationsStaxParserTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.batch;
+
+import static junit.framework.Assert.assertNull;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+
+import java.io.StringReader;
+
+import javax.xml.stream.XMLStreamException;
+
+import org.codehaus.staxmate.in.SMInputCursor;
+import org.hibernate.lob.ReaderInputStream;
+import org.junit.Test;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.rules.RulesManager;
+
+public class AbstractViolationsStaxParserTest {
+
+ @Test
+ public void testParseLineIndex() {
+
+ assertThat(AbstractViolationsStaxParser.parseLineIndex("4"), is(4));
+ assertNull(AbstractViolationsStaxParser.parseLineIndex("toto"));
+ assertNull(AbstractViolationsStaxParser.parseLineIndex(""));
+ assertNull(AbstractViolationsStaxParser.parseLineIndex(null));
+ assertNull(AbstractViolationsStaxParser.parseLineIndex("-1"));
+ }
+
+ @Test
+ public void testDoNotSaveViolationsOnUnexistedResource() throws XMLStreamException {
+ SensorContext context = mock(SensorContext.class);
+ MyViolationParser violationParser = new MyViolationParser(context, null);
+ violationParser.setDoSaveViolationsOnUnexistedResource(false);
+ violationParser.parse(new ReaderInputStream(new StringReader("<root><file/></root>")));
+ }
+
+ @Test(expected = CursorForViolationsMethodHasBeenCalled.class)
+ public void testDoSaveViolationsOnUnexistedResource() throws XMLStreamException {
+ SensorContext context = mock(SensorContext.class);
+ MyViolationParser violationParser = new MyViolationParser(context, null);
+ violationParser.parse(new ReaderInputStream(new StringReader("<root><file/></root>")));
+ }
+
+ private class MyViolationParser extends AbstractViolationsStaxParser {
+
+ protected MyViolationParser(SensorContext context, RulesManager rulesManager) {
+ super(context, rulesManager);
+ }
+
+ protected SMInputCursor cursorForResources(SMInputCursor rootCursor) throws XMLStreamException {
+ return rootCursor.descendantElementCursor("file");
+ }
+
+ protected SMInputCursor cursorForViolations(SMInputCursor resourcesCursor) throws XMLStreamException {
+ throw new CursorForViolationsMethodHasBeenCalled();
+ }
+
+ protected Resource toResource(SMInputCursor resourceCursor) throws XMLStreamException {
+ return new JavaFile("org.sonar.MyClass");
+ }
+
+ protected String messageFor(SMInputCursor violationCursor) throws XMLStreamException {
+ return null;
+ }
+
+ protected String ruleKey(SMInputCursor violationCursor) throws XMLStreamException {
+ return null;
+ }
+
+ protected String keyForPlugin() {
+ return null;
+ }
+
+ protected String lineNumberForViolation(SMInputCursor violationCursor) throws XMLStreamException {
+ return null;
+ }
+ }
+
+ private class CursorForViolationsMethodHasBeenCalled extends RuntimeException {
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheck.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheck.java
new file mode 100644
index 00000000000..da01fb10904
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheck.java
@@ -0,0 +1,27 @@
+/*
+ * 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.rules;
+
+import org.sonar.check.Check;
+import org.sonar.check.IsoCategory;
+
+@Check(title ="Annotated Check", description = "Description", isoCategory = IsoCategory.Reliability)
+public class AnnotatedCheck {
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheckWithParameters.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheckWithParameters.java
new file mode 100644
index 00000000000..cd487c3d0a0
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotatedCheckWithParameters.java
@@ -0,0 +1,44 @@
+/*
+ * 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.rules;
+
+import org.sonar.check.Check;
+import org.sonar.check.CheckProperty;
+import org.sonar.check.IsoCategory;
+
+@Check(key = "overriden_key",title ="Check with parameters", description = "Has parameters", isoCategory = IsoCategory.Efficiency)
+public class AnnotatedCheckWithParameters {
+
+ @CheckProperty(description ="Maximum value")
+ private String max;
+
+ @CheckProperty(key = "overidden_min", description ="Minimum value")
+ protected String min;
+
+ private int nonConfigurableProperty;
+
+ public String getMax() {
+ return max;
+ }
+
+ public void setMax(String max) {
+ this.max = max;
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleAnnotationUtilsTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleAnnotationUtilsTest.java
new file mode 100644
index 00000000000..98d05bbd1f4
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleAnnotationUtilsTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.rules;
+
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public class RuleAnnotationUtilsTest {
+
+ @Test
+ public void readAnnotatedClassWithoutParameters() {
+ Rule rule = RuleAnnotationUtils.readAnnotatedClass(AnnotatedCheck.class);
+ assertNotNull(rule);
+ assertThat(rule.getKey(), is(AnnotatedCheck.class.getName()));
+ assertThat(rule.getName(), is("Annotated Check"));
+ assertThat(rule.getConfigKey(), nullValue());
+ assertThat(rule.getParams().size(), is(0));
+ assertThat(rule.getDescription(), is("Description"));
+ assertThat(rule.getCardinality(), Is.is(Rule.Cardinality.SINGLE));
+ assertThat(rule.getRulesCategory().getName(), Is.is(Iso9126RulesCategories.RELIABILITY.getName()));
+ }
+
+ @Test
+ public void ruleKeyCanBeOverridden() {
+ Rule rule = RuleAnnotationUtils.readAnnotatedClass(AnnotatedCheckWithParameters.class);
+ assertNotNull(rule);
+ assertThat(rule.getKey(), is("overriden_key"));
+ }
+ @Test
+ public void readAnnotatedClassWithParameters() {
+ Rule rule = RuleAnnotationUtils.readAnnotatedClass(AnnotatedCheckWithParameters.class);
+ assertNotNull(rule);
+ assertThat(rule.getParams().size(), is(2));
+ assertThat(rule.getParam("max"), not(nullValue()));
+ assertThat(rule.getParam("max").getDescription(), is("Maximum value"));
+
+ assertThat(rule.getParam("min"), nullValue());
+ assertThat(rule.getParam("overidden_min"), not(nullValue()));
+ assertThat(rule.getParam("overidden_min").getDescription(), is("Minimum value"));
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/RulePriorityTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/RulePriorityTest.java
new file mode 100644
index 00000000000..a3ed40321b8
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/RulePriorityTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.rules;
+
+import static junit.framework.Assert.assertEquals;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class RulePriorityTest {
+
+ @Test
+ public void testValueOfString() {
+ Assert.assertEquals(RulePriority.INFO, RulePriority.valueOfString("info"));
+ Assert.assertEquals(RulePriority.MAJOR, RulePriority.valueOfString("MAJOR"));
+ Assert.assertEquals(RulePriority.MAJOR, RulePriority.valueOfString("ERROR"));
+ Assert.assertEquals(RulePriority.INFO, RulePriority.valueOfString("WARNING"));
+ Assert.assertEquals(RulePriority.MAJOR, RulePriority.valueOfString("ErRor"));
+ Assert.assertEquals(RulePriority.INFO, RulePriority.valueOfString("WaRnInG"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testUnknownValueOfString() {
+ RulePriority.valueOfString("make me crash");
+ }
+
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleTest.java
new file mode 100644
index 00000000000..c0767fda0ad
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.rules;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.*;
+
+import org.hamcrest.core.Is;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class RuleTest {
+
+ @Test
+ public void descriptionShouldBeCleaned() {
+ Rule rule = new Rule();
+ rule.setDescription(" my description ");
+ Assert.assertEquals("my description", rule.getDescription());
+
+ rule.setDescription(null);
+ assertNull(rule.getDescription());
+ }
+
+ @Test
+ public void shouldRemoveNewLineCharactersInNameWithSetter() {
+ Rule rule = new Rule();
+ for (String example : getExamplesContainingNewLineCharacter()) {
+ rule.setName(example);
+ assertThat(rule.getName(), is("test"));
+ }
+ }
+
+ @Test
+ public void shouldRemoveNewLineCharactersInNameWithfirstConstructor() {
+ Rule rule;
+ for (String example : getExamplesContainingNewLineCharacter()) {
+ rule = new Rule(null, null, example, (RulesCategory) null, null);
+ assertThat(rule.getName(), is("test"));
+ }
+ }
+
+ @Test
+ public void shouldRemoveNewLineCharactersInNameWithSecondConstructor() {
+ Rule rule;
+ for (String example : getExamplesContainingNewLineCharacter()) {
+ rule = new Rule(null, null, example, (RulesCategory)null, null);
+ assertThat(rule.getName(), is("test"));
+ }
+ }
+
+ @Test
+ public void defaultPriorityIsMajor() {
+ Rule rule = new Rule();
+ assertThat(rule.getPriority(), Is.is(RulePriority.MAJOR));
+
+ rule = new Rule("name", "key");
+ assertThat(rule.getPriority(), Is.is(RulePriority.MAJOR));
+
+ rule = new Rule("pkey", "key", "name", Iso9126RulesCategories.EFFICIENCY, null, null);
+ assertThat(rule.getPriority(), Is.is(RulePriority.MAJOR));
+
+ rule.setPriority(RulePriority.BLOCKER);
+ assertThat(rule.getPriority(), Is.is(RulePriority.BLOCKER));
+
+ rule.setPriority(null);
+ assertThat(rule.getPriority(), Is.is(RulePriority.MAJOR));
+ }
+
+
+ private List<String> getExamplesContainingNewLineCharacter() {
+ return Arrays.asList("te\nst", "te\ns\nt", "te\rst", "te\n\rst", "te\r\nst");
+ }
+
+
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java
new file mode 100644
index 00000000000..c60a5fdf604
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.rules;
+
+import org.apache.commons.configuration.Configuration;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import org.junit.Test;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.mockito.Matchers;
+import org.sonar.api.CoreProperties;
+
+import java.util.Map;
+
+public class RuleUtilsTest {
+
+ @Test
+ public void getPriorityWeights() {
+ Configuration conf = mock(Configuration.class);
+ when(conf.getString(Matchers.eq(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY), anyString())).thenReturn("info=0;minor=1;major=2;critical=5;blocker=10");
+
+ final Map<RulePriority, Integer> map = RuleUtils.getPriorityWeights(conf);
+
+ assertThat(map.get(RulePriority.BLOCKER), is(10));
+ assertThat(map.get(RulePriority.CRITICAL), is(5));
+ assertThat(map.get(RulePriority.MAJOR), is(2));
+ assertThat(map.get(RulePriority.MINOR), is(1));
+ assertThat(map.get(RulePriority.INFO), is(0));
+ }
+
+ @Test
+ public void loadMissingWeights() {
+ Configuration conf = mock(Configuration.class);
+ when(conf.getString(Matchers.eq(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY), anyString())).thenReturn("foo=0;bar=1;CRITICAL=5");
+
+ final Map<RulePriority, Integer> map = RuleUtils.getPriorityWeights(conf);
+
+ assertThat(map.get(RulePriority.BLOCKER), is(1));
+ assertThat(map.get(RulePriority.CRITICAL), is(5));
+ assertThat(map.get(RulePriority.MAJOR), is(1));
+ assertThat(map.get(RulePriority.MINOR), is(1));
+ assertThat(map.get(RulePriority.INFO), is(1));
+ }
+
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardProfileXmlParserTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardProfileXmlParserTest.java
new file mode 100644
index 00000000000..2f20bd096ca
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardProfileXmlParserTest.java
@@ -0,0 +1,172 @@
+/*
+ * 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.rules;
+
+import org.apache.commons.io.IOUtils;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.*;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.rules.xml.Profile;
+import org.sonar.api.rules.xml.Property;
+import org.sonar.api.utils.SonarException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class StandardProfileXmlParserTest {
+
+ @Test
+ public void checkAllFields() {
+ StandardProfileXmlParser parser = new StandardProfileXmlParser();
+ String xml = "<profile name='Sonar way' language='java'><rule key=\"2006\" priority=\"warning\" /><rule key=\"2007\" priority=\"error\"><property name=\"toto\" value=\"titi\" /></rule></profile>";
+ Profile profile = parser.parse(xml);
+
+ assertEquals(2, profile.getRules().size());
+ assertEquals("Sonar way", profile.getName());
+
+ org.sonar.api.rules.xml.Rule rule1 = profile.getRules().get(0);
+ assertEquals("2006", rule1.getKey());
+ assertEquals("warning", rule1.getPriority());
+ assertNull(rule1.getProperties());
+
+ org.sonar.api.rules.xml.Rule rule2 = profile.getRules().get(1);
+ assertEquals("2007", rule2.getKey());
+ assertEquals("error", rule2.getPriority());
+ assertEquals(rule2.getProperties().size(), 1);
+
+ Property property = rule2.getProperties().get(0);
+ assertEquals("toto", property.getName());
+ assertEquals("titi", property.getValue());
+ }
+
+ @Test(expected = SonarException.class)
+ public void shouldProfileNameBeNotNull() throws IOException {
+ InputStream input = getClass().getResourceAsStream("/org/sonar/api/rules/test_profile_name_null.xml");
+ StandardProfileXmlParser standardProfileXmlParser = new StandardProfileXmlParser();
+ standardProfileXmlParser.importConfiguration(IOUtils.toString(input));
+ }
+
+ @Test
+ public void shouldBuildProfileFromXml() throws IOException {
+ StandardProfileXmlParser standardProfileXmlParser = new StandardProfileXmlParser();
+ InputStream input = getClass().getResourceAsStream("/org/sonar/api/rules/test_profile.xml");
+ Profile profile = standardProfileXmlParser.buildProfileFromXml(IOUtils.toString(input));
+
+ assertThat("Sonar way", is(profile.getName()));
+ assertThat(profile.getRules().size(), is(3));
+
+ org.sonar.api.rules.xml.Rule rule1 = profile.getRules().get(0);
+ assertThat(rule1.getKey(), is("2006"));
+ assertThat(rule1.getPriority(), is("warning"));
+ assertNull(rule1.getProperties());
+
+ org.sonar.api.rules.xml.Rule rule2 = profile.getRules().get(1);
+ assertThat(rule2.getKey(), is("2007"));
+ assertThat(rule2.getPriority(), is("error"));
+ assertThat(rule2.getProperties().size(), is(1));
+
+ org.sonar.api.rules.xml.Rule rule3 = profile.getRules().get(2);
+ assertThat(rule3.getKey(), is("2008"));
+ assertThat(rule3.getPriority(), is("critical"));
+ assertNull(rule3.getProperties());
+
+ Property rule2Property = rule2.getProperties().get(0);
+ assertThat(rule2Property.getName(), is("toto"));
+ assertThat(rule2Property.getValue(), is("titi"));
+ }
+
+ @Test
+ public void shouldImportConfiguration() throws IOException {
+ final List<Rule> inputRules = buildRulesFixture();
+ List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(inputRules);
+
+ StandardProfileXmlParser standardProfileXmlParser = new StandardProfileXmlParser(inputRules);
+
+ InputStream input = getClass().getResourceAsStream("/org/sonar/api/rules/test_profile.xml");
+ RulesProfile profile = standardProfileXmlParser.importConfiguration(IOUtils.toString(input));
+ List<ActiveRule> results = profile.getActiveRules();
+
+ assertThat("Sonar way", CoreMatchers.is(profile.getName()));
+ assertThat(results.size(), is(activeRulesExpected.size()));
+ assertActiveRulesAreEquals(results, activeRulesExpected);
+ }
+
+ private List<Rule> buildRulesFixture() {
+ List<Rule> rules = new ArrayList<Rule>();
+
+ Rule rule1 = new Rule("One rule", "2006",
+ "2006", null, "MYPLUGIN", null);
+
+ Rule rule2 = new Rule("Another rule", "2007",
+ "2007", null, "MYPLUGIN", null);
+ RuleParam ruleParam2 = new RuleParam(rule2, "toto", null, "s");
+ rule2.setParams(Arrays.asList(ruleParam2));
+
+ Rule rule3 = new Rule("Third rule", "2008",
+ "2008", null, "MYPLUGIN", null);
+
+ rules.add(rule1);
+ rules.add(rule2);
+ rules.add(rule3);
+
+ return rules;
+ }
+
+
+ private List<ActiveRule> buildActiveRulesFixture(List<Rule> rules) {
+ List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
+
+ ActiveRule activeRule1 = new ActiveRule(null, rules.get(0), RulePriority.INFO);
+ activeRules.add(activeRule1);
+
+ ActiveRule activeRule2 = new ActiveRule(null, rules.get(1), RulePriority.MAJOR);
+ activeRule2.setActiveRuleParams(Arrays.asList(new ActiveRuleParam(activeRule2, rules.get(1).getParams().get(0), "titi")));
+ activeRules.add(activeRule2);
+
+ ActiveRule activeRule3 = new ActiveRule(null, rules.get(2), RulePriority.CRITICAL);
+ activeRules.add(activeRule3);
+
+ return activeRules;
+ }
+
+ private void assertActiveRulesAreEquals(List<ActiveRule> activeRules1, List<ActiveRule> activeRules2) {
+ for (int i = 0; i < activeRules1.size(); i++) {
+ ActiveRule activeRule1 = activeRules1.get(i);
+ ActiveRule activeRule2 = activeRules2.get(i);
+ assertTrue(activeRule1.getRule().equals(activeRule2.getRule()) && activeRule1.getPriority().equals(activeRule2.getPriority()));
+
+ Assert.assertEquals(activeRule1.getActiveRuleParams().size(), (activeRule2.getActiveRuleParams().size()));
+ for (int j = 0; j < activeRule1.getActiveRuleParams().size(); j++) {
+ ActiveRuleParam activeRuleParam1 = activeRule1.getActiveRuleParams().get(j);
+ ActiveRuleParam activeRuleParam2 = activeRule2.getActiveRuleParams().get(j);
+ assertTrue(activeRuleParam1.getRuleParam().equals(activeRuleParam2.getRuleParam())
+ && activeRuleParam1.getValue().equals(activeRuleParam2.getValue()));
+ }
+ }
+ }
+
+} \ No newline at end of file
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRuleXmlFormatTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRuleXmlFormatTest.java
new file mode 100644
index 00000000000..a1cfb59336c
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRuleXmlFormatTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.rules;
+
+import org.hamcrest.core.Is;
+import org.junit.Test;
+import org.sonar.api.utils.SonarException;
+
+import java.io.StringReader;
+import java.util.List;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class StandardRuleXmlFormatTest {
+
+ @Test
+ public void parseXml() {
+ List<Rule> rules = StandardRuleXmlFormat.parseXml(getClass().getResourceAsStream("/org/sonar/api/rules/StandardRuleXmlFormatTest/rules.xml"));
+ assertThat(rules.size(), is(2));
+
+ Rule rule = rules.get(0);
+ assertThat(rule.getName(), is("Local Variable Name"));
+ assertThat(rule.getDescription(), is("Checks that local, non-final variable names conform to a format specified by the format property."));
+ assertThat(rule.getPriority(), Is.is(RulePriority.BLOCKER));
+ assertThat(rule.getCardinality(), Is.is(Rule.Cardinality.MULTIPLE));
+ assertThat(rule.getConfigKey(), is("Checker/TreeWalker/LocalVariableName"));
+
+ assertThat(rule.getParams().size(), is(2));
+ RuleParam prop = rule.getParam("ignore");
+ assertThat(prop.getKey(), is("ignore"));
+ assertThat(prop.getDescription(), is("Ignore ?"));
+
+ Rule minimalRule = rules.get(1);
+ assertThat(minimalRule.getKey(), is("com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck"));
+ assertThat(minimalRule.getParams().size(), is(0));
+
+ }
+
+ @Test(expected = SonarException.class)
+ public void failIfMissingRuleKey() {
+ StandardRuleXmlFormat.parseXml(new StringReader("<rules><rule><name>Foo</name></rule></rules>"));
+ }
+
+ @Test(expected = SonarException.class)
+ public void failIfMissingPropertyKey() {
+ StandardRuleXmlFormat.parseXml(new StringReader("<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"));
+ }
+
+ @Test
+ public void utf8Encoding() {
+ List<Rule> rules = StandardRuleXmlFormat.parseXml(getClass().getResourceAsStream("/org/sonar/api/rules/StandardRuleXmlFormatTest/utf8.xml"));
+ assertThat(rules.size(), is(1));
+ Rule rule = rules.get(0);
+ assertThat(rule.getKey(), is("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck"));
+ assertThat(rule.getName(), is("M & M"));
+ assertThat(rule.getDescription().charAt(0), is('\u00E9'));
+ assertThat(rule.getDescription().charAt(1), is('\u00E0'));
+ assertThat(rule.getDescription().charAt(2), is('\u0026'));
+ }
+
+ @Test
+ public void supportDeprecatedFormat() {
+ // the deprecated format uses some attributes instead of nodes
+ List<Rule> rules = StandardRuleXmlFormat.parseXml(getClass().getResourceAsStream("/org/sonar/api/rules/StandardRuleXmlFormatTest/deprecated.xml"));
+ assertThat(rules.size(), is(1));
+ Rule rule = rules.get(0);
+ assertThat(rule.getPriority(), Is.is(RulePriority.CRITICAL));
+ assertThat(rule.getKey(), is("org.sonar.it.checkstyle.MethodsCountCheck"));
+ assertThat(rule.getParam("minMethodsCount"), not(nullValue()));
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRulesXmlParserTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRulesXmlParserTest.java
new file mode 100644
index 00000000000..30bc75f9f89
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/StandardRulesXmlParserTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.rules;
+
+import org.apache.commons.io.IOUtils;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class StandardRulesXmlParserTest {
+ @Test
+ public void checkAllFields() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1'><name>my name</name><configKey>my_config_key</configKey><description>my description</description><param key='param1'><type>s</type><description>param description</description></param><param key='param2'><type>integer</type><description>param description 2</description></param></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertEquals(1, rules.size());
+
+ Rule rule = rules.get(0);
+ Assert.assertEquals("key1", rule.getKey());
+ Assert.assertEquals("my name", rule.getName());
+ Assert.assertEquals("my_config_key", rule.getConfigKey());
+ Assert.assertEquals("my description", rule.getDescription());
+ Assert.assertEquals(2, rule.getParams().size());
+ Assert.assertEquals("param1", rule.getParams().get(0).getKey());
+ Assert.assertEquals("s", rule.getParams().get(0).getType());
+ Assert.assertEquals("param description", rule.getParams().get(0).getDescription());
+ }
+
+ @Test
+ public void ruleShouldHaveACategory() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule><category name='cat1' /></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertNotNull(rules.get(0).getRulesCategory());
+ Assert.assertEquals("cat1", rules.get(0).getRulesCategory().getName());
+ assertNull(rules.get(0).getRulesCategory().getId());
+ assertNull(rules.get(0).getRulesCategory().getDescription());
+ }
+
+ @Test
+ public void ruleCanHaveALevel() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='1' priority='CRITICAL'><category name='cat1'/></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertNotNull(rules.get(0).getRulesCategory());
+ Assert.assertEquals(RulePriority.CRITICAL, rules.get(0).getPriority());
+ assertNull(rules.get(0).getRulesCategory().getId());
+ assertNull(rules.get(0).getRulesCategory().getDescription());
+ }
+
+ @Test
+ public void ruleShouldHaveADefaultLevel() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='1'><category name='cat1'/></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ Assert.assertEquals(RulePriority.MAJOR, rules.get(0).getPriority());
+ }
+
+ @Test
+ public void shouldDefineManyRules() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1' /><rule key='key2' category='cat1' /></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertEquals(2, rules.size());
+ Assert.assertEquals("key1", rules.get(0).getKey());
+ Assert.assertEquals("key2", rules.get(1).getKey());
+ }
+
+ @Test
+ public void someFielsShouldBeNull() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1' /></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertNull(rules.get(0).getDescription());
+ assertNull(rules.get(0).getName());
+ assertNull(rules.get(0).getConfigKey());
+ }
+
+ @Test
+ public void shouldContainCDataDescription() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1'><description> <![CDATA[<xml> </nodes> and accents Žˆ˜ ]]> </description></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertEquals(1, rules.size());
+ Assert.assertEquals("<xml> </nodes> and accents Žˆ˜", rules.get(0).getDescription());
+ }
+
+ @Test
+ public void shouldBeBackwardCompatibleWithDefaultVersionProperty() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1'><name>my name</name><configKey>my_config_key</configKey><param key='param1'><type>s</type><description>param description</description><defaultValue>xxx</defaultValue></param></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertEquals(1, rules.size());
+
+ Rule rule = rules.get(0);
+ Assert.assertEquals("key1", rule.getKey());
+ Assert.assertEquals(1, rule.getParams().size());
+ Assert.assertEquals("param1", rule.getParams().get(0).getKey());
+ }
+
+ @Test
+ public void shouldParseStringInUt8() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1' ><description>\\u00E9</description></rule></rules>";
+ List<Rule> rules = parser.parse(xml);
+ assertThat(rules.get(0).getDescription(), is("\\u00E9"));
+ }
+
+ @Test
+ public void shouldParseInputStreamInUt8() {
+ StandardRulesXmlParser parser = new StandardRulesXmlParser();
+ String xml = "<rules><rule key='key1' category='cat1' ><description>\\u00E9</description></rule></rules>";
+ List<Rule> rules = parser.parse(IOUtils.toInputStream(xml));
+ assertThat(rules.get(0).getDescription(), is("\\u00E9"));
+ }
+}
diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/deprecated.xml b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/deprecated.xml
new file mode 100644
index 00000000000..44cf56d228a
--- /dev/null
+++ b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/deprecated.xml
@@ -0,0 +1,11 @@
+<rules>
+ <rule key="org.sonar.it.checkstyle.MethodsCountCheck" priority="CRITICAL">
+ <name>Methods Count Check</name>
+ <configKey>Checker/TreeWalker/org.sonar.it.checkstyle.MethodsCountCheck</configKey>
+ <category name="Usability"/>
+ <description>Count methods.</description>
+ <param key="minMethodsCount" type="i">
+ <description>Le nombre minimum de méthodes. 10 par défaut.</description>
+ </param>
+ </rule>
+</rules> \ No newline at end of file
diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/rules.xml b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/rules.xml
new file mode 100644
index 00000000000..c4038bdf96c
--- /dev/null
+++ b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/rules.xml
@@ -0,0 +1,39 @@
+<rules>
+ <rule>
+ <!-- with exhaustive fields -->
+ <key>com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck</key>
+ <name>Local Variable Name</name>
+ <description>
+ <![CDATA[Checks that local, non-final variable names conform to a format specified by the format property.]]>
+ </description>
+ <isoCategory>Efficiency</isoCategory>
+ <configKey>Checker/TreeWalker/LocalVariableName</configKey>
+ <priority>BLOCKER</priority>
+ <cardinality>MULTIPLE</cardinality>
+ <param>
+ <key>tokens</key>
+ <description>
+ <![CDATA[
+ Controls whether the check applies to variable declarations or catch clause parameters
+ ]]>
+ </description>
+ </param>
+ <param>
+ <key>ignore</key>
+ <description>
+ Ignore ?
+ </description>
+ </param>
+ </rule>
+
+
+ <rule>
+ <!-- with only required fields -->
+ <key>com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck</key>
+ <name>Magic Number</name>
+ <description>
+ <![CDATA[Checks for magic numbers.]]>
+ </description>
+ <isoCategory>Maintainability</isoCategory>
+ </rule>
+</rules> \ No newline at end of file
diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/utf8.xml b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/utf8.xml
new file mode 100644
index 00000000000..6197e030057
--- /dev/null
+++ b/sonar-deprecated/src/test/resources/org/sonar/api/rules/StandardRuleXmlFormatTest/utf8.xml
@@ -0,0 +1,11 @@
+<rules>
+ <rule>
+ <key>com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck</key>
+ <priority>BLOCKER</priority>
+ <configKey>Checker/TreeWalker/LocalVariableName</configKey>
+ <name>M &amp; M</name>
+ <description>
+ <![CDATA[éà&]]>
+ </description>
+ </rule>
+</rules>
diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile.xml b/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile.xml
new file mode 100644
index 00000000000..29e2024bf51
--- /dev/null
+++ b/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile.xml
@@ -0,0 +1,7 @@
+<profile name="Sonar way" language='java'>
+ <rule key="2006" priority="warning"/>
+ <rule key="2007" priority="error">
+ <property name="toto" value="titi"/>
+ </rule>
+ <rule key="2008" priority="critical"/>
+</profile> \ No newline at end of file
diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile_name_null.xml b/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile_name_null.xml
new file mode 100644
index 00000000000..65d39e70403
--- /dev/null
+++ b/sonar-deprecated/src/test/resources/org/sonar/api/rules/test_profile_name_null.xml
@@ -0,0 +1,6 @@
+<profile name="" language='java'>
+ <rule key="2006" priority="warning"/>
+ <rule key="2007" priority="error">
+ <property name="toto" value="titi"/>
+ </rule>
+</profile> \ No newline at end of file