-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathExpectedErrors.java
More file actions
149 lines (127 loc) · 4.31 KB
/
ExpectedErrors.java
File metadata and controls
149 lines (127 loc) · 4.31 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
package sqlancer.common.query;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
/**
* This class represents the errors that executing a statement might result in. For example, an INSERT statement might
* result in an error "UNIQUE constraint violated" when it attempts to insert a duplicate value in a column declared as
* UNIQUE.
*/
public class ExpectedErrors implements Serializable {
private static final long serialVersionUID = 1L;
private final Set<String> errors;
private final Set<Pattern> regexes;
public ExpectedErrors() {
this.errors = new HashSet<>();
this.regexes = new HashSet<>();
}
public ExpectedErrors(Collection<String> errors, Collection<Pattern> regexErrors) {
this.errors = new HashSet<>(errors);
this.regexes = new HashSet<>(regexErrors);
}
public ExpectedErrors add(String error) {
if (error == null) {
throw new IllegalArgumentException();
}
errors.add(error);
return this;
}
public ExpectedErrors addRegex(Pattern errorPattern) {
if (errorPattern == null) {
throw new IllegalArgumentException();
}
regexes.add(errorPattern);
return this;
}
public ExpectedErrors addRegexString(String errorPattern) {
if (errorPattern == null) {
throw new IllegalArgumentException();
}
regexes.add(Pattern.compile(errorPattern));
return this;
}
public ExpectedErrors addAll(Collection<String> list) {
if (list == null) {
throw new IllegalArgumentException();
}
errors.addAll(list);
return this;
}
public ExpectedErrors addAllRegexes(Collection<Pattern> list) {
if (list == null) {
throw new IllegalArgumentException();
}
regexes.addAll(list);
return this;
}
public ExpectedErrors addAllRegexStrings(Collection<String> list) {
for (String error : list) {
regexes.add(Pattern.compile(error));
}
return this;
}
public static ExpectedErrors from(String... errors) {
return newErrors().with(errors).build();
}
public static ExpectedErrorsBuilder newErrors() {
return new ExpectedErrorsBuilder();
}
/**
* Checks whether the error message (e.g., returned by the DBMS under test) contains any of the added error
* messages.
*
* @param error
* the error message
*
* @return whether the error message contains any of the substrings specified as expected errors
*/
public boolean errorIsExpected(String error) {
if (error == null) {
throw new IllegalArgumentException();
}
for (String s : this.errors) {
if (error.contains(s)) {
return true;
}
}
for (Pattern p : this.regexes) {
if (p.matcher(error).find()) {
return true;
}
}
return false;
}
public static class ExpectedErrorsBuilder {
private final Set<String> errors = new HashSet<>();
private final Set<Pattern> regexes = new HashSet<>();
public ExpectedErrorsBuilder with(String... list) {
errors.addAll(Arrays.asList(list));
return this;
}
public ExpectedErrorsBuilder with(Collection<String> list) {
return with(list.toArray(new String[0]));
}
public ExpectedErrorsBuilder withRegex(Pattern... list) {
regexes.addAll(Arrays.asList(list));
return this;
}
public ExpectedErrorsBuilder withRegex(Collection<Pattern> list) {
return withRegex(list.toArray(new Pattern[0]));
}
public ExpectedErrorsBuilder withRegexString(String... list) {
for (String error : list) {
regexes.add(Pattern.compile(error));
}
return this;
}
public ExpectedErrorsBuilder withRegexString(Collection<String> list) {
return withRegexString(list.toArray(new String[0]));
}
public ExpectedErrors build() {
return new ExpectedErrors(errors, regexes);
}
}
}