View Javadoc
1   package org.codehaus.plexus.interpolation.multi;
2   
3   /*
4    * Copyright 2001-2009 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.codehaus.plexus.interpolation.InterpolationException;
23  import org.codehaus.plexus.interpolation.MapBasedValueSource;
24  import org.codehaus.plexus.interpolation.ValueSource;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  
29  public class MultiDelimiterStringSearchInterpolatorTest {
30  
31      @Test
32      public void testInterpolationWithDifferentDelimiters() throws InterpolationException {
33          Map ctx = new HashMap();
34          ctx.put("name", "User");
35          ctx.put("otherName", "@name@");
36  
37          String input = "${otherName}";
38  
39          ValueSource vs = new MapBasedValueSource(ctx);
40          MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator()
41                  .addDelimiterSpec("@")
42                  .withValueSource(vs);
43  
44          String result = interpolator.interpolate(input);
45  
46          assertEquals(ctx.get("name"), result);
47      }
48  
49      @Test
50      public void testSuccessiveInterpolationWithDifferentDelimiters_ReversedDelimiterSequence()
51              throws InterpolationException {
52          Map ctx = new HashMap();
53          ctx.put("name", "User");
54          ctx.put("otherName", "${name}");
55  
56          String input = "@otherName@";
57  
58          ValueSource vs = new MapBasedValueSource(ctx);
59          MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator()
60                  .addDelimiterSpec("@")
61                  .withValueSource(vs);
62  
63          String result = interpolator.interpolate(input);
64  
65          assertEquals(ctx.get("name"), result);
66      }
67  
68      @Test
69      public void testInterpolationWithMultipleEscapes() throws InterpolationException {
70          Map ctx = new HashMap();
71          ctx.put("name", "User");
72          ctx.put("otherName", "##${first} and #${last}");
73  
74          String input = "${otherName}";
75  
76          ValueSource vs = new MapBasedValueSource(ctx);
77          MultiDelimiterStringSearchInterpolator interpolator =
78                  new MultiDelimiterStringSearchInterpolator().withValueSource(vs);
79          interpolator.setEscapeString("#");
80  
81          String result = interpolator.interpolate(input);
82  
83          assertEquals("#${first} and ${last}", result);
84      }
85  
86      @Test
87      public void testInterpolationWithMultipleEscapes2() throws InterpolationException {
88          Map ctx = new HashMap();
89          ctx.put("name", "User");
90          ctx.put("otherName", "#${first} and ##${last}");
91  
92          String input = "${otherName}";
93  
94          ValueSource vs = new MapBasedValueSource(ctx);
95          MultiDelimiterStringSearchInterpolator interpolator =
96                  new MultiDelimiterStringSearchInterpolator().withValueSource(vs);
97          interpolator.setEscapeString("#");
98  
99          String result = interpolator.interpolate(input);
100 
101         assertEquals("${first} and #${last}", result);
102     }
103 
104     @Test
105     public void testInterpolationWithMultipleEscapes3() throws InterpolationException {
106         Map ctx = new HashMap();
107         ctx.put("name", "User");
108         ctx.put("last", "beer");
109         ctx.put("otherName", "###${first} and ##${second} and ${last}");
110 
111         String input = "${otherName}";
112 
113         ValueSource vs = new MapBasedValueSource(ctx);
114         MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator() //
115                 .withValueSource(vs) //
116                 .escapeString("#");
117 
118         String result = interpolator.interpolate(input);
119 
120         assertEquals("##${first} and #${second} and beer", result);
121     }
122 }