aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/experimental/theories/internal/Assignments.java
blob: bd94f003cc428fe1b04d5c44808fa2a42191d745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * 
 */
package org.junit.experimental.theories.internal;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.ParametersSuppliedBy;
import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
import org.junit.runners.model.TestClass;

/**
 * A potentially incomplete list of value assignments for a method's formal
 * parameters
 */
public class Assignments {
	private List<PotentialAssignment> fAssigned;

	private final List<ParameterSignature> fUnassigned;

	private final TestClass fClass;

	private Assignments(List<PotentialAssignment> assigned,
			List<ParameterSignature> unassigned, TestClass testClass) {
		fUnassigned= unassigned;
		fAssigned= assigned;
		fClass= testClass;
	}

	/**
	 * Returns a new assignment list for {@code testMethod}, with no params
	 * assigned.
	 */
	public static Assignments allUnassigned(Method testMethod,
			TestClass testClass) throws Exception {
		List<ParameterSignature> signatures;
		signatures= ParameterSignature.signatures(testClass
				.getOnlyConstructor());
		signatures.addAll(ParameterSignature.signatures(testMethod));
		return new Assignments(new ArrayList<PotentialAssignment>(),
				signatures, testClass);
	}

	public boolean isComplete() {
		return fUnassigned.size() == 0;
	}

	public ParameterSignature nextUnassigned() {
		return fUnassigned.get(0);
	}

	public Assignments assignNext(PotentialAssignment source) {
		List<PotentialAssignment> assigned= new ArrayList<PotentialAssignment>(
				fAssigned);
		assigned.add(source);

		return new Assignments(assigned, fUnassigned.subList(1, fUnassigned
				.size()), fClass);
	}

	public Object[] getActualValues(int start, int stop, boolean nullsOk)
			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;
		}
		return values;
	}

	public List<PotentialAssignment> potentialsForNextUnassigned()
			throws InstantiationException, IllegalAccessException {
		ParameterSignature unassigned= nextUnassigned();
		return getSupplier(unassigned).getValueSources(unassigned);
	}

	public ParameterSupplier getSupplier(ParameterSignature unassigned)
			throws InstantiationException, IllegalAccessException {
		ParameterSupplier supplier= getAnnotatedSupplier(unassigned);
		if (supplier != null)
			return supplier;

		return new AllMembersSupplier(fClass);
	}

	public ParameterSupplier getAnnotatedSupplier(ParameterSignature unassigned)
			throws InstantiationException, IllegalAccessException {
		ParametersSuppliedBy annotation= unassigned
				.findDeepAnnotation(ParametersSuppliedBy.class);
		if (annotation == null)
			return null;
		return annotation.value().newInstance();
	}

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

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

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

	private int getConstructorParameterCount() {
		List<ParameterSignature> signatures= ParameterSignature
				.signatures(fClass.getOnlyConstructor());
		int constructorParameterCount= signatures.size();
		return constructorParameterCount;
	}

	public Object[] getArgumentStrings(boolean nullsOk)
			throws CouldNotGenerateValueException {
		Object[] values= new Object[fAssigned.size()];
		for (int i= 0; i < values.length; i++) {
			values[i]= fAssigned.get(i).getDescription();
		}
		return values;
	}
}