--- /dev/null
+/*
+ * 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;
+ }
+
+}
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;
/**
* 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();
@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