001: /**
002: *
003: */package org.junit.experimental.theories.internal;
004:
005: import java.lang.reflect.InvocationTargetException;
006: import java.lang.reflect.Method;
007: import java.util.ArrayList;
008: import java.util.List;
009:
010: import org.junit.Assert;
011: import org.junit.Assume.AssumptionViolatedException;
012: import org.junit.experimental.theories.ParameterSignature;
013: import org.junit.experimental.theories.PotentialParameterValue;
014: import org.junit.experimental.theories.Theory;
015: import org.junit.experimental.theories.PotentialParameterValue.CouldNotGenerateValueException;
016: import org.junit.internal.runners.TestClass;
017: import org.junit.internal.runners.TestMethod;
018:
019: public class TheoryMethod extends TestMethod {
020: public static class PotentialMethodValues {
021: public final List<PotentialParameterValue> fSources;
022:
023: public PotentialMethodValues() {
024: this (new ArrayList<PotentialParameterValue>());
025: }
026:
027: public PotentialMethodValues(
028: List<PotentialParameterValue> concat) {
029: fSources = concat;
030: }
031:
032: Object[] getValues(boolean nullsOk)
033: throws CouldNotGenerateValueException {
034: Object[] values = new Object[fSources.size()];
035: for (int i = 0; i < values.length; i++) {
036: values[i] = fSources.get(i).getValue();
037: if (values[i] == null && !nullsOk)
038: throw new CouldNotGenerateValueException();
039: }
040: return values;
041: }
042:
043: PotentialMethodValues concat(PotentialParameterValue source) {
044: List<PotentialParameterValue> list = new ArrayList<PotentialParameterValue>();
045: list.addAll(fSources);
046: list.add(source);
047: return new PotentialMethodValues(list);
048: }
049: }
050:
051: private final Method fMethod;
052:
053: private List<AssumptionViolatedException> fInvalidParameters = new ArrayList<AssumptionViolatedException>();
054:
055: public TheoryMethod(Method method, TestClass testClass) {
056: super (method, testClass);
057: fMethod = method;
058: }
059:
060: @Override
061: public void invoke(Object test) throws IllegalArgumentException,
062: IllegalAccessException, InvocationTargetException {
063: int runCount = 0;
064: try {
065: runCount += runWithDiscoveredParameterValues(test,
066: new PotentialMethodValues(), ParameterSignature
067: .signatures(fMethod));
068: } catch (Throwable e) {
069: throw new InvocationTargetException(e);
070: }
071: if (runCount == 0)
072: Assert
073: .fail("Never found parameters that satisfied method. Violated assumptions: "
074: + fInvalidParameters);
075: }
076:
077: public boolean nullsOk() {
078: Theory annotation = fMethod.getAnnotation(Theory.class);
079: if (annotation == null)
080: return false;
081: return annotation.nullsAccepted();
082: }
083:
084: int invokeWithActualParameters(Object target, Object[] params)
085: throws Throwable {
086: try {
087: try {
088: fMethod.invoke(target, params);
089: } catch (InvocationTargetException e) {
090: throw e.getTargetException();
091: }
092: } catch (AssumptionViolatedException e) {
093: fInvalidParameters.add(e);
094: return 0;
095: } catch (Throwable e) {
096: if (params.length == 0)
097: throw e;
098: throw new ParameterizedAssertionError(e, fMethod.getName(),
099: params);
100: }
101: return 1;
102: }
103:
104: int runWithDiscoveredParameterValues(Object target,
105: PotentialMethodValues valueSources,
106: List<ParameterSignature> sigs) throws Throwable {
107: if (sigs.size() == 0) {
108: try {
109: return invokeWithActualParameters(target, valueSources
110: .getValues(nullsOk()));
111: } catch (CouldNotGenerateValueException e) {
112: return 0;
113: }
114: }
115:
116: int count = 0;
117:
118: for (PotentialParameterValue source : sigs.get(0)
119: .getPotentialValues(target)) {
120: count += runWithDiscoveredParameterValues(target,
121: valueSources.concat(source), sigs.subList(1, sigs
122: .size()));
123: }
124:
125: return count;
126: }
127: }
|