Commit 1aa0d2c4 authored by Lubomir Bulej's avatar Lubomir Bulej

src-util: created to collect utility classes to be shared between parts of the code base.

src-test: moved various methods into classes in src-util and updated the test util classes accordingly.
build.xml, build.properties, .classpath: added support fo the src-util directory.
parent 716a5da8
......@@ -5,6 +5,7 @@
<classpathentry kind="src" path="src-re-server"/>
<classpathentry kind="src" path="src-agent-java"/>
<classpathentry kind="src" path="src-test"/>
<classpathentry kind="src" path="src-util"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="build/eclipse-dynamicbypass.jar"/>
<classpathentry kind="lib" path="lib/asm-debug-all-4.1.jar"/>
......
......@@ -19,6 +19,7 @@ src.reserver=src-re-server
src.redispatch=src-re-disp
src.reagent=src-re-agent
src.test=src-test
src.util=src-util
bin=bin
build=build
......
......@@ -78,7 +78,13 @@
<exec executable="make" dir="${src.reagent}" />
</target>
<target name="compile-test" depends="compile-disl,compile-reserver,compile-redispatch">
<target name="compile-util">
<javac srcdir="${src.util}" destdir="bin" debug="true" includeAntRuntime="false">
<classpath refid="buildpath" />
</javac>
</target>
<target name="compile-test" depends="compile-util,compile-disl,compile-reserver,compile-redispatch">
<javac srcdir="${src.test}" destdir="bin" debug="true" includeAntRuntime="false">
<classpath refid="buildpath" />
</javac>
......
......@@ -8,6 +8,8 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import ch.usi.dag.util.Lists;
public class ClientServerEvaluationRunner extends Runner {
......@@ -42,35 +44,35 @@ public class ClientServerEvaluationRunner extends Runner {
}
private Job __startServer (final File testInstrJar) throws IOException {
final List <String> serverCommand = newLinkedList (
final List <String> command = Lists.newLinkedList (
_JAVA_COMMAND_,
"-cp", makeClassPath (testInstrJar, _DISL_SERVER_JAR_)
"-cp", Runner.classPath (testInstrJar, _DISL_SERVER_JAR_)
);
serverCommand.addAll (propertiesStartingWith ("dislserver."));
serverCommand.addAll (propertiesStartingWith ("disl."));
serverCommand.add (_DISL_SERVER_MAIN_CLASS_.getName ());
command.addAll (propertiesStartingWith ("dislserver."));
command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_MAIN_CLASS_.getName ());
//
final Job result = new Job (serverCommand);
final Job result = new Job (command);
result.start ();
return result;
}
private Job __startShadow (final File testInstrJar) throws IOException {
final List <String> shadowCommand = newLinkedList (
final List <String> command = Lists.newLinkedList (
_JAVA_COMMAND_, "-Xms1G", "-Xmx2G",
"-cp", makeClassPath (testInstrJar, _DISL_RE_SERVER_JAR_)
"-cp", Runner.classPath (testInstrJar, _DISL_RE_SERVER_JAR_)
);
shadowCommand.addAll (propertiesStartingWith ("dislreserver."));
shadowCommand.add (_DISL_RE_SERVER_MAIN_CLASS_.getName ());
command.addAll (propertiesStartingWith ("dislreserver."));
command.add (_DISL_RE_SERVER_MAIN_CLASS_.getName ());
//
final Job result = new Job (shadowCommand);
final Job result = new Job (command);
result.start ();
return result;
}
......@@ -79,24 +81,24 @@ public class ClientServerEvaluationRunner extends Runner {
private Job __startClient (
final File testInstrJar, final File testAppJar
) throws IOException {
final List <String> clientCommand = newLinkedList (
final List <String> command = Lists.newLinkedList (
_JAVA_COMMAND_,
String.format ("-agentpath:%s", _DISL_AGENT_LIB_),
String.format ("-agentpath:%s", _DISL_RE_AGENT_LIB_),
String.format ("-javaagent:%s", _DISL_AGENT_JAR_),
String.format ("-Xbootclasspath/a:%s", makeClassPath (
String.format ("-Xbootclasspath/a:%s", Runner.classPath (
_DISL_AGENT_JAR_, testInstrJar, _DISL_RE_DISPATCH_JAR_
))
);
clientCommand.addAll (propertiesStartingWith ("disl."));
clientCommand.addAll (Arrays.asList (
command.addAll (propertiesStartingWith ("disl."));
command.addAll (Arrays.asList (
"-jar", testAppJar.toString ()
));
//
final Job result = new Job (clientCommand);
final Job result = new Job (command);
result.start ();
return result;
}
......@@ -167,7 +169,7 @@ public class ClientServerEvaluationRunner extends Runner {
clientOutNull = false;
assertEquals (
"client out does not match",
_readResource (fileName), __client.getOutput ()
_loadResource (fileName), __client.getOutput ()
);
}
......@@ -182,7 +184,7 @@ public class ClientServerEvaluationRunner extends Runner {
clientErrNull = false;
assertEquals (
"client err does not match",
_readResource (fileName), __client.getError ()
_loadResource (fileName), __client.getError ()
);
}
......@@ -197,7 +199,7 @@ public class ClientServerEvaluationRunner extends Runner {
shadowOutNull = false;
assertEquals (
"shadow out does not match",
_readResource (fileName), __shadow.getOutput ()
_loadResource (fileName), __shadow.getOutput ()
);
}
......@@ -212,7 +214,7 @@ public class ClientServerEvaluationRunner extends Runner {
shadowErrNull = false;
assertEquals (
"shadow err does not match",
_readResource (fileName), __shadow.getError ()
_loadResource (fileName), __shadow.getError ()
);
}
......@@ -227,7 +229,7 @@ public class ClientServerEvaluationRunner extends Runner {
serverOutNull = false;
assertEquals (
"server out does not match",
_readResource (fileName), __server.getOutput ()
_loadResource (fileName), __server.getOutput ()
);
}
......@@ -243,7 +245,7 @@ public class ClientServerEvaluationRunner extends Runner {
serverErrNull = false;
assertEquals (
"server err does not match",
_readResource (fileName), __server.getError ()
_loadResource (fileName), __server.getError ()
);
}
......
......@@ -8,6 +8,8 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import ch.usi.dag.util.Lists;
public class ClientServerRunner extends Runner {
private Job __client;
......@@ -55,42 +57,42 @@ public class ClientServerRunner extends Runner {
private Job __startClient (
final File testInstrJar, final File testAppJar
) throws IOException {
final List <String> clientCommand = newLinkedList (
final List <String> command = Lists.newLinkedList (
_JAVA_COMMAND_,
String.format ("-agentpath:%s", _DISL_AGENT_LIB_),
String.format ("-javaagent:%s", _DISL_AGENT_JAR_),
String.format (
"-Xbootclasspath/a:%s",
makeClassPath (_DISL_AGENT_JAR_, testInstrJar)
Runner.classPath (_DISL_AGENT_JAR_, testInstrJar)
)
);
clientCommand.addAll (propertiesStartingWith ("disl."));
clientCommand.addAll (Arrays.asList (
command.addAll (propertiesStartingWith ("disl."));
command.addAll (Arrays.asList (
"-jar", testAppJar.toString ()
));
//
final Job result = new Job (clientCommand);
final Job result = new Job (command);
result.start ();
return result;
}
private Job __startServer (final File testInstrJar) throws IOException {
final List <String> serverCommand = newLinkedList (
final List <String> command = Lists.newLinkedList (
_JAVA_COMMAND_,
"-cp", makeClassPath (testInstrJar, _DISL_SERVER_JAR_)
"-cp", Runner.classPath (testInstrJar, _DISL_SERVER_JAR_)
);
serverCommand.addAll (propertiesStartingWith ("dislserver."));
serverCommand.addAll (propertiesStartingWith ("disl."));
serverCommand.add (_DISL_SERVER_MAIN_CLASS_.getName ());
command.addAll (propertiesStartingWith ("dislserver."));
command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_MAIN_CLASS_.getName ());
//
final Job result = new Job (serverCommand);
final Job result = new Job (command);
result.start ();
return result;
}
......@@ -133,7 +135,7 @@ public class ClientServerRunner extends Runner {
clientOutNull = false;
assertEquals (
"client out does not match",
_readResource (fileName), __client.getOutput ()
_loadResource (fileName), __client.getOutput ()
);
}
......@@ -148,7 +150,7 @@ public class ClientServerRunner extends Runner {
clientErrNull = false;
assertEquals (
"client err does not match",
_readResource (fileName), __client.getError ()
_loadResource (fileName), __client.getError ()
);
}
......@@ -163,7 +165,7 @@ public class ClientServerRunner extends Runner {
serverOutNull = false;
assertEquals (
"server out does not match",
_readResource (fileName), __server.getOutput ()
_loadResource (fileName), __server.getOutput ()
);
}
......@@ -178,7 +180,7 @@ public class ClientServerRunner extends Runner {
serverErrNull = false;
assertEquals (
"server err does not match",
_readResource (fileName), __server.getError ()
_loadResource (fileName), __server.getError ()
);
}
......
......@@ -3,51 +3,64 @@ package ch.usi.dag.disl.test.utils;
import java.util.concurrent.TimeUnit;
/**
*
* @author Lubomir Bulej
*/
public final class Duration {
private final long __value;
private final long __amount;
private final TimeUnit __unit;
//
private Duration (final long value, final TimeUnit unit) {
__value = value;
private Duration (final long amount, final TimeUnit unit) {
__amount = amount;
__unit = unit;
}
//
public final long value () {
return __value;
}
public final TimeUnit unit () {
return __unit;
}
public final long to (final TimeUnit targetUnit) {
return targetUnit.convert (__value, __unit);
/**
* Converts this {@link Duration} to given time units.
*
* @param unit
* the time unit to convert this {@link Duration} to.
* @return the amount of the given time units representing this
* {@link Duration}.
*/
public long to (final TimeUnit unit) {
return unit.convert (__amount, __unit);
}
//
public final void timedWait (final Object object) throws InterruptedException {
__unit.timedWait (object, __value);
/**
* @see TimeUnit#timedWait(Object, long)
*/
public void timedWait (final Object object) throws InterruptedException {
__unit.timedWait (object, __amount);
}
public final void timedJoin (final Thread thread) throws InterruptedException {
__unit.timedJoin (thread, __value);
/**
* @see TimeUnit#timedJoin(Thread, long)
*/
public void timedJoin (final Thread thread) throws InterruptedException {
__unit.timedJoin (thread, __amount);
}
//
public final void sleep () throws InterruptedException {
__unit.sleep (__value);
/**
* @see TimeUnit#sleep(long)
*/
public void sleep () throws InterruptedException {
__unit.sleep (__amount);
}
public final void softSleep () {
public void softSleep () {
try {
__unit.sleep (__value);
__unit.sleep (__amount);
} catch (final InterruptedException ie) {
// return if interrupted, keeping the interrupted() status
......@@ -56,8 +69,18 @@ public final class Duration {
//
public static Duration of (final long duration, final TimeUnit timeUnit) {
return new Duration (duration, timeUnit);
/**
* Creates a {@link Duration} instance representing a given amount of given
* time units.
*
* @param amount
* the amount of time units
* @param unit
* the time unit representing the granularity of the duration
* @return
*/
public static Duration of (final long amount, final TimeUnit unit) {
return new Duration (amount, unit);
}
}
......@@ -2,18 +2,15 @@ package ch.usi.dag.disl.test.utils;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import ch.usi.dag.dislreserver.DiSLREServer;
import ch.usi.dag.dislserver.DiSLServer;
import ch.usi.dag.util.Files;
import ch.usi.dag.util.Strings;
public abstract class Runner {
......@@ -53,8 +50,9 @@ public abstract class Runner {
private static String __getJavaCommand () {
final String javaHome = System.getenv (_ENV_JAVA_HOME_);
if (javaHome != null) {
final File jreBinDir = new File (new File (javaHome, "jre"), "bin");
return new File (jreBinDir, "java").toString ();
return new File (
new File (new File (javaHome, "jre"), "bin"), "java"
).toString ();
} else {
return "java";
}
......@@ -94,73 +92,31 @@ public abstract class Runner {
//
static void writeString (
final String content, final String fileName
) throws FileNotFoundException {
final PrintWriter out = new PrintWriter (fileName);
try {
out.print (content);
} finally {
out.close ();
}
}
static <E> List <E> newLinkedList (final E ... elements) {
return new LinkedList <E> (Arrays.asList (elements));
protected String _loadResource (final String name) throws IOException {
return Files.loadStringResource (__testClass, name);
}
static String makeClassPath (final File ... paths) {
final StringBuilder builder = new StringBuilder ();
String effectiveSeparator = "";
for (final File path : paths) {
builder.append (effectiveSeparator);
builder.append (path);
effectiveSeparator = File.pathSeparator;
protected void _destroyIfRunningAndDumpOutputs (
final Job job, final String prefix
) throws IOException {
if (job.isRunning ()) {
job.destroy ();
}
return builder.toString ();
}
Files.storeString (
String.format ("tmp.%s.%s.out.txt", __testName, prefix),
job.getOutput ()
);
protected String _testName () {
return __testName;
Files.storeString (
String.format ("tmp.%s.%s.err.txt", __testName, prefix),
job.getError ()
);
}
//
protected String _readResource (final String fileName) throws IOException {
return __readResource (__testClass, fileName);
}
private static String __readResource (
final Class <?> refClass, final String fileName
) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader (new InputStreamReader (
refClass.getResourceAsStream (fileName), "UTF-8"
));
final StringBuffer buffer = new StringBuffer ();
for (int c = reader.read (); c != -1; c = reader.read ()) {
buffer.append ((char) c);
}
return buffer.toString ();
} finally {
if(reader != null) {
reader.close ();
}
}
}
static List <String> propertiesStartingWith (final String prefix) {
final List <String> result = new LinkedList <String> ();
......@@ -180,22 +136,8 @@ public abstract class Runner {
}
protected void _destroyIfRunningAndDumpOutputs (
final Job job, final String prefix
) throws IOException {
if (job.isRunning ()) {
job.destroy ();
}
Runner.writeString (
job.getOutput (),
String.format ("tmp.%s.%s.out.txt", __testName, prefix)
);
Runner.writeString (
job.getError (),
String.format ("tmp.%s.%s.err.txt", __testName, prefix)
);
static String classPath (final File ... paths) {
return Strings.join (File.pathSeparator, (Object []) paths);
}
}
package ch.usi.dag.util;
import java.lang.reflect.Array;
import java.util.Collection;
/**
* Utility class providing static methods for common assertions on objects from
* the Java runtime, i.e. the java.* packages. These methods are intended for
* use at internal interface boundaries to document and check contract
* assumptions, but not enforce them. For enforcement, use methods from the
* {@link Require} class.
*
* @author Lubomir Bulej
*/
public final class Assert {
/**
* Represents the state of runtime assertions.
*/
public static final boolean ENABLED = __assertionsEnabled ();
private static boolean __assertionsEnabled () {
//
// Determine the state of runtime assertions. Try to force an
// assertion error and if it succeeds, we know that assertions are
// enabled.
//
try {
assert false;
return false;
} catch (final AssertionError ae) {
return true;
}
}
//
private Assert () {
// pure static class - not to be instantiated
}
/* ***********************************************************************
* Integers
* ***********************************************************************/
public static void valueNotNegative (final long value, final String name) {
assert value >= 0 : name +" is negative ("+ value +")";
}
public static void valueIsPositive (final long value, final String name) {
assert value > 0 : name +" is not positive ("+ value +")";
}
/* ***********************************************************************
* Generic Objects
* ***********************************************************************/
public static void objectNotNull (final Object object, final String name) {
assert object != null : name + " is null";
}
public static void objectIsArray (final Object object, final String name) {
Assert.objectNotNull (object, name);
assert object.getClass ().isArray () : name +" is not an array";
}
/* ***********************************************************************
* Arrays
* ***********************************************************************/
public static void arrayNotEmpty (final int [] array, final String name) {
Assert.objectNotNull (array, name);
__arrayNotEmpty (array.length, name);
}
public static void arrayNotEmpty (final Object array, final String name) {
Assert.objectIsArray (array, name);
__arrayNotEmpty (Array.getLength (array), name);
}
private static void __arrayNotEmpty (final int length, final String name) {
assert length > 0 : name +" array is empty";
}
/* ***********************************************************************
* Collections
* ***********************************************************************/
public static void collectionNotEmpty (
final Collection <?> collection, final String name
) {
Assert.objectNotNull (collection, name);
assert collection.size () > 0 : name +" collection is empty";
}
/* ***********************************************************************
* Strings
* ***********************************************************************/
public static void stringNotEmpty (final String string, final String name) {
Assert.objectNotNull (string, name);
assert !string.isEmpty() : name + " is empty";
}
/* ***********************************************************************
* Locations
* ***********************************************************************/
public static void unreachable () {
throw new AssertionError ("unreachable code reached");
}
public static void unreachable (final String message) {
throw new AssertionError (message);
}
}
package ch.usi.dag.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
/**
* Utility class providing miscellaneous file- and path-related operations.
*
* @author Lubomir Bulej
*/
public final class Files {
private Files () {
// pure static class - not to be instantiated
}
//
/**
* Stores a string into a file.
*
* @param fileName
* the name of the file to write.
* @param content
* the string to write.
* @throws FileNotFoundException
* if the given file could not be created
* @throws IOException
* if an error occurs while writing to the file
*/
public static void storeString (
final String fileName, final String content
) throws FileNotFoundException {
__storeStringAndClose (new PrintWriter (fileName), content);
}
/**
* Stores a string into a file.
*
* @param fileName
* the name of the file to write.
* @param content
* the string to write.
* @throws FileNotFoundException
* if the given file could not be created
* @throws IOException
* if an error occurs while writing to the file
*/
public static void storeString (
final File fileName, final String content
) throws FileNotFoundException {
__storeStringAndClose (new PrintWriter (fileName), content);
}
private static void __storeStringAndClose (
final PrintWriter output, final String content
) {
try {
output.print (content);
}