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 static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.util.List;
29 import java.util.Properties;
30
31 import org.apache.maven.model.Build;
32 import org.apache.maven.model.Model;
33 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
34 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
35 import org.apache.maven.plugins.assembly.io.AssemblyReadException;
36 import org.apache.maven.plugins.assembly.io.DefaultAssemblyReader;
37 import org.apache.maven.plugins.assembly.io.DefaultAssemblyReaderTest;
38 import org.apache.maven.plugins.assembly.model.Assembly;
39 import org.apache.maven.plugins.assembly.model.DependencySet;
40 import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
41 import org.apache.maven.project.MavenProject;
42 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
43 import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.junit.MockitoJUnitRunner;
47
48 @RunWith( MockitoJUnitRunner.class )
49 public class AssemblyInterpolatorTest
50 {
51 @Test
52 public void testDependencySetOutputFileNameMappingsAreNotInterpolated()
53 throws IOException, AssemblyInterpolationException, AssemblyReadException,
54 InvalidAssemblerConfigurationException
55 {
56 final Model model = new Model();
57 model.setArtifactId( "artifact-id" );
58 model.setGroupId( "group.id" );
59 model.setVersion( "1" );
60 model.setPackaging( "jar" );
61
62 final MavenProject project = new MavenProject( model );
63
64 final Assembly assembly = new Assembly();
65
66
67 final String outputFileNameMapping = "${artifactId}.${packaging}";
68
69 final DependencySet set = new DependencySet();
70 set.setOutputFileNameMapping( outputFileNameMapping );
71
72 assembly.addDependencySet( set );
73
74 final PojoConfigSource configSourceStub = new PojoConfigSource();
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 @Test
91 public void testDependencySetOutputDirectoryIsNotInterpolated()
92 throws IOException, AssemblyInterpolationException, AssemblyReadException,
93 InvalidAssemblerConfigurationException
94 {
95 final Model model = new Model();
96 model.setArtifactId( "artifact-id" );
97 model.setGroupId( "group.id" );
98 model.setVersion( "1" );
99 model.setPackaging( "jar" );
100
101 final Assembly assembly = new Assembly();
102
103 final String outputDirectory = "${artifactId}.${packaging}";
104
105 final DependencySet set = new DependencySet();
106 set.setOutputDirectory( outputDirectory );
107
108 assembly.addDependencySet( set );
109
110 final PojoConfigSource configSourceStub = new PojoConfigSource();
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 private 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, configSource );
132 }
133
134 @Test
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 @Test
160 public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
161 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
162 IOException
163 {
164 final Model model = new Model();
165 model.setArtifactId( "artifact-id" );
166 model.setGroupId( "group.id" );
167 model.setVersion( "1" );
168 model.setPackaging( "jar" );
169
170 final Properties props = new Properties();
171 props.setProperty( "groupId", "other.id" );
172
173 model.setProperties( props );
174
175 final PojoConfigSource configSourceStub = new PojoConfigSource();
176
177 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
178 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
179
180 final Assembly assembly = new Assembly();
181
182 assembly.setId( "assembly.${groupId}" );
183
184 final MavenProject project = new MavenProject( model );
185 configSourceStub.setMavenProject( project );
186 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
187
188 assertEquals( "assembly.other.id", result.getId() );
189 }
190
191 @Test
192 public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
193 throws Exception
194 {
195 final Model model = new Model();
196 model.setArtifactId( "artifact-id" );
197 model.setGroupId( "group.id" );
198 model.setVersion( "1" );
199 model.setPackaging( "jar" );
200
201 final Properties props = new Properties();
202 props.setProperty( "groupId", "other.id" );
203
204 model.setProperties( props );
205
206 final Assembly assembly = new Assembly();
207
208 assembly.setId( "assembly.${groupId}" );
209
210 final Properties execProps = new Properties();
211 execProps.setProperty( "groupId", "still.another.id" );
212
213 final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class );
214 when( cs.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
215 when( cs.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( execProps ) ) );
216 when( cs.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() );
217
218 final MavenProject project = new MavenProject( model );
219 when( cs.getProject() ) .thenReturn( project );
220
221 final Assembly result = roundTripInterpolation( assembly, cs );
222
223 assertEquals( "assembly.still.another.id", result.getId() );
224 }
225
226 @Test
227 public void testShouldNotTouchUnresolvedExpression()
228 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
229 IOException
230 {
231 final Model model = new Model();
232 model.setArtifactId( "artifact-id" );
233 model.setGroupId( "group.id" );
234 model.setVersion( "1" );
235 model.setPackaging( "jar" );
236
237 final Assembly assembly = new Assembly();
238
239 assembly.setId( "assembly.${unresolved}" );
240
241 final PojoConfigSource configSourceStub = new PojoConfigSource();
242
243 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
244 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
245
246 final MavenProject project = new MavenProject( model );
247 configSourceStub.setMavenProject( project );
248 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
249 assertEquals( "assembly.${unresolved}", result.getId() );
250 }
251
252 @Test
253 public void testShouldInterpolateMultiDotProjectExpression()
254 throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
255 IOException
256 {
257 final Build build = new Build();
258 build.setFinalName( "final-name" );
259
260 final Model model = new Model();
261 model.setBuild( build );
262
263 final Assembly assembly = new Assembly();
264
265 assembly.setId( "assembly.${project.build.finalName}" );
266
267 final PojoConfigSource configSourceStub = new PojoConfigSource();
268
269 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
270 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
271
272 final MavenProject project = new MavenProject( model );
273 configSourceStub.setMavenProject( project );
274 final Assembly result = roundTripInterpolation( assembly, configSourceStub );
275 assertEquals( "assembly.final-name", result.getId() );
276 }
277
278 }