-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathStateToReproduce.java
More file actions
188 lines (149 loc) · 5.25 KB
/
StateToReproduce.java
File metadata and controls
188 lines (149 loc) · 5.25 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
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
package sqlancer;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import sqlancer.common.query.Query;
public class StateToReproduce implements Serializable {
private static final long serialVersionUID = 1L;
private List<Query<?>> statements = new ArrayList<>();
private final String databaseName;
private transient DatabaseProvider<?, ?, ?> databaseProvider;
public String databaseVersion;
protected long seedValue;
String exception;
public transient OracleRunReproductionState localState;
public StateToReproduce(String databaseName, DatabaseProvider<?, ?, ?> databaseProvider) {
this.databaseName = databaseName;
this.databaseProvider = databaseProvider;
}
public String getException() {
return exception;
}
public String getDatabaseName() {
return databaseName;
}
public String getDatabaseVersion() {
return databaseVersion;
}
public DatabaseProvider<?, ?, ?> getDatabaseProvider() {
return databaseProvider;
}
/**
* Logs the statement string without executing the corresponding statement.
*
* @param queryString
* the query string to be logged
*/
public void logStatement(String queryString) {
if (queryString == null) {
throw new IllegalArgumentException();
}
logStatement(databaseProvider.getLoggableFactory().getQueryForStateToReproduce(queryString));
}
/**
* Logs the statement without executing it.
*
* @param query
* the query to be logged
*/
public void logStatement(Query<?> query) {
if (query == null) {
throw new IllegalArgumentException();
}
statements.add(query);
}
public List<Query<?>> getStatements() {
return Collections.unmodifiableList(statements);
}
/**
* @deprecated
*/
@Deprecated
public void commentStatements() {
for (int i = 0; i < statements.size(); i++) {
Query<?> statement = statements.get(i);
Query<?> newQuery = databaseProvider.getLoggableFactory().commentOutQuery(statement);
statements.set(i, newQuery);
}
}
public long getSeedValue() {
return seedValue;
}
/**
* Returns a local state in which a test oracle can save useful information about a single run. If the local state
* is closed without indicating access to it, the local statements will be added to the global state.
*
* @return the local state for logging
*/
public OracleRunReproductionState getLocalState() {
return localState;
}
/**
* State information that is logged if the test oracle finds a bug or if an exception is thrown.
*/
public class OracleRunReproductionState implements Closeable {
private final List<Query<?>> statements = new ArrayList<>();
private boolean success;
public OracleRunReproductionState() {
StateToReproduce.this.localState = this;
}
public void executedWithoutError() {
this.success = true;
}
public void log(String s) {
statements.add(databaseProvider.getLoggableFactory().getQueryForStateToReproduce(s));
}
@Override
public void close() {
if (!success) {
StateToReproduce.this.statements.addAll(statements);
}
}
}
public OracleRunReproductionState createLocalState() {
return new OracleRunReproductionState();
}
public void serialize(Path path) {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(path))) {
oos.writeObject(this);
} catch (IOException e) {
throw new AssertionError(e);
}
}
public static StateToReproduce deserialize(Path path) {
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(path))) {
return (StateToReproduce) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new AssertionError(e);
}
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(this.databaseProvider != null ? this.databaseProvider.getDBMSName() : null);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
String dbmsName = (String) in.readObject();
DatabaseProvider<?, ?, ?> provider = null;
if (dbmsName != null) {
List<DatabaseProvider<?, ?, ?>> providers = Main.getDBMSProviders();
for (DatabaseProvider<?, ?, ?> p : providers) {
if (p.getDBMSName().equals(dbmsName)) {
provider = p;
break;
}
}
}
this.databaseProvider = provider;
}
public void setStatements(List<Query<?>> statements) {
this.statements = statements;
}
}