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.shared.release.phase;
20  
21  import java.util.Collections;
22  
23  import com.google.inject.Module;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.model.Scm;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.shared.release.PlexusJUnit4TestCase;
28  import org.apache.maven.shared.release.ReleaseFailureException;
29  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
30  import org.apache.maven.shared.release.config.ReleaseUtils;
31  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
32  import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertTrue;
37  import static org.junit.Assert.fail;
38  
39  /**
40   * Test the POM verification check phase.
41   *
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   */
44  public class CheckPomPhaseTest extends PlexusJUnit4TestCase {
45      private ReleasePhase phase;
46  
47      @Override
48      public void setUp() throws Exception {
49          super.setUp();
50  
51          phase = lookup(ReleasePhase.class, "check-poms");
52      }
53  
54      @Override
55      protected Module[] getCustomModules() {
56          return new Module[0]; // real SCM needed
57      }
58  
59      @Test
60      public void testCorrectlyConfigured() throws Exception {
61          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
62          builder.setScmSourceUrl("scm:svn:file://localhost/tmp/repo");
63  
64          phase.execute(
65                  ReleaseUtils.buildReleaseDescriptor(builder),
66                  new DefaultReleaseEnvironment(),
67                  Collections.singletonList(createProject("1.0-SNAPSHOT")));
68  
69          phase.simulate(
70                  ReleaseUtils.buildReleaseDescriptor(builder),
71                  new DefaultReleaseEnvironment(),
72                  Collections.singletonList(createProject("1.0-SNAPSHOT")));
73  
74          // successful execution is verification enough
75          assertTrue(true);
76      }
77  
78      @Test
79      public void testGetUrlFromProjectConnection() throws Exception {
80          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
81          builder.setScmSourceUrl("scm:svn:file://localhost/tmp/repo");
82  
83          MavenProject project = createProject("1.0-SNAPSHOT");
84  
85          phase.execute(
86                  ReleaseUtils.buildReleaseDescriptor(builder),
87                  new DefaultReleaseEnvironment(),
88                  Collections.singletonList(project));
89  
90          assertEquals(
91                  "Check URL",
92                  "scm:svn:file://localhost/tmp/repo",
93                  ReleaseUtils.buildReleaseDescriptor(builder).getScmSourceUrl());
94      }
95  
96      @Test
97      public void testGetUrlFromProjectConnectionSimulate() throws Exception {
98          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
99          builder.setScmSourceUrl("scm:svn:file://localhost/tmp/repo");
100 
101         MavenProject project = createProject("1.0-SNAPSHOT");
102 
103         phase.simulate(
104                 ReleaseUtils.buildReleaseDescriptor(builder),
105                 new DefaultReleaseEnvironment(),
106                 Collections.singletonList(project));
107 
108         assertEquals(
109                 "Check URL",
110                 "scm:svn:file://localhost/tmp/repo",
111                 ReleaseUtils.buildReleaseDescriptor(builder).getScmSourceUrl());
112     }
113 
114     @Test
115     public void testGetUrlFromProjectDevConnection() throws Exception {
116         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
117         builder.setScmSourceUrl("scm:svn:https://localhost/tmp/repo");
118 
119         MavenProject project = createProject("1.0-SNAPSHOT");
120 
121         phase.execute(
122                 ReleaseUtils.buildReleaseDescriptor(builder),
123                 new DefaultReleaseEnvironment(),
124                 Collections.singletonList(project));
125 
126         assertEquals(
127                 "Check URL",
128                 "scm:svn:https://localhost/tmp/repo",
129                 ReleaseUtils.buildReleaseDescriptor(builder).getScmSourceUrl());
130     }
131 
132     @Test
133     public void testGetUrlFromProjectDevConnectionSimulate() throws Exception {
134         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
135         builder.setScmSourceUrl("scm:svn:https://localhost/tmp/repo");
136 
137         MavenProject project = createProject("1.0-SNAPSHOT");
138 
139         phase.simulate(
140                 ReleaseUtils.buildReleaseDescriptor(builder),
141                 new DefaultReleaseEnvironment(),
142                 Collections.singletonList(project));
143 
144         assertEquals(
145                 "Check URL",
146                 "scm:svn:https://localhost/tmp/repo",
147                 ReleaseUtils.buildReleaseDescriptor(builder).getScmSourceUrl());
148     }
149 
150     @Test
151     public void testGetInvalidUrl() throws Exception {
152         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
153         builder.setScmSourceUrl("scm:svn:");
154 
155         MavenProject project = createProject("1.0-SNAPSHOT");
156 
157         try {
158             phase.execute(
159                     ReleaseUtils.buildReleaseDescriptor(builder),
160                     new DefaultReleaseEnvironment(),
161                     Collections.singletonList(project));
162 
163             fail("Should have thrown an exception");
164         } catch (ReleaseScmRepositoryException e) {
165             assertTrue(true);
166         }
167     }
168 
169     @Test
170     public void testGetInvalidProvider() throws Exception {
171         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
172         MavenProject project = createProject("1.0-SNAPSHOT");
173         Scm scm = new Scm();
174         scm.setConnection("scm:foo:");
175         project.setScm(scm);
176 
177         try {
178             phase.execute(
179                     ReleaseUtils.buildReleaseDescriptor(builder),
180                     new DefaultReleaseEnvironment(),
181                     Collections.singletonList(project));
182 
183             fail("Should have thrown an exception");
184         } catch (ReleaseFailureException e) {
185             assertTrue(true);
186         }
187     }
188 
189     @Test
190     public void testMissingUrl() throws Exception {
191         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
192 
193         try {
194             phase.execute(
195                     ReleaseUtils.buildReleaseDescriptor(builder),
196                     new DefaultReleaseEnvironment(),
197                     Collections.singletonList(createProject("1.0-SNAPSHOT")));
198 
199             fail("Should have failed to execute");
200         } catch (ReleaseFailureException e) {
201             assertTrue(true);
202         }
203 
204         try {
205             phase.simulate(
206                     ReleaseUtils.buildReleaseDescriptor(builder),
207                     new DefaultReleaseEnvironment(),
208                     Collections.singletonList(createProject("1.0-SNAPSHOT")));
209 
210             fail("Should have failed to simulate");
211         } catch (ReleaseFailureException e) {
212             assertTrue(true);
213         }
214     }
215 
216     @Test
217     public void testReleasingNonSnapshot() throws Exception {
218         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
219         builder.setScmSourceUrl("scm:svn:file://localhost/tmp/repo");
220 
221         try {
222             phase.execute(
223                     ReleaseUtils.buildReleaseDescriptor(builder),
224                     new DefaultReleaseEnvironment(),
225                     Collections.singletonList(createProject("1.0")));
226 
227             fail("Should have failed to execute");
228         } catch (ReleaseFailureException e) {
229             assertTrue(true);
230         }
231 
232         try {
233             phase.simulate(
234                     ReleaseUtils.buildReleaseDescriptor(builder),
235                     new DefaultReleaseEnvironment(),
236                     Collections.singletonList(createProject("1.0")));
237 
238             fail("Should have failed to simulate");
239         } catch (ReleaseFailureException e) {
240             assertTrue(true);
241         }
242     }
243 
244     private static MavenProject createProject(String version) {
245         Model model = new Model();
246 
247         model.setArtifactId("artifactId");
248         model.setGroupId("groupId");
249         model.setVersion(version);
250 
251         return new MavenProject(model);
252     }
253 }