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