Skip to content

Commit

Permalink
Fixes #449, stopping AllMembersSupplier hiding DataPoint method excep…
Browse files Browse the repository at this point in the history
…tions

Also includes a rewrite of the Theory nullsAccepted code, since that relied on the previous behaviour of this.
  • Loading branch information
pimterry committed Mar 16, 2013
1 parent e012e06 commit 5c2070d
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.List;

public abstract class ParameterSupplier {
public abstract List<PotentialAssignment> getValueSources(ParameterSignature sig);
public abstract List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable;
}
23 changes: 14 additions & 9 deletions src/main/java/org/junit/experimental/theories/Theories.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

import org.junit.Assert;
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
import org.junit.Assume;
import org.junit.experimental.theories.internal.Assignments;
import org.junit.experimental.theories.internal.ParameterizedAssertionError;
import org.junit.internal.AssumptionViolatedException;
Expand Down Expand Up @@ -202,8 +202,13 @@ protected Statement methodInvoker(FrameworkMethod method, Object test) {

@Override
public Object createTest() throws Exception {
return getTestClass().getOnlyConstructor().newInstance(
complete.getConstructorArguments(nullsOk()));
Object[] params = complete.getConstructorArguments();

if (!nullsOk()) {
Assume.assumeNotNull(params);
}

return getTestClass().getOnlyConstructor().newInstance(params);
}
}.methodBlock(fTestMethod).evaluate();
}
Expand All @@ -213,13 +218,13 @@ private Statement methodCompletesWithParameters(
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
final Object[] values = complete.getMethodArguments(
nullsOk());
method.invokeExplosively(freshInstance, values);
} catch (CouldNotGenerateValueException e) {
// ignore
final Object[] values = complete.getMethodArguments();

if (!nullsOk()) {
Assume.assumeNotNull(values);
}

method.invokeExplosively(freshInstance, values);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public AllMembersSupplier(TestClass type) {
}

@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
public List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable {
List<PotentialAssignment> list = new ArrayList<PotentialAssignment>();

addSinglePointFields(sig, list);
Expand All @@ -69,13 +69,9 @@ public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
return list;
}

private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) {
private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) throws Throwable {
for (FrameworkMethod dataPointsMethod : getDataPointsMethods(sig)) {
try {
addMultiPointArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
} catch (Throwable e) {
// ignore and move on
}
addMultiPointArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,17 @@ public Assignments assignNext(PotentialAssignment source) {
fUnassigned.size()), fClass);
}

public Object[] getActualValues(int start, int stop, boolean nullsOk)
public Object[] getActualValues(int start, int stop)
throws CouldNotGenerateValueException {
Object[] values = new Object[stop - start];
for (int i = start; i < stop; i++) {
Object value = fAssigned.get(i).getValue();
if (value == null && !nullsOk) {
throw new CouldNotGenerateValueException();
}
values[i - start] = value;
values[i - start] = fAssigned.get(i).getValue();
}
return values;
}

