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