This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateCheckerTests.java
332 lines (264 loc) · 12 KB
/
UpdateCheckerTests.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package de.griefed.versionchecker;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
@SuppressWarnings("BusyWait")
public class UpdateCheckerTests {
private static final Logger LOG = LogManager.getLogger(UpdateCheckerTests.class);
private GitHubChecker GITHUB;
private GitLabChecker GITGRIEFED;
public UpdateCheckerTests() throws IOException {
this.GITHUB = new GitHubChecker("Griefed/ServerPackCreator");
this.GITGRIEFED = new GitLabChecker("https://git.griefed.de/api/v4/projects/63/releases");
}
@Test
void checkForUpdates() {
try {
Assertions.assertNotNull(this.GITHUB.refresh());
} catch (Exception ex) {
LOG.error("Error refreshing GitHub.", ex);
this.GITHUB = null;
}
try {
Assertions.assertNotNull(this.GITGRIEFED.refresh());
} catch (Exception ex) {
LOG.error("Error refreshing GitGriefed.", ex);
this.GITGRIEFED = null;
}
if (GITHUB == null) {
do {
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Assertions.assertNotNull(this.GITHUB.refresh());
} catch (Exception ex) {
LOG.error("Error refreshing GitHub.", ex);
this.GITHUB = null;
}
} while (GITHUB == null);
}
if (GITGRIEFED == null) {
do {
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Assertions.assertNotNull(this.GITGRIEFED.refresh());
} catch (Exception ex) {
LOG.error("Error refreshing GitGriefed.", ex);
this.GITGRIEFED = null;
}
} while (GITGRIEFED == null);
}
Assertions.assertNotNull(GITHUB.latestVersion(false));
Assertions.assertNotNull(GITHUB.latestVersion(true));
Assertions.assertNotNull(GITHUB.latestAlpha());
Assertions.assertNotNull(GITHUB.latestBeta());
GITHUB.allVersions();
Assertions.assertNotNull(GITHUB.getDownloadUrl("2.1.1"));
Assertions.assertNotNull(GITHUB.getAssetsDownloadUrls("2.1.1"));
Assertions.assertNotNull(GITGRIEFED.latestVersion(false));
Assertions.assertNotNull(GITGRIEFED.latestVersion(true));
Assertions.assertNotNull(GITGRIEFED.latestAlpha());
Assertions.assertNotNull(GITGRIEFED.latestBeta());
Assertions.assertNotNull(GITGRIEFED.getDownloadUrl("2.1.1"));
Assertions.assertNotNull(GITGRIEFED.getAssetsDownloadUrls("2.1.1"));
String latest = GITHUB.latestVersion(false);
String latestPre = GITHUB.latestVersion(true);
System.out.println("latest = " + latest);
System.out.println("latestPre = " + latestPre);
System.out.println("Old version should return the newest regular release, whilst not checking for pre-releases.");
Assertions.assertEquals(
latest,
checkForUpdate(
"2.0.0",
false
).split(";")[0]);
System.out.println("Old version should return the newest pre-release release, whilst checking for pre-releases, too.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
"2.0.0",
true
).split(";")[0]);
System.out.println("Old alpha should return the newest regular release, whilst not checking for pre-releases.");
Assertions.assertEquals(
latest,
checkForUpdate(
"2.0.0-alpha.2",
false
).split(";")[0]);
System.out.println("Old alpha should return the newest alpha/beta, whilst checking for pre-releases, too.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
"2.0.0-alpha.2",
true
).split(";")[0]);
System.out.println("Old beta should return the newest regular release, whilst not checking for pre-releases.");
Assertions.assertEquals(
latest,
checkForUpdate(
"2.0.0-beta.2",
false
).split(";")[0]);
System.out.println("Old beta should return the newest alpha/beta, whilst checking for pre-releases, too.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
"2.0.0-beta.2",
true
).split(";")[0]);
System.out.println("Old beta, but newer than the newest regular release, should the latest available alpha/beta, whilst checking for pre-releases, too.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
"3.0.0-beta.2",
true
).split(";")[0]);
System.out.println("Old alpha, but newer than the newest regular release, should return the newest alpha/beta, whilst checking for pre-releases, too.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
"3.0.0-alpha.2",
true
).split(";")[0]);
System.out.println("Future alpha should return no available updates, whilst not checking for pre-releases.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789-alpha.2",
false
)
);
System.out.println("Future alpha should return no available updates, whilst checking for pre-releases, too.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789-alpha.2",
true
)
);
System.out.println("Future beta should return no available updates, whilst not checking for pre-releases.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789-beta.2",
false
)
);
System.out.println("Future beta should return no available updates, whilst checking for pre-releases, too.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789-beta.2",
true
)
);
System.out.println("Newer version should return no available updates, whilst not checking for pre-releases.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789",
false
)
);
System.out.println("Newer version should return no available updates, whilst checking for pre-releases.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
"123.456.789",
true
)
);
System.out.println("Latest version should return no available updates, whilst not checking for pre-releases.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
latest,
false
)
);
System.out.println("Latest alpha/beta should return no available updates, whilst checking for pre-releases, too.");
Assertions.assertEquals(
"No updates available.",
checkForUpdate(
latestPre,
true
)
);
System.out.println("Beta for latest version should return latest version as available update, whilst not checking for pre-releases.");
Assertions.assertEquals(
latest,
checkForUpdate(
latest + "-beta.2",
false
).split(";")[0]);
System.out.println("Beta for latest version should return latest version as available update.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
latest + "-beta.2",
true
).split(";")[0]);
System.out.println("Alpha for latest version should return latest version as available update.");
Assertions.assertEquals(
latest,
checkForUpdate(
latest + "-alpha.12",
false
).split(";")[0]);
System.out.println("Alpha for latest version should return latest version as available update.");
Assertions.assertEquals(
latestPre,
checkForUpdate(
latest + "-alpha.2",
true
).split(";")[0]);
System.out.println("Update-instance from GitHub");
Assertions.assertTrue(GITHUB.check("2.0.0",false).isPresent());
Assertions.assertFalse(GITHUB.check(latest,false).isPresent());
Update gitHubUpdate = GITHUB.check("2.0.0",false).get();
Assertions.assertEquals(gitHubUpdate.version(), latest);
Assertions.assertNotNull(gitHubUpdate.description());
Assertions.assertNotNull(gitHubUpdate.url());
Assertions.assertNotNull(gitHubUpdate.releaseDate());
Assertions.assertNotEquals(0, gitHubUpdate.assets().get().size());
Assertions.assertNotEquals(0, gitHubUpdate.sources().size());
}
private String checkForUpdate(String version, boolean pre) {
String updater = null;
// Check GitHub for the most recent release.
if (GITHUB != null) {
// Check GitHub for new versions which are not pre-releases. Run with true to check pre-releases as well.
updater = GITHUB.checkForUpdate(version, pre);
}
if (GITGRIEFED != null && updater != null) {
// After checking GitLab, and we did not get a version, check GitGriefed.
// Check GitGriefed for new versions which are not pre-releases. Run with true to check pre-releases as well.
// Only check if we did not already get a version from prior checks.
if (!updater.contains(";") && GITGRIEFED.checkForUpdate(version, pre).contains(";")) {
updater = GITGRIEFED.checkForUpdate(version, pre);
// Check GitGriefed for a newer version, with the version received from GitHub, if we received a new version from GitHub.
// Don't check for pre-releases.
} else if (updater.contains(";") && GITGRIEFED.checkForUpdate(updater.split(";")[0], pre).contains(";")) {
updater = GITGRIEFED.checkForUpdate(updater.split(";")[0], pre);
}
}
// Output can be either "No updates available." if...well...no updates are available.
// or "2.1.1;https://github.com/Griefed/ServerPackCreator/releases/download/2.1.1/serverpackcreator-2.1.1.jar"
// if you ran this for ServerPackCreator, with version 2.1.1, without checking for pre-releases. (at the time of me writing this)
System.out.println("Check pre: " + pre );
System.out.println("Checked version: " + version);
System.out.println("Result: " + updater);
System.out.println();
return updater;
}
}