]> source.dussan.org Git - sonarqube.git/commitdiff
Revert move of TimeMachineConfigution
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 22 Jun 2011 17:11:39 +0000 (19:11 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 22 Jun 2011 17:11:39 +0000 (19:11 +0200)
17 files changed:
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzer.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java [deleted file]
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java [deleted file]
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java
plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest/shared.xml [deleted file]
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml [new file with mode: 0644]

index 87d1e4064420ea517dea6fc6f8bf5bac808b6f1f..76c1bdf43dcb44a124de0059758a253819f801ec 100644 (file)
@@ -218,7 +218,6 @@ public class CorePlugin extends SonarPlugin {
     extensions.add(ReferenceAnalysis.class);
 
     // time machine
-    extensions.add(TimeMachineConfiguration.class);
     extensions.add(TendencyDecorator.class);
     extensions.add(VariationDecorator.class);
     extensions.add(ViolationTrackingDecorator.class);
index 666d89082b576c9b521992e15a5a5cc98d1dbc17..d44ffd056e1c9646a81da437aa54da48b72ed6f9 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.core.NotDryRun;
 
 import java.util.Arrays;
index 4e00eb4c99fb00fa219962f404d7bbf68d9b1257..86a150e28cdcba5020c5a22eee44125974917d2f 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.api.rules.Violation;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.core.NotDryRun;
 
 import java.util.*;
index 5a52622f9ec3e45a15cc2ad741566e249815d13f..ade65afb788b3be99ff2bc48f81a232dee38cd01 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.core.NotDryRun;
 
 import java.util.List;
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java
deleted file mode 100644 (file)
index 0ef49a9..0000000
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 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.core.timemachine;
-
-import com.google.common.collect.Lists;
-import org.apache.commons.configuration.Configuration;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.BatchExtension;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.utils.Logs;
-import org.sonar.batch.components.PastSnapshot;
-import org.sonar.batch.components.PastSnapshotFinder;
-
-import javax.persistence.Query;
-import java.util.Collections;
-import java.util.List;
-
-public class TimeMachineConfiguration implements BatchExtension {
-
-  private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
-
-  private Project project;
-  private final Configuration configuration;
-  private List<PastSnapshot> projectPastSnapshots;
-  private DatabaseSession session;
-
-  public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration, PastSnapshotFinder pastSnapshotFinder) {
-    this.session = session;
-    this.project = project;
-    this.configuration = configuration;
-    initPastSnapshots(pastSnapshotFinder);
-  }
-
-  private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder) {
-    Snapshot projectSnapshot = buildProjectSnapshot();
-
-    projectPastSnapshots = Lists.newLinkedList();
-    if (projectSnapshot != null) {
-      for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
-        PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index);
-        if (pastSnapshot != null) {
-          log(pastSnapshot);
-          projectPastSnapshots.add(pastSnapshot);
-        }
-      }
-    }
-  }
-
-  private Snapshot buildProjectSnapshot() {
-    Query query = session.createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
-    query.setParameter("resourceKey", project.getKey());
-    query.setParameter("lib", Qualifiers.LIBRARY);
-    query.setParameter("enabled", Boolean.TRUE);
-
-    Snapshot snapshot = null;
-    Number projectId = session.getSingleResult(query, null);
-    if (projectId != null) {
-      snapshot = new Snapshot();
-      snapshot.setResourceId(projectId.intValue());
-      snapshot.setCreatedAt(project.getAnalysisDate());
-    }
-    return snapshot;
-  }
-
-  private void log(PastSnapshot pastSnapshot) {
-    String qualifier = pastSnapshot.getQualifier();
-    // hack to avoid too many logs when the views plugin is installed
-    if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
-      LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString());
-    } else {
-      Logs.INFO.info(pastSnapshot.toString());
-    }
-  }
-
-  public boolean skipTendencies() {
-    return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE);
-  }
-
-  public int getTendencyPeriodInDays() {
-    return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE);
-  }
-
-  public List<PastSnapshot> getProjectPastSnapshots() {
-    return projectPastSnapshots;
-  }
-
-  public boolean isFileVariationEnabled() {
-    return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE);
-  }
-}
index 6021e9ef78c785f13829cb2efd83f7cd35b077e8..889a9010d78086f02cbd6c02b07df7d93780e4d6 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.core.NotDryRun;
 
 import java.util.List;
