]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 7 Feb 2012 16:50:01 +0000 (20:50 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 7 Feb 2012 16:57:18 +0000 (20:57 +0400)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java [new file with mode: 0644]

index 3a0e34881701fd7ff3c3dbe609fe1fe8dc021be4..ba5ca082512852f774935bad2c7ad7e99d44f4d9 100644 (file)
  */
 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());
   }
 
 }
index f78c6e5b76b2ba45691035466eb08731ef53168c..028b6f707a24c434027d676de084b56427a134d0 100644 (file)
@@ -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 (file)
index 0000000..840a08a
--- /dev/null
@@ -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));
+  }
+
+}