public List<PotentialAssignment> potentialsForNextUnassigned()
throws Exception {
throws Throwable {
ParameterSignature unassigned = nextUnassigned();
return getSupplier(unassigned).getValueSources(unassigned);
}
Expand Down Expand Up @@ -107,20 +103,17 @@ private ParameterSupplier buildParameterSupplierFromClass(
return cls.newInstance();
}

public Object[] getConstructorArguments(boolean nullsOk)
public Object[] getConstructorArguments()
throws CouldNotGenerateValueException {
return getActualValues(0, getConstructorParameterCount(), nullsOk);
return getActualValues(0, getConstructorParameterCount());
}

public Object[] getMethodArguments(boolean nullsOk)
throws CouldNotGenerateValueException {
return getActualValues(getConstructorParameterCount(),
fAssigned.size(), nullsOk);
public Object[] getMethodArguments() throws CouldNotGenerateValueException {
return getActualValues(getConstructorParameterCount(), fAssigned.size());
}

public Object[] getAllArguments(boolean nullsOk)
throws CouldNotGenerateValueException {
return getActualValues(0, fAssigned.size(), nullsOk);
public Object[] getAllArguments() throws CouldNotGenerateValueException {
return getActualValues(0, fAssigned.size());
}

private int getConstructorParameterCount() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.junit.tests.experimental.theories;

import static org.junit.tests.experimental.theories.TheoryTestUtils.runTheoryClass;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.model.InitializationError;

@RunWith(Theories.class)
Expand All @@ -23,10 +21,7 @@ public void noTheoryAnnotationMeansAssumeShouldIgnore() {

@Test
public void theoryMeansOnlyAssumeShouldFail() throws InitializationError {
JUnitCore junitRunner = new JUnitCore();
Runner theoryRunner = new Theories(TheoryWithNoUnassumedParameters.class);
Request request = Request.runner(theoryRunner);
Result result = junitRunner.run(request);
Result result = runTheoryClass(TheoryWithNoUnassumedParameters.class);
Assert.assertEquals(1, result.getFailureCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
import java.util.List;

import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.internal.Assignments;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;

public final class TheoryTestUtils {

private TheoryTestUtils() { }

public static List<PotentialAssignment> potentialAssignments(Method method)
throws Exception {
throws Throwable {
return Assignments.allUnassigned(method,
new TestClass(method.getDeclaringClass()))
.potentialsForNextUnassigned();
}

public static Result runTheoryClass(Class<?> testClass) throws InitializationError {
Runner theoryRunner = new Theories(testClass);
Request request = Request.runner(theoryRunner);
return new JUnitCore().run(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void runWithIncompleteAssignment(Assignments incomplete)
}

private GuesserQueue createGuesserQueue(Assignments incomplete)
throws Exception {
throws Throwable {
ParameterSignature nextUnassigned = incomplete.nextUnassigned();

if (nextUnassigned.hasAnnotation(Stub.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.internal.AllMembersSupplier;
import org.junit.rules.ExpectedException;
import org.junit.runners.model.TestClass;

public class AllMembersSupplierTest {
@Rule
public ExpectedException expected = ExpectedException.none();

public static class HasDataPoints {
@DataPoints
public static Object[] objects = {1, 2};
Expand All @@ -22,13 +27,9 @@ public HasDataPoints(Object obj) {
}

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

Expand All @@ -41,13 +42,9 @@ 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));
public void dataPointsArrayFieldMayContainNullValue() throws Throwable {
List<PotentialAssignment> valueSources = allMemberValuesFor(
HasDataPointsFieldWithNullValue.class, Object.class);
assertThat(valueSources.size(), is(2));
}

Expand All @@ -62,13 +59,34 @@ public HasDataPointsMethodWithNullValue(Integer i) {
}

@Test
public void dataPointsArrayMethodMayContainNullValue()
throws SecurityException, NoSuchMethodException {
List<PotentialAssignment> valueSources = new AllMembersSupplier(
new TestClass(HasDataPointsMethodWithNullValue.class))
public void dataPointsArrayMethodMayContainNullValue() throws Throwable {
List<PotentialAssignment> valueSources = allMemberValuesFor(
HasDataPointsMethodWithNullValue.class, Integer.class);
assertThat(valueSources.size(), is(2));
}

public static class HasFailingDataPointsArrayMethod {
@DataPoints
public static Object[] objects() {
throw new RuntimeException("failing method");
}

public HasFailingDataPointsArrayMethod(Object obj) {
}
}

@Test
public void allMembersFailsOnFailingDataPointsArrayMethod() throws Throwable {
expected.expect(RuntimeException.class);
expected.expectMessage("failing method");
allMemberValuesFor(HasFailingDataPointsArrayMethod.class, Object.class);
}

private List<PotentialAssignment> allMemberValuesFor(Class<?> testClass,
Class<?>... constructorParameterTypes) throws Throwable {
return new AllMembersSupplier(new TestClass(testClass))
.getValueSources(ParameterSignature.signatures(
HasDataPointsMethodWithNullValue.class.getConstructor(Integer.class))
testClass.getConstructor(constructorParameterTypes))
.get(0));
assertThat(valueSources.size(), is(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static String[] getOtherValues() {
}

@Test
public void shouldReturnOnlyTheNamedDataPoints() throws Exception {
public void shouldReturnOnlyTheNamedDataPoints() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingAllNamedStrings"));
Expand All @@ -67,7 +67,7 @@ public void shouldReturnOnlyTheNamedDataPoints() throws Exception {
}

@Test
public void shouldReturnOnlyTheNamedFieldDataPoints() throws Exception {
public void shouldReturnOnlyTheNamedFieldDataPoints() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingNamedFieldString"));
Expand All @@ -78,7 +78,7 @@ public void shouldReturnOnlyTheNamedFieldDataPoints() throws Exception {
}

@Test
public void shouldReturnOnlyTheNamedMethodDataPoints() throws Exception {
public void shouldReturnOnlyTheNamedMethodDataPoints() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingNamedMethodString"));
Expand All @@ -89,7 +89,7 @@ public void shouldReturnOnlyTheNamedMethodDataPoints() throws Exception {
}

@Test
public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Exception {
public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingNamedSingleFieldString"));
Expand All @@ -100,7 +100,7 @@ public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Exception {
}

@Test
public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Exception {
public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingNamedSingleMethodString"));
Expand All @@ -111,7 +111,7 @@ public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Exception {
}

@Test
public void shouldReturnNothingIfTheNamedDataPointsAreMissing() throws Exception {
public void shouldReturnNothingIfTheNamedDataPointsAreMissing() throws Throwable {
SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class));

List<PotentialAssignment> assignments = supplier.getValueSources(signature("methodWantingWrongNamedString"));
Expand Down
Loading

0 comments on commit 5c2070d

Please sign in to comment.