1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
25 import org.apache.maven.plugin.dependency.fromConfiguration.UnpackMojo;
26 import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
27 import org.apache.maven.plugin.dependency.utils.markers.UnpackFileMarkerHandler;
28 import org.apache.maven.plugin.testing.stubs.StubArtifactCollector;
29 import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
30 import org.codehaus.plexus.archiver.manager.ArchiverManager;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.List;
36
37 public class TestIncludeExcludeUnpackMojo
38 extends AbstractDependencyMojoTestCase
39 {
40 private final String PACKED_FILE = "test.zip";
41
42 private final String UNPACKED_FILE_PREFIX = "test";
43
44 private final String UNPACKED_FILE_SUFFIX = ".txt";
45
46 private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
47
48 UnpackMojo mojo;
49
50 protected void setUp()
51 throws Exception
52 {
53
54 super.setUp( "unpack", true );
55
56 File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml" );
57 mojo = (UnpackMojo) lookupMojo( "unpack", testPom );
58 mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) );
59
60
61
62
63
64
65 stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + PACKED_FILE_PATH ) );
66 Artifact artifact = stubFactory.createArtifact( "test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null );
67 ArtifactItem item = stubFactory.getArtifactItem( artifact );
68 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
69 list.add( item );
70 assertNotNull( mojo );
71 assertNotNull( mojo.getProject() );
72
73 mojo.setArchiverManager( (ArchiverManager) lookup( ArchiverManager.ROLE ) );
74
75 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
76 mojo.setResolver( new StubArtifactResolver( stubFactory, false, false ) );
77 mojo.setMarkersDirectory( new File( this.testDir, "markers" ) );
78 mojo.setArtifactCollector( new StubArtifactCollector() );
79 mojo.setArtifactItems( list );
80 }
81
82 protected void tearDown()
83 {
84 super.tearDown();
85
86 mojo = null;
87 System.gc();
88 }
89
90 public void assertMarkerFiles( Collection<ArtifactItem> items, boolean exist )
91 {
92 for ( ArtifactItem item : items )
93 {
94 assertMarkerFile( exist, item );
95 }
96 }
97
98 public void assertMarkerFile( boolean val, ArtifactItem item )
99 {
100 UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler( item, mojo.getMarkersDirectory() );
101 try
102 {
103 assertEquals( val, handle.isMarkerSet() );
104 }
105 catch ( MojoExecutionException e )
106 {
107 fail( e.getLongMessage() );
108 }
109 }
110
111 private void assertUnpacked( boolean unpacked, String fileName )
112 {
113 File destFile = new File( mojo.getOutputDirectory().getAbsolutePath(), fileName );
114 assertEquals( unpacked, destFile.exists() );
115 }
116
117
118
119
120
121
122 public void testUnpackIncludesManyFiles()
123 throws Exception
124 {
125 mojo.setIncludes( "**/*1" + UNPACKED_FILE_SUFFIX );
126 mojo.execute();
127 assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
128 assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
129 assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
130 assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
131 }
132
133
134
135
136
137
138 public void testUnpackIncludesSingleFile()
139 throws Exception
140 {
141 mojo.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
142 mojo.execute();
143 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
144 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
145 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
146 assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
147 }
148
149
150
151
152
153
154 public void testUnpackIncludesAllFiles()
155 throws Exception
156 {
157 mojo.setIncludes( "**/*" );
158 mojo.execute();
159 assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
160 assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
161 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
162 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
163 }
164
165
166
167
168
169
170 public void testUnpackExcludesManyFiles()
171 throws Exception
172 {
173 mojo.setExcludes( "**/*1" + UNPACKED_FILE_SUFFIX );
174 mojo.execute();
175 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
176 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
177 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
178 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
179 }
180
181
182
183
184
185
186 public void testUnpackExcludesSingleFile()
187 throws Exception
188 {
189 mojo.setExcludes( "**/test2" + UNPACKED_FILE_SUFFIX );
190 mojo.execute();
191 assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
192 assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
193 assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
194 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
195 }
196
197
198
199
200
201
202 public void testUnpackExcludesAllFiles()
203 throws Exception
204 {
205 mojo.setExcludes( "**/*" );
206 mojo.execute();
207 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
208 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
209 assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
210 assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
211 }
212
213 public void testNoIncludeExcludes()
214 throws Exception
215 {
216 mojo.execute();
217 assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
218 assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
219 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
220 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
221 }
222
223 public void testIncludeArtifactItemOverride()
224 throws Exception
225 {
226 Artifact artifact = stubFactory.createArtifact( "test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null );
227 ArtifactItem item = stubFactory.getArtifactItem( artifact );
228 item.setIncludes( "**/*" );
229 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
230 list.add( item );
231 mojo.setArtifactItems( list );
232 mojo.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
233 mojo.execute();
234 assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
235 assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
236 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
237 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
238 }
239
240 public void testExcludeArtifactItemOverride()
241 throws Exception
242 {
243 Artifact artifact = stubFactory.createArtifact( "test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null );
244 ArtifactItem item = stubFactory.getArtifactItem( artifact );
245 item.setExcludes( "**/*" );
246 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
247 list.add( item );
248 mojo.setArtifactItems( list );
249 mojo.setExcludes( "**/test2" + UNPACKED_FILE_SUFFIX );
250 mojo.execute();
251 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
252 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
253 assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
254 assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
255 }
256
257 public void testIncludeArtifactItemMultipleMarker()
258 throws Exception
259 {
260 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
261 Artifact artifact = stubFactory.createArtifact( "test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null );
262 ArtifactItem item = stubFactory.getArtifactItem( artifact );
263 item.setOverWrite( "false" );
264 item.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
265 list.add( item );
266 item = stubFactory.getArtifactItem( artifact );
267 item.setOverWrite( "false" );
268 item.setIncludes( "**/test3" + UNPACKED_FILE_SUFFIX );
269 list.add( item );
270 mojo.setArtifactItems( list );
271 mojo.execute();
272 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
273 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
274 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
275 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
276 assertMarkerFiles( mojo.getArtifactItems(), true );
277 }
278
279 public void testIncludeArtifactItemMultipleExecutions()
280 throws Exception
281 {
282 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
283 Artifact artifact = stubFactory.createArtifact( "test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null );
284 ArtifactItem item = stubFactory.getArtifactItem( artifact );
285 item.setOverWrite( "false" );
286 item.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
287 list.add( item );
288 item = stubFactory.getArtifactItem( artifact );
289 item.setOverWrite( "false" );
290 item.setIncludes( "**/test3" + UNPACKED_FILE_SUFFIX );
291 list.add( item );
292 mojo.setArtifactItems( list );
293 mojo.execute();
294 assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
295 assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
296 assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
297 assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
298 assertMarkerFiles( mojo.getArtifactItems(), true );
299
300
301 File destFile2 =
302 new File( mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
303 File destFile3 =
304 new File( mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
305 long time = System.currentTimeMillis();
306 time = time - ( time % 1000 );
307 destFile2.setLastModified( time );
308 destFile3.setLastModified( time );
309 assertEquals( time, destFile2.lastModified() );
310 assertEquals( time, destFile3.lastModified() );
311 Thread.sleep( 100 );
312 mojo.execute();
313 assertEquals( time, destFile2.lastModified() );
314 assertEquals( time, destFile3.lastModified() );
315 }
316 }