aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-05-17 16:00:59 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-05-19 15:15:36 +0200
commita1150367ee755be95f1cc605df983a928aa9136b (patch)
tree04defc5b2924fc48a846ec4c67efa798c0dc2687 /sonar-plugin-api/src/test
parentb58544df961c44b518dfbcc164b836ec23a03c69 (diff)
downloadsonarqube-a1150367ee755be95f1cc605df983a928aa9136b.tar.gz
sonarqube-a1150367ee755be95f1cc605df983a928aa9136b.zip
SONAR-6517 merge sonar-deprecated into sonar-plugin-api
It allows to use the shaded and relocated dependencies of sonar-plugin-api
Diffstat (limited to 'sonar-plugin-api/src/test')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/SquidUtilsTest.java41
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/charts/ChartParametersTest.java79
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java32
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java199
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithIntegerProperty.java33
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java28
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithOverriddenPropertyKey.java35
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithPrimitiveProperties.java43
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithStringProperty.java36
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithUnsupportedPropertyType.java32
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithoutProperties.java29
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java28
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java90
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java114
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java78
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaTest.java40
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/MethodTest.java46
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java171
18 files changed, 1154 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/SquidUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/SquidUtilsTest.java
new file mode 100644
index 00000000000..643d24d575e
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/SquidUtilsTest.java
@@ -0,0 +1,41 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch;
+
+import org.junit.Test;
+import org.sonar.api.resources.JavaFile;
+
+public class SquidUtilsTest {
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void convertJavaFileKeyFromSquidFormat() {
+ SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldConvertJavaPackageKeyFromSquidFormat() {
+ SquidUtils.convertJavaPackageKeyFromSquidFormat("java/lang");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldConvertToSquidKeyFormat() {
+ SquidUtils.convertToSquidKeyFormat(new JavaFile("com.foo.Bar"));
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/charts/ChartParametersTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/charts/ChartParametersTest.java
new file mode 100644
index 00000000000..7ef39e3b730
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/charts/ChartParametersTest.java
@@ -0,0 +1,79 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.charts;
+
+import org.junit.Test;
+
+import java.util.Locale;
+
+import static org.junit.Assert.*;
+
+public class ChartParametersTest {
+ @Test
+ public void shouldForbidHighSizeForSecurityReasons() {
+ String url = ChartParameters.PARAM_WIDTH + "=100000&" + ChartParameters.PARAM_HEIGHT + "=9999999";
+ ChartParameters params = new ChartParameters(url);
+ assertEquals(ChartParameters.MAX_WIDTH, params.getWidth());
+ assertEquals(ChartParameters.MAX_HEIGHT, params.getHeight());
+ }
+
+ @Test
+ public void shouldReadImageSizeFromParameters() {
+ String url = ChartParameters.PARAM_WIDTH + "=200&" + ChartParameters.PARAM_HEIGHT + "=300";
+ ChartParameters params = new ChartParameters(url);
+ assertEquals(200, params.getWidth());
+ assertEquals(300, params.getHeight());
+ }
+
+ @Test
+ public void shouldGetDefaultSizesIfNoParameters() {
+ ChartParameters params = new ChartParameters("foo=bar");
+ assertEquals(ChartParameters.DEFAULT_WIDTH, params.getWidth());
+ assertEquals(ChartParameters.DEFAULT_HEIGHT, params.getHeight());
+ }
+
+ @Test
+ public void shouldDecodeValue() {
+ ChartParameters params = new ChartParameters("foo=0%3D10,3%3D8");
+ assertEquals("0=10,3=8", params.getValue("foo", "", true));
+ assertEquals("0%3D10,3%3D8", params.getValue("foo"));
+ assertNull(params.getValue("bar", null, true));
+ }
+
+ @Test
+ public void shouldDecodeValues() {
+ ChartParameters params = new ChartParameters("foo=0%3D10,3%3D8|5%3D5,7%3D17");
+ assertArrayEquals(new String[]{"0%3D10,3%3D8", "5%3D5,7%3D17"}, params.getValues("foo", "|"));
+ assertArrayEquals(new String[]{"0=10,3=8", "5=5,7=17"}, params.getValues("foo", "|", true));
+ assertArrayEquals(new String[0], params.getValues("bar", "|", true));
+ }
+
+ @Test
+ public void getLocale() {
+ ChartParameters params = new ChartParameters("foo=0&locale=fr");
+ assertEquals(Locale.FRENCH, params.getLocale());
+
+ params = new ChartParameters("foo=0&locale=fr-CH");
+ assertEquals("fr-ch", params.getLocale().getLanguage());
+
+ params = new ChartParameters("foo=0");
+ assertEquals(Locale.ENGLISH, params.getLocale());
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java
new file mode 100644
index 00000000000..fbdda5849f6
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java
@@ -0,0 +1,32 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.RuleProperty;
+
+abstract class AbstractCheck {
+
+ @RuleProperty
+ private Integer max;
+
+ public Integer getMax() {
+ return max;
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java
new file mode 100644
index 00000000000..f783eade75f
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java
@@ -0,0 +1,199 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.utils.SonarException;
+
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+public class AnnotationCheckFactoryTest {
+
+ @org.junit.Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void createCheckWithoutProperties() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ ActiveRule activeRule = profile.activateRule(Rule.create("repo", "org.sonar.api.checks.CheckWithoutProperties", ""), null);
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithoutProperties.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertThat(check).isInstanceOf(CheckWithoutProperties.class);
+ }
+
+ @Test
+ public void createCheckWithStringProperty() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithStringProperty", "");
+ rule.createParameter("pattern");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("pattern", "foo");
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithStringProperty.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertThat(check).isInstanceOf(CheckWithStringProperty.class);
+ assertThat(((CheckWithStringProperty) check).getPattern()).isEqualTo("foo");
+ }
+
+ @Test
+ public void failIfMissingProperty() {
+ thrown.expect(SonarException.class);
+
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithStringProperty", "");
+ rule.createParameter("unknown");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("unknown", "bar");
+ AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithStringProperty.class));
+ }
+
+ @Test
+ public void createCheckWithPrimitiveProperties() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithPrimitiveProperties", "");
+ rule.createParameter("max");
+ rule.createParameter("ignore");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("max", "300");
+ activeRule.setParameter("ignore", "true");
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithPrimitiveProperties.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300);
+ assertThat(((CheckWithPrimitiveProperties) check).isIgnore()).isTrue();
+ }
+
+ @Test
+ public void createCheckWithIntegerProperty() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithIntegerProperty", "");
+ rule.createParameter("max");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("max", "300");
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithIntegerProperty.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertThat(((CheckWithIntegerProperty) check).getMax()).isEqualTo(300);
+ }
+
+ /**
+ * SONAR-3164
+ */
+ @Test
+ public void setValueOfInheritedField() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.ImplementedCheck", "");
+ rule.createParameter("max");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("max", "300");
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(ImplementedCheck.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertThat(((ImplementedCheck) check).getMax()).isEqualTo(300);
+ }
+
+ @Test
+ public void failIfPropertyTypeIsNotSupported() {
+ thrown.expect(SonarException.class);
+
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithUnsupportedPropertyType", "");
+ rule.createParameter("max");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("max", "300");
+ AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithUnsupportedPropertyType.class));
+ }
+
+ @Test
+ public void shouldOverridePropertyKey() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithOverriddenPropertyKey", "");
+ rule.createParameter("maximum");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("maximum", "300");
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithOverriddenPropertyKey.class));
+
+ Object check = factory.getCheck(activeRule);
+ assertThat(((CheckWithOverriddenPropertyKey) check).getMax()).isEqualTo(300);
+ }
+
+ @Test
+ public void shouldWorkWithClonedRules() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "CheckWithKey", "");
+ Rule clonedRule = Rule.create("repo", "CheckWithKey_2", "").setConfigKey("CheckWithKey").setTemplate(rule);
+
+ profile.activateRule(rule, null);
+ profile.activateRule(clonedRule, null);
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithKey.class));
+
+ assertThat(factory.getChecks()).doesNotContain(new Object[] {null});
+ }
+
+ /**
+ * SONAR-2900
+ */
+ @Test
+ public void create_accept_objects() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ ActiveRule activeRule = profile.activateRule(Rule.create("repo", "org.sonar.api.checks.CheckWithoutProperties", ""), null);
+ CheckWithoutProperties checkInstance = new CheckWithoutProperties();
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(checkInstance));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertSame(check, checkInstance);
+ }
+
+ @Test
+ public void create_instance_with_string_property() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithStringProperty", "");
+ rule.createParameter("pattern");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("pattern", "foo");
+ CheckWithStringProperty checkInstance = new CheckWithStringProperty();
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(checkInstance));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertSame(check, checkInstance);
+ assertThat(checkInstance.getPattern()).isEqualTo("foo");
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithIntegerProperty.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithIntegerProperty.java
new file mode 100644
index 00000000000..1e48ebf494e
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithIntegerProperty.java
@@ -0,0 +1,33 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.*;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithIntegerProperty {
+
+ @RuleProperty
+ private Integer max;
+
+ public Integer getMax() {
+ return max;
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java
new file mode 100644
index 00000000000..47ac8e601f7
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java
@@ -0,0 +1,28 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+
+@Rule(key = "CheckWithKey", priority = Priority.CRITICAL)
+public class CheckWithKey {
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithOverriddenPropertyKey.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithOverriddenPropertyKey.java
new file mode 100644
index 00000000000..7677ce5f791
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithOverriddenPropertyKey.java
@@ -0,0 +1,35 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.check.RuleProperty;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithOverriddenPropertyKey{
+
+ @RuleProperty(key = "maximum")
+ private int max = 50;
+
+ public int getMax() {
+ return max;
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithPrimitiveProperties.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithPrimitiveProperties.java
new file mode 100644
index 00000000000..f33308bf5b4
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithPrimitiveProperties.java
@@ -0,0 +1,43 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.check.RuleProperty;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithPrimitiveProperties {
+
+ @RuleProperty(description = "Maximum threshold")
+ private int max = 50;
+
+ @RuleProperty
+ private boolean ignore;
+
+ public int getMax() {
+ return max;
+ }
+
+ public boolean isIgnore() {
+ return ignore;
+ }
+}
+
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithStringProperty.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithStringProperty.java
new file mode 100644
index 00000000000..260fd41779a
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithStringProperty.java
@@ -0,0 +1,36 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.check.RuleProperty;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithStringProperty {
+
+ @RuleProperty
+ private String pattern;
+
+ public String getPattern() {
+ return pattern;
+ }
+}
+
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithUnsupportedPropertyType.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithUnsupportedPropertyType.java
new file mode 100644
index 00000000000..fd4def57f8d
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithUnsupportedPropertyType.java
@@ -0,0 +1,32 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.check.RuleProperty;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithUnsupportedPropertyType {
+
+ @RuleProperty
+ private StringBuilder max = null;
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithoutProperties.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithoutProperties.java
new file mode 100644
index 00000000000..b8facf9371a
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithoutProperties.java
@@ -0,0 +1,29 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+
+@Rule(priority = Priority.CRITICAL)
+class CheckWithoutProperties {
+
+}
+
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java
new file mode 100644
index 00000000000..cb3c78d5793
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java
@@ -0,0 +1,28 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+
+@Rule(priority = Priority.CRITICAL)
+class ImplementedCheck extends AbstractCheck {
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java
new file mode 100644
index 00000000000..8656fa5d48a
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java
@@ -0,0 +1,90 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.issue.Issue;
+import org.sonar.api.issue.batch.IssueFilterChain;
+import org.sonar.api.resources.File;
+import org.sonar.api.rule.RuleKey;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class NoSonarFilterTest {
+
+ private SonarIndex sonarIndex = mock(SonarIndex.class);
+ NoSonarFilter filter = new NoSonarFilter(sonarIndex);
+ private File javaFile;
+ IssueFilterChain chain = mock(IssueFilterChain.class);
+
+ @Before
+ public void prepare() {
+ when(chain.accept(isA(Issue.class))).thenReturn(true);
+ javaFile = File.create("org/foo/Bar.java");
+ javaFile.setEffectiveKey("struts:org/foo/Bar.java");
+ when(sonarIndex.getResource(javaFile)).thenReturn(javaFile);
+ }
+
+ @Test
+ public void ignoreLinesCommentedWithNoSonar() {
+ Set<Integer> noSonarLines = new HashSet<>();
+ noSonarLines.add(31);
+ noSonarLines.add(55);
+ filter.addResource(javaFile, noSonarLines);
+
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org/foo/Bar.java");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "Foo"));
+
+ // violation on class
+ assertThat(filter.accept(issue, chain)).isTrue();
+
+ // violation on lines
+ when(issue.line()).thenReturn(30);
+ assertThat(filter.accept(issue, chain)).isTrue();
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue, chain)).isFalse();
+ }
+
+ @Test
+ public void should_accept_violations_from_no_sonar_rules() {
+ // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !!
+
+ Set<Integer> noSonarLines = new HashSet<>();
+ noSonarLines.add(31);
+ filter.addResource(javaFile, noSonarLines);
+
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org.apache.Action");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "NoSonarCheck"));
+
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue, chain)).isTrue();
+
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java
new file mode 100644
index 00000000000..017e6c4a34b
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java
@@ -0,0 +1,114 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+import java.util.List;
+
+public class JavaFileTest {
+
+ JavaFile javaFile = new JavaFile();
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testConstructor() {
+ JavaFile javaClass = new JavaFile("", "");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testConstructor2() {
+ JavaFile javaClass = new JavaFile("", "", true);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testConstructor3() {
+ JavaFile javaClass = new JavaFile("");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testConstructor4() {
+ JavaFile javaClass = new JavaFile("", true);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetParent() {
+ javaFile.getParent();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetDescription() {
+ javaFile.getDescription();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetLanguage() {
+ javaFile.getLanguage();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetName() {
+ javaFile.getName();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetLongName() {
+ javaFile.getLongName();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetScope() {
+ javaFile.getScope();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetQualifier() {
+ javaFile.getQualifier();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testIsUnitTest() {
+ javaFile.isUnitTest();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testMathFilePattern() {
+ javaFile.matchFilePattern("");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fromIoFile1() {
+ JavaFile.fromIOFile(null, (Project) null, true);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fromIoFile2() {
+ JavaFile.fromIOFile(null, (List<java.io.File>) null, true);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fromRelativePath() {
+ JavaFile.fromRelativePath("", false);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fromAbsolutePath() {
+ JavaFile.fromAbsolutePath("", (List<java.io.File>) null, false);
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java
new file mode 100644
index 00000000000..10479b91229
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java
@@ -0,0 +1,78 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+public class JavaPackageTest {
+
+ JavaPackage javaPackage = new JavaPackage();
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testConstructor() {
+ new JavaPackage("");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetParent() {
+ javaPackage.getParent();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetDescription() {
+ javaPackage.getDescription();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetLanguage() {
+ javaPackage.getLanguage();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetName() {
+ javaPackage.getName();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetLongName() {
+ javaPackage.getLongName();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetScope() {
+ javaPackage.getScope();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetQualifier() {
+ javaPackage.getQualifier();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testIsUnitTest() {
+ javaPackage.isDefault();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testMathFilePattern() {
+ javaPackage.matchFilePattern("");
+ }
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaTest.java
new file mode 100644
index 00000000000..f77fba190b1
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaTest.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JavaTest {
+
+ @Test
+ public void test() {
+ Java language = new Java();
+ assertThat(language.getFileSuffixes()).isEqualTo(new String[] {".java", ".jav"});
+
+ assertThat(Java.isJavaFile(new java.io.File("Example.java"))).isTrue();
+ assertThat(Java.isJavaFile(new java.io.File("Example.JAVA"))).isTrue();
+ assertThat(Java.isJavaFile(new java.io.File("Example.jav"))).isTrue();
+ assertThat(Java.isJavaFile(new java.io.File("Example.Jav"))).isTrue();
+ assertThat(Java.isJavaFile(new java.io.File("Example.notjava"))).isFalse();
+ }
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/MethodTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/MethodTest.java
new file mode 100644
index 00000000000..8d8c7b66f80
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/MethodTest.java
@@ -0,0 +1,46 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class MethodTest {
+
+ @Test
+ public void shouldAcceptOverridding() {
+ Method method = Method.createMethod("Foo.foo()", Java.INSTANCE);
+ Method fakeMethod = new FakeMethod("Foo.foo()");
+
+ assertThat(method.equals(fakeMethod), is(true));
+ assertThat(fakeMethod.equals(method), is(true));
+
+ assertThat(method.hashCode(), is(fakeMethod.hashCode()));
+ assertThat(fakeMethod.hashCode(), is(method.hashCode()));
+ }
+
+ static class FakeMethod extends Method {
+ protected FakeMethod(String key) {
+ super(key, Qualifiers.METHOD, Java.INSTANCE);
+ }
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
new file mode 100644
index 00000000000..6f5f7c9c9bd
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
@@ -0,0 +1,171 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.rules;
+
+import java.util.Collections;
+import java.util.List;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.PropertyType;
+import org.sonar.api.utils.SonarException;
+import org.sonar.check.Priority;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class AnnotationRuleParserTest {
+
+ @org.junit.Rule
+ public final ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void rule_with_property() {
+ List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class);
+ assertThat(rules).hasSize(1);
+ Rule rule = rules.get(0);
+ assertThat(rule.getKey()).isEqualTo("foo");
+ assertThat(rule.getName()).isEqualTo("bar");
+ assertThat(rule.getDescription()).isEqualTo("Foo Bar");
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(rule.getParams()).hasSize(1);
+
+ RuleParam prop = rule.getParam("property");
+ assertThat(prop.getKey()).isEqualTo("property");
+ assertThat(prop.getDescription()).isEqualTo("Ignore ?");
+ assertThat(prop.getDefaultValue()).isEqualTo("false");
+ assertThat(prop.getType()).isEqualTo(PropertyType.STRING.name());
+ }
+
+ @Test
+ public void rule_with_integer_property() {
+ List<Rule> rules = parseAnnotatedClass(RuleWithIntegerProperty.class);
+
+ RuleParam prop = rules.get(0).getParam("property");
+ assertThat(prop.getDescription()).isEqualTo("Max");
+ assertThat(prop.getDefaultValue()).isEqualTo("12");
+ assertThat(prop.getType()).isEqualTo(PropertyType.INTEGER.name());
+ }
+
+ @Test
+ public void rule_with_text_property() {
+ List<Rule> rules = parseAnnotatedClass(RuleWithTextProperty.class);
+
+ RuleParam prop = rules.get(0).getParam("property");
+ assertThat(prop.getDescription()).isEqualTo("text");
+ assertThat(prop.getDefaultValue()).isEqualTo("Long text");
+ assertThat(prop.getType()).isEqualTo(PropertyType.TEXT.name());
+ }
+
+ @Test
+ public void should_reject_invalid_property_types() {
+ exception.expect(SonarException.class);
+ exception.expectMessage("Invalid property type [INVALID]");
+
+ parseAnnotatedClass(RuleWithInvalidPropertyType.class);
+ }
+
+ @Test
+ public void should_recognize_type() {
+ assertThat(AnnotationRuleParser.guessType(Integer.class)).isEqualTo(PropertyType.INTEGER);
+ assertThat(AnnotationRuleParser.guessType(int.class)).isEqualTo(PropertyType.INTEGER);
+ assertThat(AnnotationRuleParser.guessType(Float.class)).isEqualTo(PropertyType.FLOAT);
+ assertThat(AnnotationRuleParser.guessType(float.class)).isEqualTo(PropertyType.FLOAT);
+ assertThat(AnnotationRuleParser.guessType(Boolean.class)).isEqualTo(PropertyType.BOOLEAN);
+ assertThat(AnnotationRuleParser.guessType(boolean.class)).isEqualTo(PropertyType.BOOLEAN);
+ assertThat(AnnotationRuleParser.guessType(String.class)).isEqualTo(PropertyType.STRING);
+ assertThat(AnnotationRuleParser.guessType(Object.class)).isEqualTo(PropertyType.STRING);
+ }
+
+ @Test
+ public void rule_without_name_nor_description() {
+ List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class);
+ assertThat(rules).hasSize(1);
+ Rule rule = rules.get(0);
+ assertThat(rule.getKey()).isEqualTo("foo");
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
+ assertThat(rule.getName()).isNull();
+ assertThat(rule.getDescription()).isNull();
+ }
+
+ @Test
+ public void rule_without_key() {
+ List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class);
+ assertThat(rules).hasSize(1);
+ Rule rule = rules.get(0);
+ assertThat(rule.getKey()).isEqualTo(RuleWithoutKey.class.getCanonicalName());
+ assertThat(rule.getName()).isEqualTo("foo");
+ assertThat(rule.getDescription()).isNull();
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
+ }
+
+ @Test
+ public void overridden_rule() {
+ List<Rule> rules = parseAnnotatedClass(OverridingRule.class);
+ assertThat(rules).hasSize(1);
+ Rule rule = rules.get(0);
+ assertThat(rule.getKey()).isEqualTo("overriding_foo");
+ assertThat(rule.getName()).isEqualTo("Overriding Foo");
+ assertThat(rule.getDescription()).isNull();
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
+ assertThat(rule.getParams()).hasSize(2);
+ }
+
+ private List<Rule> parseAnnotatedClass(Class annotatedClass) {
+ return new AnnotationRuleParser().parse("repo", Collections.singleton(annotatedClass));
+ }
+
+ @org.sonar.check.Rule(name = "foo")
+ static class RuleWithoutKey {
+ }
+
+ @org.sonar.check.Rule(key = "foo")
+ static class RuleWithoutNameNorDescription {
+ }
+
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
+ static class RuleWithProperty {
+ @org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false")
+ private String property;
+ }
+
+ @org.sonar.check.Rule(key = "overriding_foo", name = "Overriding Foo")
+ static class OverridingRule extends RuleWithProperty {
+ @org.sonar.check.RuleProperty
+ private String additionalProperty;
+ }
+
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
+ static class RuleWithIntegerProperty {
+ @org.sonar.check.RuleProperty(description = "Max", defaultValue = "12")
+ private Integer property;
+ }
+
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
+ static class RuleWithTextProperty {
+ @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "TEXT")
+ protected String property;
+ }
+
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
+ static class RuleWithInvalidPropertyType {
+ @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "INVALID")
+ public String property;
+ }
+}