-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathProviderAdapter.java
More file actions
90 lines (75 loc) · 3.32 KB
/
ProviderAdapter.java
File metadata and controls
90 lines (75 loc) · 3.32 KB
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
package sqlancer;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.StateToReproduce.OracleRunReproductionState;
import sqlancer.common.oracle.CompositeTestOracle;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.schema.AbstractSchema;
public abstract class ProviderAdapter<G extends GlobalState<O, ? extends AbstractSchema<G, ?>, C>, O extends DBMSSpecificOptions<? extends OracleFactory<G>>, C extends SQLancerDBConnection>
implements DatabaseProvider<G, O, C> {
private final Class<G> globalClass;
private final Class<O> optionClass;
public ProviderAdapter(Class<G> globalClass, Class<O> optionClass) {
this.globalClass = globalClass;
this.optionClass = optionClass;
}
@Override
public StateToReproduce getStateToReproduce(String databaseName) {
return new StateToReproduce(databaseName, this);
}
@Override
public Class<G> getGlobalStateClass() {
return globalClass;
}
@Override
public Class<O> getOptionClass() {
return optionClass;
}
@Override
public void generateAndTestDatabase(G globalState) throws Exception {
try {
generateDatabase(globalState);
checkViewsAreValid(globalState);
globalState.getManager().incrementCreateDatabase();
TestOracle oracle = getTestOracle(globalState);
for (int i = 0; i < globalState.getOptions().getNrQueries(); i++) {
try (OracleRunReproductionState localState = globalState.getState().createLocalState()) {
assert localState != null;
try {
oracle.check();
globalState.getManager().incrementSelectQueryCount();
} catch (IgnoreMeException e) {
}
assert localState != null;
localState.executedWithoutError();
}
}
} finally {
globalState.getConnection().close();
}
}
protected abstract void checkViewsAreValid(G globalState);
protected TestOracle getTestOracle(G globalState) throws Exception {
List<? extends OracleFactory<G>> testOracleFactory = globalState.getDmbsSpecificOptions()
.getTestOracleFactory();
boolean testOracleRequiresMoreThanZeroRows = testOracleFactory.stream()
.anyMatch(p -> p.requiresAllTablesToContainRows());
boolean userRequiresMoreThanZeroRows = globalState.getOptions().testOnlyWithMoreThanZeroRows();
boolean checkZeroRows = testOracleRequiresMoreThanZeroRows || userRequiresMoreThanZeroRows;
if (checkZeroRows && globalState.getSchema().containsTableWithZeroRows(globalState)) {
throw new IgnoreMeException();
}
if (testOracleFactory.size() == 1) {
return testOracleFactory.get(0).create(globalState);
} else {
return new CompositeTestOracle(testOracleFactory.stream().map(o -> {
try {
return o.create(globalState);
} catch (Exception e1) {
throw new AssertionError(e1);
}
}).collect(Collectors.toList()), globalState);
}
}
public abstract void generateDatabase(G globalState) throws Exception;
}