From fda319b541eaa63b080da32f6526f4c83e94c416 Mon Sep 17 00:00:00 2001 From: Godin Date: Mon, 25 Oct 2010 14:42:24 +0000 Subject: [PATCH] Add unit test for cloned rules to AnnotationCheckFactory --- .../checks/AnnotationCheckFactoryTest.java | 32 +++++++++++++------ .../org/sonar/api/checks/CheckWithKey.java | 10 ++++++ 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java 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 index 7eec4aaac6a..4c4d4bdafb3 100644 --- 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 @@ -27,6 +27,9 @@ import org.sonar.api.utils.SonarException; import java.util.Arrays; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -37,7 +40,7 @@ public class AnnotationCheckFactoryTest { 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.asList(CheckWithoutProperties.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithoutProperties.class)); Object check = factory.getCheck(activeRule); assertNotNull(check); @@ -52,7 +55,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("pattern", "foo"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithStringProperty.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithStringProperty.class)); Object check = factory.getCheck(activeRule); assertNotNull(check); @@ -68,7 +71,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("unknown", "bar"); - AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithStringProperty.class)); + AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithStringProperty.class)); } @Test @@ -81,7 +84,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); activeRule.setParameter("ignore", "true"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithPrimitiveProperties.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithPrimitiveProperties.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithPrimitiveProperties) check).getMax(), is(300)); @@ -96,13 +99,12 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithIntegerProperty.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithIntegerProperty.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithIntegerProperty) check).getMax(), is(300)); } - @Test(expected = SonarException.class) public void failIfPropertyTypeIsNotSupported() { RulesProfile profile = RulesProfile.create("repo", "java"); @@ -111,7 +113,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); - AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithUnsupportedPropertyType.class)); + AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithUnsupportedPropertyType.class)); } @Test @@ -122,10 +124,22 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("maximum", "300"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(CheckWithOverriddenPropertyKey.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithOverriddenPropertyKey.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithOverriddenPropertyKey) check).getMax(), is(300)); } -} + @Test + public void shouldWorkWithClonedRules() { + RulesProfile profile = RulesProfile.create("repo", "java"); + Rule rule = Rule.create("repo", "CheckWithKey", ""); + Rule clonedRule = Rule.create("repo", "CheckWithKey_2", "").setParent(rule); + + profile.activateRule(rule, null); + profile.activateRule(clonedRule, null); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays. asList(CheckWithKey.class)); + + assertThat(factory.getChecks(), not(hasItems(nullValue()))); + } +} 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..9cd020d1404 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/CheckWithKey.java @@ -0,0 +1,10 @@ +package org.sonar.api.checks; + +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; +import org.sonar.check.Rule; + +@Rule(key = "CheckWithKey", isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +public class CheckWithKey { + +} -- 2.39.5