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 org.codehaus.plexus.interpolation.InterpolationException;
20  import org.codehaus.plexus.interpolation.MapBasedValueSource;
21  import org.codehaus.plexus.interpolation.ValueSource;
22  import org.codehaus.plexus.interpolation.multi.MultiDelimiterStringSearchInterpolator;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import junit.framework.TestCase;
28  
29  public class MultiDelimiterStringSearchInterpolatorTest
30      extends TestCase
31  {
32  
33      public void testInterpolationWithDifferentDelimiters()
34          throws InterpolationException
35      {
36          Map ctx = new HashMap();
37          ctx.put( "name", "User" );
38          ctx.put( "otherName", "@name@" );
39  
40          String input = "${otherName}";
41  
42          ValueSource vs = new MapBasedValueSource( ctx );
43          MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator().addDelimiterSpec( "@" )
44                                                                        .withValueSource( vs );
45  
46          String result = interpolator.interpolate( input );
47  
48          assertEquals( ctx.get( "name" ), result );
49      }
50  
51      public void testSuccessiveInterpolationWithDifferentDelimiters_ReversedDelimiterSequence()
52          throws InterpolationException
53      {
54          Map ctx = new HashMap();
55          ctx.put( "name", "User" );
56          ctx.put( "otherName", "${name}" );
57  
58          String input = "@otherName@";
59  
60          ValueSource vs = new MapBasedValueSource( ctx );
61          MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator().addDelimiterSpec( "@" )
62                                                                        .withValueSource( vs );
63  
64          String result = interpolator.interpolate( input );
65  
66          assertEquals( ctx.get( "name" ), result );
67      }
68  
69      public void testInterpolationWithMultipleEscapes()
70              throws InterpolationException
71          {
72              Map ctx = new HashMap();
73              ctx.put( "name", "User" );
74              ctx.put( "otherName", "##${first} and #${last}" );
75  
76              String input = "${otherName}";
77  
78              ValueSource vs = new MapBasedValueSource( ctx );
79              MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator()
80                                                                            .withValueSource( vs );
81              interpolator.setEscapeString("#");
82  
83              String result = interpolator.interpolate( input );
84  
85              assertEquals( "#${first} and ${last}", result );
86          }
87  
88      public void testInterpolationWithMultipleEscapes2()
89          throws InterpolationException
90      {
91          Map ctx = new HashMap();
92          ctx.put( "name", "User" );
93          ctx.put( "otherName", "#${first} and ##${last}" );
94  
95          String input = "${otherName}";
96  
97          ValueSource vs = new MapBasedValueSource( ctx );
98          MultiDelimiterStringSearchInterpolator interpolator =
99              new MultiDelimiterStringSearchInterpolator().withValueSource( vs );
100         interpolator.setEscapeString( "#" );
101 
102         String result = interpolator.interpolate( input );
103 
104         assertEquals( "${first} and #${last}", result );
105     }
106 
107     public void testInterpolationWithMultipleEscapes3()
108         throws InterpolationException
109     {
110         Map ctx = new HashMap();
111         ctx.put( "name", "User" );
112         ctx.put( "last", "beer" );
113         ctx.put( "otherName", "###${first} and ##${second} and ${last}" );
114 
115         String input = "${otherName}";
116 
117         ValueSource vs = new MapBasedValueSource( ctx );
118         MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator() //
119             .withValueSource( vs ) //
120             .escapeString( "#" );
121 
122         String result = interpolator.interpolate( input );
123 
124         assertEquals( "##${first} and #${second} and beer", result );
125     }
126 }