JML

org.multijava.util.testing
Class Utils

java.lang.Object
  extended byorg.multijava.util.testing.Utils

public class Utils
extends Object

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


Nested Class Summary
static class Utils.QuoteTokenizer
           
 
Field Summary
(package private) static String ORACLE_SUFFIX
           
private static PrintStream pse
          A cached value of the usual System err stream.
private static PrintStream pso
          A cached value of the usual System out stream.
(package private) static String SAVED_SUFFIX
           
 
Constructor Summary
Utils()
           
 
Method Summary
static Diff compareStringToFile(String s, String rootname)
          Compares the given string to the content of the given file using a comparator that ignores platform differences in line-endings.
static String executeCompile(Class cls, String[] args)
           
static String executeMethod(Class cls, String methodname, 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 String executeMethod(Method method, String[] args)
          Calls the given method on the given String[] argument.
static ArrayList parseFoundLine(String content, String filename)
          Finds the first line with the given String in the given file and parses the content into tokens.
static ArrayList parseResult(String command)
          Executes the given command as an external executable, reads the text produced and tokenizes it into Strings (separated by whitespace).
static String readFile(String filename)
          Reads the contents of the file with the given name, returning a String.
static String readFile(String filename, byte[] cb)
          Reads the contents of the file with the given name, returning a String.
static String readFileX(String filename)
           
static boolean recursivelyRemoveDirectory(File d, boolean removeDirectoryItself)
          Deletes the contents of a directory, including subdirectories.
static void removeFiles(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 setStreams(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

pso

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


pse

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


ORACLE_SUFFIX

static final String ORACLE_SUFFIX

SAVED_SUFFIX

static final String SAVED_SUFFIX
Constructor Detail

Utils

public Utils()
Method Detail

setStreams

public static void setStreams(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.


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.


parseResult

public static ArrayList parseResult(String command)
                             throws IOException
Executes the given command as an external executable, reads the text produced and tokenizes it into Strings (separated by whitespace). This differs from ExternalInputIterator that simply returns lines.

Throws:
IOException

parseFoundLine

public static ArrayList parseFoundLine(String content,
                                       String filename)
                                throws IOException
Finds the first line with the given String in the given file and parses the content into tokens. Returns an empty array if the String is not present in the file.

Throws:
IOException

recursivelyRemoveDirectory

public static boolean recursivelyRemoveDirectory(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.


readFile

public static String readFile(String filename,
                              byte[] cb)
                       throws 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.

Throws:
IOException

readFileX

public static String readFileX(String filename)

readFile

public static String readFile(String filename)
                       throws 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.

Throws:
IOException

executeCompile

public static String executeCompile(Class cls,
                                    String[] args)

executeMethod

public static String executeMethod(Class cls,
                                   String methodname,
                                   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.


executeMethod

public static String executeMethod(Method method,
                                   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.


compareStringToFile

public static Diff compareStringToFile(String s,
                                       String rootname)
                                throws 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.

Throws:
IOException

removeFiles

public static void removeFiles(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.


JML

JML is Copyright (C) 1998-2002 by Iowa State University and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This release depends on code from the MultiJava project and is based in part on the Kopi project Copyright (C) 1990-99 DMS Decision Management Systems Ges.m.b.H.