View Javadoc
1   package org.codehaus.plexus.interpolation.object;
2   
3   /*
4    * Copyright 2001-2008 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.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import junit.framework.TestCase;
27  
28  import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
29  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
30  
31  public class FieldBasedObjectInterpolatorTest
32      extends TestCase
33  {
34  
35      public void testInterpolateStringArray()
36          throws Exception
37      {
38          Properties p = new Properties();
39          p.setProperty( "key", "value" );
40          p.setProperty( "key2", "value2" );
41  
42          String[] values = { "${key}", "${key2}" };
43  
44          StringSearchInterpolator interpolator = new StringSearchInterpolator();
45          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
46  
47          new FieldBasedObjectInterpolator().interpolate( values, interpolator );
48  
49          assertEquals( "value", values[0] );
50          assertEquals( "value2", values[1] );
51      }
52  
53      public void testInterpolateObjectWithStringArrayField()
54          throws Exception
55      {
56          Properties p = new Properties();
57          p.setProperty( "key", "value" );
58          p.setProperty( "key2", "value2" );
59  
60          String[] values = { "${key}", "${key2}" };
61  
62          ObjectWithStringArrayField obj = new ObjectWithStringArrayField( values );
63  
64          StringSearchInterpolator interpolator = new StringSearchInterpolator();
65          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
66  
67          new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
68  
69          assertEquals( "value", obj.values[0] );
70          assertEquals( "value2", obj.values[1] );
71      }
72  
73      public void testInterpolateObjectWithStringListField()
74          throws Exception
75      {
76          Properties p = new Properties();
77          p.setProperty( "key", "value" );
78          p.setProperty( "key2", "value2" );
79  
80          List<String> values = new ArrayList<String>();
81          values.add( "${key}" );
82          values.add( "${key2}" );
83  
84          ObjectWithListField obj = new ObjectWithListField( values );
85  
86          StringSearchInterpolator interpolator = new StringSearchInterpolator();
87          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
88  
89          new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
90  
91          assertEquals( "value", obj.values.get( 0 ) );
92          assertEquals( "value2", obj.values.get( 1 ) );
93      }
94  
95      public void testInterpolateObjectWithStringListFieldAndOneLiteralValue()
96          throws Exception
97      {
98          Properties p = new Properties();
99          p.setProperty( "key", "value" );
100         p.setProperty( "key2", "value2" );
101 
102         List<String> values = new ArrayList<String>();
103         values.add( "key" );
104         values.add( "${key2}" );
105 
106         ObjectWithListField obj = new ObjectWithListField( values );
107 
108         StringSearchInterpolator interpolator = new StringSearchInterpolator();
109         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
110 
111         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
112 
113         assertEquals( "key", obj.values.get( 0 ) );
114         assertEquals( "value2", obj.values.get( 1 ) );
115     }
116 
117     public void testInterpolateObjectWithUnmodifiableStringListField()
118         throws Exception
119     {
120         Properties p = new Properties();
121         p.setProperty( "key", "value" );
122         p.setProperty( "key2", "value2" );
123 
124         List values = Collections.unmodifiableList( Collections.singletonList( "${key}" ) );
125 
126         ObjectWithListField obj = new ObjectWithListField( values );
127 
128         StringSearchInterpolator interpolator = new StringSearchInterpolator();
129         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
130 
131         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
132 
133         assertEquals( "${key}", obj.values.get( 0 ) );
134     }
135 
136     public void testInterpolateObjectWithStringArrayListField()
137         throws Exception
138     {
139         Properties p = new Properties();
140         p.setProperty( "key", "value" );
141         p.setProperty( "key2", "value2" );
142         p.setProperty( "key3", "value3" );
143         p.setProperty( "key4", "value4" );
144 
145         List<String[]> values = new ArrayList<String[]>();
146         values.add( new String[] { "${key}", "${key2}" } );
147         values.add( new String[] { "${key3}", "${key4}" } );
148 
149         ObjectWithListField obj = new ObjectWithListField( values );
150 
151         StringSearchInterpolator interpolator = new StringSearchInterpolator();
152         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
153 
154         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
155 
156         assertEquals( "value", ( (String[]) obj.values.get( 0 ) )[0] );
157         assertEquals( "value2", ( (String[]) obj.values.get( 0 ) )[1] );
158         assertEquals( "value3", ( (String[]) obj.values.get( 1 ) )[0] );
159         assertEquals( "value4", ( (String[]) obj.values.get( 1 ) )[1] );
160     }
161 
162     public void testInterpolateObjectWithStringToStringMapField()
163         throws Exception
164     {
165         Properties p = new Properties();
166         p.setProperty( "key", "value" );
167         p.setProperty( "key2", "value2" );
168 
169         Map<String,String> values = new HashMap<String,String>();
170         values.put( "key", "${key}" );
171         values.put( "key2", "${key2}" );
172 
173         ObjectWithMapField obj = new ObjectWithMapField( values );
174 
175         StringSearchInterpolator interpolator = new StringSearchInterpolator();
176         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
177 
178         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
179 
180         assertEquals( "value", obj.values.get( "key" ) );
181         assertEquals( "value2", obj.values.get( "key2" ) );
182     }
183 
184     public void testInterpolateObjectWithStringToStringMapFieldAndOneLiteralValue()
185         throws Exception
186     {
187         Properties p = new Properties();
188         p.setProperty( "key", "value" );
189         p.setProperty( "key2", "value2" );
190 
191         Map<String,String> values = new HashMap<String,String>();
192         values.put( "key", "val" );
193         values.put( "key2", "${key2}" );
194 
195         ObjectWithMapField obj = new ObjectWithMapField( values );
196 
197         StringSearchInterpolator interpolator = new StringSearchInterpolator();
198         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
199 
200         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
201 
202         assertEquals( "val", obj.values.get( "key" ) );
203         assertEquals( "value2", obj.values.get( "key2" ) );
204     }
205 
206     public void testInterpolateObjectWithUnmodifiableStringToStringMapField()
207         throws Exception
208     {
209         Properties p = new Properties();
210         p.setProperty( "key", "value" );
211         p.setProperty( "key2", "value2" );
212 
213         Map values = Collections.unmodifiableMap( Collections.singletonMap( "key", "${key}" ) );
214 
215         ObjectWithMapField obj = new ObjectWithMapField( values );
216 
217         StringSearchInterpolator interpolator = new StringSearchInterpolator();
218         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
219 
220         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
221 
222         assertEquals( "${key}", obj.values.get( "key" ) );
223     }
224 
225     public void testInterpolateObjectWithStringToStringArrayMapField()
226         throws Exception
227     {
228         Properties p = new Properties();
229         p.setProperty( "key", "value" );
230         p.setProperty( "key2", "value2" );
231         p.setProperty( "key3", "value3" );
232         p.setProperty( "key4", "value4" );
233 
234         Map<String,String[]> values = new HashMap<String,String[]>();
235         values.put( "key", new String[] { "${key}", "${key2}" } );
236         values.put( "key2", new String[] { "${key3}", "${key4}" } );
237 
238         ObjectWithMapField obj = new ObjectWithMapField( values );
239 
240         StringSearchInterpolator interpolator = new StringSearchInterpolator();
241         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
242 
243         new FieldBasedObjectInterpolator().interpolate( obj, interpolator );
244 
245         assertEquals( "value", ( (String[]) obj.values.get( "key" ) )[0] );
246         assertEquals( "value2", ( (String[]) obj.values.get( "key" ) )[1] );
247         assertEquals( "value3", ( (String[]) obj.values.get( "key2" ) )[0] );
248         assertEquals( "value4", ( (String[]) obj.values.get( "key2" ) )[1] );
249     }
250 
251     private static final class ObjectWithStringArrayField
252     {
253         private final String[] values;
254 
255         public ObjectWithStringArrayField( String[] values )
256         {
257             this.values = values;
258         }
259     }
260 
261     private static final class ObjectWithListField
262     {
263         private final List values;
264 
265         public ObjectWithListField( List values )
266         {
267             this.values = values;
268         }
269     }
270 
271     private static final class ObjectWithMapField
272     {
273         private final Map values;
274 
275         public ObjectWithMapField( Map values )
276         {
277             this.values = values;
278         }
279     }
280 }