15
15
*/
16
16
package com .arpnetworking .utility ;
17
17
18
+ import com .google .common .collect .ImmutableList ;
18
19
import com .google .common .collect .ImmutableMap ;
19
20
import org .junit .Assert ;
20
21
import org .junit .Test ;
21
22
23
+ import java .util .LinkedHashMap ;
24
+ import java .util .Map ;
22
25
import java .util .regex .Pattern ;
23
26
24
27
/**
@@ -41,23 +44,31 @@ public void testInvalidEscape() {
41
44
final Pattern pattern = Pattern .compile ("test" );
42
45
final String input = "test" ;
43
46
final String replace = "${\\ avariable}" ; // \a is an invalid escape sequence
44
- final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , ImmutableMap . of ()).getReplacement ();
47
+ final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , new LinkedHashMap <> ()).getReplacement ();
45
48
}
46
49
47
50
@ Test (expected = IllegalArgumentException .class )
48
51
public void testMissingClosingCurly () {
49
52
final Pattern pattern = Pattern .compile ("test" );
50
53
final String input = "test" ;
51
54
final String replace = "${0" ; // no ending }
52
- final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , ImmutableMap . of ()).getReplacement ();
55
+ final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , new LinkedHashMap <> ()).getReplacement ();
53
56
}
54
57
55
58
@ Test (expected = IllegalArgumentException .class )
56
59
public void testInvalidEscapeAtEnd () {
57
60
final Pattern pattern = Pattern .compile ("test" );
58
61
final String input = "test" ;
59
62
final String replace = "${0}\\ " ; // trailing \
60
- final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , ImmutableMap .of ()).getReplacement ();
63
+ final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , new LinkedHashMap <>()).getReplacement ();
64
+ }
65
+
66
+ @ Test (expected = IllegalArgumentException .class )
67
+ public void testInvalidNoVariableName () {
68
+ final Pattern pattern = Pattern .compile ("test" );
69
+ final String input = "test" ;
70
+ final String replace = "${prefix:}" ; // variable prefix, but no name
71
+ final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , new LinkedHashMap <>()).getReplacement ();
61
72
}
62
73
63
74
@ Test
@@ -74,7 +85,7 @@ public void testInvalidReplacementTokenMissingOpen() {
74
85
final Pattern pattern = Pattern .compile ("test" );
75
86
final String input = "test" ;
76
87
final String replace = "$variable" ; // replacement variable has no {
77
- final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , ImmutableMap . of ()).getReplacement ();
88
+ final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , new LinkedHashMap <> ()).getReplacement ();
78
89
}
79
90
80
91
@ Test
@@ -167,6 +178,54 @@ public void testSingleMatchPartialMultipleGroupNameOverridesVariablesReplace() {
167
178
testExpression (pattern , input , replace , expected , ImmutableMap .of ("g1" , "bad" , "g2" , "value" ));
168
179
}
169
180
181
+ @ Test
182
+ public void testMatchPrefix () {
183
+ final Pattern pattern = Pattern .compile ("input_string_(?<var>.*)_(.*)" );
184
+ final String input = "input_string_ending_ending2" ;
185
+ final String replace = "${prefix1:var}_${prefix2:var}_${capture:var}_${capture:2}" ;
186
+ final String expected = "var1_var2_ending_ending2" ;
187
+ final LinkedHashMap <String , Map <String , String >> variables = new LinkedHashMap <>();
188
+ variables .put ("prefix1" , ImmutableMap .of ("var" , "var1" ));
189
+ variables .put ("prefix2" , ImmutableMap .of ("var" , "var2" ));
190
+ final RegexAndMapReplacer .Replacement replacement = testExpression (pattern , input , replace , expected , variables );
191
+ Assert .assertEquals (ImmutableList .of ("prefix1:var" , "prefix2:var" ), replacement .getVariablesMatched ());
192
+ }
193
+
194
+ @ Test
195
+ public void testMatchPrecedence () {
196
+ final Pattern pattern = Pattern .compile ("input_string_(?<var>.*)" );
197
+ final String input = "input_string_ending" ;
198
+ final String replace = "${var}_${var2}_${var3}" ;
199
+ final String expected = "ending_1-var2_2-var3" ;
200
+ final LinkedHashMap <String , Map <String , String >> variables = new LinkedHashMap <>();
201
+ variables .put ("prefix1" , ImmutableMap .of ("var" , "1-var" , "var2" , "1-var2" ));
202
+ variables .put ("prefix2" , ImmutableMap .of ("var" , "2-var" , "var2" , "2-var2" , "var3" , "2-var3" ));
203
+ final RegexAndMapReplacer .Replacement replacement = testExpression (pattern , input , replace , expected , variables );
204
+ Assert .assertEquals (ImmutableList .of ("prefix1:var2" , "prefix2:var3" ), replacement .getVariablesMatched ());
205
+ }
206
+
207
+ @ Test
208
+ public void testMatchMissingVars () {
209
+ final Pattern pattern = Pattern .compile ("input_string" );
210
+ final String input = "input_string_ending" ;
211
+ final String replace = "${prefix1:missing}1${capture:missing}2${missing}" ;
212
+ final String expected = "12_ending" ;
213
+ final LinkedHashMap <String , Map <String , String >> variables = new LinkedHashMap <>();
214
+ variables .put ("prefix1" , ImmutableMap .of ("var" , "var1" ));
215
+ testExpression (pattern , input , replace , expected , variables );
216
+ }
217
+
218
+ @ Test (expected = IllegalArgumentException .class )
219
+ public void testMatchInvalidPrefix () {
220
+ final Pattern pattern = Pattern .compile ("input_string_(?<var>.*)" );
221
+ final String input = "input_string_ending" ;
222
+ final String replace = "${notfound:var}_value" ;
223
+ final String expected = "illegal arg exception" ;
224
+ final LinkedHashMap <String , Map <String , String >> variables = new LinkedHashMap <>();
225
+ variables .put ("prefix1" , ImmutableMap .of ("var" , "var1" ));
226
+ testExpression (pattern , input , replace , expected , variables );
227
+ }
228
+
170
229
@ Test
171
230
public void testMultipleMatchFullStaticReplace () {
172
231
final Pattern pattern = Pattern .compile ("test" );
@@ -185,13 +244,30 @@ public void testMultipleMatchPartialStaticReplace() {
185
244
testExpression (pattern , input , replace , expected , ImmutableMap .of ());
186
245
}
187
246
188
- private void testExpression (final Pattern pattern , final String input , final String replace , final String expected ,
247
+ private RegexAndMapReplacer .Replacement testExpression (
248
+ final Pattern pattern ,
249
+ final String input ,
250
+ final String replace ,
251
+ final String expected ,
189
252
final ImmutableMap <String , String > variables ) {
190
- final String result = RegexAndMapReplacer .replaceAll (pattern , input , replace , variables ).getReplacement ();
253
+ final LinkedHashMap <String , Map <String , String >> variablesMap = new LinkedHashMap <>();
254
+ variablesMap .put ("myvars" , variables );
255
+ return testExpression (pattern , input , replace , expected , variablesMap );
256
+ }
257
+
258
+ private RegexAndMapReplacer .Replacement testExpression (
259
+ final Pattern pattern ,
260
+ final String input ,
261
+ final String replace ,
262
+ final String expected ,
263
+ final LinkedHashMap <String , Map <String , String >> variables ) {
264
+ final RegexAndMapReplacer .Replacement replacement = RegexAndMapReplacer .replaceAll (pattern , input , replace , variables );
265
+ final String result = replacement .getReplacement ();
191
266
Assert .assertEquals (expected , result );
192
267
try {
193
268
final String stockResult = pattern .matcher (input ).replaceAll (replace );
194
269
Assert .assertEquals (expected , stockResult );
195
270
} catch (final IllegalArgumentException ignored ) { }
271
+ return replacement ;
196
272
}
197
273
}
0 commit comments