ESC/Java2
© 2003,2004,2005 David Cok and Joseph Kiniry
© 2005 UCD Dublin
© 2003,2004 Radboud University Nijmegen
© 1999,2000 Compaq Computer Corporation
© 1997,1998,1999 Digital Equipment Corporation
All Rights Reserved

junitutils
Class Utils

java.lang.Object
  extended byjunitutils.Utils

public class Utils
extends java.lang.Object

This class contains miscellaneous (static) utility functions that are useful in writing JUnit functional tests.


Nested Class Summary
static class Utils.QuoteTokenizer
          An enumerator that parses a string into tokens, according to the rules a command-line would use.
 
Field Summary
static boolean disable
          Setting this field to true disables the capturing of the output; one would do this only for debugging purposes.
private static java.lang.String ORACLE_SUFFIX
          The suffix to append to create the golden output filename
private static java.io.PrintStream pse
          A cached value of the usual System err stream.
private static java.io.PrintStream pso
          A cached value of the usual System out stream.
private static java.lang.String SAVED_SUFFIX
          The suffix to append to create the actual output filename
 
Constructor Summary
Utils()
           
 
Method Summary
static Diff compareStringToFile(java.lang.String s, java.lang.String rootname)
          Compares the given string to the content of the given file using a comparator that ignores platform differences in line-endings.
static java.lang.String executeCompile(java.lang.Class cls, java.lang.String[] args)
          Executes the static compile(String[]) method of the given class
static java.lang.String executeMethod(java.lang.Class cls, java.lang.String methodname, java.lang.String[] args)
          Finds and executes the method with the given name in the given class; the method must have a single argument of type String[].
static java.lang.String executeMethod(java.lang.reflect.Method method, java.lang.String[] args)
          Calls the given method on the given String[] argument.
static java.lang.String[] parseLine(java.lang.String s)
          Parses a string into arguments as if it were a command-line, using the QuoteTokenizer to parse the tokens.
static java.lang.String readFile(java.lang.String filename)
          Reads the contents of the file with the given name, returning a String.
static java.lang.String readFile(java.lang.String filename, byte[] cb)
          Reads the contents of the file with the given name, returning a String.
static java.lang.String readFileX(java.lang.String filename)
          Reads a file, returning a String containing the contents
static boolean recursivelyRemoveDirectory(java.io.File d, boolean removeDirectoryItself)
          Deletes the contents of a directory, including subdirectories.
static void removeFiles(java.lang.String pattern)
          This deletes all files (in the current directory) whose names match the given pattern in a regular-expression sense; however, it is only implemented for patterns consisting of characters and at most one '*', since I'm not going to rewrite an RE library.
static void restoreStreams()
          Restores System.out and System.err to the initial, system-defined values.
static void restoreStreams(boolean close)
          Restores System.out and System.err to the initial, system-defined values.
static java.io.ByteArrayOutputStream setStreams()
          Creates a new output stream (which is returned) and makes it the stream into which the standard and error outputs are captured.
static void setStreams(java.io.PrintStream ps)
          Redirects System.out and System.err to the given PrintStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

disable

public static boolean disable
Setting this field to true disables the capturing of the output; one would do this only for debugging purposes.


pso

private static final java.io.PrintStream pso
A cached value of the usual System out stream.


pse

private static final java.io.PrintStream pse
A cached value of the usual System err stream.


ORACLE_SUFFIX

private static final java.lang.String ORACLE_SUFFIX
The suffix to append to create the golden output filename

See Also:
Constant Field Values

SAVED_SUFFIX

private static final java.lang.String SAVED_SUFFIX
The suffix to append to create the actual output filename

See Also:
Constant Field Values
Constructor Detail

Utils

public Utils()
Method Detail

setStreams

public static void setStreams(java.io.PrintStream ps)
Redirects System.out and System.err to the given PrintStream. Note that setStreams/restoreStreams operate on the global values of System.out and System.err; these implementations are not synchronized - you will need to take care of any race conditions if you utilize these in more than one thread.

Parameters:
ps - The stream that is the new output and error stream

setStreams

public static java.io.ByteArrayOutputStream setStreams()
Creates a new output stream (which is returned) and makes it the stream into which the standard and error outputs are captured.

Returns:
an output stream into which standard and error output is captured

restoreStreams

public static void restoreStreams()
Restores System.out and System.err to the initial, system-defined values. It is ok to call this method even if setStreams has not been called.

