]> source.dussan.org Git - sonarqube.git/commitdiff
Create DebtModelHolderRule
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 9 Sep 2015 13:00:33 +0000 (15:00 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 10 Sep 2015 15:06:52 +0000 (17:06 +0200)
server/sonar-server/src/test/java/org/sonar/server/computation/debt/DebtModelHolderRule.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/issue/DebtAggregatorTest.java

diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/debt/DebtModelHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/debt/DebtModelHolderRule.java
new file mode 100644 (file)
index 0000000..7fe16e7
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.debt;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.After;
+import org.junit.rules.ExternalResource;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Rule to easily use a {@link DebtModelHolder} in a test.
+ * List of characteristics will be reset at the end of each test case.
+ *
+ * It will not fail if no characteristic have been set.
+ */
+public class DebtModelHolderRule extends ExternalResource implements DebtModelHolder {
+
+  private final List<Characteristic> rootCharacteristics = new ArrayList<>();
+  private final Map<Integer, Characteristic> characteristicById = new HashMap<>();
+
+  @After
+  public void after() {
+    rootCharacteristics.clear();
+    characteristicById.clear();
+  }
+
+  public DebtModelHolderRule addCharacteristics(Characteristic rootCharacteristic, Iterable<? extends Characteristic> subCharacteristics) {
+    requireNonNull(rootCharacteristic, "rootCharacteristic cannot be null");
+    requireNonNull(subCharacteristics, "subCharacteristics cannot be null");
+    checkArgument(subCharacteristics.iterator().hasNext(), "subCharacteristics cannot be empty");
+
+    rootCharacteristics.add(rootCharacteristic);
+    characteristicById.put(rootCharacteristic.getId(), rootCharacteristic);
+    for (Characteristic characteristic : subCharacteristics) {
+      characteristicById.put(characteristic.getId(), characteristic);
+    }
+    return this;
+  }
+
+  @Override
+  public Characteristic getCharacteristicById(int id) {
+    Characteristic characteristic = characteristicById.get(id);
+    if (characteristic == null) {
+      throw new IllegalStateException("Debt characteristic with id [" + id + "] does not exist");
+    }
+    return characteristic;
+  }
+
+  @Override
+  public boolean hasCharacteristicById(int id) {
+    return characteristicById.get(id) != null;
+  }
+
+  @Override
+  public List<Characteristic> getRootCharacteristics() {
+    return rootCharacteristics;
+  }
+
+}
index ccdcaaffb61b28f83643513699d0ca3b799cb1fb..044739a713f748cee54f9c901dde96310bf1420c 100644 (file)
@@ -29,8 +29,7 @@ import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.ReportComponent;
 import org.sonar.server.computation.debt.CharacteristicImpl;
-import org.sonar.server.computation.debt.DebtModelHolderImpl;
-import org.sonar.server.computation.debt.MutableDebtModelHolder;
+import org.sonar.server.computation.debt.DebtModelHolderRule;
 import org.sonar.server.computation.measure.Measure;
 import org.sonar.server.computation.measure.MeasureRepositoryRule;
 import org.sonar.server.computation.metric.MetricRepositoryRule;
@@ -45,22 +44,27 @@ public class DebtAggregatorTest {
   /**
    * Root characteristic
    */
-  public static final int PORTABILITY_ID = 1000;
+  static final int PORTABILITY_ID = 1000;
 
   /**
    * Sub-characteristic of {@link #PORTABILITY_ID}
    */
-  public static final int PORTABILITY_SOFT_ID = 1001;
+  static final int PORTABILITY_SOFT_ID = 1001;
 
   /**
    * Sub-characteristic of {@link #PORTABILITY_ID}
    */
-  public static final int PORTABILITY_HARD_ID = 1002;
+  static final int PORTABILITY_HARD_ID = 1002;
 
   /**
    * Root characteristic
    */
-  public static final int RELIABILITY_ID = 1003;
+  static final int RELIABILITY_ID = 1003;
+
+  /**
+   * Sub-characteristic of {@link #RELIABILITY_ID}
+   */
+  static final int DATA_RELIABILITY_ID = 1004;
 
   static final Component FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 2).addChildren(FILE).build();
@@ -70,11 +74,14 @@ public class DebtAggregatorTest {
   @org.junit.Rule
   public RuleRepositoryRule ruleRepository = new RuleRepositoryRule().add(RULE);
 
-  MutableDebtModelHolder debtModelHolder = new DebtModelHolderImpl()
-    .addCharacteristics(new CharacteristicImpl(PORTABILITY_ID, "PORTABILITY", null),
-      asList(new CharacteristicImpl(PORTABILITY_SOFT_ID, "PORTABILITY_HARDWARE", PORTABILITY_ID), new CharacteristicImpl(PORTABILITY_HARD_ID, "PORTABILITY_SOFTWARE", PORTABILITY_ID)))
+  @org.junit.Rule
+  public DebtModelHolderRule debtModelHolder = new DebtModelHolderRule()
+    .addCharacteristics(
+      new CharacteristicImpl(PORTABILITY_ID, "PORTABILITY", null),
+      asList(new CharacteristicImpl(PORTABILITY_SOFT_ID, "PORTABILITY_HARDWARE", PORTABILITY_ID),
+        new CharacteristicImpl(PORTABILITY_HARD_ID, "PORTABILITY_SOFTWARE", PORTABILITY_ID)))
     .addCharacteristics(new CharacteristicImpl(RELIABILITY_ID, "RELIABILITY", null),
-      asList(new CharacteristicImpl(1004, "DATA_RELIABILITY", RELIABILITY_ID))
+      asList(new CharacteristicImpl(DATA_RELIABILITY_ID, "DATA_RELIABILITY", RELIABILITY_ID))
     );
 
   @org.junit.Rule