Skip to content

Commit

Permalink
Merge pull request junit-team#485 from awulder/issue-55
Browse files Browse the repository at this point in the history
Fix for issue junit-team#55
  • Loading branch information
David Saff committed Aug 20, 2012
2 parents 87fe505 + 61326c9 commit f9ec047
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
@@ -1,6 +1,3 @@
/**
*
*/
package org.junit.experimental.theories.internal;

import java.lang.reflect.Array;
Expand Down Expand Up @@ -65,40 +62,42 @@ public List<PotentialAssignment> getValueSources(ParameterSignature sig) {

addFields(sig, list);
addSinglePointMethods(sig, list);
addMultiPointMethods(list);
addMultiPointMethods(sig, list);

return list;
}

private void addMultiPointMethods(List<PotentialAssignment> list) {
private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) {
for (FrameworkMethod dataPointsMethod : fClass
.getAnnotatedMethods(DataPoints.class))
try {
addArrayValues(dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
addMultiPointArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
} catch (Throwable e) {
// ignore and move on
}
}

@SuppressWarnings("deprecation")
private void addSinglePointMethods(ParameterSignature sig,
List<PotentialAssignment> list) {
List<PotentialAssignment> list) {
for (FrameworkMethod dataPointMethod : fClass
.getAnnotatedMethods(DataPoint.class)) {
Class<?> type= sig.getType();
if ((dataPointMethod.producesType(type)))
if (isCorrectlyTyped(sig, dataPointMethod.getType()))
list.add(new MethodParameterValue(dataPointMethod));
}
}

private void addFields(ParameterSignature sig,
List<PotentialAssignment> list) {
List<PotentialAssignment> list) {
for (final Field field : fClass.getJavaClass().getFields()) {
if (Modifier.isStatic(field.getModifiers())) {
Class<?> type= field.getType();
if (sig.canAcceptArrayType(type)
&& field.getAnnotation(DataPoints.class) != null) {
addArrayValues(field.getName(), list, getStaticFieldValue(field));
try {
addArrayValues(field.getName(), list, getStaticFieldValue(field));
} catch (Throwable e) {
// ignore and move on
}
} else if (sig.canAcceptType(type)
&& field.getAnnotation(DataPoint.class) != null) {
list.add(PotentialAssignment
Expand All @@ -113,6 +112,21 @@ private void addArrayValues(String name, List<PotentialAssignment> list, Object
list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i)));
}

private void addMultiPointArrayValues(ParameterSignature sig, String name, List<PotentialAssignment> list,
Object array) throws Throwable {
for (int i= 0; i < Array.getLength(array); i++) {
if (!isCorrectlyTyped(sig, Array.get(array, i).getClass())) {
return;
}
list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i)));
}
}

@SuppressWarnings("deprecation")
private boolean isCorrectlyTyped(ParameterSignature parameterSignature, Class<?> type) {
return parameterSignature.canAcceptType(type);
}

private Object getStaticFieldValue(final Field field) {
try {
return field.get(null);
Expand Down
@@ -0,0 +1,57 @@
package org.junit.tests.experimental.theories.runner;

import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

public class TypeMatchingBetweenMultiDataPointsMethod {

@RunWith(Theories.class)
public static class WithWrongfullyTypedDataPointsMethod {
@DataPoint
public static String[] correctlyTyped = {"Good", "Morning"};

@DataPoints
public static String[] wrongfullyTyped() {
return new String[]{"Hello", "World"};
}

@Theory
public void testTheory(String[] array) {
}
}

@Test
public void ignoreWrongTypedDataPointsMethod() {
assertThat(testResult(WithWrongfullyTypedDataPointsMethod.class), isSuccessful());
}

@RunWith(Theories.class)
public static class WithCorrectlyTypedDataPointsMethod {
@DataPoint
public static String[] correctlyTyped = {"Good", "Morning"};

@DataPoints
public static String[][] anotherCorrectlyTyped() {
return new String[][]{
{"Hello", "World"}
};
}

@Theory
public void testTheory(String[] array) {
}
}

@Test
public void pickUpMultiPointDataPointMethods() throws Exception {
assertThat(testResult(WithCorrectlyTypedDataPointsMethod.class), isSuccessful());
}
}

0 comments on commit f9ec047

Please sign in to comment.