1package org.testng;
2
3import java.lang.reflect.Constructor;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.List;
7
8/**
9 * Helper methods used by the Eclipse plug-in when converting tests from JUnit.
10 *
11 * @author Cedric Beust <cedric@beust.com>
12 *
13 */
14public class ConversionUtils {
15  /**
16   * Turns the output of a JUnit 4 @Parameters style data provider into
17   * one that is suitable for TestNG's @DataProvider.
18   */
19  public static Object[] wrapDataProvider(Class cls, Collection<Object[]> data) {
20    List result = new ArrayList();
21    for (Object o : data) {
22      Object[] parameters = (Object[]) o;
23      Constructor ctor = null;
24      try {
25        for (Constructor c : cls.getConstructors()) {
26          // Just comparing parameter array sizes. Comparing the parameter types
27          // is more error prone since we need to take conversions into account
28          // (int -> Integer, etc...).
29          if (c.getParameterTypes().length == parameters.length) {
30            ctor = c;
31            break;
32          }
33        }
34        if (ctor == null) {
35          throw new TestNGException("Couldn't find a constructor in " + cls);
36        }
37
38        result.add(ctor.newInstance(parameters));
39      } catch (Exception ex) {
40        ex.printStackTrace();
41      }
42    }
43    return result.toArray();
44  }
45
46}
47