1
1
/*
2
- * Copyright 2006-2022 the original author or authors.
2
+ * Copyright 2006-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
23
23
import java .util .HashMap ;
24
24
import java .util .Map ;
25
+ import java .util .regex .PatternSyntaxException ;
25
26
26
27
import org .junit .jupiter .api .Test ;
27
28
28
29
/**
29
30
* @author Dan Garrette
31
+ * @author Injae Kim
30
32
* @since 2.0
31
33
*/
32
34
class PatternMatcherTests {
@@ -37,6 +39,7 @@ class PatternMatcherTests {
37
39
map .put ("an*" , 3 );
38
40
map .put ("a*" , 2 );
39
41
map .put ("big*" , 4 );
42
+ map .put ("bcd.*" , 5 );
40
43
}
41
44
42
45
private static final Map <String , Integer > defaultMap = new HashMap <>();
@@ -49,6 +52,15 @@ class PatternMatcherTests {
49
52
defaultMap .put ("*" , 1 );
50
53
}
51
54
55
+ private static final Map <String , Integer > regexMap = new HashMap <>();
56
+
57
+ static {
58
+ regexMap .put ("abc.*" , 1 );
59
+ regexMap .put ("a...e" , 2 );
60
+ regexMap .put ("123.[0-9][0-9]\\ d" , 3 );
61
+ regexMap .put ("*............" , 100 ); // invalid regex format
62
+ }
63
+
52
64
@ Test
53
65
void testMatchNoWildcardYes () {
54
66
assertTrue (PatternMatcher .match ("abc" , "abc" ));
@@ -104,6 +116,29 @@ void testMatchStarNo() {
104
116
assertFalse (PatternMatcher .match ("a*c" , "abdeg" ));
105
117
}
106
118
119
+ @ Test
120
+ void testMatchRegex () {
121
+ assertTrue (PatternMatcher .matchRegex ("abc.*" , "abcde" ));
122
+ }
123
+
124
+ @ Test
125
+ void testMatchRegex_notMatched () {
126
+ assertFalse (PatternMatcher .matchRegex ("abc.*" , "cdefg" ));
127
+ assertFalse (PatternMatcher .matchRegex ("abc." , "abcde" ));
128
+ }
129
+
130
+ @ Test
131
+ void testMatchRegex_thrown_invalidRegexFormat () {
132
+ assertThrows (PatternSyntaxException .class , () -> PatternMatcher .matchRegex ("*.." , "abc" ));
133
+ }
134
+
135
+ @ Test
136
+ void testMatchRegex_thrown_notNullParam () {
137
+ assertThrows (IllegalArgumentException .class , () -> PatternMatcher .matchRegex ("regex" , null ));
138
+ assertThrows (IllegalArgumentException .class , () -> PatternMatcher .matchRegex (null , "str" ));
139
+ }
140
+
141
+
107
142
@ Test
108
143
void testMatchPrefixSubsumed () {
109
144
assertEquals (2 , new PatternMatcher <>(map ).match ("apple" ).intValue ());
@@ -119,6 +154,11 @@ void testMatchPrefixUnrelated() {
119
154
assertEquals (4 , new PatternMatcher <>(map ).match ("biggest" ).intValue ());
120
155
}
121
156
157
+ @ Test
158
+ void testMatchByRegex () {
159
+ assertEquals (5 , new PatternMatcher <>(map ).match ("bcdef12345" ).intValue ());
160
+ }
161
+
122
162
@ Test
123
163
void testMatchPrefixNoMatch () {
124
164
PatternMatcher <Integer > matcher = new PatternMatcher <>(map );
@@ -140,4 +180,24 @@ void testMatchPrefixDefaultValueNoMatch() {
140
180
assertEquals (1 , new PatternMatcher <>(defaultMap ).match ("bat" ).intValue ());
141
181
}
142
182
183
+ @ Test
184
+ void testMatchRegexPrefix () {
185
+ assertEquals (1 , new PatternMatcher <>(regexMap ).match ("abcdefg" ).intValue ());
186
+ }
187
+
188
+ @ Test
189
+ void testMatchRegexWildCards () {
190
+ assertEquals (2 , new PatternMatcher <>(regexMap ).match ("a123e" ).intValue ());
191
+ }
192
+
193
+ @ Test
194
+ void testMatchRegexDigits () {
195
+ assertEquals (3 , new PatternMatcher <>(regexMap ).match ("123-789" ).intValue ());
196
+ }
197
+
198
+ @ Test
199
+ void testMatchRegexNotMatched () {
200
+ assertThrows (IllegalStateException .class , () -> new PatternMatcher <>(regexMap ).match ("Hello world!" ));
201
+ }
202
+
143
203
}
0 commit comments