1 package org.apache.maven.project.interpolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.apache.maven.model.Model;
31 import org.apache.maven.project.DefaultProjectBuilderConfiguration;
32 import org.apache.maven.project.ProjectBuilderConfiguration;
33 import org.apache.maven.project.path.PathTranslator;
34
35
36
37
38
39 public class StringSearchModelInterpolatorTest
40 extends AbstractModelInterpolatorTest
41 {
42 protected ModelInterpolator createInterpolator( PathTranslator translator )
43 throws Exception
44 {
45 StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator( translator );
46 interpolator.initialize();
47
48 return interpolator;
49 }
50
51 protected ModelInterpolator createInterpolator()
52 throws Exception
53 {
54 StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
55 interpolator.initialize();
56
57 return interpolator;
58 }
59
60 public void testInterpolateStringArray()
61 throws Exception
62 {
63 Model model = new Model();
64
65 Properties p = new Properties();
66 p.setProperty( "key", "value" );
67 p.setProperty( "key2", "value2" );
68
69 String[] values = { "${key}", "${key2}" };
70
71 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
72
73 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
74
75 interpolator.interpolateObject( values, model, new File( "." ), config, true );
76
77 assertEquals( "value", values[0] );
78 assertEquals( "value2", values[1] );
79 }
80
81 public void testInterpolateObjectWithStringArrayField()
82 throws Exception
83 {
84 Model model = new Model();
85
86 Properties p = new Properties();
87 p.setProperty( "key", "value" );
88 p.setProperty( "key2", "value2" );
89
90 String[] values = { "${key}", "${key2}" };
91
92 ObjectWithStringArrayField obj = new ObjectWithStringArrayField( values );
93
94 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
95
96 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
97
98 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
99
100 assertEquals( "value", obj.values[0] );
101 assertEquals( "value2", obj.values[1] );
102 }
103
104 public void testInterpolateObjectWithStringListField()
105 throws Exception
106 {
107 Model model = new Model();
108
109 Properties p = new Properties();
110 p.setProperty( "key", "value" );
111 p.setProperty( "key2", "value2" );
112
113 List values = new ArrayList();
114 values.add( "${key}" );
115 values.add( "${key2}" );
116
117 ObjectWithListField obj = new ObjectWithListField( values );
118
119 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
120
121 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
122
123 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
124
125 assertEquals( "value", obj.values.get( 0 ) );
126 assertEquals( "value2", obj.values.get( 1 ) );
127 }
128
129 public void testInterpolateObjectWithStringListFieldAndOneLiteralValue()
130 throws Exception
131 {
132 Model model = new Model();
133
134 Properties p = new Properties();
135 p.setProperty( "key", "value" );
136 p.setProperty( "key2", "value2" );
137
138 List values = new ArrayList();
139 values.add( "key" );
140 values.add( "${key2}" );
141
142 ObjectWithListField obj = new ObjectWithListField( values );
143
144 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
145
146 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
147
148 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
149
150 assertEquals( "key", obj.values.get( 0 ) );
151 assertEquals( "value2", obj.values.get( 1 ) );
152 }
153
154 public void testInterpolateObjectWithUnmodifiableStringListField()
155 throws Exception
156 {
157 Model model = new Model();
158
159 Properties p = new Properties();
160 p.setProperty( "key", "value" );
161 p.setProperty( "key2", "value2" );
162
163 List values = Collections.unmodifiableList( Collections.singletonList( "${key}" ) );
164
165 ObjectWithListField obj = new ObjectWithListField( values );
166
167 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
168
169 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
170
171 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
172
173 assertEquals( "${key}", obj.values.get( 0 ) );
174 }
175
176 public void testInterpolateObjectWithStringArrayListField()
177 throws Exception
178 {
179 Model model = new Model();
180
181 Properties p = new Properties();
182 p.setProperty( "key", "value" );
183 p.setProperty( "key2", "value2" );
184 p.setProperty( "key3", "value3" );
185 p.setProperty( "key4", "value4" );
186
187 List values = new ArrayList();
188 values.add( new String[] { "${key}", "${key2}" } );
189 values.add( new String[] { "${key3}", "${key4}" } );
190
191 ObjectWithListField obj = new ObjectWithListField( values );
192
193 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
194
195 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
196
197 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
198
199 assertEquals( "value", ( (String[]) obj.values.get( 0 ) )[0] );
200 assertEquals( "value2", ( (String[]) obj.values.get( 0 ) )[1] );
201 assertEquals( "value3", ( (String[]) obj.values.get( 1 ) )[0] );
202 assertEquals( "value4", ( (String[]) obj.values.get( 1 ) )[1] );
203 }
204
205 public void testInterpolateObjectWithStringToStringMapField()
206 throws Exception
207 {
208 Model model = new Model();
209
210 Properties p = new Properties();
211 p.setProperty( "key", "value" );
212 p.setProperty( "key2", "value2" );
213
214 Map values = new HashMap();
215 values.put( "key", "${key}" );
216 values.put( "key2", "${key2}" );
217
218 ObjectWithMapField obj = new ObjectWithMapField( values );
219
220 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
221
222 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
223
224 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
225
226 assertEquals( "value", obj.values.get( "key" ) );
227 assertEquals( "value2", obj.values.get( "key2" ) );
228 }
229
230 public void testInterpolateObjectWithStringToStringMapFieldAndOneLiteralValue()
231 throws Exception
232 {
233 Model model = new Model();
234
235 Properties p = new Properties();
236 p.setProperty( "key", "value" );
237 p.setProperty( "key2", "value2" );
238
239 Map values = new HashMap();
240 values.put( "key", "val" );
241 values.put( "key2", "${key2}" );
242
243 ObjectWithMapField obj = new ObjectWithMapField( values );
244
245 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
246
247 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
248
249 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
250
251 assertEquals( "val", obj.values.get( "key" ) );
252 assertEquals( "value2", obj.values.get( "key2" ) );
253 }
254
255 public void testInterpolateObjectWithUnmodifiableStringToStringMapField()
256 throws Exception
257 {
258 Model model = new Model();
259
260 Properties p = new Properties();
261 p.setProperty( "key", "value" );
262 p.setProperty( "key2", "value2" );
263
264 Map values = Collections.unmodifiableMap( Collections.singletonMap( "key", "${key}" ) );
265
266 ObjectWithMapField obj = new ObjectWithMapField( values );
267
268 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
269
270 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
271
272 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
273
274 assertEquals( "${key}", obj.values.get( "key" ) );
275 }
276
277 public void testInterpolateObjectWithStringToStringArrayMapField()
278 throws Exception
279 {
280 Model model = new Model();
281
282 Properties p = new Properties();
283 p.setProperty( "key", "value" );
284 p.setProperty( "key2", "value2" );
285 p.setProperty( "key3", "value3" );
286 p.setProperty( "key4", "value4" );
287
288 Map values = new HashMap();
289 values.put( "key", new String[] { "${key}", "${key2}" } );
290 values.put( "key2", new String[] { "${key3}", "${key4}" } );
291
292 ObjectWithMapField obj = new ObjectWithMapField( values );
293
294 StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
295
296 ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties( p );
297
298 interpolator.interpolateObject( obj, model, new File( "." ), config, true );
299
300 assertEquals( "value", ( (String[]) obj.values.get( "key" ) )[0] );
301 assertEquals( "value2", ( (String[]) obj.values.get( "key" ) )[1] );
302 assertEquals( "value3", ( (String[]) obj.values.get( "key2" ) )[0] );
303 assertEquals( "value4", ( (String[]) obj.values.get( "key2" ) )[1] );
304 }
305
306 private static final class ObjectWithStringArrayField
307 {
308 private final String[] values;
309
310 public ObjectWithStringArrayField( String[] values )
311 {
312 this.values = values;
313 }
314 }
315
316 private static final class ObjectWithListField
317 {
318 private final List values;
319
320 public ObjectWithListField( List values )
321 {
322 this.values = values;
323 }
324 }
325
326 private static final class ObjectWithMapField
327 {
328 private final Map values;
329
330 public ObjectWithMapField( Map values )
331 {
332 this.values = values;
333 }
334 }
335
336 }