View Javadoc
1   package org.apache.maven.plugins.dependency.fromDependencies;
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.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.plugins.dependency.fromDependencies.UnpackDependenciesMojo;
28  import org.apache.maven.project.MavenProject;
29  
30  public class TestIncludeExcludeUnpackDependenciesMojo
31      extends AbstractDependencyMojoTestCase
32  {
33      private final String PACKED_FILE = "test.zip";
34  
35      private final String UNPACKED_FILE_PREFIX = "test";
36  
37      private final String UNPACKED_FILE_SUFFIX = ".txt";
38  
39      private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
40  
41      UnpackDependenciesMojo mojo;
42  
43      protected void setUp()
44          throws Exception
45      {
46          // required for mojo lookups to work
47          super.setUp( "unpack-dependencies", true );
48  
49          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
50          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
51          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
52          // mojo.silent = true;
53  
54          // it needs to get the archivermanager
55          // stubFactory.setUnpackableFile( mojo.getArchiverManager() );
56          // i'm using one file repeatedly to archive so I can test the name
57          // programmatically.
58          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + PACKED_FILE_PATH ) );
59  
60          assertNotNull( mojo );
61          assertNotNull( mojo.getProject() );
62          MavenProject project = mojo.getProject();
63  
64          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
65          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
66          artifacts.addAll( directArtifacts );
67  
68          project.setArtifacts( artifacts );
69          project.setDependencyArtifacts( directArtifacts );
70          mojo.markersDirectory = new File( this.testDir, "markers" );
71  
72      }
73  
74      protected void tearDown()
75      {
76          super.tearDown();
77  
78          mojo = null;
79          System.gc();
80      }
81  
82      private void assertUnpacked( boolean unpacked, String fileName )
83      {
84          File destFile = new File( mojo.getOutputDirectory().getAbsolutePath(), fileName );
85          assertEquals( unpacked, destFile.exists() );
86      }
87  
88      /**
89       * This test will validate that only the 1 and 11 files get unpacked
90       * 
91       * @throws Exception in case of errors
92       */
93      public void testUnpackIncludesManyFiles()
94          throws Exception
95      {
96          mojo.setIncludes( "**/*1" + UNPACKED_FILE_SUFFIX );
97          mojo.execute();
98          assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
99          assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
100         assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
101         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
102     }
103 
104     /**
105      * This test will verify only the 2 file gets unpacked
106      * 
107      * @throws Exception in case of errors
108      */
109     public void testUnpackIncludesSingleFile()
110         throws Exception
111     {
112         mojo.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
113         mojo.execute();
114         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
115         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
116         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
117         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
118     }
119 
120     /**
121      * This test will verify all files get unpacked
122      * 
123      * @throws Exception in case of errors
124      */
125     public void testUnpackIncludesAllFiles()
126         throws Exception
127     {
128         mojo.setIncludes( "**/*" );
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( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
133         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
134     }
135 
136     /**
137      * This test will validate that only the 2 and 3 files get unpacked
138      * 
139      * @throws Exception in case of errors
140      */
141     public void testUnpackExcludesManyFiles()
142         throws Exception
143     {
144         mojo.setExcludes( "**/*1" + 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( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
150     }
151 
152     /**
153      * This test will verify only the 1, 11 &amp; 3 files get unpacked
154      * 
155      * @throws Exception in case of errors
156      */
157     public void testUnpackExcludesSingleFile()
158         throws Exception
159     {
160         mojo.setExcludes( "**/test2" + UNPACKED_FILE_SUFFIX );
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( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
165         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
166     }
167 
168     /**
169      * This test will verify no files get unpacked
170      * 
171      * @throws Exception in case of errors
172      */
173     public void testUnpackExcludesAllFiles()
174         throws Exception
175     {
176         mojo.setExcludes( "**/*" );
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( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
181         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
182     }
183 
184     public void testNoIncludeExcludes()
185         throws Exception
186     {
187         mojo.execute();
188         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
189         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
190         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
191         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
192     }
193 }