Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for issue #55

Added Type check for MultiPointsMethod
  • Loading branch information...
commit 48aabe1b16bd288f74ac4ff92e3a38792533be39 1 parent 6a8215a
Arjan Wulder authored
View
48 src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java
@@ -1,6 +1,3 @@
-/**
- *
- */
package org.junit.experimental.theories.internal;
import java.lang.reflect.Array;
@@ -25,7 +22,7 @@
private final FrameworkMethod fMethod;
private MethodParameterValue(FrameworkMethod dataPointMethod) {
- fMethod= dataPointMethod;
+ fMethod = dataPointMethod;
}
@Override
@@ -56,49 +53,51 @@ public String getDescription() throws CouldNotGenerateValueException {
* Constructs a new supplier for {@code type}
*/
public AllMembersSupplier(TestClass type) {
- fClass= type;
+ fClass = type;
}
@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
- List<PotentialAssignment> list= new ArrayList<PotentialAssignment>();
+ List<PotentialAssignment> list = new ArrayList<PotentialAssignment>();
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();
+ 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
@@ -109,10 +108,25 @@ private void addFields(ParameterSignature sig,
}
private void addArrayValues(String name, List<PotentialAssignment> list, Object array) {
- for (int i= 0; i < Array.getLength(array); i++)
+ for (int i = 0; i < Array.getLength(array); i++)
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);
View
57 src/test/java/org/junit/tests/experimental/theories/runner/TypeMatchingBetweenMultiDataPointsMethod.java
@@ -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[][] wrongfullyTyped() {
+ return new String[][]{
+ {"Hello", "World"}
+ };
+ }
+
+ @Theory
+ public void testTheory(String[] array) {
+ }
+ }
+
+ @Test
+ public void pickUpMultiPointDataPointMethods() throws Exception {
+ assertThat(testResult(WithCorrectlyTypedDataPointsMethod.class), isSuccessful());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.