From 7907bbe3283ae606f08569e40a2e4e15f770cc24 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Aug 2015 16:30:27 +0200 Subject: [PATCH] SONAR-6730 Add Test classes for MeasureComputer API --- .../api/test/ce/measure/ComponentImpl.java | 127 +++++++++++++++ .../test/ce/measure/MeasureComputerImpl.java | 112 +++++++++++++ .../MeasureComputerImplementationContext.java | 153 ++++++++++++++++++ .../MeasureComputerProviderContext.java | 74 +++++++++ .../ce/measure/MeasureComputerTester.java | 73 +++++++++ .../api/test/ce/measure/MeasureImpl.java | 84 ++++++++++ .../api/test/ce/measure/SettingsImpl.java | 58 +++++++ 7 files changed, 681 insertions(+) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/ComponentImpl.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImpl.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImplementationContext.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerProviderContext.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerTester.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureImpl.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/SettingsImpl.java diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/ComponentImpl.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/ComponentImpl.java new file mode 100644 index 00000000000..ca2936def4f --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/ComponentImpl.java @@ -0,0 +1,127 @@ +/* + * 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.api.test.ce.measure; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; +import org.sonar.api.ce.measure.Component; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + +@Immutable +public class ComponentImpl implements Component { + + private final String key; + + private final Type type; + + @CheckForNull + private final FileAttributes fileAttributes; + + public ComponentImpl(String key, Type type, @Nullable FileAttributes fileAttributes) { + this.key = requireNonNull(key, "Key cannot be null"); + this.type = requireNonNull(type, "Type cannot be null"); + this.fileAttributes = checkFileAttributes(fileAttributes); + } + + @CheckForNull + private FileAttributes checkFileAttributes(@Nullable FileAttributes fileAttributes) { + if (fileAttributes == null && type == Type.FILE) { + throw new IllegalArgumentException("Component of type FILE must have a FileAttributes object"); + } else if (fileAttributes != null && type != Type.FILE) { + throw new IllegalArgumentException("Only component of type FILE have a FileAttributes object"); + } + return fileAttributes; + } + + @Override + public Type getType() { + return type; + } + + @Override + public FileAttributes getFileAttributes() { + checkState(this.type == Component.Type.FILE, "Only component of type FILE have a FileAttributes object"); + return fileAttributes; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + ComponentImpl component = (ComponentImpl) o; + + return key.equals(component.key); + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return "ComponentImpl{" + + "key=" + key + + ", type='" + type + '\'' + + ", fileAttributes=" + fileAttributes + + '}'; + } + + @Immutable + public static class FileAttributesImpl implements FileAttributes { + + private final boolean unitTest; + private final String languageKey; + + public FileAttributesImpl(@Nullable String languageKey, boolean unitTest) { + this.languageKey = languageKey; + this.unitTest = unitTest; + } + + @Override + public boolean isUnitTest() { + return unitTest; + } + + @Override + @CheckForNull + public String getLanguageKey() { + return languageKey; + } + + @Override + public String toString() { + return "FileAttributesImpl{" + + "languageKey='" + languageKey + '\'' + + ", unitTest=" + unitTest + + '}'; + } + } +} + diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImpl.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImpl.java new file mode 100644 index 00000000000..653499c6efe --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImpl.java @@ -0,0 +1,112 @@ +/* + * 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.api.test.ce.measure; + +import com.google.common.collect.ImmutableSet; +import java.util.Set; +import org.sonar.api.ce.measure.MeasureComputer; + +import static com.google.common.base.Preconditions.checkArgument; + +public class MeasureComputerImpl implements MeasureComputer { + + private final Set inputMetricKeys; + private final Set outputMetrics; + private final Implementation measureComputerImplementation; + + public MeasureComputerImpl(MeasureComputerBuilderImpl builder) { + this.inputMetricKeys = ImmutableSet.copyOf(builder.inputMetricKeys); + this.outputMetrics = ImmutableSet.copyOf(builder.outputMetrics); + this.measureComputerImplementation = builder.measureComputerImplementation; + } + + @Override + public Set getInputMetrics() { + return inputMetricKeys; + } + + @Override + public Set getOutputMetrics() { + return outputMetrics; + } + + @Override + public Implementation getImplementation() { + return measureComputerImplementation; + } + + @Override + public String toString() { + return "MeasureComputerImpl{" + + "inputMetricKeys=" + inputMetricKeys + + ", outputMetrics=" + outputMetrics + + ", implementation=" + measureComputerImplementation.toString() + + '}'; + } + + public static class MeasureComputerBuilderImpl implements MeasureComputerBuilder { + + private String[] inputMetricKeys; + private String[] outputMetrics; + private Implementation measureComputerImplementation; + + @Override + public MeasureComputerBuilder setInputMetrics(String... inputMetrics) { + this.inputMetricKeys = inputMetrics; + checkInputMetricKeys(); + return this; + } + + @Override + public MeasureComputerBuilder setOutputMetrics(String... outputMetrics) { + this.outputMetrics = outputMetrics; + checkOutputMetricKeys(); + return this; + } + + @Override + public MeasureComputerBuilder setImplementation(Implementation impl) { + this.measureComputerImplementation = impl; + checkImplementation(); + return this; + } + + @Override + public MeasureComputer build() { + checkInputMetricKeys(); + checkOutputMetricKeys(); + checkImplementation(); + return new MeasureComputerImpl(this); + } + + private void checkInputMetricKeys(){ + checkArgument(this.inputMetricKeys != null && inputMetricKeys.length > 0, "At least one input metrics must be defined"); + } + + private void checkOutputMetricKeys(){ + checkArgument(this.outputMetrics != null && outputMetrics.length > 0, "At least one output metrics must be defined"); + } + + private void checkImplementation(){ + checkArgument(this.measureComputerImplementation != null, "The implementation is missing"); + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImplementationContext.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImplementationContext.java new file mode 100644 index 00000000000..f8182f5483f --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerImplementationContext.java @@ -0,0 +1,153 @@ +/* + * 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.api.test.ce.measure; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.annotation.CheckForNull; +import org.sonar.api.ce.measure.Component; +import org.sonar.api.ce.measure.Measure; +import org.sonar.api.ce.measure.MeasureComputer; +import org.sonar.api.ce.measure.Settings; + +import static com.google.common.base.Preconditions.checkArgument; + +public class MeasureComputerImplementationContext implements MeasureComputer.Implementation.Context { + + private final Component component; + private final MeasureComputer measureComputer; + private final Settings settings; + + private Map componentMeasureByMetricKey = new HashMap<>(); + private Multimap childrenComponentMeasureByMetricKey = ArrayListMultimap.create(); + + public MeasureComputerImplementationContext(Component component, Settings settings, MeasureComputer measureComputer) { + this.measureComputer = measureComputer; + this.settings = settings; + this.component = component; + } + + @Override + public Component getComponent() { + return component; + } + + @Override + public Settings getSettings() { + return settings; + } + + @Override + @CheckForNull + public Measure getMeasure(String metric) { + validateInputMetric(metric); + return componentMeasureByMetricKey.get(metric); + } + + @Override + public Iterable getChildrenMeasures(String metric) { + validateInputMetric(metric); + return childrenComponentMeasureByMetricKey.get(metric); + } + + @Override + public void addMeasure(String metricKey, int value) { + validateAddMeasure(metricKey); + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addInputMeasure(String metricKey, int value) { + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addIntegerChildrenMeasures(String metricKey, List values) { + for (Integer value : values) { + childrenComponentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + } + + @Override + public void addMeasure(String metricKey, double value) { + validateAddMeasure(metricKey); + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addInputMeasure(String metricKey, double value) { + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addDoubleChildrenMeasures(String metricKey, List values) { + for (Double value : values) { + childrenComponentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + } + + @Override + public void addMeasure(String metricKey, long value) { + validateAddMeasure(metricKey); + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addInputMeasure(String metricKey, long value) { + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addLongChildrenMeasures(String metricKey, List values) { + for (Long value : values) { + childrenComponentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + } + + @Override + public void addMeasure(String metricKey, String value) { + validateAddMeasure(metricKey); + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addInputMeasure(String metricKey, String value) { + componentMeasureByMetricKey.put(metricKey, MeasureImpl.createMeasure(value)); + } + + public void addStringChildrenMeasures(String metricKey, List values) { + for (String value : values) { + childrenComponentMeasureByMetricKey.put(metricKey,MeasureImpl.createMeasure(value)); + } + } + + private void validateInputMetric(String metric) { + Set allowedMetrics = new HashSet<>(); + allowedMetrics.addAll(measureComputer.getInputMetrics()); + allowedMetrics.addAll(measureComputer.getOutputMetrics()); + checkArgument(allowedMetrics.contains(metric), "Only metrics in %s can be used to load measures", measureComputer.getInputMetrics()); + } + + private void validateAddMeasure(String metricKey) { + checkArgument(measureComputer.getOutputMetrics().contains(metricKey), "Only metrics in %s can be used to add measures", measureComputer.getOutputMetrics()); + if (componentMeasureByMetricKey.get(metricKey) != null) { + throw new UnsupportedOperationException(String.format("A measure on metric '%s' already exists", metricKey)); + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerProviderContext.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerProviderContext.java new file mode 100644 index 00000000000..66318c0a967 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerProviderContext.java @@ -0,0 +1,74 @@ +/* + * 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.api.test.ce.measure; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.sonar.api.ce.measure.MeasureComputer; +import org.sonar.api.ce.measure.MeasureComputerProvider; + +public class MeasureComputerProviderContext implements MeasureComputerProvider.Context { + + private final List measureComputers = new ArrayList<>(); + private final Map computerByOutputMetrics = new HashMap<>(); + + @Override + public MeasureComputerProvider.Context add(MeasureComputer measureComputer) { + checkOutputMetricsNotAlreadyDefinedByAnotherComputer(measureComputer); + this.measureComputers.add(measureComputer); + for (String metric : measureComputer.getOutputMetrics()) { + computerByOutputMetrics.put(metric, measureComputer); + } + return this; + } + + public List getMeasureComputers() { + return measureComputers; + } + + private void checkOutputMetricsNotAlreadyDefinedByAnotherComputer(MeasureComputer measureComputer) { + Set duplicated = ImmutableSet.copyOf(Sets.intersection(computerByOutputMetrics.keySet(), measureComputer.getOutputMetrics())); + if (!duplicated.isEmpty()) { + throw new UnsupportedOperationException(generateErrorMsg(duplicated)); + } + } + + private String generateErrorMsg(Set duplicated){ + StringBuilder errorMsg = new StringBuilder(); + for (String duplicationMetric : duplicated) { + MeasureComputer otherComputer = computerByOutputMetrics.get(duplicationMetric); + errorMsg.append(String.format( + "The output metric '%s' is already declared by another computer. This computer has these input metrics '%s' and these output metrics '%s'. ", + duplicationMetric, otherComputer.getInputMetrics(), otherComputer.getOutputMetrics())); + } + return errorMsg.toString(); + } + + @Override + public MeasureComputer.MeasureComputerBuilder newMeasureComputerBuilder() { + return new MeasureComputerImpl.MeasureComputerBuilderImpl(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerTester.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerTester.java new file mode 100644 index 00000000000..f9d62ca11f5 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureComputerTester.java @@ -0,0 +1,73 @@ +/* + * 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.api.test.ce.measure; + +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; +import java.util.List; +import javax.annotation.CheckForNull; +import org.sonar.api.ce.measure.MeasureComputer; +import org.sonar.api.ce.measure.MeasureComputerProvider; + +public class MeasureComputerTester { + + private final MeasureComputerProviderContext context = new MeasureComputerProviderContext(); + + public MeasureComputerTester(MeasureComputerProvider... providers) { + for (MeasureComputerProvider provider : providers) { + provider.register(context); + } + } + + public MeasureComputerProvider.Context context() { + return context; + } + + public List getMeasureComputers() { + return context.getMeasureComputers(); + } + + /** + * Return the measure computer that provide the output metric + */ + @CheckForNull + public MeasureComputer measureComputer(String outputMetric){ + Optional measureComputer = FluentIterable.from(getMeasureComputers()).firstMatch(new MatchingOuputMetricKey(outputMetric)); + return measureComputer.isPresent() ? measureComputer.get() : null; + } + + private static class MatchingOuputMetricKey implements Predicate { + + private final String metricKey; + + public MatchingOuputMetricKey(String metricKey) { + this.metricKey = metricKey; + } + + @Override + public boolean apply(MeasureComputer input) { + return input.getOutputMetrics().contains(metricKey); + } + } + + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureImpl.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureImpl.java new file mode 100644 index 00000000000..43dc4bdb36a --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/MeasureImpl.java @@ -0,0 +1,84 @@ +/* + * 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.api.test.ce.measure; + +import javax.annotation.concurrent.Immutable; +import org.sonar.api.ce.measure.Measure; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + +@Immutable +public class MeasureImpl implements Measure { + + private Integer intValue; + private Long longValue; + private Double doubleValue; + private String stringValue; + + public static MeasureImpl createMeasure(double doubleValue){ + MeasureImpl measure = new MeasureImpl(); + measure.doubleValue = doubleValue; + return measure; + } + + public static MeasureImpl createMeasure(int intValue) { + MeasureImpl measure = new MeasureImpl(); + measure.intValue = intValue; + return measure; + } + + public static MeasureImpl createMeasure(long longValue) { + MeasureImpl measure = new MeasureImpl(); + measure.longValue = longValue; + return measure; + } + + public static MeasureImpl createMeasure(String stringValue) { + MeasureImpl measure = new MeasureImpl(); + measure.stringValue = requireNonNull(stringValue, "Value cannot be null"); + return measure; + } + + @Override + public int getIntValue() { + checkState(intValue != null, "Not an int measure"); + return intValue; + } + + @Override + public long getLongValue() { + checkState(longValue != null, "Not a long measure"); + return longValue; + } + + @Override + public double getDoubleValue() { + checkState(doubleValue != null, "Not a double measure"); + return doubleValue; + } + + @Override + public String getStringValue() { + checkState(stringValue != null, "Not a string measure"); + return stringValue; + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/SettingsImpl.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/SettingsImpl.java new file mode 100644 index 00000000000..e713dc39c24 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/ce/measure/SettingsImpl.java @@ -0,0 +1,58 @@ +/* + * 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.api.test.ce.measure; + +import java.util.HashMap; +import java.util.Map; +import javax.annotation.CheckForNull; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.ce.measure.Settings; + +public class SettingsImpl implements Settings { + + private Map valuesByKey = new HashMap<>(); + + public Settings setValue(String key, String value){ + valuesByKey.put(key, value); + return this; + } + + @Override + @CheckForNull + public String getString(String key) { + return valuesByKey.get(key); + } + + @Override + public String[] getStringArray(String key) { + String value = getString(key); + if (value != null) { + String[] strings = StringUtils.splitByWholeSeparator(value, ","); + String[] result = new String[strings.length]; + for (int index = 0; index < strings.length; index++) { + result[index] = StringUtils.trim(strings[index]); + } + return result; + } + return ArrayUtils.EMPTY_STRING_ARRAY; + } +} -- 2.39.5