11/*
2- * Copyright 2006-2022 the original author or authors.
2+ * Copyright 2006-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2222
2323import java .util .HashMap ;
2424import java .util .Map ;
25+ import java .util .regex .PatternSyntaxException ;
2526
2627import org .junit .jupiter .api .Test ;
2728
2829/**
2930 * @author Dan Garrette
31+ * @author Injae Kim
3032 * @since 2.0
3133 */
3234class PatternMatcherTests {
@@ -37,6 +39,7 @@ class PatternMatcherTests {
3739 map .put ("an*" , 3 );
3840 map .put ("a*" , 2 );
3941 map .put ("big*" , 4 );
42+ map .put ("bcd.*" , 5 );
4043 }
4144
4245 private static final Map <String , Integer > defaultMap = new HashMap <>();
@@ -49,6 +52,15 @@ class PatternMatcherTests {
4952 defaultMap .put ("*" , 1 );
5053 }
5154
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+
5264 @ Test
5365 void testMatchNoWildcardYes () {
5466 assertTrue (PatternMatcher .match ("abc" , "abc" ));
@@ -104,6 +116,29 @@ void testMatchStarNo() {
104116 assertFalse (PatternMatcher .match ("a*c" , "abdeg" ));
105117 }
106118
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+
107142 @ Test
108143 void testMatchPrefixSubsumed () {
109144 assertEquals (2 , new PatternMatcher <>(map ).match ("apple" ).intValue ());
@@ -119,6 +154,11 @@ void testMatchPrefixUnrelated() {
119154 assertEquals (4 , new PatternMatcher <>(map ).match ("biggest" ).intValue ());
120155 }
121156
157+ @ Test
158+ void testMatchByRegex () {
159+ assertEquals (5 , new PatternMatcher <>(map ).match ("bcdef12345" ).intValue ());
160+ }
161+
122162 @ Test
123163 void testMatchPrefixNoMatch () {
124164 PatternMatcher <Integer > matcher = new PatternMatcher <>(map );
@@ -140,4 +180,24 @@ void testMatchPrefixDefaultValueNoMatch() {
140180 assertEquals (1 , new PatternMatcher <>(defaultMap ).match ("bat" ).intValue ());
141181 }
142182
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+
143203}
0 commit comments