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