diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-15 22:54:26 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-15 22:54:26 +0100 |
commit | 93e365a6833d41304b654c2e9da87c58a694339e (patch) | |
tree | 841c265fa94ff1b33d5e1ffb91f3c5b5aa5f2286 /sonar-deprecated/src/test | |
parent | bc5680768a184c41e8c8871f8262ef607e01172c (diff) | |
download | sonarqube-93e365a6833d41304b654c2e9da87c58a694339e.tar.gz sonarqube-93e365a6833d41304b654c2e9da87c58a694339e.zip |
SONAR-4908 move some classes to module sonar-deprecated
Diffstat (limited to 'sonar-deprecated/src/test')
-rw-r--r-- | sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java | 172 | ||||
-rw-r--r-- | sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java | 65 |
2 files changed, 237 insertions, 0 deletions
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java new file mode 100644 index 00000000000..f844582db70 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java @@ -0,0 +1,172 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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 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 java.util.Collections; +import java.util.List; + +import static org.fest.assertions.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; + } +} 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..831e462e6b4 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/RuleUtilsTest.java @@ -0,0 +1,65 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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 org.apache.commons.configuration.Configuration; +import org.junit.Test; +import org.mockito.Matchers; +import org.sonar.api.CoreProperties; + +import java.util.Map; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class 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)); + } + +} |