View Javadoc
1   package org.apache.maven.plugins.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Model;
23  import org.apache.maven.model.Parent;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.io.File;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * @author <a href="jerome@coffeebreaks.org">Jerome Lacoste</a>
36   */
37  public class DeployFileMojoUnitTest
38  {
39      MockDeployFileMojo mojo;
40      Parent parent;
41  
42      @Before
43      public void setUp()
44      {
45          Model pomModel = new Model();
46          pomModel.setPackaging( null );
47  
48          parent = new Parent();
49          parent.setGroupId( "parentGroup" );
50          parent.setArtifactId( "parentArtifact" );
51          parent.setVersion( "parentVersion" );
52  
53          mojo = new MockDeployFileMojo( pomModel );
54      }
55  
56      @After
57      public void tearDown()
58      {
59          mojo = null;
60      }
61  
62      static class MockDeployFileMojo extends DeployFileMojo {
63          private Model model;
64  
65          public MockDeployFileMojo(Model model) {
66              this.model = model;
67          }
68  
69          public void setModel(Model model) {
70              this.model = model;
71          }
72  
73          protected Model readModel(File pomFile) throws MojoExecutionException {
74              return model;
75          }
76      }
77  
78      @Test
79      public void testProcessPomFromPomFileWithParent1() throws MojoExecutionException
80      {
81          mojo.setPomFile( new File( "foo.bar" ) );
82  
83          setMojoModel( mojo.model, null, null, null, null, parent );
84  
85          try {
86              mojo.initProperties();
87          } catch (MojoExecutionException expected) {
88              assertTrue( true ); // missing artifactId and packaging
89          }
90  
91          checkMojoProperties("parentGroup", null, "parentVersion", null);
92      }
93  
94      @Test
95      public void testProcessPomFromPomFileWithParent2() throws MojoExecutionException
96      {
97          mojo.setPomFile( new File( "foo.bar" ) );
98          setMojoModel( mojo.model, null, "artifact", null, null, parent );
99  
100         try {
101             mojo.initProperties();
102         } catch (MojoExecutionException expected) {
103             assertTrue( true ); // missing packaging
104         }
105 
106         checkMojoProperties("parentGroup", "artifact", "parentVersion", null );
107 
108     }
109 
110     @Test
111     public void testProcessPomFromPomFileWithParent3() throws MojoExecutionException
112     {
113         mojo.setPomFile( new File( "foo.bar" ) );
114         setMojoModel( mojo.model, null, "artifact", "version", null, parent );
115 
116         try {
117             mojo.initProperties();
118         } catch (MojoExecutionException expected) {
119             assertTrue( true ); // missing version and packaging
120         }
121 
122         checkMojoProperties( "parentGroup", "artifact", "version", null );
123     }
124 
125     @Test
126     public void testProcessPomFromPomFileWithParent4() throws MojoExecutionException
127     {
128         mojo.setPomFile( new File( "foo.bar" ) );
129         setMojoModel( mojo.model, null, "artifact", "version", "packaging", parent );
130 
131         mojo.initProperties();
132 
133         checkMojoProperties("parentGroup", "artifact", "version", "packaging");
134     }
135 
136     @Test
137     public void testProcessPomFromPomFileWithParent5() throws MojoExecutionException
138     {
139         mojo.setPomFile( new File( "foo.bar" ) );
140         setMojoModel( mojo.model, "group", "artifact", "version", "packaging", parent );
141 
142         mojo.initProperties();
143 
144         checkMojoProperties("group", "artifact", "version", "packaging");
145     }
146 
147     @Test
148     public void testProcessPomFromPomFileWithParent6() throws MojoExecutionException
149     {
150         mojo.setPomFile( new File( "foo.bar" ) );
151         setMojoModel( mojo.model, "group", "artifact", "version", "packaging", null );
152 
153         mojo.initProperties();
154 
155         checkMojoProperties("group", "artifact", "version", "packaging");
156 
157     }
158 
159     @Test
160     public void testProcessPomFromPomFileWithOverrides() throws MojoExecutionException
161     {
162         mojo.setPomFile( new File( "foo.bar" ) );
163         setMojoModel( mojo.model, "group", "artifact", "version", "packaging", null );
164 
165         mojo.setGroupId( "groupO" );
166         mojo.setArtifactId( "artifactO" );
167         mojo.setVersion( "versionO" );
168         mojo.setPackaging( "packagingO" );
169 
170         mojo.initProperties();
171 
172         checkMojoProperties("groupO", "artifactO", "versionO", "packagingO");
173     }
174 
175     private void checkMojoProperties(final String expectedGroup, final String expectedArtifact, final String expectedVersion, final String expectedPackaging) {
176         assertEquals( expectedGroup, mojo.getGroupId() );
177         assertEquals( expectedArtifact, mojo.getArtifactId() );
178         assertEquals( expectedVersion, mojo.getVersion() );
179         assertEquals( expectedPackaging, mojo.getPackaging() );
180     }
181 
182     private void setMojoModel(Model model, String group, String artifact, String version, String packaging, Parent parent ) {
183         model.setGroupId( group );
184         model.setArtifactId( artifact );
185         model.setVersion( version );
186         model.setPackaging( packaging );
187         model.setParent( parent );
188     }
189 
190 }