-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSplitMainTest.java
More file actions
213 lines (182 loc) · 8.29 KB
/
TestSplitMainTest.java
File metadata and controls
213 lines (182 loc) · 8.29 KB
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
package de.donnerbart.split;
import com.beust.jcommander.JCommander;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.time.Instant;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import static de.donnerbart.split.TestUtil.copyResourceToTarget;
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;
import static org.assertj.core.api.Assertions.assertThat;
class TestSplitMainTest {
private static final @NotNull Set<PosixFilePermission> PERMISSIONS = Set.of(OWNER_READ, OWNER_WRITE);
private final @NotNull Arguments arguments = new Arguments();
private final @NotNull JCommander jCommander = JCommander.newBuilder().addObject(arguments).build();
private final @NotNull AtomicReference<Integer> exitCode = new AtomicReference<>();
private @NotNull Path projectFolder;
@TempDir
private @NotNull Path tmp;
@BeforeEach
void setUp() throws Exception {
projectFolder = tmp.resolve("example-project")
.resolve("src")
.resolve("main")
.resolve("java")
.resolve("de")
.resolve("donnerbart")
.resolve("example");
Files.createDirectories(projectFolder);
// tests
copyResourceToTarget(projectFolder, "tests/FastTest.java", "FastTest.java", PERMISSIONS);
copyResourceToTarget(projectFolder, "tests/SlowTest.java", "SlowTest.java", PERMISSIONS);
copyResourceToTarget(projectFolder, "tests/SlowestTest.java", "SlowestTest.java", PERMISSIONS);
final var reportFolder = tmp.resolve("junit-reports");
copyResourceToTarget(reportFolder,
"reports/TEST-de.donnerbart.example.FastTest.xml",
"TEST-de.donnerbart.example.FastTest.xml",
PERMISSIONS);
copyResourceToTarget(reportFolder,
"reports/TEST-de.donnerbart.example.SlowTest.xml",
"TEST-de.donnerbart.example.SlowTest.xml",
PERMISSIONS);
copyResourceToTarget(reportFolder,
"reports/TEST-de.donnerbart.example.SlowestTest.xml",
"TEST-de.donnerbart.example.SlowestTest.xml",
PERMISSIONS);
}
@Test
void run() throws Exception {
final var splits = TestSplitMain.run(exitCode::set, new String[]{
"-i",
"0",
"-t",
"2",
"-g",
"**/example-project/**/*Test.java",
"-j",
"**/junit-reports/*.xml",
"-w",
tmp.toAbsolutePath().toString(),
"-d"});
assertThat(exitCode).hasNullValue();
assertThat(splits.size()).isEqualTo(2);
assertThat(splits.get(0).sortedTests()).hasSize(1) //
.containsExactly("de.donnerbart.example.SlowestTest");
assertThat(splits.get(1).sortedTests()).hasSize(2)
.containsExactly("de.donnerbart.example.SlowTest", "de.donnerbart.example.FastTest");
}
@Test
void validateArguments() {
jCommander.parse("-i", "0", "-t", "1", "-g", "**/*Test.java");
assertThat(TestSplitMain.validateArguments(arguments, tmp)).isTrue();
}
@Test
void validateArguments_withZeroSplitTotal() {
jCommander.parse("-i", "0", "-t", "0", "-g", "**/*Test.java");
assertThat(TestSplitMain.validateArguments(arguments, tmp)).isFalse();
}
@Test
void validateArguments_withNegativeSplitTotal() {
jCommander.parse("-i", "0", "-t", "-1", "-g", "**/*Test.java");
assertThat(TestSplitMain.validateArguments(arguments, tmp)).isFalse();
}
@Test
void validateArguments_withTooSmallSplitIndex() {
jCommander.parse("-i", "1", "-t", "1", "-g", "**/*Test.java");
assertThat(TestSplitMain.validateArguments(arguments, tmp)).isFalse();
}
@Test
void validateArguments_withInvalidWorkingDirectory() {
jCommander.parse("-i", "0", "-t", "1", "-g", "**/*Test.java");
assertThat(TestSplitMain.validateArguments(arguments, tmp.resolve("does-not-exist"))).isFalse();
}
@Test
void calculateOptimalTotalSplit() throws Exception {
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);
jCommander.parse("-i", "0", "-t", "2", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(2);
}
@Test
void calculateOptimalTotalSplit_withMaxCalculations() throws Exception {
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);
jCommander.parse("-i",
"0",
"-t",
"4",
"-g",
"**/*Test.java",
"-j",
"**/junit-reports/*.xml",
"-n",
"max",
"-m",
"5");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(4);
}
@Test
void calculateOptimalTotalSplit_withTooLowMaxCalculations() throws Exception {
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);
jCommander.parse("-i",
"0",
"-t",
"1",
"-g",
"**/*Test.java",
"-j",
"**/junit-reports/*.xml",
"-n",
"max",
"-m",
"4");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(0);
}
@Test
void calculateOptimalTotalSplit_withTotalSplitMismatch() throws Exception {
jCommander.parse("-i", "0", "-t", "1", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(2);
}
@Test
void calculateOptimalTotalSplit_withoutJUnitGlob() throws Exception {
jCommander.parse("-i", "0", "-t", "1", "-g", "**/*Test.java");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(0);
}
@Test
void calculateOptimalTotalSplit_withInvalidSplitIndex() throws Exception {
jCommander.parse("-i", "1", "-t", "1", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml");
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(0);
}
@Test
void readProperties() throws Exception {
final var properties = TestSplitMain.readProperties("split-tests-java-test.properties");
assertThat(properties).isNotNull();
assertThat(properties.getProperty("git.commit.time")).isEqualTo("2023-01-01T00:01:02+0000");
assertThat(properties.getProperty("git.commit.id")).isEqualTo("4c204731e327bc2e06d2a1b02f46e4195c210d0e");
assertThat(properties.getProperty("git.commit.id.abbrev")).isEqualTo("4c20473");
assertThat(properties.getProperty("git.branch")).isEqualTo("master");
assertThat(properties.getProperty("version")).isEqualTo("1.2.3-SNAPSHOT");
}
@Test
void readProperties_whenFileNotFound() throws Exception {
final var properties = TestSplitMain.readProperties("not-found.properties");
assertThat(properties).isNotNull();
assertThat(properties).isEmpty();
}
@Test
void getBuiltTime() {
assertThat(TestSplitMain.getBuiltTime("2023-01-01T00:01:02+0000")).isEqualTo("2023-01-01T00:01:02+0000");
}
@Test
void getBuiltTime_whenTimeNotParseable() {
assertThat(TestSplitMain.getBuiltTime("")).isEqualTo(Date.from(Instant.EPOCH));
}
}