-
Notifications
You must be signed in to change notification settings - Fork 267
/
PomHelperTest.java
279 lines (230 loc) · 11.2 KB
/
PomHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package org.codehaus.mojo.versions.api;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.StringReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.DefaultModelWriter;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.stax2.XMLInputFactory2;
import org.hamcrest.MatcherAssert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import static org.codehaus.mojo.versions.utils.ModifiedPomXMLEventReaderUtils.matches;
import static org.codehaus.stax2.XMLInputFactory2.P_PRESERVE_LOCATION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
/**
* Tests the methods of {@link PomHelper}.
*/
public class PomHelperTest extends AbstractMojoTestCase {
private static final int NUMBER_OF_CHILD_PROJECTS = 30;
private static final String DEFAULT_VERSION = "1.0.0-SNAPSHOT";
@Rule
public MojoRule mojoRule = new MojoRule(this);
private static final XMLInputFactory INPUT_FACTORY = XMLInputFactory2.newInstance();
@BeforeClass
public static void setUpClass() {
INPUT_FACTORY.setProperty(P_PRESERVE_LOCATION, Boolean.TRUE);
}
/**
* Tests what happens when changing a long property substitution pattern, e.g.
* <a href="http://jira.codehaus.org/browse/MVERSIONS-44">MVERSIONS-44</a>
*
* @throws Exception if the test fails.
*/
@Test
public void testLongProperties() throws Exception {
URL url = getClass().getResource("PomHelperTest.testLongProperties.pom.xml");
assert url != null;
File file = new File(url.getPath());
StringBuilder input = PomHelper.readXmlFile(file);
XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty(P_PRESERVE_LOCATION, Boolean.TRUE);
ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader(input, inputFactory, file.getAbsolutePath());
String oldVersion = PomHelper.getProjectVersion(pom);
String newVersion = "1";
assertTrue("The pom has been modified", PomHelper.setProjectVersion(pom, newVersion));
assertEquals(newVersion, PomHelper.getProjectVersion(pom));
assertNotSame(oldVersion, newVersion);
}
@Test
public void testGroupIdNotOnChildPom() throws Exception {
URL url = getClass().getResource("PomHelperTest.noGroupIdOnChild.pom.xml");
assert url != null;
StringBuilder input = PomHelper.readXmlFile(new File(url.getPath()));
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new StringReader(input.toString()));
assertEquals("org.myorg", PomHelper.getGroupId(model));
}
@Test
public void testVersionVersionEqual() throws Exception {
assertTrue(PomHelper.isVersionOverlap("1.0.8", "1.0.8"));
}
@Test
public void testVersionVersionDiffer() throws Exception {
assertFalse(PomHelper.isVersionOverlap("1.0.8", "1.0.0"));
}
@Test
public void testVersionRangeIntersect() throws Exception {
assertTrue(PomHelper.isVersionOverlap("1.0.8", "[1.0.3,1.1.0]"));
}
@Test
public void testVersionRangeDisjoint() throws Exception {
assertFalse(PomHelper.isVersionOverlap("1.0.8", "[0.0.1,1.0.0]"));
}
@Test
public void testVersionLeftOpenRangeDisjoint() throws Exception {
assertFalse(PomHelper.isVersionOverlap("1.0.8", "[,1.0.0]"));
}
@Test
public void testVersionRightOpenRangeDisjoint() throws Exception {
assertFalse(PomHelper.isVersionOverlap("1.0.8", "[1.1.0,)"));
}
@Test
public void testEmptyRange() throws Exception {
assertTrue(PomHelper.isVersionOverlap("1.0.8", ""));
}
@Test
public void testRangeEmpty() throws Exception {
assertTrue(PomHelper.isVersionOverlap("[1.0.5,1.0.8]", ""));
}
@Test
public void testRangeRangeIntersect() throws Exception {
assertTrue(PomHelper.isVersionOverlap("[1.0.5,1.0.8]", "[1.0.7,1.1.0]"));
}
@Test
public void testRangeRangeDisjoint() throws Exception {
assertFalse(PomHelper.isVersionOverlap("[1.0.5,1.0.6]", "[1.0.7,1.1.0]"));
}
@Test
public void testRangeVersionDisjoint() throws Exception {
assertFalse(PomHelper.isVersionOverlap("[1.0.5,1.0.6]", "1.0.8"));
}
@Test
public void testRangeVersionIntersect() throws Exception {
assertTrue(PomHelper.isVersionOverlap("[1.0.0,2.0.0]", "1.0.8"));
}
@Test
public void testSetElementValueExistingValue() throws XMLStreamException {
ModifiedPomXMLEventReader xmlEventReader = new ModifiedPomXMLEventReader(
new StringBuilder("<super-parent><parent><child>test</child></parent></super-parent>"),
INPUT_FACTORY,
null);
assertThat(PomHelper.setElementValue(xmlEventReader, "/super-parent/parent", "child", "value"), is(true));
MatcherAssert.assertThat(
xmlEventReader, matches("<super-parent><parent><child>value</child></parent></super-parent>"));
}
@Test
public void testSetElementValueEmptyChild() throws XMLStreamException {
ModifiedPomXMLEventReader xmlEventReader = new ModifiedPomXMLEventReader(
new StringBuilder("<super-parent><parent><child/></parent></super-parent>"), INPUT_FACTORY, null);
assertThat(PomHelper.setElementValue(xmlEventReader, "/super-parent/parent", "child", "value"), is(true));
MatcherAssert.assertThat(
xmlEventReader, matches("<super-parent><parent><child>value</child></parent></super-parent>"));
}
@Test
public void testSetElementValueNewValueEmptyParent() throws XMLStreamException {
ModifiedPomXMLEventReader xmlEventReader = new ModifiedPomXMLEventReader(
new StringBuilder("<super-parent><parent/></super-parent>"), INPUT_FACTORY, null);
assertThat(PomHelper.setElementValue(xmlEventReader, "/super-parent/parent", "child", "value"), is(true));
MatcherAssert.assertThat(
xmlEventReader, matches("<super-parent><parent><child>value</child></parent></super-parent>"));
}
@Test
public void testSetElementValueNewValueNoChild() throws XMLStreamException {
ModifiedPomXMLEventReader xmlEventReader = new ModifiedPomXMLEventReader(
new StringBuilder("<super-parent><parent><child2/></parent></super-parent>"), INPUT_FACTORY, null);
assertThat(PomHelper.setElementValue(xmlEventReader, "/super-parent/parent", "child", "value"), is(true));
MatcherAssert.assertThat(
xmlEventReader, matches("<super-parent><parent><child2/><child>value</child></parent></super-parent>"));
}
@Test
public void testSetProjectValueNewValueNonEmptyParent() throws XMLStreamException {
ModifiedPomXMLEventReader xmlEventReader = new ModifiedPomXMLEventReader(
new StringBuilder("<super-parent><parent><child>test</child></parent></super-parent>"),
INPUT_FACTORY,
null);
assertThat(PomHelper.setElementValue(xmlEventReader, "/super-parent/parent", "child", "value"), is(true));
MatcherAssert.assertThat(
xmlEventReader, matches("<super-parent><parent><child>value</child></parent></super-parent>"));
}
@Test
public void testIssue505ChildModules() throws Exception {
MavenProject project =
mojoRule.readMavenProject(new File("src/test/resources/org/codehaus/mojo/versions/api/issue-505"));
assertThat(PomHelper.getChildModels(project, new SystemStreamLog()).entrySet(), hasSize(3));
}
@Test
public void testChildModelsForMultiLevelProject() throws Exception {
Path tempDirectory = Files.createTempDirectory("testChildModelsForLargeNumberOfModules");
ModelWriter modelWriter = new DefaultModelWriter();
Map<Path, Model> createdModels = new LinkedHashMap<>();
try {
Model rootProject = createSimpleModel("root");
createdModels.put(tempDirectory, rootProject);
for (int levelOne = 0; levelOne < NUMBER_OF_CHILD_PROJECTS; levelOne++) {
Model levelOneProject = createSimpleModel("child-" + levelOne);
Path levelOneProjectDirectory = tempDirectory.resolve(levelOneProject.getArtifactId());
rootProject.addModule(levelOneProject.getArtifactId());
createdModels.put(levelOneProjectDirectory, levelOneProject);
for (int levelTwo = 0; levelTwo < NUMBER_OF_CHILD_PROJECTS; levelTwo++) {
Model levelTwoProject = createSimpleModel("child-" + levelOne + "-" + levelTwo);
Path levelTwoProjectDirectory = levelOneProjectDirectory.resolve(levelTwoProject.getArtifactId());
levelOneProject.addModule(levelTwoProject.getArtifactId());
createdModels.put(levelTwoProjectDirectory, levelTwoProject);
}
}
for (Map.Entry<Path, Model> entry : createdModels.entrySet()) {
modelWriter.write(entry.getKey().resolve("pom.xml").toFile(), Collections.emptyMap(), entry.getValue());
}
MavenProject project = mojoRule.readMavenProject(tempDirectory.toFile());
assertThat(
PomHelper.getChildModels(project, new SystemStreamLog()).entrySet(), hasSize(createdModels.size()));
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
}
}
private Model createSimpleModel(String artifactId) {
Model module = new Model();
module.setGroupId("child.test");
module.setArtifactId(artifactId);
module.setVersion(DEFAULT_VERSION);
module.setPackaging("pom");
module.setModelVersion("4.0.0");
return module;
}
}