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.
This commit is contained in:
asofold 2013-02-26 23:14:33 +01:00
parent 4958913d1e
commit 33af656b29
6 changed files with 199 additions and 2 deletions

View File

@ -19,8 +19,49 @@
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.1</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<description>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.</description>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>true</ignoreMissingFile>
<file>src/main/resources/_BuildParameters.properties</file>
<outputFile>src/main/resources/BuildParameters.properties</outputFile>
<regex>false</regex>
<replacements>
<replacement>
<token>@GENERATED_NOTE@</token>
<value>!!! THIS FILE IS AUTO GENERATED, ANY MANUAL CHANGES TO IT WILL GET LOST !!!</value>
</replacement>
<replacement>
<token>@EXTENSIVE_TESTING@</token>
<value>${EXTENSIVE_TESTING}</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<String, String> fileContents = new HashMap<String, String>();
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);
}

View File

@ -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.<br>
* 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.<br>
* 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<String, String> 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());
}
}
}
}

View File

@ -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@

View File

@ -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));
}

View File

@ -78,6 +78,14 @@
<include>LICENSE.txt</include>
</includes>
</resource>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>../NCPCommons/src/main/resources</directory>
<includes>
<include>BuildParameters.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>