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