From 33af656b29cbab4f83fc7de26ef19ec607a8a4e2 Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 26 Feb 2013 23:14:33 +0100 Subject: [PATCH] Add support for build parameters (example: EXTENSIVE_TESTING). We can put through build parameters for testing and other purposes so NCP can see them during testing or runtime otherwise. Not sure this is the final version, though. --- NCPCommons/pom.xml | 41 +++++++++ .../utilities/build/BuildParameters.java | 53 +++++++++++ .../utilities/build/ResourceUtil.java | 87 +++++++++++++++++++ .../resources/_BuildParameters.properties | 4 + .../nocheatplus/test/TestRayTracing.java | 8 +- NCPPlugin/pom.xml | 8 ++ 6 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/BuildParameters.java create mode 100644 NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/ResourceUtil.java create mode 100644 NCPCommons/src/main/resources/_BuildParameters.properties diff --git a/NCPCommons/pom.xml b/NCPCommons/pom.xml index 0f1cd3bc..a8cfc883 100644 --- a/NCPCommons/pom.xml +++ b/NCPCommons/pom.xml @@ -19,8 +19,49 @@ junit 4.8.2 + + com.google.code.maven-replacer-plugin + maven-replacer-plugin + 1.4.1 + maven-plugin + Common data structures and other, no use of Bukkit. Version updating is done for NCPPlugin mainly, expect the other poms version to change randomly rather. + + + + + com.google.code.maven-replacer-plugin + maven-replacer-plugin + 1.4.1 + + + validate + + replace + + + + + true + src/main/resources/_BuildParameters.properties + src/main/resources/BuildParameters.properties + false + + + @GENERATED_NOTE@ + !!! THIS FILE IS AUTO GENERATED, ANY MANUAL CHANGES TO IT WILL GET LOST !!! + + + @EXTENSIVE_TESTING@ + ${EXTENSIVE_TESTING} + + + + + + + \ No newline at end of file diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/BuildParameters.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/BuildParameters.java new file mode 100644 index 00000000..f761e613 --- /dev/null +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/BuildParameters.java @@ -0,0 +1,53 @@ +package fr.neatmonster.nocheatplus.utilities.build; + +import java.util.HashMap; +import java.util.Map; + +/** + * Experimental support for build parameters. + * @author mc_dev + * + */ +public class BuildParameters { + + private static final Map fileContents = new HashMap(); + + static{ + // Fetch file content from resources. + String content = null; + try{ + content = ResourceUtil.fetchResource(BuildParameters.class, "BuildParameters.properties"); + } + catch(Throwable t){ + t.printStackTrace(); + } + // Parse properties. + if (content != null){ + ResourceUtil.parseToMap(content, fileContents); + } + } + + ////////////////////// + // Auxiliary methods. + ///////////////////// + + public static String getString(String path, String preset){ + String input = fileContents.get(path); + if (input == null) return preset; + else return input; + } + + public static Boolean getBoolean(String path, Boolean preset){ + String input = fileContents.get(path); + if (input == null) return preset; + else return ResourceUtil.getBoolean(input, preset); + } + + ////////////////////// + // Public members. + ////////////////////// + + /** Extend testing amount. */ + public static final boolean extensiveTesting = getBoolean("EXTENSIVE_TESTING", false); + +} diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/ResourceUtil.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/ResourceUtil.java new file mode 100644 index 00000000..8be71b47 --- /dev/null +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/ResourceUtil.java @@ -0,0 +1,87 @@ +package fr.neatmonster.nocheatplus.utilities.build; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; + +public class ResourceUtil { + + public static Boolean getBoolean(String input, Boolean preset){ + if (input == null) return preset; + input = input.trim().toLowerCase(); + if (input.matches("1|true|yes")) return true; + else if (input.matches("0|false|no")) return false; + else return preset; + } + + /** + * Might have a newline at the end.
+ * TODO: Move to other utility. + * + * @param name + * @param clazz + * @param folderPart + * @return + */ + public static String fetchResource(Class clazz, String path) { + String className = clazz.getSimpleName() + ".class"; + String classPath = clazz.getResource(className).toString(); + if (!classPath.startsWith("jar")) return null; + String absPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/"+path; + try { + URL url = new URL(absPath); + BufferedReader r = null; + try { + Object obj = url.getContent(); + if (obj instanceof InputStream){ + r = new BufferedReader(new InputStreamReader((InputStream) obj)); + StringBuilder builder = new StringBuilder(); + String last = r.readLine(); + while (last != null){ + builder.append(last); + builder.append("\n"); // does not hurt if one too many. + last = r.readLine(); + } + r.close(); + return builder.toString(); + } + else return null; + } catch (IOException e) { + if (r != null){ + try { + r.close(); + } catch (IOException e1) { + } + } + return null; + } + } catch (MalformedURLException e) { + } + return null; + } + + /** + * New line separated entries, lines starting with '#' are ignored (trim + check), otherwise ini-file style x=y.
+ * All keys and values are trimmed, lines without assignment still get added, all mappings will be the empty string or some content. + * @param input + * @param map + */ + public static void parseToMap(String input, Map map){ + final String[] split = input.split("\n"); + for (final String line : split){ + final String trimmed = line.trim(); + if (trimmed.startsWith("#")) continue; + final String[] parts = line.split("=", 2); + if (parts.length == 1){ + map.put(parts[0].trim(), ""); + } + else{ + map.put(parts[0].trim(), parts[1].trim()); + } + } + } +} diff --git a/NCPCommons/src/main/resources/_BuildParameters.properties b/NCPCommons/src/main/resources/_BuildParameters.properties new file mode 100644 index 00000000..999b7df4 --- /dev/null +++ b/NCPCommons/src/main/resources/_BuildParameters.properties @@ -0,0 +1,4 @@ +# @GENERATED_NOTE@ +# These parameters are filled in during building (maven), they are not strictly needed. +# Replacement mappings are defined in the pom.xml. +EXTENSIVE_TESTING=@EXTENSIVE_TESTING@ \ No newline at end of file diff --git a/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java b/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java index 3b633a08..bb49d86d 100644 --- a/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java +++ b/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java @@ -9,6 +9,7 @@ import org.junit.Test; import fr.neatmonster.nocheatplus.utilities.RayTracing; import fr.neatmonster.nocheatplus.utilities.StringUtil; +import fr.neatmonster.nocheatplus.utilities.build.BuildParameters; public class TestRayTracing { @@ -219,13 +220,16 @@ public class TestRayTracing { }){ checkConsistency(coords); } + + int f = BuildParameters.extensiveTesting ? 100 : 1; + // Random tests. - for (int i = 0; i < 100000; i++){ + for (int i = 0; i < 100000 * f; i++){ checkConsistency(randomCoords(10.0)); } // TODO: make these work. - for (int i = 0; i < 1000; i++){ + for (int i = 0; i < 1000 * f * f; i++){ checkConsistency(randomBlockCoords(6)); } diff --git a/NCPPlugin/pom.xml b/NCPPlugin/pom.xml index 6a8d8b3d..cd77ac5f 100644 --- a/NCPPlugin/pom.xml +++ b/NCPPlugin/pom.xml @@ -78,6 +78,14 @@ LICENSE.txt + + . + true + ../NCPCommons/src/main/resources + + BuildParameters.properties + +