index 5299904a13b67da4151734f7781f19e1727c8d3c..c475fbfbe092d9905efb0b4d1b2a46394dde6d08 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
 import org.sonar.batch.components.PastMeasuresLoader;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 
 import java.util.Collection;
 import java.util.List;
index b027004067be1c47354d8a1853dbbfb2239314db..22a239811b3cd3257970b8a373d83e3ffe79384c 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.api.rules.Violation;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 
 import java.util.Arrays;
 import java.util.Date;
index 5999463ec2174864956ff34d8c74e2dbe54f5dcf..3420aaefed1eff12499eae5224f37234120e2e6c 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.plugins.core.timemachine;
 
-import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.Test;
 import org.sonar.api.batch.DecoratorContext;
 import org.sonar.api.batch.TimeMachine;
@@ -29,6 +28,7 @@ import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.JavaPackage;
 import org.sonar.api.resources.Project;
+import org.sonar.batch.components.TimeMachineConfiguration;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
index 21dcfefffecf244674b4a3d8af4e37eb450e13b5..afc51cf8969e7ac81729f6d50c245c9abfd56c1d 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
 import java.text.ParseException;
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java
deleted file mode 100644 (file)
index dda4f1a..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 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.core.timemachine;
-
-import org.apache.commons.configuration.PropertiesConfiguration;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.junit.Test;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
-import org.sonar.batch.components.PastSnapshotFinder;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
-
-public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase {
-
-  @Test
-  public void shouldSkipTendencies() {
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    conf.setProperty(CoreProperties.SKIP_TENDENCIES_PROPERTY, true);
-    assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(true));
-  }
-
-  @Test
-  public void shouldNotSkipTendenciesByDefault() {
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(false));
-  }
-
-  @Test
-  public void shouldInitPastSnapshots() {
-    setupData("shared");
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
-
-    new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, pastSnapshotFinder);
-
-    verify(pastSnapshotFinder).find(argThat(new BaseMatcher<Snapshot>() {
-      public boolean matches(Object o) {
-        return ((Snapshot) o).getResourceId() == 2 /* see database in shared.xml */;
-      }
-
-      public void describeTo(Description description) {
-      }
-    }), eq(conf), eq(1));
-  }
-
-  @Test
-  public void shouldNotInitPastSnapshotsIfFirstAnalysis() {
-    setupData("shared");
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
-
-    new TimeMachineConfiguration(getSession(), new Project("new:project"), conf, pastSnapshotFinder);
-
-    verifyZeroInteractions(pastSnapshotFinder);
-  }
-}
index 27c2fff471b7666a26f3b1fffdb7925218ba3cdd..9e4ac766aafde1eca95dc00d9e8bd43cdcf4144d 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.*;
 import org.sonar.batch.components.PastMeasuresLoader;
 import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
 import java.util.Arrays;
diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest/shared.xml
deleted file mode 100644 (file)
index 5e53bd4..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<dataset>
-
-  <projects long_name="[null]" id="1" scope="PRJ" kee="my:project" qualifier="LIB" name="my project as lib"
-            root_id="[null]" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
-
-  <projects long_name="[null]" id="2" scope="PRJ" kee="my:project" qualifier="TRK" name="my project"
-            root_id="[null]" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
-
-  <projects long_name="[null]" id="3" scope="DIR" kee="my:project:path/to/dir" qualifier="TRK" name="my dir"
-            root_id="2" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
-
-</dataset>
index ee7c07ebb3f3beed291863f02692edc8c4499503..532848bb4f29b9f1a9a79393b4f4210a7b55c745 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.resources.ProjectFileSystem;
 import org.sonar.api.rules.DefaultRulesManager;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.*;
+import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.batch.events.EventBus;
 import org.sonar.batch.index.DefaultIndex;
 import org.sonar.batch.index.DefaultResourcePersister;
@@ -78,6 +79,7 @@ public class ProjectModule extends Module {
       // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor)
       addComponent(getComponent(DefaultResourcePersister.class).getSnapshot(project));
     }
+    addComponent(TimeMachineConfiguration.class);
     addComponent(org.sonar.api.database.daos.MeasuresDao.class);
     addComponent(ProfilesDao.class);
     addComponent(AsyncMeasuresDao.class);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
