Skip to content

Commit

Permalink
Merge pull request junit-team#529 from Stephan202/null-datapoints
Browse files Browse the repository at this point in the history
Allow the array returned by a @DataPoints-annotated method to contain null values
  • Loading branch information
David Saff committed Nov 15, 2012
2 parents ea96d2e + 902b156 commit 7ae6e67
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ private ParameterSignature(Class<?> type, Annotation[] annotations) {
this.annotations = annotations;
}

public boolean canAcceptValue(Object candidate) {
return (candidate == null) ? !type.isPrimitive() : canAcceptType(candidate.getClass());
}

public boolean canAcceptType(Class<?> candidate) {
return type.isAssignableFrom(candidate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void addSinglePointMethods(ParameterSignature sig,
List<PotentialAssignment> list) {
for (FrameworkMethod dataPointMethod : fClass
.getAnnotatedMethods(DataPoint.class)) {
if (isCorrectlyTyped(sig, dataPointMethod.getType())) {
if (sig.canAcceptType(dataPointMethod.getType())) {
list.add(new MethodParameterValue(dataPointMethod));
}
}
Expand Down Expand Up @@ -118,18 +118,13 @@ private void addArrayValues(String name, List<PotentialAssignment> list, Object
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())) {
if (!sig.canAcceptValue(Array.get(array, i))) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,44 @@ public void dataPointsAnnotationMeansTreatAsArrayOnly()
.get(0));
assertThat(valueSources.size(), is(2));
}

public static class HasDataPointsFieldWithNullValue {
@DataPoints
public static Object[] objects = {null, "a"};

public HasDataPointsFieldWithNullValue(Object obj) {
}
}

@Test
public void dataPointsArrayFieldMayContainNullValue()
throws SecurityException, NoSuchMethodException {
List<PotentialAssignment> valueSources = new AllMembersSupplier(
new TestClass(HasDataPointsFieldWithNullValue.class))
.getValueSources(ParameterSignature.signatures(
HasDataPointsFieldWithNullValue.class.getConstructor(Object.class))
.get(0));
assertThat(valueSources.size(), is(2));
}

public static class HasDataPointsMethodWithNullValue {
@DataPoints
public static Integer[] getObjects() {
return new Integer[] {null, 1};
}

public HasDataPointsMethodWithNullValue(Integer i) {
}
}

@Test
public void dataPointsArrayMethodMayContainNullValue()
throws SecurityException, NoSuchMethodException {
List<PotentialAssignment> valueSources = new AllMembersSupplier(
new TestClass(HasDataPointsMethodWithNullValue.class))
.getValueSources(ParameterSignature.signatures(
HasDataPointsMethodWithNullValue.class.getConstructor(Integer.class))
.get(0));
assertThat(valueSources.size(), is(2));
}
}

0 comments on commit 7ae6e67

Please sign in to comment.