Skip to content

Commit

Permalink
Add support for build parameters (example: EXTENSIVE_TESTING).
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
asofold committed Feb 26, 2013
1 parent 4958913 commit 33af656
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 2 deletions.
41 changes: 41 additions & 0 deletions NCPCommons/pom.xml
Expand Up @@ -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>
@@ -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);

}
@@ -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());
}
}
}
}
4 changes: 4 additions & 0 deletions 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@
Expand Up @@ -9,6 +9,7 @@

import fr.neatmonster.nocheatplus.utilities.RayTracing;
import fr.neatmonster.nocheatplus.utilities.StringUtil;
import fr.neatmonster.nocheatplus.utilities.build.BuildParameters;

public class TestRayTracing {

Expand Down Expand Up @@ -219,13 +220,16 @@ public void testConsistency(){
}){
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));
}

Expand Down
8 changes: 8 additions & 0 deletions NCPPlugin/pom.xml
Expand Up @@ -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>
Expand Down

0 comments on commit 33af656

Please sign in to comment.