1   package org.apache.maven.plugin.dependency;
2   
3   import java.io.File;
4   import java.util.Set;
5   
6   import org.apache.maven.project.MavenProject;
7   
8   public class TestIncludeExcludeUnpackDependenciesMojo 
9   	extends AbstractDependencyMojoTestCase
10  {
11  	private final String PACKED_FILE = "test.zip";
12  	
13  	private final String UNPACKED_FILE_PREFIX = "test";
14  	private final String UNPACKED_FILE_SUFFIX = ".txt";
15  	
16  	private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
17  	
18  	UnpackDependenciesMojo mojo;
19  
20      protected void setUp()
21          throws Exception
22      {
23      	// required for mojo lookups to work
24          super.setUp( "unpack-dependencies", true );
25  
26          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
27          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
28          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
29          // mojo.silent = true;
30  
31          // it needs to get the archivermanager
32          //stubFactory.setUnpackableFile( mojo.getArchiverManager() );
33          // i'm using one file repeatedly to archive so I can test the name
34          // programmatically.
35          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + PACKED_FILE_PATH ) );
36  
37          assertNotNull( mojo );
38          assertNotNull( mojo.getProject() );
39          MavenProject project = mojo.getProject();
40  
41          Set artifacts = this.stubFactory.getScopedArtifacts();
42          Set directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
43          artifacts.addAll( directArtifacts );
44  
45          project.setArtifacts( artifacts );
46          project.setDependencyArtifacts( directArtifacts );
47          mojo.markersDirectory = new File( this.testDir, "markers" );
48  
49      }
50      
51      protected void tearDown()
52      {
53          super.tearDown();
54          
55          mojo = null;
56          System.gc();
57      }
58      
59      private void assertUnpacked(boolean unpacked, String fileName)
60      {
61      	File destFile = new File( mojo.getOutputDirectory().getAbsolutePath() , fileName );    	
62      	assertEquals(unpacked, destFile.exists());
63      }
64      
65      /**
66       * This test will validate that only the 1 and 11 files get unpacked
67       * @throws Exception
68       */
69      public void testUnpackIncludesManyFiles()
70  		throws Exception
71  	{
72      	mojo.setIncludes("**/*1" + UNPACKED_FILE_SUFFIX);
73      	mojo.execute();
74      	assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
75      	assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
76      	assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
77      	assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
78  	}
79      
80      /**
81       * This test will verify only the 2 file gets unpacked
82       * @throws Exception
83       */
84      public void testUnpackIncludesSingleFile()
85      	throws Exception
86  	{
87      	mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
88      	mojo.execute();
89      	assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
90      	assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
91      	assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
92      	assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
93  	}
94      
95      /**
96       * This test will verify all files get unpacked
97       * @throws Exception
98       */
99      public void testUnpackIncludesAllFiles()
100     	throws Exception
101 	{
102     	mojo.setIncludes("**/*");
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(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
107     	assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
108 	}
109     
110     /**
111      * This test will validate that only the 2 and 3 files get unpacked
112      * @throws Exception
113      */
114     public void testUnpackExcludesManyFiles()
115 		throws Exception
116 	{
117     	mojo.setExcludes("**/*1" + 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(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
123 	}
124     
125     /**
126      * This test will verify only the 1, 11 & 3 files get unpacked
127      * @throws Exception
128      */
129     public void testUnpackExcludesSingleFile()
130     	throws Exception
131 	{
132     	mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
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(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
137     	assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
138 	}
139     
140     /**
141      * This test will verify no files get unpacked
142      * @throws Exception
143      */
144     public void testUnpackExcludesAllFiles()
145     	throws Exception
146 	{
147     	mojo.setExcludes("**/*");
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(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
152     	assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
153 	}
154     
155     public void testNoIncludeExcludes()
156     	throws Exception
157 	{
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 }