View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.fromDependencies;
20  
21  import java.io.File;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
26  import org.apache.maven.project.MavenProject;
27  
28  public class TestIncludeExcludeUnpackDependenciesMojo extends AbstractDependencyMojoTestCase {
29      private final String PACKED_FILE = "test.zip";
30  
31      private final String UNPACKED_FILE_PREFIX = "test";
32  
33      private final String UNPACKED_FILE_SUFFIX = ".txt";
34  
35      private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
36  
37      private UnpackDependenciesMojo mojo;
38  
39      protected void setUp() throws Exception {
40          // required for mojo lookups to work
41          super.setUp("unpack-dependencies", true);
42  
43          File testPom = new File(getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml");
44          mojo = (UnpackDependenciesMojo) lookupMojo("unpack-dependencies", testPom);
45          mojo.outputDirectory = new File(this.testDir, "outputDirectory");
46          // mojo.silent = true;
47  
48          // it needs to get the archivermanager
49          // stubFactory.setUnpackableFile( mojo.getArchiverManager() );
50          // i'm using one file repeatedly to archive so I can test the name
51          // programmatically.
52          stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + PACKED_FILE_PATH));
53  
54          assertNotNull(mojo);
55          assertNotNull(mojo.getProject());
56          MavenProject project = mojo.getProject();
57  
58          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
59          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
60          artifacts.addAll(directArtifacts);
61  
62          project.setArtifacts(artifacts);
63          project.setDependencyArtifacts(directArtifacts);
64          mojo.markersDirectory = new File(this.testDir, "markers");
65      }
66  
67      protected void tearDown() {
68          super.tearDown();
69  
70          mojo = null;
71          System.gc();
72      }
73  
74      private void assertUnpacked(boolean unpacked, String fileName) {
75          File destFile = new File(mojo.getOutputDirectory().getAbsolutePath(), fileName);
76          assertEquals(unpacked, destFile.exists());
77      }
78  
79      /**
80       * This test will validate that only the 1 and 11 files get unpacked
81       *
82       * @throws Exception in case of errors
83       */
84      public void testUnpackIncludesManyFiles() throws Exception {
85          mojo.setIncludes("**/*1" + UNPACKED_FILE_SUFFIX);
86          mojo.execute();
87          assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
88          assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
89          assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
90          assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
91      }
92  
93      /**
94       * This test will verify only the 2 file gets unpacked
95       *
96       * @throws Exception in case of errors
97       */
98      public void testUnpackIncludesSingleFile() throws Exception {
99          mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
100         mojo.execute();
101         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
102         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
103         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
104         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
105     }
106 
107     /**
108      * This test will verify all files get unpacked
109      *
110      * @throws Exception in case of errors
111      */
112     public void testUnpackIncludesAllFiles() throws Exception {
113         mojo.setIncludes("**/*");
114         mojo.execute();
115         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
116         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
117         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
118         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
119     }
120 
121     /**
122      * This test will validate that only the 2 and 3 files get unpacked
123      *
124      * @throws Exception in case of errors
125      */
126     public void testUnpackExcludesManyFiles() throws Exception {
127         mojo.setExcludes("**/*1" + UNPACKED_FILE_SUFFIX);
128         mojo.execute();
129         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
130         assertUnpacked(false, 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 verify only the 1, 11 &amp; 3 files get unpacked
137      *
138      * @throws Exception in case of errors
139      */
140     public void testUnpackExcludesSingleFile() throws Exception {
141         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
142         mojo.execute();
143         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
144         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
145         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
146         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
147     }
148 
149     /**
150      * This test will verify no files get unpacked
151      *
152      * @throws Exception in case of errors
153      */
154     public void testUnpackExcludesAllFiles() throws Exception {
155         mojo.setExcludes("**/*");
156         mojo.execute();
157         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
158         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
159         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
160         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
161     }
162 
163     public void testNoIncludeExcludes() throws Exception {
164         mojo.execute();
165         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
166         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
167         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
168         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
169     }
170 }