From 45fc5038ad1a362e7006dde7dfcdf624de59022f Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Tue, 7 Feb 2012 20:50:01 +0400 Subject: [PATCH] Fix some quality flaws --- .../sonar/plugins/cpd/index/IndexFactory.java | 17 +++-- .../org/sonar/plugins/cpd/CpdSensorTest.java | 8 +- .../plugins/cpd/index/IndexFactoryTest.java | 74 +++++++++++++++++++ 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java index 3a0e3488170..ba5ca082512 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java @@ -19,11 +19,13 @@ */ package org.sonar.plugins.cpd.index; +import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.batch.index.ResourcePersister; import org.sonar.core.duplication.DuplicationDao; @@ -32,17 +34,19 @@ public class IndexFactory implements BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(IndexFactory.class); + private final Settings settings; private final ResourcePersister resourcePersister; private final DuplicationDao dao; /** * For dry run, where is no access to database. */ - public IndexFactory() { - this(null, null); + public IndexFactory(Settings settings) { + this(settings, null, null); } - public IndexFactory(ResourcePersister resourcePersister, DuplicationDao dao) { + public IndexFactory(Settings settings, ResourcePersister resourcePersister, DuplicationDao dao) { + this.settings = settings; this.resourcePersister = resourcePersister; this.dao = dao; } @@ -60,10 +64,11 @@ public class IndexFactory implements BatchExtension { /** * @return true, if was enabled by user and database is available */ - private boolean isCrossProject(Project project) { - return project.getConfiguration().getBoolean(CoreProperties.CPD_CROSS_RPOJECT, CoreProperties.CPD_CROSS_RPOJECT_DEFAULT_VALUE) + @VisibleForTesting + boolean isCrossProject(Project project) { + return settings.getBoolean(CoreProperties.CPD_CROSS_RPOJECT) && resourcePersister != null && dao != null - && StringUtils.isBlank(project.getConfiguration().getString(CoreProperties.PROJECT_BRANCH_PROPERTY)); + && StringUtils.isBlank(project.getBranch()); } } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java index f78c6e5b76b..028b6f707a2 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java @@ -39,7 +39,7 @@ public class CpdSensorTest { Project project = createJavaProject().setConfiguration(conf); - CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory()), new PmdEngine(new CpdMapping[0])); + CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory(null)), new PmdEngine(new CpdMapping[0])); assertTrue(sensor.isSkipped(project)); } @@ -47,7 +47,7 @@ public class CpdSensorTest { public void doNotSkipByDefault() { Project project = createJavaProject().setConfiguration(new PropertiesConfiguration()); - CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory()), new PmdEngine(new CpdMapping[0])); + CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory(null)), new PmdEngine(new CpdMapping[0])); assertFalse(sensor.isSkipped(project)); } @@ -60,7 +60,7 @@ public class CpdSensorTest { Project phpProject = createPhpProject().setConfiguration(conf); Project javaProject = createJavaProject().setConfiguration(conf); - CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory()), new PmdEngine(new CpdMapping[0])); + CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory(null)), new PmdEngine(new CpdMapping[0])); assertTrue(sensor.isSkipped(phpProject)); assertFalse(sensor.isSkipped(javaProject)); } @@ -69,7 +69,7 @@ public class CpdSensorTest { public void engine() { PropertiesConfiguration conf = new PropertiesConfiguration(); Project project = createJavaProject().setConfiguration(conf); - CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory()), new PmdEngine(new CpdMapping[0])); + CpdSensor sensor = new CpdSensor(new SonarEngine(new IndexFactory(null)), new PmdEngine(new CpdMapping[0])); assertThat(sensor.isSonarEngineEnabled(project), is(true)); conf.setProperty("sonar.cpd.engine", "pmd"); diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java new file mode 100644 index 00000000000..840a08a1af9 --- /dev/null +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java @@ -0,0 +1,74 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.plugins.cpd.index; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Project; +import org.sonar.batch.index.ResourcePersister; +import org.sonar.core.duplication.DuplicationDao; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public class IndexFactoryTest { + + private Project project; + private Settings settings; + + @Before + public void setUp() { + project = new Project("foo"); + settings = new Settings(); + } + + @Test + public void crossProjectEnabled() { + settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); + IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); + assertThat(factory.isCrossProject(project), is(true)); + } + + @Test + public void noCrossProjectWithBranch() { + settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); + IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); + project.setBranch("branch"); + assertThat(factory.isCrossProject(project), is(false)); + } + + @Test + public void noCrossProjectWithoutDatabase() { + settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); + IndexFactory factory = new IndexFactory(settings); + assertThat(factory.isCrossProject(project), is(false)); + } + + @Test + public void crossProjectDisabled() { + settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "false"); + IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); + assertThat(factory.isCrossProject(project), is(false)); + } + +} -- 2.39.5