Verified Commit ca15a7d1 authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Added update mechanism for config file

parent f4bd7c91
package h05;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* This class contains constants that are used as settings for the tests.
......@@ -11,6 +11,8 @@ import java.util.Random;
@SuppressWarnings({"JavadocReference", "unused"})
public class Config {
// >>>## UPDATE MARKER, DO NOT REMOVE, ONLY MODIFY THE LINES BELOW
/**
* Whether this file exists. Used by Utils.java to determine whether or not the config file is missing
*/
......@@ -65,4 +67,14 @@ public class Config {
* @see h05.provider.RandomTreeProvider
*/
public static final int MAX_TREE_DEPTH = 5;
// ##<<< UPDATE MARKER, DO NOT REMOVE, DO NOT CHANGE ANYTHING BELOW THIS LINE
/**
* Returns a set of the names of all fields in this class
* @return a set of all fields defined in this class
*/
public static Set<String> getConfigs() {
return Arrays.stream(Config.class.getDeclaredFields()).map(Field::getName).collect(Collectors.toUnmodifiableSet());
}
}
......@@ -29,7 +29,7 @@ public class Utils {
public static final Map<Class<?>, Boolean> CLASS_CORRECT = new HashMap<>();
public static final Map<Method, Boolean> METHOD_CORRECT = new HashMap<>();
private static final String LOCAL_VERSION = "1.1", ASSIGNMENT_ID = "H05";
private static final String LOCAL_VERSION = "2.0.0", ASSIGNMENT_ID = "H05";
private static final Map<TestType.Type, String> METHOD_LOOKUP = Map.of(
TestType.Type.CLASS, "checkClass",
TestType.Type.INTERFACE, "checkInterface"
......@@ -208,6 +208,7 @@ public class Utils {
*/
private static boolean checkForUpdates() {
HttpResponse<String> response = getHttpResource(".test_version");
boolean persistentChanges = false;
if (response == null || response.statusCode() != 200) {
System.err.println("Unable to fetch version from repository");
......@@ -216,7 +217,6 @@ public class Utils {
try {
JSONObject remoteData = new JSONObject(response.body());
boolean persistentChanges = false;
Version localVersion = new Version(LOCAL_VERSION),
remoteVersion = new Version(remoteData.getString("version"));
......@@ -269,11 +269,51 @@ public class Utils {
}
}
return persistentChanges;
} catch (IOException | InterruptedException | NoSuchAlgorithmException e) {
if (AUTO_UPDATE && (Boolean) getConfigOrDefault("EXISTS", false)) {
JSONObject configData = remoteData.getJSONObject("config");
String configStub = configData.getString("stub");
StringBuilder configFileContents = new StringBuilder(" // >>>## UPDATE MARKER, DO NOT REMOVE, ONLY MODIFY THE LINES BELOW\n\n");
//noinspection unchecked
Set<String> existingFields = (Set<String>) Class.forName("h05.Config").getDeclaredMethod("getConfigs").invoke(null);
try (BufferedReader reader = new BufferedReader(new FileReader(configData.getString("file")))) {
boolean insert = false;
for (String line = reader.readLine(); !line.matches("\\w*// ##<<<.*"); line = reader.readLine()) {
if (line.matches("\\w*// >>>##.*")) {
reader.readLine();
insert = true;
}
if (insert)
configFileContents.append(line);
}
}
for (Map.Entry<String, Object> constantEntry : configData.getJSONObject("constants").toMap().entrySet()) {
String fieldName = constantEntry.getKey(), fieldData = (String) constantEntry.getValue();
if (!existingFields.contains(fieldName))
configFileContents.append(fieldData).append("\n\n");
}
configFileContents.append(" // ##<<< UPDATE MARKER, DO NOT REMOVE, DO NOT CHANGE ANYTHING BELOW THIS LINE\n\n");
for (Map.Entry<String, Object> methodEntry : configData.getJSONObject("methods").toMap().entrySet())
configFileContents.append((String) methodEntry.getValue()).append("\n\n");
persistentChanges = true;
try (BufferedWriter writer = new BufferedWriter(new FileWriter(configData.getString("file")))) {
writer.write(configStub.replaceFirst(">>>##<<<", configFileContents.toString()));
}
}
} catch (IOException | InterruptedException | NoSuchAlgorithmException | ReflectiveOperationException e) {
e.printStackTrace();
return true;
}
return persistentChanges;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment