forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionValueMatcher.java
More file actions
96 lines (86 loc) · 3.53 KB
/
RegularExpressionValueMatcher.java
File metadata and controls
96 lines (86 loc) · 3.53 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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skyscreamer.jsonassert;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.skyscreamer.jsonassert.ValueMatcher;
/**
* A JSONassert value matcher that matches actual value to regular expression.
* If non-null regular expression passed to constructor, then all actual values
* will be compared against this constant pattern, ignoring any expected value
* passed to equal method. If null regular expression passed to constructor,
* then expected value passed to equals method will be used to dynamically
* specify regular expression pattern that actual value must match.
*
* @author Duncan Mackinder
*
*/
public class RegularExpressionValueMatcher<T> implements ValueMatcher<T> {
private final Pattern expectedPattern;
/**
* Create RegularExpressionValueMatcher in which the pattern the actual
* value must match with be specified dynamically from the expected string
* passed to this matcher in the equals method.
*/
public RegularExpressionValueMatcher() {
this(null);
}
/**
* Create RegularExpressionValueMatcher with specified pattern. If pattern
* is not null, it must be a valid regular expression that defines a
* constant expected pattern that every actual value must match (in this
* case the expected value passed to equal method will be ignored). If
* pattern is null, the pattern the actual value must match with be
* specified dynamically from the expected string passed to this matcher in
* the equals method.
*
* @param pattern
* if non null, regular expression pattern which all actual
* values this matcher is applied to must match. If null, this
* matcher will apply pattern specified dynamically via the
* expected parameter to the equal method.
* @throws IllegalArgumentException
* if pattern is non-null and not a valid regular expression.
*/
public RegularExpressionValueMatcher(String pattern) throws IllegalArgumentException {
try {
expectedPattern = pattern == null ? null : Pattern.compile(pattern);
}
catch (PatternSyntaxException e) {
throw new IllegalArgumentException("Constant expected pattern invalid: " + e.getMessage(), e);
}
}
@Override
public boolean equal(T actual, T expected) {
String actualString = actual.toString();
String expectedString = expected.toString();
try {
Pattern pattern = isStaticPattern() ? expectedPattern : Pattern
.compile(expectedString);
if (!pattern.matcher(actualString).matches()) {
throw new ValueMatcherException(getPatternType() + " expected pattern did not match value", pattern.toString(), actualString);
}
}
catch (PatternSyntaxException e) {
throw new ValueMatcherException(getPatternType() + " expected pattern invalid: " + e.getMessage(), e, expectedString, actualString);
}
return true;
}
private boolean isStaticPattern() {
return expectedPattern != null;
}
private String getPatternType() {
return isStaticPattern()? "Constant": "Dynamic";
}
}