From: Sébastien Lesaint Date: Fri, 6 Nov 2015 14:17:20 +0000 (+0100) Subject: factorize Predicate to wrap into single element object array X-Git-Tag: 5.3-RC1~354 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=986288c0dc43b59d683d430c2c122ad6f8afc513;p=sonarqube.git factorize Predicate to wrap into single element object array this function is very convenient when writting DataProvider methods --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java index ee8bb960778..072caa9bacf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.computation.measure; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; import com.tngtech.java.junit.dataprovider.DataProvider; @@ -27,12 +26,12 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.List; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.sonar.server.computation.measure.Measure.ValueType; +import org.sonar.server.util.WrapInSingleElementArray; import static com.google.common.collect.FluentIterable.from; import static org.assertj.core.api.Assertions.assertThat; @@ -91,18 +90,18 @@ public class MeasureTest { @DataProvider public static Object[][] all() { - return from(MEASURES).transform(WrapInArray.INSTANCE).toArray(Measure[].class); + return from(MEASURES).transform(WrapInSingleElementArray.INSTANCE).toArray(Object[].class); } - private static Measure[][] getMeasuresExcept(final ValueType valueType) { + private static Object[][] getMeasuresExcept(final ValueType valueType) { return from(MEASURES) .filter(new Predicate() { @Override public boolean apply(@Nonnull Measure input) { return input.getValueType() != valueType; } - }).transform(WrapInArray.INSTANCE) - .toArray(Measure[].class); + }).transform(WrapInSingleElementArray.INSTANCE) + .toArray(Object[].class); } @Test @@ -364,13 +363,4 @@ public class MeasureTest { newMeasureBuilder().create(Double.NaN, "some data"); } - private enum WrapInArray implements Function { - INSTANCE; - - @Nullable - @Override - public Measure[] apply(@Nonnull Measure input) { - return new Measure[] {input}; - } - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java index ab560db9327..15f9f2da406 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java @@ -20,20 +20,17 @@ package org.sonar.server.computation.step; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.sonar.api.config.Settings; -import org.sonar.server.computation.dbcleaner.ProjectCleaner; import org.sonar.db.DbSession; import org.sonar.db.purge.IdUuidPair; import org.sonar.server.computation.batch.TreeRootHolderRule; @@ -42,7 +39,9 @@ import org.sonar.server.computation.component.MutableDbIdsRepositoryRule; import org.sonar.server.computation.component.ReportComponent; import org.sonar.server.computation.component.SettingsRepository; import org.sonar.server.computation.component.ViewsComponent; +import org.sonar.server.computation.dbcleaner.ProjectCleaner; import org.sonar.server.db.DbClient; +import org.sonar.server.util.WrapInSingleElementArray; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -143,17 +142,13 @@ public class PurgeDatastoresStepTest extends BaseStepTest { private static Object[][] dataproviderFromComponentTypeValues(Predicate predicate) { return FluentIterable.from(asList(Component.Type.values())) .filter(predicate) - .transform(new Function() { - @Nullable - @Override - public Object[] apply(Object input) { - return new Object[]{input}; - } - }).toArray(Object[].class); + .transform(WrapInSingleElementArray.INSTANCE) + .toArray(Object[].class); } @Override protected ComputationStep step() { return underTest; } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/WrapInSingleElementArray.java b/server/sonar-server/src/test/java/org/sonar/server/util/WrapInSingleElementArray.java new file mode 100644 index 00000000000..a1e130f13d7 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/util/WrapInSingleElementArray.java @@ -0,0 +1,33 @@ +/* + * 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.util; + +import com.google.common.base.Function; +import javax.annotation.Nonnull; + +public enum WrapInSingleElementArray implements Function { + INSTANCE; + + @Override + @Nonnull + public Object[] apply(Object input) { + return new Object[]{input}; + } +}