1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.release.phase;
20
21 import javax.inject.Singleton;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import com.google.inject.AbstractModule;
31 import com.google.inject.Module;
32 import com.google.inject.name.Names;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.scm.ScmFile;
35 import org.apache.maven.scm.ScmFileSet;
36 import org.apache.maven.scm.ScmFileStatus;
37 import org.apache.maven.scm.command.add.AddScmResult;
38 import org.apache.maven.scm.manager.ScmManager;
39 import org.apache.maven.scm.manager.ScmManagerStub;
40 import org.apache.maven.scm.provider.ScmProvider;
41 import org.apache.maven.scm.repository.ScmRepository;
42 import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
43 import org.apache.maven.shared.release.config.ReleaseUtils;
44 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
45 import org.apache.maven.shared.release.scm.ScmTranslator;
46 import org.apache.maven.shared.release.util.ReleaseUtil;
47 import org.junit.Test;
48
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Matchers.argThat;
51 import static org.mockito.Matchers.isA;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.verifyNoMoreInteractions;
55 import static org.mockito.Mockito.when;
56
57
58
59
60
61
62 public class GenerateReleasePomsPhaseTest extends AbstractRewritingReleasePhaseTestCase {
63 private static final String NEXT_VERSION = "1.0";
64
65 private static final String ALTERNATIVE_NEXT_VERSION = "2.0";
66
67 private ScmProvider scmProviderMock;
68
69 public GenerateReleasePomsPhaseTest(String modelETL) {
70 super(modelETL);
71 }
72
73 @Override
74 public void setUp() throws Exception {
75 super.setUp();
76
77 scmProviderMock = null;
78 }
79
80 @Override
81 protected Module[] getCustomModules() {
82 return new Module[] {
83 new AbstractModule() {
84 @Override
85 protected void configure() {
86 bind(ScmManager.class)
87 .to(org.apache.maven.shared.release.stubs.ScmManagerStub.class)
88 .in(Singleton.class);
89 bind(ScmTranslator.class)
90 .annotatedWith(Names.named("stub-provider"))
91 .to(org.apache.maven.shared.release.scm.SubversionScmTranslator.class)
92 .in(Singleton.class);
93 }
94 }
95 };
96 }
97
98 @Override
99 protected String getRoleHint() {
100 return "generate-release-poms";
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 @Test
115 public void testRewriteExternalRangeDependency() throws Exception {
116 List<MavenProject> reactorProjects = createReactorProjects("external-range-dependency");
117 ReleaseDescriptorBuilder builder = createMappedConfiguration(reactorProjects, "external-range-dependency");
118
119 phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects);
120
121 comparePomFiles(reactorProjects);
122 }
123
124
125 @Test
126 public void testSuppressCommitBeforeTagOrBranch() throws Exception {
127 List<MavenProject> reactorProjects = createReactorProjects("basic-pom");
128 ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
129 builder.setGenerateReleasePoms(true);
130 builder.setSuppressCommitBeforeTagOrBranch(true);
131 builder.setRemoteTagging(false);
132 builder.setPinExternals(false);
133 mapNextVersion(builder, "groupId:artifactId");
134
135 phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects);
136
137 verify(scmProviderMock).add(isA(ScmRepository.class), isA(ScmFileSet.class));
138
139 verifyNoMoreInteractions(scmProviderMock);
140 }
141
142 @Test
143 public void testSuppressCommitBeforeTagOrBranchAndReomoteTagging() throws Exception {
144 List<MavenProject> reactorProjects = createReactorProjects("basic-pom");
145 ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
146 builder.setGenerateReleasePoms(true);
147 builder.setSuppressCommitBeforeTagOrBranch(true);
148 builder.setRemoteTagging(true);
149 builder.setPinExternals(false);
150 mapNextVersion(builder, "groupId:artifactId");
151
152 phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects);
153
154 verify(scmProviderMock).add(isA(ScmRepository.class), isA(ScmFileSet.class));
155
156 verifyNoMoreInteractions(scmProviderMock);
157 }
158
159
160 @Test
161 public void testFinalName() throws Exception {
162 List<MavenProject> reactorProjects = createReactorProjects("pom-with-finalname");
163 ReleaseDescriptorBuilder builder =
164 createConfigurationForWithParentNextVersion(reactorProjects, "pom-with-finalname");
165 builder.setGenerateReleasePoms(true);
166
167 phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects);
168
169 assertTrue(comparePomFiles(reactorProjects));
170 }
171
172
173
174
175
176
177 @Override
178 protected ReleaseDescriptorBuilder createDescriptorFromProjects(
179 List<MavenProject> reactorProjects, String workingDirectory) {
180 ReleaseDescriptorBuilder builder = super.createDescriptorFromProjects(reactorProjects, workingDirectory);
181 builder.setScmReleaseLabel("release-label");
182 builder.setGenerateReleasePoms(true);
183 return builder;
184 }
185
186
187
188
189
190 @Override
191 protected List<MavenProject> prepareReactorProjects(String path) throws Exception {
192 String dir = "generate-release-poms/" + path;
193 List<MavenProject> reactorProjects = createReactorProjects(dir, dir, null);
194
195 scmProviderMock = mock(ScmProvider.class);
196
197 List<File> releasePoms = new ArrayList<>();
198
199 for (MavenProject project : reactorProjects) {
200 releasePoms.add(ReleaseUtil.getReleasePom(project));
201 }
202
203 MavenProject rootProject = ReleaseUtil.getRootProject(reactorProjects);
204 ScmFileSet fileSet = new ScmFileSet(rootProject.getFile().getParentFile(), releasePoms);
205
206 when(scmProviderMock.add(isA(ScmRepository.class), argThat(new IsScmFileSetEquals(fileSet))))
207 .thenReturn(new AddScmResult(
208 "...", Collections.singletonList(new ScmFile("pom.xml", ScmFileStatus.ADDED))));
209
210 ScmManagerStub stub = (ScmManagerStub) lookup(ScmManager.class);
211 stub.setScmProvider(scmProviderMock);
212
213 return reactorProjects;
214 }
215
216 @Override
217 protected void verifyReactorProjects(String path, boolean copyFiles) throws Exception {
218 String dir = "generate-release-poms/" + path;
219 List<MavenProject> reactorProjects = createReactorProjects(dir, dir, null);
220
221 List<File> releasePoms = new ArrayList<>();
222
223 for (Iterator<MavenProject> iterator = reactorProjects.iterator(); iterator.hasNext(); ) {
224 MavenProject project = iterator.next();
225
226 releasePoms.add(ReleaseUtil.getReleasePom(project));
227 }
228
229 MavenProject rootProject = ReleaseUtil.getRootProject(reactorProjects);
230 ScmFileSet fileSet = new ScmFileSet(rootProject.getFile().getParentFile(), releasePoms);
231
232 verify(scmProviderMock).add(isA(ScmRepository.class), argThat(new IsScmFileSetEquals(fileSet)));
233 verifyNoMoreInteractions(scmProviderMock);
234 }
235
236 @Override
237 protected void mapNextVersion(ReleaseDescriptorBuilder config, String projectId) {
238 config.addReleaseVersion(projectId, NEXT_VERSION);
239 }
240
241
242
243
244
245
246 @Override
247 protected void mapAlternateNextVersion(ReleaseDescriptorBuilder config, String projectId) {
248 config.addReleaseVersion(projectId, ALTERNATIVE_NEXT_VERSION);
249 }
250
251
252
253
254
255
256 @Override
257 protected void unmapNextVersion(ReleaseDescriptorBuilder config, String projectId) {
258
259 }
260
261
262
263
264
265 @Override
266 protected ReleaseDescriptorBuilder createConfigurationForPomWithParentAlternateNextVersion(
267 List<MavenProject> reactorProjects, String workingDirectory) throws Exception {
268 ReleaseDescriptorBuilder builder = createDescriptorFromProjects(reactorProjects, workingDirectory);
269
270 builder.addReleaseVersion("groupId:artifactId", NEXT_VERSION);
271 builder.addReleaseVersion("groupId:subproject1", ALTERNATIVE_NEXT_VERSION);
272
273 return builder;
274 }
275
276
277
278
279
280 @Override
281 protected ReleaseDescriptorBuilder createConfigurationForWithParentNextVersion(
282 List<MavenProject> reactorProjects, String workingDirectory) throws Exception {
283 ReleaseDescriptorBuilder builder = createDescriptorFromProjects(reactorProjects, workingDirectory);
284
285 builder.addReleaseVersion("groupId:artifactId", NEXT_VERSION);
286 builder.addReleaseVersion("groupId:subproject1", NEXT_VERSION);
287
288 return builder;
289 }
290
291
292
293
294
295 @Override
296 protected String readTestProjectFile(String fileName) throws IOException {
297 return ReleaseUtil.readXmlFile(getTestFile("target/test-classes/projects/generate-release-poms/" + fileName));
298 }
299
300
301
302
303
304
305
306 @Override
307 protected void comparePomFiles(MavenProject project, String expectedFileSuffix, boolean normalizeLineEndings)
308 throws IOException {
309 File actualFile = ReleaseUtil.getReleasePom(project);
310 File expectedFile = new File(actualFile.getParentFile(), "expected-release-pom" + expectedFileSuffix + ".xml");
311
312 comparePomFiles(expectedFile, actualFile, normalizeLineEndings, true);
313 }
314 }