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