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