Skip to content

Issue 110 #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.experimental.theories.DataPoint;
Expand Down Expand Up @@ -65,16 +66,21 @@ 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));
Object dataPoints= dataPointsMethod.invokeExplosively(null);
try {
addArrayValues(dataPointsMethod.getName(), list, dataPoints);
} catch (IllegalArgumentException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to do this that doesn't involve throwing an exception?

addCollectionValues(dataPointsMethod.getName(), list, dataPoints, sig.getType());
}
} catch (Throwable e) {
// ignore and move on
}
Expand All @@ -95,9 +101,13 @@ private void addFields(ParameterSignature sig,
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));
if (field.getAnnotation(DataPoints.class) != null) {
if (sig.canAcceptArrayType(type)
&& field.getAnnotation(DataPoints.class) != null) {
addArrayValues(field.getName(), list, getStaticFieldValue(field));
} else {
addCollectionValues(field.getName(), list, getStaticFieldValue(field), sig.getType());
}
} else if (sig.canAcceptType(type)
&& field.getAnnotation(DataPoint.class) != null) {
list.add(PotentialAssignment
Expand All @@ -107,6 +117,22 @@ private void addFields(ParameterSignature sig,
}
}

private void addCollectionValues(String name,
List<PotentialAssignment> assignments, Object staticFieldValue, Class<?> type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a weird linebreak.

try {
Collection<?> collection = (Collection<?>) staticFieldValue;
int i = 0;
for (Object each : collection) {
if (type.isInstance(each)) {
assignments.add(PotentialAssignment.forValue(name + "(" + i + ")", each));
}
i++;
}
} catch (ClassCastException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this ClassCastException coming from?

// ignore and move on
}
}

private void addArrayValues(String name, List<PotentialAssignment> list, Object array) {
for (int i= 0; i < Array.getLength(array); i++)
list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.junit.tests.experimental.theories.runner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.swing.JFrame;

import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
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;

/**
* A simple test class in which Collections of Strings (not just arrays of Strings) are
* annotated with the DataPoints annotation.
*/
@RunWith(Theories.class)
public class DataPointsTest {

@DataPoint // 1
public static String singleDataPoint = "single data point";

@DataPoints // 2, 3
public static String[] stringArray = {"one string in an array", "another string in an array", };

@DataPoints // 4, 5
public static String[] stringArrayMethod() {
return new String[] {
"one string in an array returned from a method",
"another string in an array returned from a method"
};
}

@DataPoints // 6, 7
public static List<String> stringList = Arrays.asList("one string in a list", "another string in a list");

@DataPoints // 8, 9
public static Collection<String> stringCollection = new HashSet<String>();
@BeforeClass public static void initStringCollection() {
stringCollection.add("one string in a set");
stringCollection.add("another string in a set");
};

@DataPoints // 10, 11
public static List<String> stringList() {
ArrayList<String> list = new ArrayList<String>();
list.add("one string in a list returned from a method");
list.add("another string in a list returned from a method");
return list;
}

@DataPoints // 12
public static List<Object> mixedCollection = new ArrayList<Object>();
@BeforeClass public static void initMixedCollection() {
mixedCollection.add(true);
mixedCollection.add("string in mixed collection");
mixedCollection.add(new Object());
}

@DataPoint
public static JFrame someRandomObject = new JFrame();

@DataPoints
public static Integer[] integerArray = {1, 2, 3};

@DataPoints public static Collection<Integer> integerCollection = Arrays.asList(4, 5, 6);

@DataPoints
public static Collection<Integer> integerCollectionMethod() {
return Arrays.asList(4, 5, 6);
}

public static List<String> collectedStrings = new ArrayList<String>();

@Theory
public void theory(String s) {
collectedStrings.add(s);
}

@AfterClass
public static void allStringsFedIntoTheory() {
// ensure that all 12 strings were passed into the Theory
Assert.assertEquals(12, collectedStrings.size());
}
}