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 javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.Set;
25  
26  import org.apache.maven.api.plugin.testing.Basedir;
27  import org.apache.maven.api.plugin.testing.InjectMojo;
28  import org.apache.maven.api.plugin.testing.MojoExtension;
29  import org.apache.maven.api.plugin.testing.MojoTest;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
33  import org.apache.maven.project.MavenProject;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.api.io.TempDir;
37  
38  import static org.junit.jupiter.api.Assertions.assertEquals;
39  
40  @MojoTest
41  @Basedir("/unit/unpack-dependencies-test")
42  class TestIncludeExcludeUnpackDependenciesMojo {
43      @TempDir
44      private File tempDir;
45  
46      @Inject
47      private MavenSession session;
48  
49      @Inject
50      private MavenProject project;
51  
52      private static final String UNPACKED_FILE_PREFIX = "test";
53  
54      private static final String UNPACKED_FILE_SUFFIX = ".txt";
55  
56      @BeforeEach
57      void setUp() throws Exception {
58          DependencyArtifactStubFactory stubFactory = new DependencyArtifactStubFactory(tempDir, true, false);
59          stubFactory.setSrcFile(MojoExtension.getTestFile("test.zip"));
60  
61          project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath());
62  
63          Set<Artifact> artifacts = stubFactory.getScopedArtifacts();
64          project.setArtifacts(artifacts);
65      }
66  
67      private void assertUnpacked(UnpackDependenciesMojo mojo, boolean unpacked, String fileName) {
68          File destFile = new File(mojo.getOutputDirectory().getAbsolutePath(), fileName);
69          assertEquals(unpacked, destFile.exists());
70      }
71  
72      /**
73       * This test will validate that only the 1 and 11 files get unpacked
74       *
75       * @throws Exception in case of errors
76       */
77      @Test
78      @InjectMojo(goal = "unpack-dependencies")
79      void testUnpackIncludesManyFiles(UnpackDependenciesMojo mojo) throws Exception {
80          mojo.setIncludes("**/*1" + UNPACKED_FILE_SUFFIX);
81          mojo.execute();
82          assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
83          assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
84          assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
85          assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
86      }
87  
88      /**
89       * This test will verify only the 2 file gets unpacked
90       *
91       * @throws Exception in case of errors
92       */
93      @Test
94      @InjectMojo(goal = "unpack-dependencies")
95      void testUnpackIncludesSingleFile(UnpackDependenciesMojo mojo) throws Exception {
96          mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
97          mojo.execute();
98          assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
99          assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
100         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
101         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
102     }
103 
104     /**
105      * This test will verify all files get unpacked
106      *
107      * @throws Exception in case of errors
108      */
109     @Test
110     @InjectMojo(goal = "unpack-dependencies")
111     void testUnpackIncludesAllFiles(UnpackDependenciesMojo mojo) throws Exception {
112         mojo.setIncludes("**/*");
113         mojo.execute();
114         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
115         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
116         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
117         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
118     }
119 
120     /**
121      * This test will validate that only the 2 and 3 files get unpacked
122      *
123      * @throws Exception in case of errors
124      */
125     @Test
126     @InjectMojo(goal = "unpack-dependencies")
127     void testUnpackExcludesManyFiles(UnpackDependenciesMojo mojo) throws Exception {
128         mojo.setExcludes("**/*1" + UNPACKED_FILE_SUFFIX);
129         mojo.execute();
130         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
131         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
132         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
133         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
134     }
135 
136     /**
137      * This test will verify only the 1, 11 &amp; 3 files get unpacked
138      *
139      * @throws Exception in case of errors
140      */
141     @Test
142     @InjectMojo(goal = "unpack-dependencies")
143     void testUnpackExcludesSingleFile(UnpackDependenciesMojo mojo) throws Exception {
144         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
145         mojo.execute();
146         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
147         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
148         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
149         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
150     }
151 
152     /**
153      * This test will verify no files get unpacked
154      *
155      * @throws Exception in case of errors
156      */
157     @Test
158     @InjectMojo(goal = "unpack-dependencies")
159     void testUnpackExcludesAllFiles(UnpackDependenciesMojo mojo) throws Exception {
160         mojo.setExcludes("**/*");
161         mojo.execute();
162         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
163         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
164         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
165         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
166     }
167 
168     @Test
169     @InjectMojo(goal = "unpack-dependencies")
170     void testNoIncludeExcludes(UnpackDependenciesMojo mojo) throws Exception {
171         mojo.execute();
172         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
173         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
174         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
175         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
176     }
177 }