diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-09 15:00:33 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-10 17:06:52 +0200 |
commit | e21b7e7d1128ea6469a286d2ab5526aa3bc457ea (patch) | |
tree | 94e104b55f2ba7df610a755dd44b50de9899e82b | |
parent | 1d496ada626be5dbdfe6dce8c71b9adc4fbd813d (diff) | |
download | sonarqube-e21b7e7d1128ea6469a286d2ab5526aa3bc457ea.tar.gz sonarqube-e21b7e7d1128ea6469a286d2ab5526aa3bc457ea.zip |
Create DebtModelHolderRule
2 files changed, 99 insertions, 10 deletions
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 index 00000000000..7fe16e70aad --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/debt/DebtModelHolderRule.java @@ -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; + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/DebtAggregatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/DebtAggregatorTest.java index ccdcaaffb61..044739a713f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/DebtAggregatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/DebtAggregatorTest.java @@ -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 |