/
SetMojoTest.java
206 lines (182 loc) · 7.8 KB
/
SetMojoTest.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
package org.codehaus.mojo.versions;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
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.utils.TestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
public class SetMojoTest extends AbstractMojoTestCase {
@Rule
public MojoRule mojoRule = new MojoRule(this);
private Path tempDir;
@Before
public void setUp() throws Exception {
super.setUp();
tempDir = TestUtils.createTempDir("set");
}
@After
public void tearDown() throws IOException {
TestUtils.tearDownTempDir(tempDir);
}
@Test
public void testGetIncrementedVersion() throws MojoExecutionException {
new SetMojo(null, null, null, null, null, null) {
{
assertThat(getIncrementedVersion("1.0.0", null), is("1.0.1-SNAPSHOT"));
assertThat(getIncrementedVersion("1.0.0-SNAPSHOT", null), is("1.0.1-SNAPSHOT"));
assertThat(getIncrementedVersion("1.0.0-SNAPSHOT", 1), is("2.0.0-SNAPSHOT"));
assertThat(getIncrementedVersion("1.0.0-SNAPSHOT", 2), is("1.1.0-SNAPSHOT"));
assertThat(getIncrementedVersion("1.0.0-SNAPSHOT", 3), is("1.0.1-SNAPSHOT"));
}
};
}
@Test
public void testNextSnapshotIndexLowerBound() {
new SetMojo(null, null, null, null, null, null) {
{
try {
getIncrementedVersion("1.0.0", 0);
fail();
} catch (MojoExecutionException e) {
assertThat(e.getMessage(), containsString("nextSnapshotIndexToIncrement cannot be less than 1"));
}
}
};
}
@Test
public void testNextSnapshotIndexUpperBound() {
new SetMojo(null, null, null, null, null, null) {
{
try {
getIncrementedVersion("1.0.0", 4);
fail();
} catch (MojoExecutionException e) {
assertThat(
e.getMessage(),
containsString(
"nextSnapshotIndexToIncrement cannot be greater than the last version index"));
}
}
};
}
@Test
public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureException {
try {
new SetMojo(null, null, null, null, null, null) {
{
project = new MavenProject();
project.setParent(new MavenProject());
project.setOriginalModel(new Model());
project.getOriginalModel().setVersion("1.2.3-SNAPSHOT");
nextSnapshotIndexToIncrement = 4;
}
}.execute();
} catch (MojoExecutionException e) {
assertThat(
e.getMessage(),
containsString("nextSnapshotIndexToIncrement is not valid when nextSnapshot is false"));
}
}
@Test
public void testVersionlessDependency() throws Exception {
SetMojo myMojo = (SetMojo) mojoRule.lookupConfiguredMojo(
new File("target/test-classes/org/codehaus/mojo/set/versionless-01"), "set");
myMojo.execute();
}
@Test
public void testRemoveSnapshotIdempotency() throws Exception {
Files.copy(
Paths.get("src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml"),
Paths.get(tempDir.toString(), "pom.xml"),
REPLACE_EXISTING);
SetMojo firstRun = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
firstRun.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>1.0</version>"));
// no exception should be thrown, the file should stay with version "1.0"
SetMojo secondRun = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
secondRun.session.getSettings().setInteractiveMode(false);
secondRun.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>1.0</version>"));
}
@Test
public void testSetOldVersionMismatch() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/issue-794"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
setVariableValueToObject(mojo, "oldVersion", "foo");
setVariableValueToObject(mojo, "newVersion", "bar");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>bar</version>")));
}
@Test
public void testSetOldVersionMismatchProcessAllModules() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/issue-794"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
setVariableValueToObject(mojo, "oldVersion", "foo");
setVariableValueToObject(mojo, "newVersion", "bar");
setVariableValueToObject(mojo, "processAllModules", true);
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>bar</version>")));
}
private void testSetParameterValue(String filename) throws Exception {
Files.copy(
Paths.get("src/test/resources/org/codehaus/mojo/set/issue-855/").resolve(filename),
tempDir.resolve("pom.xml"));
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>testing</version>"));
}
@Test
public void testSetParameterValueSimple() throws Exception {
testSetParameterValue("pom-simple.xml");
}
@Test
public void testSetParameterValueBuildNumber() throws Exception {
testSetParameterValue("pom-build-number.xml");
}
@Test
public void testSetParameterValueUndefined() throws Exception {
testSetParameterValue("pom-undefined.xml");
}
@Test
public void testSetParameterValueMultipleProps() throws Exception {
testSetParameterValue("pom-multiple-props.xml");
}
@Test
public void testParentWithProperty() throws Exception {
TestUtils.copyDir(
Paths.get("src/test/resources/org/codehaus/mojo/set/issue-855/parent-with-property"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>testing</version>"));
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("child/pom.xml"))),
containsString("<version>testing</version>"));
}
}