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

Removed dependency of Utils.java on Config.java

Removed the direct dependency by adding Utils#getConfigOrDefault(String, Object). See the documentation for more info
parent 95d99556
......@@ -8,9 +8,14 @@ import java.util.Random;
* This class contains constants that are used as settings for the tests.
* They will not be overwritten when an update is downloaded (assuming this file is in {@link Config#EXCLUDED_FILES})
*/
@SuppressWarnings("JavadocReference")
@SuppressWarnings({"JavadocReference", "unused"})
public class Config {
/**
* Whether this file exists. Used by Utils.java to determine whether or not the config file is missing
*/
public static final boolean EXISTS = true;
/**
* Seed that is used for initialization of Random object, set to fixed value for (hopefully) reproducible tests / results
* @see Utils#RANDOM
......
......@@ -13,14 +13,17 @@ import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;
import static h05.Config.*;
public class Utils {
// ----------------------------------- //
// DO NOT CHANGE ANYTHING IN THIS FILE //
// ----------------------------------- //
private static final long SEED = (Long) getConfigOrDefault("SEED", new Random().nextLong());
private static final boolean CHECK_FOR_UPDATES = (Boolean) getConfigOrDefault("CHECK_FOR_UPDATES", true),
CHECK_HASHES = (Boolean) getConfigOrDefault("CHECK_HASHES", true),
AUTO_UPDATE = (Boolean) getConfigOrDefault("AUTO_UPDATE", true);
public static final Random RANDOM = new Random(SEED);
public static final Map<Class<?>, Boolean> CLASS_CORRECT = new HashMap<>();
public static final Map<Method, Boolean> METHOD_CORRECT = new HashMap<>();
......@@ -32,26 +35,15 @@ public class Utils {
);
static {
try {
getClassForName("h05.Config");
if (!(Boolean) getConfigOrDefault("EXISTS", false))
System.err.println("Configurations could not be found, they must be downloaded from the repository. " +
"They will be automatically downloaded when using the installer or when CHECK_FOR_UPDATES and AUTO_UPDATE are true. " +
"Please re-run the tests.");
if (CHECK_FOR_UPDATES)
Updater.checkForUpdates();
System.out.println("Seed: " + SEED);
} catch (TestAbortedException e) {
System.out.println("Configurations could not be found, they must be downloaded from the repository. " +
"They will automatically be downloaded if AUTO_UPDATE is true. Please re-run the tests.");
if (AUTO_UPDATE)
try {
Updater.updateLocal("src/test/h05/Config.java");
} catch (IOException | InterruptedException ee) {
ee.printStackTrace();
}
System.exit(0);
}
}
/**
......@@ -180,6 +172,22 @@ public class Utils {
}
}
/**
* Attempts to get the value of a given field from {@link Config}. Returns either the actual value or the given default value
* @param fieldName name of the field
* @param defaultValue the default value to return should a exception occur
* @return the actual value of the field or the given default value
*/
public static Object getConfigOrDefault(String fieldName, Object defaultValue) {
try {
Class<?> c = Class.forName("h05.Config");
return c.getDeclaredField(fieldName).get(null);
} catch (Exception e) {
return defaultValue;
}
}
private static class Updater {
private static final String REPOSITORY_URL = "https://git.rwth-aachen.de/aud-tests/AuD-2021-" + ASSIGNMENT_ID + "-Student/-/";
......@@ -223,7 +231,7 @@ public class Utils {
) {
String fileName = line.split(" +")[0], expectedHash = line.split(" +")[1];
if (EXCLUDED_FILES.contains(fileName))
if (((List<?>) getConfigOrDefault("EXCLUDED_FILES", List.of())).contains(fileName))
continue;
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
......
Supports Markdown
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