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