-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCFUpdate.java
91 lines (86 loc) · 3.67 KB
/
CFUpdate.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
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.prefs.Preferences;
/**
* Öffentliche Testfälle von Christian Femers. Siehe
* https://github.com/MaisiKoleni/GAD-Extras
* <p>
* This class is for test update checks only. DO NOT SUBMIT THIS CLASS.
* <p>
* Place this class next to the test classes to enable automatic version checks
*
* @version 1.2
*
* @since 1.1 increased confirmation period and added updater updater
* @since 1.2 made checkForNewVersion public, so the test classes can be in
* whatever package the user prefers, as long as CFUpdate is in default.
*
* @author Christian Femers
*/
public final class CFUpdate {
/**
* self update check
*/
static {
checkForNewVersion("version", CFUpdate.class, "1.2");
}
/**
* Automatically checks for new versions. This should help to communicate
* problems with the tests themselves and when new or extended tests are
* available. Checks are done only every 30 minutes (if run, of course).
*/
public static void checkForNewVersion(String blatt, Class<?> testClass, String currentVersion) {
String testName = testClass.getSimpleName();
if (!testName.endsWith("Test") && !testClass.equals(CFUpdate.class))
throw new IllegalArgumentException("Please pass a valid test class. Found: " + testName);
String key = blatt + "." + testName + "_last_update_check";
String url = "https://raw.githubusercontent.com/MaisiKoleni/GAD-Extras/master/" + blatt + "/" + testName
+ ".java";
// prevent checking every single run [user-registry > Software > JavaSoft > ..]
String in = Preferences.userRoot().node("christian_femers_tum").node("gad_extras").get(key, "0");
if (in.matches("\\d{1,19}") && Long.parseLong(in) < System.currentTimeMillis() / 1_800_000) {
new Thread(() -> {
// get the raw code from GitHub
try (InputStream is = new URL(url).openStream()) {
String content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
// search for version in the code and compare both
String v = content.replaceFirst("(?s).*? @version (\\S+).*", "$1");
if (v.compareTo(currentVersion) > 0) {
// print that a newer version is there and wait 20 seconds for input
System.err.format("%n>>> There is a newer Version (%s) available under <<<%n%s%n%n", v, url);
try (Scanner sc = new Scanner(System.in)) {
System.out.format("Print the new source code for %s now? y/n (10 sec)%n", testName);
for (int i = 0; i < 200 && System.in.available() == 0; i++) {
try {
Thread.sleep(50);
} catch (@SuppressWarnings("unused") InterruptedException e) {
// should not happen
Thread.currentThread().interrupt();
}
}
// print code if typed "y" or "Y"
if (System.in.available() != 0 && sc.nextLine().equalsIgnoreCase("y"))
System.out.println(content);
else
System.out.println("Exiting...");
} catch (@SuppressWarnings("unused") IOException e) {
System.err.println("System.in already in use (two updates at the same time?)");
}
}
} catch (IOException e) {
System.err.println("Cannot reach update URL (no internet connection? renamed something?)");
e.printStackTrace();
}
// store the current time again
Preferences.userRoot().node("christian_femers_tum").node("gad_extras").putLong(key,
System.currentTimeMillis() / 1_800_000);
}).start();
}
}
public static void main(String[] args) {
System.out.println("Please do not upload CFUpdate.java");
}
}