-
Notifications
You must be signed in to change notification settings - Fork 2
/
ValueProviderFactory.java
250 lines (211 loc) · 9.95 KB
/
ValueProviderFactory.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.tngtech.valueprovider;
import java.util.Map;
import java.util.Set;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.tngtech.valueprovider.InitializationCreator.DefaultInitializationCreator;
import com.tngtech.valueprovider.InitializationCreator.TestClassInitializationCreator;
import com.tngtech.valueprovider.InitializationCreator.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkState;
import static com.tngtech.valueprovider.InitializationCreator.TestMethodInitializationCreator;
import static com.tngtech.valueprovider.InitializationCreator.Type.DEFAULT;
import static com.tngtech.valueprovider.InitializationCreator.Type.TEST_CLASS;
import static com.tngtech.valueprovider.InitializationCreator.Type.TEST_METHOD;
import static java.lang.System.identityHashCode;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
public final class ValueProviderFactory {
private static final Logger logger = LoggerFactory.getLogger(ValueProviderFactory.class);
private static final ThreadLocal<ValueProviderFactory> INSTANCE = ThreadLocal.withInitial(ValueProviderFactory::new);
private static final Map<Type, Set<Type>> LEGAL_TRANSITIONS = ImmutableMap.of(
DEFAULT, ImmutableSet.of(TEST_CLASS, TEST_METHOD),
// TEST_CLASS -> TEST_CLASS happens e.g. in discovery phase of DataProviderRunner, which loads EVERY test class
TEST_CLASS, ImmutableSet.of(TEST_CLASS, TEST_METHOD),
TEST_METHOD, ImmutableSet.of()
);
private final DefaultInitializationCreator defaultVpiCreator = new DefaultInitializationCreator();
private TestClassInitializationCreator testClassVpiCreator;
private final TestMethodInitializationCreator testMethodVpiCreator = new TestMethodInitializationCreator();
@VisibleForTesting
InitializationCreator activeVpiCreator = defaultVpiCreator;
@VisibleForTesting
InitializationCreator previousVpiCreator = defaultVpiCreator;
private ValueProviderFactory() {
logger.debug("Instantiation {}", identityHashCode(this));
}
private TestClassInitializationCreator doStartTestClassCycle() {
checkMayStart(TEST_CLASS);
TestClassInitializationCreator testClassVpiCreator = new TestClassInitializationCreator();
doActivateOrSwitchTestClassCreator(testClassVpiCreator);
testClassVpiCreator.startTestCycle(empty());
return testClassVpiCreator;
}
private void doResumeTestClassCycle(TestClassInitializationCreator testClassVpiCreator) {
checkMayStart(TEST_CLASS);
doActivateOrSwitchTestClassCreator(testClassVpiCreator);
}
private void doFinishTestClassCycle() {
checkMayFinish(TEST_CLASS);
testClassVpiCreator.finishTestCycle();
doActivateCreator(defaultVpiCreator);
resetTestClassVpiCreator();
}
private void doStartTestMethodCycle() {
checkMayStart(TEST_METHOD);
testMethodVpiCreator.startTestCycle(ofNullable(testClassVpiCreator));
doActivateCreator(testMethodVpiCreator);
}
private void doFinishTestMethodCycle() {
checkMayFinish(TEST_METHOD);
doActivateCreator(previousVpiCreator);
testMethodVpiCreator.finishTestCycle();
}
private void checkMayStart(Type type) {
Type activeType = activeVpiCreator.getType();
Set<Type> legalTransitions = LEGAL_TRANSITIONS.get(activeType);
checkState(legalTransitions.contains(type),
"Illegal transition, cannot switch from %s to %s (legal transitions %s)",
activeType, type, legalTransitions);
}
private void checkMayFinish(Type type) {
Type activeType = activeVpiCreator.getType();
checkState(activeType == type,
"Illegal transition, cannot switch from %s back to %s", activeType, type);
}
private void doResetToDefaultVpiCreator() {
logger.debug("{} doResetToDefaultValueCreator (active {}, previous{})",
identityHashCode(this),
activeVpiCreator.getId(),
previousVpiCreator.getId());
previousVpiCreator = defaultVpiCreator;
activeVpiCreator = defaultVpiCreator;
defaultVpiCreator.finishTestCycle();
if (testClassVpiCreator != null) {
testClassVpiCreator.finishTestCycle();
resetTestClassVpiCreator();
}
testMethodVpiCreator.finishTestCycle();
}
private void doActivateOrSwitchTestClassCreator(TestClassInitializationCreator newTestClassVpiCreator) {
TestClassInitializationCreator previousTestClassVpiCreator = testClassVpiCreator;
testClassVpiCreator = newTestClassVpiCreator;
if (previousTestClassVpiCreator == null) {
doActivateCreator(testClassVpiCreator);
} else {
doSwitchTestClassCreator(previousTestClassVpiCreator, testClassVpiCreator);
}
}
private void doActivateCreator(InitializationCreator creator) {
logger.debug("{} activateCreator from {} to {}", identityHashCode(this),
activeVpiCreator.getId(), creator.getId());
previousVpiCreator = activeVpiCreator;
activeVpiCreator = creator;
}
private void doSwitchTestClassCreator(TestClassInitializationCreator from, TestClassInitializationCreator to) {
logger.debug("{} switchTestClassCreator from {} to {}", identityHashCode(this),
identityHashCode(from), identityHashCode(to));
// previousVpiCreator NOT changed as it should only reflect cycle transitions
activeVpiCreator = to;
}
private void resetTestClassVpiCreator() {
testClassVpiCreator = null;
}
@VisibleForTesting
static void initializeTestClassSeed() {
instance().testClassVpiCreator.initialize();
}
@VisibleForTesting
static void resetToDefaultVpiCreator() {
instance().doResetToDefaultVpiCreator();
}
static TestCycle startTestClassCycle() {
return instance().doStartTestClassCycle();
}
static void finishTestClassCycle() {
instance().doFinishTestClassCycle();
}
static void resumeTestClassCycle(TestCycle testCycle) {
instance().doResumeTestClassCycle((TestClassInitializationCreator) testCycle);
}
static TestCycleSnapshot takeTestClassCycleSnapshot(TestCycle testCycle) {
return ((TestClassInitializationCreator) testCycle).takeSnapshot();
}
static void resumeTestClassCycle(TestCycleSnapshot testCycleSnapshot) {
instance().doResumeTestClassCycle(new TestClassInitializationCreator((InitializationCreatorSnapshot) testCycleSnapshot));
}
static void startTestMethodCycle() {
instance().doStartTestMethodCycle();
}
static void finishTestMethodCycle() {
instance().doFinishTestMethodCycle();
}
/**
* Creates a random {@link ValueProviderInitialization}. Used to create the initialization for custom {@link ValueProvider} implementations, that extend the {@link ValueProvider}.
*
* @return a random initialization that is used in the constructor of an (extension of) {@link ValueProvider}.
*/
public static ValueProviderInitialization createRandomValueProviderInitialization() {
return instance().doCreateRandomValueProviderInitialization();
}
private ValueProviderInitialization doCreateRandomValueProviderInitialization() {
return activeVpiCreator.createRandomValueProviderInitialization();
}
/**
* Factory method, that creates a random {@link ValueProvider}.
*
* @return a {@link ValueProvider} with random seed, suffix and no prefix.
* @see #createReproducibleValueProvider(long)
*/
public static ValueProvider createRandomValueProvider() {
return new ValueProvider(createRandomValueProviderInitialization());
}
/**
* Factory method, that creates a {@link ValueProvider} using {@code seed}.
* <p>
* If the method is called multiple times with the same {@code seed}, the returned {@link ValueProvider}
* will provide the same (sequence of) result(s) for the same (sequence of) method invocation(s).
* </p>
*
* @return a {@link ValueProvider} using {@code seed}, the respective suffix, and no prefix.
* @see #createRandomValueProvider()
*/
public static ValueProvider createReproducibleValueProvider(long seed) {
return new ValueProvider(createReproducibleValueProviderInitialization(seed));
}
/**
* Creates a reproducible {@link ValueProviderInitialization} using {@code seed}. Used to create the initialization for custom {@link ValueProvider} implementations, that extend the {@link ValueProvider}.
*
* @return an initialization that is used in the constructor of an (extension of) {@link ValueProvider}.
*/
public static ValueProviderInitialization createReproducibleValueProviderInitialization(long seed) {
return ValueProviderInitialization.createReproducibleInitialization(seed);
}
/**
* @return seed value for test class cycle or 0, if test class cycle is currently not active
*/
static long getTestClassSeed() {
TestClassInitializationCreator testClassVpiCreator = instance().testClassVpiCreator;
return getSeedOrZero(testClassVpiCreator);
}
private static long getSeedOrZero(InitializationCreator creator) {
return ofNullable(creator)
.map(InitializationCreator::getSeed)
.orElse(0L);
}
/**
* @return seed value for (last) test method cycle
*/
static long getTestMethodSeed() {
return instance().testMethodVpiCreator.getSeed();
}
static String getFormattedReferenceDateTime() {
return instance().testMethodVpiCreator.getFormattedReferenceDateTime();
}
@VisibleForTesting
static ValueProviderFactory instance() {
return INSTANCE.get();
}
}