diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-01-06 17:56:32 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-01-06 18:28:20 +0400 |
commit | 0840eed58de37f06a5f8fabfe953e8faf721b0ba (patch) | |
tree | e91435966c47237afd5ca73754af95f278a9e339 | |
parent | a13a46a5027574d4efe4d656b1d650f2e3d6396f (diff) | |
download | sonarqube-0840eed58de37f06a5f8fabfe953e8faf721b0ba.tar.gz sonarqube-0840eed58de37f06a5f8fabfe953e8faf721b0ba.zip |
SONAR-3144 Fix corner case, when implementations of CpdMapping not presented
-rw-r--r-- | plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java | 17 | ||||
-rw-r--r-- | plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/PmdEngineTest.java | 37 |
2 files changed, 43 insertions, 11 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java index 48584a83d65..dbf96393691 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java @@ -33,9 +33,15 @@ import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; import org.sonar.duplications.cpd.CPD; +import com.google.common.annotations.VisibleForTesting; + public class PmdEngine extends CpdEngine { - private CpdMapping[] mappings; + private final CpdMapping[] mappings; + + public PmdEngine() { + this.mappings = null; + } public PmdEngine(CpdMapping[] mappings) { this.mappings = mappings; @@ -47,9 +53,11 @@ public class PmdEngine extends CpdEngine { } private CpdMapping getMapping(Language language) { - for (CpdMapping cpdMapping : mappings) { - if (cpdMapping.getLanguage().equals(language)) { - return cpdMapping; + if (mappings != null) { + for (CpdMapping cpdMapping : mappings) { + if (cpdMapping.getLanguage().equals(language)) { + return cpdMapping; + } } } return null; @@ -94,6 +102,7 @@ public class PmdEngine extends CpdEngine { return cpd; } + @VisibleForTesting int getMinimumTokens(Project project) { Configuration conf = project.getConfiguration(); return conf.getInt("sonar.cpd." + project.getLanguageKey() + ".minimumTokens", diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/PmdEngineTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/PmdEngineTest.java index 11201041de9..361cfaff624 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/PmdEngineTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/PmdEngineTest.java @@ -20,21 +20,44 @@ package org.sonar.plugins.cpd; import static junit.framework.Assert.assertEquals; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.batch.CpdMapping; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; public class PmdEngineTest { @Test + public void shouldNotFailWhenNoMappings() { + PmdEngine engine = new PmdEngine(); + assertThat(engine.isLanguageSupported(Java.INSTANCE), is(false)); + } + + @Test + public void shouldCheckLanguageSupport() { + CpdMapping mapping = mock(CpdMapping.class); + when(mapping.getLanguage()).thenReturn(Java.INSTANCE); + PmdEngine engine = new PmdEngine(new CpdMapping[] { mapping }); + assertThat(engine.isLanguageSupported(Java.INSTANCE), is(true)); + + Language anotherLanguage = mock(Language.class); + assertThat(engine.isLanguageSupported(anotherLanguage), is(false)); + } + + @Test public void defaultMinimumTokens() { Project project = createJavaProject().setConfiguration(new PropertiesConfiguration()); - PmdEngine sensor = new PmdEngine(new CpdMapping[0]); - assertEquals(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE, sensor.getMinimumTokens(project)); + PmdEngine engine = new PmdEngine(); + assertEquals(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE, engine.getMinimumTokens(project)); } @Test @@ -43,8 +66,8 @@ public class PmdEngineTest { conf.setProperty("sonar.cpd.minimumTokens", "33"); Project project = createJavaProject().setConfiguration(conf); - PmdEngine sensor = new PmdEngine(new CpdMapping[0]); - assertEquals(33, sensor.getMinimumTokens(project)); + PmdEngine engine = new PmdEngine(); + assertEquals(33, engine.getMinimumTokens(project)); } @Test @@ -56,9 +79,9 @@ public class PmdEngineTest { Project phpProject = createPhpProject().setConfiguration(conf); Project javaProject = createJavaProject().setConfiguration(conf); - PmdEngine sensor = new PmdEngine(new CpdMapping[0]); - assertEquals(100, sensor.getMinimumTokens(javaProject)); - assertEquals(33, sensor.getMinimumTokens(phpProject)); + PmdEngine engine = new PmdEngine(); + assertEquals(100, engine.getMinimumTokens(javaProject)); + assertEquals(33, engine.getMinimumTokens(phpProject)); } private Project createJavaProject() { |