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