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  
23  import org.apache.maven.model.Model;
24  import org.apache.maven.model.Parent;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * @author <a href="jerome@coffeebreaks.org">Jerome Lacoste</a>
35   */
36  public class DeployFileMojoUnitTest {
37      MockDeployFileMojo mojo;
38      Parent parent;
39  
40      @Before
41      public void setUp() {
42          Model pomModel = new Model();
43          pomModel.setPackaging(null);
44  
45          parent = new Parent();
46          parent.setGroupId("parentGroup");
47          parent.setArtifactId("parentArtifact");
48          parent.setVersion("parentVersion");
49  
50          mojo = new MockDeployFileMojo(pomModel);
51      }
52  
53      @After
54      public void tearDown() {
55          mojo = null;
56      }
57  
58      static class MockDeployFileMojo extends DeployFileMojo {
59          private Model model;
60  
61          public MockDeployFileMojo(Model model) {
62              this.model = model;
63          }
64  
65          public void setModel(Model model) {
66              this.model = model;
67          }
68  
69          protected Model readModel(File pomFile) {
70              return model;
71          }
72      }
73  
74      @Test
75      public void testProcessPomFromPomFileWithParent1() {
76          mojo.setPomFile(new File("foo.bar"));
77  
78          setMojoModel(mojo.model, null, null, null, null, parent);
79  
80          try {
81              mojo.initProperties();
82          } catch (MojoExecutionException expected) {
83              assertTrue(true); // missing artifactId and packaging
84          }
85  
86          checkMojoProperties("parentGroup", null, "parentVersion", null);
87      }
88  
89      @Test
90      public void testProcessPomFromPomFileWithParent2() {
91          mojo.setPomFile(new File("foo.bar"));
92          setMojoModel(mojo.model, null, "artifact", null, null, parent);
93  
94          try {
95              mojo.initProperties();
96          } catch (MojoExecutionException expected) {
97              assertTrue(true); // missing packaging
98          }
99  
100         checkMojoProperties("parentGroup", "artifact", "parentVersion", null);
101     }
102 
103     @Test
104     public void testProcessPomFromPomFileWithParent3() {
105         mojo.setPomFile(new File("foo.bar"));
106         setMojoModel(mojo.model, null, "artifact", "version", null, parent);
107 
108         try {
109             mojo.initProperties();
110         } catch (MojoExecutionException expected) {
111             assertTrue(true); // missing version and packaging
112         }
113 
114         checkMojoProperties("parentGroup", "artifact", "version", null);
115     }
116 
117     @Test
118     public void testProcessPomFromPomFileWithParent4() throws MojoExecutionException {
119         mojo.setPomFile(new File("foo.bar"));
120         setMojoModel(mojo.model, null, "artifact", "version", "packaging", parent);
121 
122         mojo.initProperties();
123 
124         checkMojoProperties("parentGroup", "artifact", "version", "packaging");
125     }
126 
127     @Test
128     public void testProcessPomFromPomFileWithParent5() throws MojoExecutionException {
129         mojo.setPomFile(new File("foo.bar"));
130         setMojoModel(mojo.model, "group", "artifact", "version", "packaging", parent);
131 
132         mojo.initProperties();
133 
134         checkMojoProperties("group", "artifact", "version", "packaging");
135     }
136 
137     @Test
138     public void testProcessPomFromPomFileWithParent6() throws MojoExecutionException {
139         mojo.setPomFile(new File("foo.bar"));
140         setMojoModel(mojo.model, "group", "artifact", "version", "packaging", null);
141 
142         mojo.initProperties();
143 
144         checkMojoProperties("group", "artifact", "version", "packaging");
145     }
146 
147     @Test
148     public void testProcessPomFromPomFileWithOverrides() throws MojoExecutionException {
149         mojo.setPomFile(new File("foo.bar"));
150         setMojoModel(mojo.model, "group", "artifact", "version", "packaging", null);
151 
152         mojo.setGroupId("groupO");
153         mojo.setArtifactId("artifactO");
154         mojo.setVersion("versionO");
155         mojo.setPackaging("packagingO");
156 
157         mojo.initProperties();
158 
159         checkMojoProperties("groupO", "artifactO", "versionO", "packagingO");
160     }
161 
162     private void checkMojoProperties(
163             final String expectedGroup,
164             final String expectedArtifact,
165             final String expectedVersion,
166             final String expectedPackaging) {
167         assertEquals(expectedGroup, mojo.getGroupId());
168         assertEquals(expectedArtifact, mojo.getArtifactId());
169         assertEquals(expectedVersion, mojo.getVersion());
170         assertEquals(expectedPackaging, mojo.getPackaging());
171     }
172 
173     private void setMojoModel(
174             Model model, String group, String artifact, String version, String packaging, Parent parent) {
175         model.setGroupId(group);
176         model.setArtifactId(artifact);
177         model.setVersion(version);
178         model.setPackaging(packaging);
179         model.setParent(parent);
180     }
181 }