Testdatenerstellung: Permutationen
Veröffentlicht: Januar 30, 2011 Einsortiert unter: Java | Tags: dataprovider, Java, permutate, permutation, permutieren, testdaten Schreibe einen Kommentar »
Beim Testen mit DataProvidern kann es manchmal sinnvoll sein, dass man viele
verschieden Eingabeparameter testen möchte. Z.B. wenn man eine Methode tested,
die als einen Parameter eine Buffergröße hat, macht es Sinn dieselben Testdaten
mit verschiedenen Buffereinstellungen durchzuprobieren. Da die Ergebnisse
nicht von der Puffergröße abhängen kann man einfach alle Tests mit allen
Puffergrößen laufen lassen.
/** * <p> * Creates a permutation of the given 2-dimensional array * and the given 1-dimensional array. * </p> * <code> * <pre> * permutate(new Object[][] { {"foo", "bar"}, {"alice","bob"}}, * new Object[] {1,2}, * 1) * </pre> * </code> returns <code> * <pre> * new Object[][] { * {"foo", 1, "bar"}, * {"alice", 2,"bob"}, * {"foo", 1, "bar"}, * {"alice", 2, "bob"} * } * </pre> * </code> * * @param aData a 2-dimensional data object * @param aAdditionalColumn the additional column's data * @param aIndex the index where the additional column * is added to <code>aData</code> * @return the permutated array */ public Object[][] permutate( Object[][] aData, Object[] aAdditionalColumn, int aIndex) { final List<Object[]> result = new ArrayList<Object[]>(); for (Object additionalValue : aAdditionalColumn) { for (Object[] data : aData) { final Object[] newData = new Object[data.length + 1]; if (aIndex > 0) { System.arraycopy(data, 0, newData, 0, aIndex); } newData[aIndex] = additionalValue; if (aIndex + 1 < newData.length) { System.arraycopy(data, aIndex, newData, aIndex + 1, newData.length - aIndex - 1); } result.add(newData); } } return result.toArray(new Object[0][]); } Note: The code above is released to public domain.