new file mode 100644 (file)
index 0000000..02068a0
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 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.batch.components;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.BatchExtension;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.Logs;
+import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.PastSnapshotFinder;
+
+import javax.persistence.Query;
+import java.util.Collections;
+import java.util.List;
+
+public class TimeMachineConfiguration implements BatchExtension {
+
+  private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
+
+  private Project project;
+  private final Configuration configuration;
+  private List<PastSnapshot> projectPastSnapshots;
+  private DatabaseSession session;
+
+  public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration, PastSnapshotFinder pastSnapshotFinder) {
+    this.session = session;
+    this.project = project;
+    this.configuration = configuration;
+    initPastSnapshots(pastSnapshotFinder);
+  }
+
+  private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder) {
+    Snapshot projectSnapshot = buildProjectSnapshot();
+
+    projectPastSnapshots = Lists.newLinkedList();
+    if (projectSnapshot != null) {
+      for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
+        PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index);
+        if (pastSnapshot != null) {
+          log(pastSnapshot);
+          projectPastSnapshots.add(pastSnapshot);
+        }
+      }
+    }
+  }
+
+  private Snapshot buildProjectSnapshot() {
+    Query query = session.createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
+    query.setParameter("resourceKey", project.getKey());
+    query.setParameter("lib", Qualifiers.LIBRARY);
+    query.setParameter("enabled", Boolean.TRUE);
+
+    Snapshot snapshot = null;
+    Number projectId = session.getSingleResult(query, null);
+    if (projectId != null) {
+      snapshot = new Snapshot();
+      snapshot.setResourceId(projectId.intValue());
+      snapshot.setCreatedAt(project.getAnalysisDate());
+    }
+    return snapshot;
+  }
+
+  private void log(PastSnapshot pastSnapshot) {
+    String qualifier = pastSnapshot.getQualifier();
+    // hack to avoid too many logs when the views plugin is installed
+    if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
+      LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString());
+    } else {
+      Logs.INFO.info(pastSnapshot.toString());
+    }
+  }
+
+  public boolean skipTendencies() {
+    return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE);
+  }
+
+  public int getTendencyPeriodInDays() {
+    return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE);
+  }
+
+  public List<PastSnapshot> getProjectPastSnapshots() {
+    return projectPastSnapshots;
+  }
+
+  public boolean isFileVariationEnabled() {
+    return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE);
+  }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
new file mode 100644 (file)
index 0000000..78bd760
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 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.batch.components;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Project;
+import org.sonar.batch.components.PastSnapshotFinder;
+import org.sonar.batch.components.TimeMachineConfiguration;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.*;
+
+public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase {
+
+  @Test
+  public void shouldSkipTendencies() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.SKIP_TENDENCIES_PROPERTY, true);
+    assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(true));
+  }
+
+  @Test
+  public void shouldNotSkipTendenciesByDefault() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(false));
+  }
+
+  @Test
+  public void shouldInitPastSnapshots() {
+    setupData("shared");
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
+
+    new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, pastSnapshotFinder);
+
+    verify(pastSnapshotFinder).find(argThat(new BaseMatcher<Snapshot>() {
+      public boolean matches(Object o) {
+        return ((Snapshot) o).getResourceId() == 2 /* see database in shared.xml */;
+      }
+
+      public void describeTo(Description description) {
+      }
+    }), eq(conf), eq(1));
+  }
+
+  @Test
+  public void shouldNotInitPastSnapshotsIfFirstAnalysis() {
+    setupData("shared");
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
+
+    new TimeMachineConfiguration(getSession(), new Project("new:project"), conf, pastSnapshotFinder);
+
+    verifyZeroInteractions(pastSnapshotFinder);
+  }
+}
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml
new file mode 100644 (file)
index 0000000..5e53bd4
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <projects long_name="[null]" id="1" scope="PRJ" kee="my:project" qualifier="LIB" name="my project as lib"
+            root_id="[null]" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
+
+  <projects long_name="[null]" id="2" scope="PRJ" kee="my:project" qualifier="TRK" name="my project"
+            root_id="[null]" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
+
+  <projects long_name="[null]" id="3" scope="DIR" kee="my:project:path/to/dir" qualifier="TRK" name="my dir"
+            root_id="2" description="[null]" enabled="true" profile_id="[null]" language="java" copy_resource_id="[null]"/>
+
+</dataset>