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.deploy;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Objects;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.eclipse.aether.DefaultRepositorySystemSession;
30  import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
31  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
32  import org.eclipse.aether.repository.LocalRepository;
33  import org.mockito.InjectMocks;
34  import org.mockito.Mock;
35  import org.mockito.MockitoAnnotations;
36  
37  import static org.mockito.Mockito.when;
38  
39  /**
40   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
41   */
42  public class DeployFileMojoTest extends AbstractMojoTestCase {
43      private final String LOCAL_REPO = getBasedir() + "/target/local-repo";
44  
45      private List<String> expectedFiles;
46  
47      private List<String> fileList;
48  
49      private File remoteRepo;
50  
51      private AutoCloseable openMocks;
52  
53      @Mock
54      private MavenSession session;
55  
56      @InjectMocks
57      private DeployFileMojo mojo;
58  
59      public void setUp() throws Exception {
60          super.setUp();
61  
62          remoteRepo = new File(getBasedir(), "target/remote-repo");
63  
64          if (!remoteRepo.exists()) {
65              remoteRepo.mkdirs();
66          }
67      }
68  
69      @Override
70      public void tearDown() throws Exception {
71          super.tearDown();
72          if (openMocks != null) {
73              openMocks.close();
74          }
75      }
76  
77      public void testDeployTestEnvironment() throws Exception {
78          File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");
79  
80          AbstractDeployMojo mojo = (AbstractDeployMojo) lookupMojo("deploy-file", testPom);
81  
82          assertNotNull(mojo);
83      }
84  
85      public void testBasicDeployFile() throws Exception {
86          File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");
87  
88          mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
89  
90          openMocks = MockitoAnnotations.openMocks(this);
91  
92          assertNotNull(mojo);
93  
94          DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
95          repositorySession.setLocalRepositoryManager(
96                  new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
97                          .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
98          when(session.getRepositorySession()).thenReturn(repositorySession);
99  
100         String groupId = (String) getVariableValueFromObject(mojo, "groupId");
101 
102         String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
103 
104         String version = (String) getVariableValueFromObject(mojo, "version");
105 
106         String packaging = (String) getVariableValueFromObject(mojo, "packaging");
107 
108         File file = (File) getVariableValueFromObject(mojo, "file");
109 
110         String repositoryId = (String) getVariableValueFromObject(mojo, "repositoryId");
111 
112         String url = (String) getVariableValueFromObject(mojo, "url");
113 
114         String skip = (String) getVariableValueFromObject(mojo, "skip");
115 
116         assertEquals("org.apache.maven.test", groupId);
117 
118         assertEquals("maven-deploy-file-test", artifactId);
119 
120         assertEquals("1.0", version);
121 
122         assertEquals("jar", packaging);
123 
124         assertEquals("snapshots", skip);
125 
126         assertTrue(file.exists());
127 
128         assertEquals("deploy-test", repositoryId);
129 
130         assertEquals("file://" + getBasedir() + "/target/remote-repo/deploy-file-test", url);
131 
132         mojo.execute();
133 
134         // check the generated pom
135         File pom = new File(
136                 remoteRepo,
137                 "deploy-file-test/" + groupId.replace('.', '/') + "/"
138                         + artifactId + "/" + version + "/" + artifactId + "-"
139                         + version + ".pom");
140 
141         assertTrue(pom.exists());
142 
143         Model model = mojo.readModel(pom);
144 
145         assertEquals("4.0.0", model.getModelVersion());
146 
147         assertEquals(groupId, model.getGroupId());
148 
149         assertEquals(artifactId, model.getArtifactId());
150 
151         assertEquals(version, model.getVersion());
152 
153         assertEquals(packaging, model.getPackaging());
154 
155         assertEquals("POM was created from deploy:deploy-file", model.getDescription());
156 
157         // check the remote-repo
158         expectedFiles = new ArrayList<>();
159         fileList = new ArrayList<>();
160 
161         File repo = new File(remoteRepo, "deploy-file-test");
162 
163         File[] files = repo.listFiles();
164 
165         for (File file1 : Objects.requireNonNull(files)) {
166             addFileToList(file1, fileList);
167         }
168 
169         expectedFiles.add("org");
170         expectedFiles.add("apache");
171         expectedFiles.add("maven");
172         expectedFiles.add("test");
173         expectedFiles.add("maven-deploy-file-test");
174         expectedFiles.add("1.0");
175         expectedFiles.add("maven-metadata.xml");
176         expectedFiles.add("maven-metadata.xml.md5");
177         expectedFiles.add("maven-metadata.xml.sha1");
178         expectedFiles.add("maven-deploy-file-test-1.0.jar");
179         expectedFiles.add("maven-deploy-file-test-1.0.jar.md5");
180         expectedFiles.add("maven-deploy-file-test-1.0.jar.sha1");
181         expectedFiles.add("maven-deploy-file-test-1.0.pom");
182         expectedFiles.add("maven-deploy-file-test-1.0.pom.md5");
183         expectedFiles.add("maven-deploy-file-test-1.0.pom.sha1");
184 
185         assertEquals(expectedFiles.size(), fileList.size());
186 
187         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
188     }
189 
190     public void testDeployIfClassifierIsSet() throws Exception {
191         File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-classifier/plugin-config.xml");
192 
193         mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
194 
195         openMocks = MockitoAnnotations.openMocks(this);
196 
197         assertNotNull(mojo);
198 
199         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
200         repositorySession.setLocalRepositoryManager(
201                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
202                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
203         when(session.getRepositorySession()).thenReturn(repositorySession);
204 
205         String classifier = (String) getVariableValueFromObject(mojo, "classifier");
206 
207         String groupId = (String) getVariableValueFromObject(mojo, "groupId");
208 
209         String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
210 
211         String version = (String) getVariableValueFromObject(mojo, "version");
212 
213         assertEquals("bin", classifier);
214 
215         mojo.execute();
216 
217         File deployedArtifact = new File(
218                 remoteRepo,
219                 "deploy-file-classifier/" + groupId.replace('.', '/') + "/"
220                         + artifactId + "/" + version + "/" + artifactId + "-"
221                         + version + "-" + classifier + ".jar");
222 
223         assertTrue(deployedArtifact.exists());
224 
225         mojo.setClassifier("prod");
226 
227         assertEquals("prod", mojo.getClassifier());
228 
229         mojo.execute();
230 
231         File prodDeployedArtifact = new File(
232                 remoteRepo,
233                 "deploy-file-classifier/" + groupId.replace('.', '/') + "/"
234                         + artifactId + "/" + version + "/" + artifactId + "-"
235                         + version + "-" + mojo.getClassifier() + ".jar");
236 
237         assertTrue(prodDeployedArtifact.exists());
238     }
239 
240     public void testDeployIfArtifactIsNotJar() throws Exception {
241         File testPom =
242                 new File(getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml");
243 
244         mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
245 
246         openMocks = MockitoAnnotations.openMocks(this);
247 
248         assertNotNull(mojo);
249 
250         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
251         repositorySession.setLocalRepositoryManager(
252                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
253                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
254         when(session.getRepositorySession()).thenReturn(repositorySession);
255 
256         String groupId = (String) getVariableValueFromObject(mojo, "groupId");
257 
258         String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
259 
260         String version = (String) getVariableValueFromObject(mojo, "version");
261 
262         assertEquals("org.apache.maven.test", groupId);
263 
264         assertEquals("maven-deploy-file-test", artifactId);
265 
266         assertEquals("1.0", version);
267 
268         mojo.execute();
269 
270         File file = new File(
271                 remoteRepo,
272                 "deploy-file-artifact-not-jar/" + groupId.replace('.', '/') + "/"
273                         + artifactId + "/" + version + "/" + artifactId + "-"
274                         + version + ".zip");
275 
276         assertTrue(file.exists());
277     }
278 
279     public void testDeployFileIfPackagingIsSet() throws Exception {
280         File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml");
281         mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
282 
283         openMocks = MockitoAnnotations.openMocks(this);
284 
285         assertNotNull(mojo);
286 
287         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
288         repositorySession.setLocalRepositoryManager(
289                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
290                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
291         when(session.getRepositorySession()).thenReturn(repositorySession);
292 
293         String packaging = (String) getVariableValueFromObject(mojo, "packaging");
294 
295         String groupId = (String) getVariableValueFromObject(mojo, "groupId");
296 
297         String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
298 
299         String version = (String) getVariableValueFromObject(mojo, "version");
300 
301         assertEquals("differentpackaging", packaging);
302 
303         mojo.execute();
304 
305         File deployedArtifact = new File(
306                 remoteRepo,
307                 "deploy-file-packaging/" + groupId.replace('.', '/') + "/"
308                         + artifactId + "/" + version + "/" + artifactId + "-"
309                         + version + "." + packaging);
310 
311         assertTrue(deployedArtifact.exists());
312     }
313 
314     private void addFileToList(File file, List<String> fileList) {
315         if (!file.isDirectory()) {
316             fileList.add(file.getName());
317         } else {
318             fileList.add(file.getName());
319 
320             File[] files = file.listFiles();
321 
322             for (File file1 : Objects.requireNonNull(files)) {
323                 addFileToList(file1, fileList);
324             }
325         }
326     }
327 
328     private int getSizeOfExpectedFiles(List<String> fileList, List<String> expectedFiles) {
329         for (String fileName : fileList) {
330             if (expectedFiles.contains(fileName)) {
331                 expectedFiles.remove(fileName);
332             } else {
333                 fail(fileName + " is not included in the expected files");
334             }
335         }
336         return expectedFiles.size();
337     }
338 }