-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Closed
Issue 110 #175
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
cc06d9c
adding support for putting the @DataPoints annotation on Collections …
MatrixFrog ac1171a
fixed it so that it won't try to pass Collections of the wrong type t…
MatrixFrog 2a99fac
Adding test to demonstrate new '@DataPoints on Collections' feature
MatrixFrog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
addCollectionValues(dataPointsMethod.getName(), list, dataPoints, sig.getType()); | ||
} | ||
} catch (Throwable e) { | ||
// ignore and move on | ||
} | ||
|
@@ -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 | ||
|
@@ -107,6 +117,22 @@ private void addFields(ParameterSignature sig, | |
} | ||
} | ||
|
||
private void addCollectionValues(String name, | ||
List<PotentialAssignment> assignments, Object staticFieldValue, Class<?> type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))); | ||
|
91 changes: 91 additions & 0 deletions
91
src/test/java/org/junit/tests/experimental/theories/runner/DataPointsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?