1 package org.apache.maven.plugin.assembly.interpolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.execution.MavenSession;
25 import org.apache.maven.model.Build;
26 import org.apache.maven.model.Model;
27 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
28 import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
29 import org.apache.maven.plugin.assembly.io.AssemblyReadException;
30 import org.apache.maven.plugin.assembly.io.DefaultAssemblyReader;
31 import org.apache.maven.plugin.assembly.io.DefaultAssemblyReaderTest;
32 import org.apache.maven.plugin.assembly.model.Assembly;
33 import org.apache.maven.plugin.assembly.model.DependencySet;
34 import org.apache.maven.plugin.assembly.testutils.PojoConfigSource;
35 import org.apache.maven.project.MavenProject;
36 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
37 import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
38 import org.easymock.classextension.EasyMockSupport;
39
40 import java.io.IOException;
41 import java.io.StringReader;
42 import java.util.List;
43 import java.util.Properties;
44
45 import static org.easymock.EasyMock.expect;
46
47 public class AssemblyInterpolatorTest
48 extends TestCase
49 {
50
51 public void testDependencySetOutputFileNameMappingsAreNotInterpolated()
52 throws IOException, AssemblyInterpolationException, AssemblyReadException,
53 InvalidAssemblerConfigurationException
54 {
55 final Model model = new Model();
56 model.setArtifactId( "artifact-id" );
57 model.setGroupId( "group.id" );
58 model.setVersion( "1" );
59 model.setPackaging( "jar" );
60
61 final MavenProject project = new MavenProject( model );
62
63 final Assembly assembly = new Assembly();
64
65
66 final String outputFileNameMapping = "${artifactId}.${packaging}";
67
68 final DependencySet set = new DependencySet();
69 set.setOutputFileNameMapping( outputFileNameMapping );
70
71 assembly.addDependencySet( set );
72
73 final PojoConfigSource configSourceStub = new PojoConfigSource();
74
75
76 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create( ) );
77 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
78
79 configSourceStub.setMavenProject( project);
80 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
81
82 final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
83 assertEquals( 1, outputDependencySets.size() );
84
85 final DependencySet outputSet = outputDependencySets.get( 0 );
86
87 assertEquals( "${artifactId}.${packaging}", outputSet.getOutputFileNameMapping() );
88 }
89
90 public void testDependencySetOutputDirectoryIsNotInterpolated()
91 throws IOException, AssemblyInterpolationException, AssemblyReadException,
92 InvalidAssemblerConfigurationException
93 {
94 final Model model = new Model();
95 model.setArtifactId( "artifact-id" );
96 model.setGroupId( "group.id" );
97 model.setVersion( "1" );
98 model.setPackaging( "jar" );
99
100 final Assembly assembly = new Assembly();
101
102 final String outputDirectory = "${artifactId}.${packaging}";
103
104 final DependencySet set = new DependencySet();
105 set.setOutputDirectory( outputDirectory );
106
107 assembly.addDependencySet( set );
108
109 final PojoConfigSource configSourceStub = new PojoConfigSource();
110
111
112 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
113 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
114
115 final MavenProject project = new MavenProject( model );
116 configSourceStub.setMavenProject( project);
117 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
118
119 final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
120 assertEquals( 1, outputDependencySets.size() );
121
122 final DependencySet outputSet = outputDependencySets.get( 0 );
123
124 assertEquals( "${artifactId}.${packaging}", outputSet.getOutputDirectory() );
125 }
126
127 public Assembly roundTripInterpolation( Assembly assembly, AssemblerConfigurationSource configSource )
128 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
129 {
130 final StringReader stringReader = DefaultAssemblyReaderTest.writeToStringReader( assembly );
131 return new DefaultAssemblyReader().readAssembly( stringReader, "testLocation", null,
132 configSource );
133 }
134
135 public void testShouldResolveModelGroupIdInAssemblyId()
136 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
137 IOException
138 {
139 final Model model = new Model();
140 model.setArtifactId( "artifact-id" );
141 model.setGroupId( "group.id" );
142 model.setVersion( "1" );
143 model.setPackaging( "jar" );
144
145 final Assembly assembly = new Assembly();
146
147 assembly.setId( "assembly.${groupId}" );
148
149 final MavenProject project = new MavenProject( model );
150 final PojoConfigSource configSourceStub = new PojoConfigSource();
151
152 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
153 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
154 configSourceStub.setMavenProject( project);
155 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
156 assertEquals( "assembly.group.id", outputAssembly.getId() );
157 }
158
159 public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
160 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
161 IOException
162 {
163 final Model model = new Model();
164 model.setArtifactId( "artifact-id" );
165 model.setGroupId( "group.id" );
166 model.setVersion( "1" );
167 model.setPackaging( "jar" );
168
169 final Properties props = new Properties();
170 props.setProperty( "groupId", "other.id" );
171
172 model.setProperties( props );
173
174 final PojoConfigSource configSourceStub = new PojoConfigSource();
175
176 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create( ) );
177 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
178
179 final Assembly assembly = new Assembly();
180
181 assembly.setId( "assembly.${groupId}" );
182
183 final MavenProject project = new MavenProject( model );
184 configSourceStub.setMavenProject( project);
185 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
186
187 assertEquals( "assembly.other.id", result.getId() );
188 }
189
190 public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
191 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
192 IOException
193 {
194 final Model model = new Model();
195 model.setArtifactId( "artifact-id" );
196 model.setGroupId( "group.id" );
197 model.setVersion( "1" );
198 model.setPackaging( "jar" );
199
200 final Properties props = new Properties();
201 props.setProperty( "groupId", "other.id" );
202
203 model.setProperties( props );
204
205 final Assembly assembly = new Assembly();
206
207 assembly.setId( "assembly.${groupId}" );
208
209 final EasyMockSupport mm = new EasyMockSupport();
210
211 final MavenSession session = mm.createMock( MavenSession.class );
212
213 final Properties execProps = new Properties();
214 execProps.setProperty( "groupId", "still.another.id" );
215
216 expect( session.getExecutionProperties()).andReturn( execProps ).anyTimes();
217
218 expect( session.getUserProperties()).andReturn( new Properties()).anyTimes();
219
220 final PojoConfigSource cs = new PojoConfigSource();
221
222 final ArtifactRepository lr = mm.createMock( ArtifactRepository.class );
223
224 cs.setLocalRepository( lr );
225 cs.setMavenSession( session );
226 cs.setRootInterpolator( FixedStringSearchInterpolator.create() );
227 cs.setEnvironmentInterpolator( FixedStringSearchInterpolator.create(
228 new PropertiesBasedValueSource(execProps) ));
229 cs.setEnvInterpolator( FixedStringSearchInterpolator.empty() );
230
231 expect( lr.getBasedir() ).andReturn( "/path/to/local/repo").anyTimes();
232
233 mm.replayAll();
234
235 final MavenProject project = new MavenProject( model );
236 cs.setMavenProject( project );
237 final Assembly result = roundTripInterpolation( assembly, cs );
238
239 assertEquals( "assembly.still.another.id", result.getId() );
240
241 mm.verifyAll();
242 mm.resetAll();
243 }
244
245 public void testShouldNotTouchUnresolvedExpression()
246 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
247 IOException
248 {
249 final Model model = new Model();
250 model.setArtifactId( "artifact-id" );
251 model.setGroupId( "group.id" );
252 model.setVersion( "1" );
253 model.setPackaging( "jar" );
254
255 final Assembly assembly = new Assembly();
256
257 assembly.setId( "assembly.${unresolved}" );
258
259 final PojoConfigSource configSourceStub = new PojoConfigSource();
260
261 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create( ) );
262 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
263
264
265 final MavenProject project = new MavenProject( model );
266 configSourceStub.setMavenProject( project);
267 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
268 assertEquals( "assembly.${unresolved}", result.getId() );
269 }
270
271 public void testShouldInterpolateMultiDotProjectExpression()
272 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
273 IOException
274 {
275 final Build build = new Build();
276 build.setFinalName( "final-name" );
277
278 final Model model = new Model();
279 model.setBuild( build );
280
281 final Assembly assembly = new Assembly();
282
283 assembly.setId( "assembly.${project.build.finalName}" );
284
285 final PojoConfigSource configSourceStub = new PojoConfigSource();
286
287 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create( ) );
288 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
289
290 final MavenProject project = new MavenProject( model );
291 configSourceStub.setMavenProject( project);
292 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
293 assertEquals( "assembly.final-name", result.getId() );
294 }
295
296 }