View Javadoc

1   package org.apache.maven.plugin.dependency;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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      	// required for mojo lookups to work
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          // mojo.silent = true;
58  
59          // it needs to get the archivermanager
60          //stubFactory.setUnpackableFile( mojo.getArchiverManager() );
61          // i'm using one file repeatedly to archive so I can test the name
62          // programmatically.
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      * This test will validate that only the 1 and 11 files get unpacked
115      * @throws Exception
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      * This test will verify only the 2 file gets unpacked
130      * @throws Exception
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      * This test will verify all files get unpacked
145      * @throws Exception
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      * This test will validate that only the 2 and 3 files get unpacked
160      * @throws Exception
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      * This test will verify only the 1, 11 & 3 files get unpacked
175      * @throws Exception
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      * This test will verify no files get unpacked
190      * @throws Exception
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     	// Now run again and make sure the extracted files haven't gotten overwritten
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 }