Note that setStreams/restoreStreams operate on the global values of System.out and System.err; these implementations are not synchronized - you will need to take care of any race conditions if you utilize these in more than one thread.


restoreStreams

public static void restoreStreams(boolean close)
Restores System.out and System.err to the initial, system-defined values. It is ok to call this method even if setStreams has not been called.

Note that setStreams/restoreStreams operate on the global values of System.out and System.err; these implementations are not synchronized - you will need to take care of any race conditions if you utilize these in more than one thread.

Parameters:
close - if true, the current output and error streams are closed before being reset (if they are not currently the System output and error streams)

parseLine

public static java.lang.String[] parseLine(java.lang.String s)
Parses a string into arguments as if it were a command-line, using the QuoteTokenizer to parse the tokens.

Parameters:
s - The String to parse
Returns:
The input string parsed into command-line arguments

recursivelyRemoveDirectory

public static boolean recursivelyRemoveDirectory(java.io.File d,
                                                 boolean removeDirectoryItself)
Deletes the contents of a directory, including subdirectories. If the second argument is true, the directory itself is deleted as well.

Parameters:
d - The directory whose contents are deleted
removeDirectoryItself - if true, the directory itself is deleted
Returns:
false if something could not be deleted; true if everything was successfully deleted

readFile

public static java.lang.String readFile(java.lang.String filename,
                                        byte[] cb)
                                 throws java.io.IOException
Reads the contents of the file with the given name, returning a String. This is an optimized version that uses the byte array provided and presumes that the file is shorter than the length of the array.

Parameters:
filename - The file to be read
cb - A temporary byte array to speed reading
Returns:
The contents of the file
Throws:
java.io.IOException

readFileX

public static java.lang.String readFileX(java.lang.String filename)
Reads a file, returning a String containing the contents

Parameters:
filename - the file to be read
Returns:
the contents of the file as a String, or null if the file could not be read

readFile

public static java.lang.String readFile(java.lang.String filename)
                                 throws java.io.IOException
Reads the contents of the file with the given name, returning a String. This version presumes the file is shorter than an internal buffer. FIXME

Parameters:
filename - The file to be read
Returns:
The contents of the file
Throws:
java.io.IOException

executeCompile

public static java.lang.String executeCompile(java.lang.Class cls,
                                              java.lang.String[] args)
Executes the static compile(String[]) method of the given class

Parameters:
cls - The class whose 'compile' method is be invoked
args - The String[] argument of the method
Returns:
The standard output and error output of the invocation

executeMethod

public static java.lang.String executeMethod(java.lang.Class cls,
                                             java.lang.String methodname,
                                             java.lang.String[] args)
Finds and executes the method with the given name in the given class; the method must have a single argument of type String[]. The 'args' parameter is supplied to it as its argument.

Parameters:
cls - The class whose method is to be invoked
methodname - The method to be invoked
args - The argument of the method
Returns:
The standard output and error output of the invocation

executeMethod

public static java.lang.String executeMethod(java.lang.reflect.Method method,
                                             java.lang.String[] args)
Calls the given method on the given String[] argument. Any standard output and error output is collected and returned as the String return value.

Parameters:
method - The static method to be invoked
args - The argument of the method
Returns:
The standard output and error output of the method

compareStringToFile

public static Diff compareStringToFile(java.lang.String s,
                                       java.lang.String rootname)
                                throws java.io.IOException
Compares the given string to the content of the given file using a comparator that ignores platform differences in line-endings. The method has the side effect of saving the string value in a file for later comparison if the string and file are different, and making sure that the actual output file (the -ckd file) is deleted if the string and file are the same. The expected output filename is the rootname + "-expected"; the actual output filename is the rootname + "-ckd".

Parameters:
s - the String to compare
rootname - the prefix of the file name
Returns:
The Diff structure that contains the comparison
Throws:
java.io.IOException

removeFiles

public static void removeFiles(java.lang.String pattern)
This deletes all files (in the current directory) whose names match the given pattern in a regular-expression sense; however, it is only implemented for patterns consisting of characters and at most one '*', since I'm not going to rewrite an RE library.

Parameters:
pattern - the pattern to match against filenames

ESC/Java2
© 2003,2004,2005 David Cok and Joseph Kiniry
© 2005 UCD Dublin
© 2003,2004 Radboud University Nijmegen
© 1999,2000 Compaq Computer Corporation
© 1997,1998,1999 Digital Equipment Corporation
All Rights Reserved

The ESC/Java2 Project Homepage