]> source.dussan.org Git - sonarqube.git/commitdiff
factorize Predicate to wrap into single element object array
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 6 Nov 2015 14:17:20 +0000 (15:17 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 9 Nov 2015 15:34:20 +0000 (16:34 +0100)
this function is very convenient when writting DataProvider methods

server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java
server/sonar-server/src/test/java/org/sonar/server/util/WrapInSingleElementArray.java [new file with mode: 0644]

index ee8bb9607789f2523eb690d1197e2aa327f52c08..072caa9bacf36a2097137cf14915488ce2439789 100644 (file)
@@ -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<Measure>() {
         @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<Measure, Measure[]> {
-    INSTANCE;
-
-    @Nullable
-    @Override
-    public Measure[] apply(@Nonnull Measure input) {
-      return new Measure[] {input};
-    }
-  }
 }
index ab560db9327d3882ef5e100b0cbf137b03629b57..15f9f2da4067b0cea984282b3bdedebfee9d3d64 100644 (file)
 
 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<Component.Type> predicate) {
     return FluentIterable.from(asList(Component.Type.values()))
         .filter(predicate)
-        .transform(new Function<Object, Object[]>() {
-          @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 (file)
index 0000000..a1e130f
--- /dev/null
@@ -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<Object, Object[]> {
+  INSTANCE;
+
+  @Override
+  @Nonnull
+  public Object[] apply(Object input) {
+    return new Object[]{input};
+  }
+}