/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2017 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.galenium.providers;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang3.ArrayUtils;
import org.testng.annotations.DataProvider;

/**
 * Utility class to help with writing {@link DataProvider} code.
 */
public final class TestNgProviderUtil {

  private TestNgProviderUtil() {
    // do not instantiate
  }

  /**
   * Transforms argument list into Object array. For use with DataProviders for single argument methods and
   * constructors.
   * @param argumentList iterable to turn into object array
   * @return a two dimensional object array containing the arguments
   */
  public static Object[][] combine(Iterable argumentList) {
    Collection<Object[]> combinedArguments = new ArrayList<>();
    for (Object object : argumentList) {
      combinedArguments.add(new Object[] { object });
    }
    return combinedArguments.toArray(new Object[combinedArguments.size()][]);
  }

  /**
   * Combines argument lists into Object array. Each {@link Iterable} will be used to provide the argument corresponding
   * to its position.
   * @param argumentLists iterables to combine
   * @return a two dimensional object array containing the Cartesian product of the iterable arguments
   */
  public static Object[][] combine(Iterable... argumentLists) {
    Collection<Object[]> combinedArguments = new ArrayList<>();
    for (Iterable argumentList : argumentLists) {
      combinedArguments = addArguments(combinedArguments, argumentList);
    }

    return combinedArguments.toArray(new Object[combinedArguments.size()][]);
  }

  private static Collection<Object[]> addArguments(Collection<Object[]> initialArguments, Iterable newArguments) {
    Collection<Object[]> combinedArguments = new ArrayList<>();
    for (Object newArgument : newArguments) {
      for (Object[] objects : initialArguments) {
        combinedArguments.add(ArrayUtils.add(objects, newArgument));
      }
    }

    return combinedArguments;
  }

}

@@ -718,16 +718,72 @@
 ction;%0A%0A
+import org.apache.commons.collections4.CollectionUtils;%0A
 import o
@@ -863,16 +863,73 @@
 vider;%0A%0A
+import io.wcm.qa.galenium.reporting.GaleniumReportUtil;%0A%0A
 /**%0A * U
@@ -1591,30 +1591,34 @@
 ;%0A    %7D%0A    
-return
+int size =
  combinedArg
@@ -1616,32 +1616,192 @@
 mbinedArguments.
+size();%0A    if (size %3E 0) %7B%0A      trace(%22after combining: %22 + size);%0A    %7D%0A    else %7B%0A      trace(%22empty after combining.%22);%0A    %7D%0A    return combinedArguments.
 toArray(new Obje
@@ -1807,32 +1807,12 @@
 ect%5B
-combinedArguments.
 size
-()
 %5D%5B%5D)
@@ -2183,24 +2183,87 @@
 entLists) %7B%0A
+    trace(%22combining %22 + argumentLists.length + %22 Iterables%22);%0A
     Collecti
@@ -2360,24 +2360,64 @@
 entLists) %7B%0A
+      trace(%22adding: %22 + argumentList);%0A
       combin
@@ -2480,38 +2480,42 @@
 st);%0A    %7D%0A%0A    
-return
+int size =
  combinedArgumen
@@ -2509,32 +2509,192 @@
 mbinedArguments.
+size();%0A    if (size %3E 0) %7B%0A      trace(%22after combining: %22 + size);%0A    %7D%0A    else %7B%0A      trace(%22empty after combining.%22);%0A    %7D%0A    return combinedArguments.
 toArray(new Obje
@@ -2700,32 +2700,12 @@
 ect%5B
-combinedArguments.
 size
-()
 %5D%5B%5D)
@@ -2932,24 +2932,148 @@
 rguments) %7B%0A
+      if (initialArguments.isEmpty()) %7B%0A        combinedArguments.add(new Object%5B%5D %7B newArgument %7D);%0A      %7D%0A      else %7B%0A  
       for (O
@@ -3106,24 +3106,26 @@
 rguments) %7B%0A
+  
         comb
@@ -3181,16 +3181,26 @@
 ment));%0A
+        %7D%0A
       %7D%0A
@@ -3214,32 +3214,531 @@
     
-return combinedArguments
+if (combinedArguments.isEmpty()) %7B%0A      trace(%22empty after adding.%22);%0A    %7D%0A    else %7B%0A      trace(%22after adding arguments: %22 + combinedArguments.size() + %22x%22 + getLengthOfFirstElement(combinedArguments));%0A    %7D%0A    return combinedArguments;%0A  %7D%0A%0A  @SuppressWarnings(%22deprecation%22)%0A  private static int getLengthOfFirstElement(Collection%3CObject%5B%5D%3E combinedArguments) %7B%0A    return CollectionUtils.get(combinedArguments, 0).length;%0A  %7D%0A%0A  private static void trace(String msg) %7B%0A    GaleniumReportUtil.getLogger().trace(msg)
 ;%0A  

fix provider util, add trace logging