View Javadoc
1   package org.apache.maven.plugins.dependency.fromConfiguration;
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.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          // required for mojo lookups to work
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          // 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<>( 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      * This test will validate that only the 1 and 11 files get unpacked
121      *
122      * @throws Exception in case of errors.
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      * This test will verify only the 2 file gets unpacked
137      *
138      * @throws Exception in case of errors.
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      * This test will verify all files get unpacked
153      *
154      * @throws Exception in case of errors.
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      * This test will validate that only the 2 and 3 files get unpacked
169      *
170      * @throws Exception in case of errors.
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      * This test will verify only the 1, 11 &amp; 3 files get unpacked
185      *
186      * @throws Exception in case of errors.
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      * This test will verify no files get unpacked
201      *
202      * @throws Exception in case of errors.
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         // Now run again and make sure the extracted files haven't gotten overwritten
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 }