mjc

org.multijava.util.testing
Class Utils

java.lang.Object
  extended byorg.multijava.util.testing.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
           
 
Field Summary
(package private) static java.lang.String ORACLE_SUFFIX
           
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.
(package private) static java.lang.String SAVED_SUFFIX
           
 
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)
           
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.util.ArrayList parseFoundLine(java.lang.String content, java.lang.String filename)
          Finds the first line with the given String in the given file and parses the content into tokens.
static java.util.ArrayList parseResult(java.lang.String command)
          Executes the given command as an external executable, reads the text produced and tokenizes it into Strings (separated by whitespace).
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)
           
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 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

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

static final java.lang.String ORACLE_SUFFIX

SAVED_SUFFIX

static final java.lang.String SAVED_SUFFIX
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.


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 java.util.ArrayList parseResult(java.lang.String command)
                                       throws java.io.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:
java.io.IOException

parseFoundLine

public static java.util.ArrayList parseFoundLine(java.lang.String content,
                                                 java.lang.String filename)
                                          throws java.io.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:
java.io.IOException

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.


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.

Throws:
java.io.IOException

readFileX

public static java.lang.String readFileX(java.lang.String filename)

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 is an optimized version that uses the byte array provided and presumes that the file is shorter than the length of the array.

Throws:
java.io.IOException

executeCompile

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

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.


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.


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.

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.


mjc

mjc is Copyright (C) 2000-2004 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. mjc is based in part on the Kopi project Copyright (C) 1990-99 DMS Decision Management Systems Ges.m.b.H.