On Mon, Aug 6, 2012 at 9:00 PM, Mark Derricutt <m
...@talios.com> wrote:
> Cedric,
> I was just updating some tests and came up with a pattern that I was
> thinking would be good to include in TestNG directly. The concept is a
> simple cartesian product support for data providers.
> In my test I know have three providers, the first two providing discreet
> sets of data, with their concerns separated out, the third being the
> cartesian procut:
> @DataProvider
> public Object[][] provideBillingReferences() {
> return new Object[][] {...};
> }
> @DataProvider
> public Object[][] provideSplitData() {
> return new Object[][] {...};
> }
> @DataProvider
> public Iterator<Object[]> provideMergedSplitData() {
> return
> DataProviderUtil.cartesianProviderFrom(ImmutableList.of(provideSplitData(),
> provideBillingReferences()));
> }
> @Test(dataProvider = "provideMergedSplitData")
> ….
> What I was thinking was this could be handy to incorporate directly into
> TestNG then I could do something
> @Test(dataProvider = {"provideBillingReferences", "provideSplitData"})
> and have TestNG handle the cartesian itself. Thoughts?
> My current implementation of this uses Google Guava:
> public class DataProviderUtil {
> /**
> * Given a list of @DataProvider results, generate a cartesian product
> of available combinations.
> * @param dataProviderData A list of @DataProvider results
> * @return The cartesian product of available combinations.
> */
> public static Iterator<Object[]>
> cartesianProviderFrom(List<Object[][]> dataProviderData) {
> ImmutableList.Builder<Set<Object[]>> cartesianSets =
> ImmutableList.builder();
> for (Object[][] objects : dataProviderData) {
> cartesianSets.add(Sets.newHashSet(objects));
> }
> Set<List<Object[]>> cartesianData =
> Sets.cartesianProduct(cartesianSets.build());
> List<Object[]> data = Lists.newArrayList();
> for (List<Object[]> objects : cartesianData) {
> Object[] mergedArray = flattenObjectArray(objects);
> data.add(mergedArray);
> }
> return data.iterator();
> }
> private static Object[] flattenObjectArray(List<Object[]> arrays) {
> int len = 0;
> for (Object[] array : arrays) {
> len += array.length;
> }
> int index = 0;
> Object[] mergedArray = new Object[len];
> for (Object[] array : arrays) {
> for (int i = 0; i < array.length; i++) {
> mergedArray[index++] = array[i];
> }
> }
> return mergedArray;
> }
> }
> --
> You received this message because you are subscribed to the Google Groups
> "testng-dev" group.
> To post to this group, send email to testng-dev@googlegroups.com.
> To unsubscribe from this group, send email to
> testng-dev+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/testng-dev?hl=en.