1 package org.apache.maven.plugins.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.plugins.assembly.AssemblerConfigurationSource;
28 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
29 import org.apache.maven.plugins.assembly.io.AssemblyReadException;
30 import org.apache.maven.plugins.assembly.io.DefaultAssemblyReader;
31 import org.apache.maven.plugins.assembly.io.DefaultAssemblyReaderTest;
32 import org.apache.maven.plugins.assembly.model.Assembly;
33 import org.apache.maven.plugins.assembly.model.DependencySet;
34 import org.apache.maven.plugins.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 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
76 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
77
78 configSourceStub.setMavenProject( project );
79 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
80
81 final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
82 assertEquals( 1, outputDependencySets.size() );
83
84 final DependencySet outputSet = outputDependencySets.get( 0 );
85
86 assertEquals( "${artifactId}.${packaging}", outputSet.getOutputFileNameMapping() );
87 }
88
89 public void testDependencySetOutputDirectoryIsNotInterpolated()
90 throws IOException, AssemblyInterpolationException, AssemblyReadException,
91 InvalidAssemblerConfigurationException
92 {
93 final Model model = new Model();
94 model.setArtifactId( "artifact-id" );
95 model.setGroupId( "group.id" );
96 model.setVersion( "1" );
97 model.setPackaging( "jar" );
98
99 final Assembly assembly = new Assembly();
100
101 final String outputDirectory = "${artifactId}.${packaging}";
102
103 final DependencySet set = new DependencySet();
104 set.setOutputDirectory( outputDirectory );
105
106 assembly.addDependencySet( set );
107
108 final PojoConfigSource configSourceStub = new PojoConfigSource();
109
110 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
111 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
112
113 final MavenProject project = new MavenProject( model );
114 configSourceStub.setMavenProject( project );
115 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
116
117 final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
118 assertEquals( 1, outputDependencySets.size() );
119
120 final DependencySet outputSet = outputDependencySets.get( 0 );
121
122 assertEquals( "${artifactId}.${packaging}", outputSet.getOutputDirectory() );
123 }
124
125 public Assembly roundTripInterpolation( Assembly assembly, AssemblerConfigurationSource configSource )
126 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
127 {
128 final StringReader stringReader = DefaultAssemblyReaderTest.writeToStringReader( assembly );
129 return new DefaultAssemblyReader().readAssembly( stringReader, "testLocation", null, configSource );
130 }
131
132 public void testShouldResolveModelGroupIdInAssemblyId()
133 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
134 IOException
135 {
136 final Model model = new Model();
137 model.setArtifactId( "artifact-id" );
138 model.setGroupId( "group.id" );
139 model.setVersion( "1" );
140 model.setPackaging( "jar" );
141
142 final Assembly assembly = new Assembly();
143
144 assembly.setId( "assembly.${groupId}" );
145
146 final MavenProject project = new MavenProject( model );
147 final PojoConfigSource configSourceStub = new PojoConfigSource();
148
149 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
150 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
151 configSourceStub.setMavenProject( project );
152 final Assembly outputAssembly = roundTripInterpolation( assembly, configSourceStub );
153 assertEquals( "assembly.group.id", outputAssembly.getId() );
154 }
155
156 public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
157 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
158 IOException
159 {
160 final Model model = new Model();
161 model.setArtifactId( "artifact-id" );
162 model.setGroupId( "group.id" );
163 model.setVersion( "1" );
164 model.setPackaging( "jar" );
165
166 final Properties props = new Properties();
167 props.setProperty( "groupId", "other.id" );
168
169 model.setProperties( props );
170
171 final PojoConfigSource configSourceStub = new PojoConfigSource();
172
173 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
174 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
175
176 final Assembly assembly = new Assembly();
177
178 assembly.setId( "assembly.${groupId}" );
179
180 final MavenProject project = new MavenProject( model );
181 configSourceStub.setMavenProject( project );
182 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
183
184 assertEquals( "assembly.other.id", result.getId() );
185 }
186
187 public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
188 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
189 IOException
190 {
191 final Model model = new Model();
192 model.setArtifactId( "artifact-id" );
193 model.setGroupId( "group.id" );
194 model.setVersion( "1" );
195 model.setPackaging( "jar" );
196
197 final Properties props = new Properties();
198 props.setProperty( "groupId", "other.id" );
199
200 model.setProperties( props );
201
202 final Assembly assembly = new Assembly();
203
204 assembly.setId( "assembly.${groupId}" );
205
206 final EasyMockSupport mm = new EasyMockSupport();
207
208 final MavenSession session = mm.createMock( MavenSession.class );
209
210 final Properties execProps = new Properties();
211 execProps.setProperty( "groupId", "still.another.id" );
212
213 expect( session.getExecutionProperties() ).andReturn( execProps ).anyTimes();
214
215 expect( session.getUserProperties() ).andReturn( new Properties() ).anyTimes();
216
217 final PojoConfigSource cs = new PojoConfigSource();
218
219 final ArtifactRepository lr = mm.createMock( ArtifactRepository.class );
220
221 cs.setLocalRepository( lr );
222 cs.setMavenSession( session );
223 cs.setRootInterpolator( FixedStringSearchInterpolator.create() );
224 cs.setEnvironmentInterpolator(
225 FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( execProps ) ) );
226 cs.setEnvInterpolator( FixedStringSearchInterpolator.empty() );
227
228 expect( lr.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes();
229
230 mm.replayAll();
231
232 final MavenProject project = new MavenProject( model );
233 cs.setMavenProject( project );
234 final Assembly result = roundTripInterpolation( assembly, cs );
235
236 assertEquals( "assembly.still.another.id", result.getId() );
237
238 mm.verifyAll();
239 mm.resetAll();
240 }
241
242 public void testShouldNotTouchUnresolvedExpression()
243 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
244 IOException
245 {
246 final Model model = new Model();
247 model.setArtifactId( "artifact-id" );
248 model.setGroupId( "group.id" );
249 model.setVersion( "1" );
250 model.setPackaging( "jar" );
251
252 final Assembly assembly = new Assembly();
253
254 assembly.setId( "assembly.${unresolved}" );
255
256 final PojoConfigSource configSourceStub = new PojoConfigSource();
257
258 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
259 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
260
261 final MavenProject project = new MavenProject( model );
262 configSourceStub.setMavenProject( project );
263 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
264 assertEquals( "assembly.${unresolved}", result.getId() );
265 }
266
267 public void testShouldInterpolateMultiDotProjectExpression()
268 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
269 IOException
270 {
271 final Build build = new Build();
272 build.setFinalName( "final-name" );
273
274 final Model model = new Model();
275 model.setBuild( build );
276
277 final Assembly assembly = new Assembly();
278
279 assembly.setId( "assembly.${project.build.finalName}" );
280
281 final PojoConfigSource configSourceStub = new PojoConfigSource();
282
283 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
284 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
285
286 final MavenProject project = new MavenProject( model );
287 configSourceStub.setMavenProject( project );
288 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
289 assertEquals( "assembly.final-name", result.getId() );
290 }
291
292 }