Permalink
Browse files

Merge pull request #485 from awulder/issue-55

Fix for issue #55
  • Loading branch information...
2 parents 87fe505 + 61326c9 commit f9ec0474bb646a2751f1f2c73f3fee524dbb4b52 @dsaff dsaff committed Aug 20, 2012
@@ -1,6 +1,3 @@
-/**
- *
- */
package org.junit.experimental.theories.internal;
import java.lang.reflect.Array;
@@ -65,40 +62,42 @@ public AllMembersSupplier(TestClass type) {
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
@@ -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);
@@ -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.