Simple utility for testing program output : Unit Test « Development Class « Java






Simple utility for testing program output

Simple utility for testing program output
// : com:bruceeckel:simpletest:Test.java //Simple utility for testing program output. Intercepts //System.out to print both to the console and a buffer. //From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 //www.BruceEckel.com. See copyright notice in CopyRight.txt. import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.regex.Pattern; publicclass Alias2 { privatestatic Test monitor = new Test(); privateint i; public Alias2(int ii) { i = ii; } publicstaticvoid f(Alias2 reference) { reference.i++; } publicstaticvoid main(String[] args) { Alias2 x = new Alias2(7); System.out.println("x: " + x.i); System.out.println("Calling f(x)"); f(x); System.out.println("x: " + x.i); monitor.expect(new String[] { "x: 7", "Calling f(x)", "x: 8" }); } } ///:~ class Test { // Bit-shifted so they can be added together: publicstaticfinalint EXACT = 1 << 0, // Lines must match exactly  AT_LEAST = 1 << 1, // Must be at least these lines  IGNORE_ORDER = 1 << 2, // Ignore line order  WAIT = 1 << 3; // Delay until all lines are output private String className; private TestStream testStream; public Test() { // Discover the name of the class this // object was created within:  className = new Throwable().getStackTrace()[1].getClassName(); testStream = new TestStream(className); } publicstatic List fileToList(String fname) { ArrayList list = new ArrayList(); try { BufferedReader in = new BufferedReader(new FileReader(fname)); try { String line; while ((line = in.readLine()) != null) { if (fname.endsWith(".txt")) list.add(line); else list.add(new TestExpression(line)); } } finally { in.close(); } } catch (IOException e) { thrownew RuntimeException(e); } return list; } publicstatic List arrayToList(Object[] array) { List l = new ArrayList(); for (int i = 0; i < array.length; i++) { if (array[i] instanceof TestExpression) { TestExpression re = (TestExpression) array[i]; for (int j = 0; j < re.getNumber(); j++) l.add(re); } else { l.add(new TestExpression(array[i].toString())); } } return l; } publicvoid expect(Object[] exp, int flags) { if ((flags & WAIT) != 0) while (testStream.numOfLines < exp.length) { try { Thread.sleep(1000); } catch (InterruptedException e) { thrownew RuntimeException(e); } } List output = fileToList(className + "Output.txt"); if ((flags & IGNORE_ORDER) == IGNORE_ORDER) OutputVerifier.verifyIgnoreOrder(output, exp); elseif ((flags & AT_LEAST) == AT_LEAST) OutputVerifier.verifyAtLeast(output, arrayToList(exp)); else OutputVerifier.verify(output, arrayToList(exp)); // Clean up the output file - see c06:Detergent.java  testStream.openOutputFile(); } publicvoid expect(Object[] expected) { expect(expected, EXACT); } publicvoid expect(Object[] expectFirst, String fname, int flags) { List expected = fileToList(fname); for (int i = 0; i < expectFirst.length; i++) expected.add(i, expectFirst[i]); expect(expected.toArray(), flags); } publicvoid expect(Object[] expectFirst, String fname) { expect(expectFirst, fname, EXACT); } publicvoid expect(String fname) { expect(new Object[] {}, fname, EXACT); } } ///:~ class TestExpression implements Comparable { private Pattern p; private String expression; privateboolean isRegEx; // Default to only one instance of this expression: privateint duplicates = 1; public TestExpression(String s) { this.expression = s; if (expression.startsWith("%% ")) { this.isRegEx = true; expression = expression.substring(3); this.p = Pattern.compile(expression); } } // For duplicate instances: public TestExpression(String s, int duplicates) { this(s); this.duplicates = duplicates; } public String toString() { if (isRegEx) return p.pattern(); return expression; } publicboolean equals(Object obj) { if (this == obj) return true; if (isRegEx) return (compareTo(obj) == 0); return expression.equals(obj.toString()); } publicint compareTo(Object obj) { if ((isRegEx) && (p.matcher(obj.toString()).matches())) return 0; return expression.compareTo(obj.toString()); } publicint getNumber() { return duplicates; } public String getExpression() { return expression; } publicboolean isRegEx() { return isRegEx; } } ///:~ class TestStream extends PrintStream { protectedint numOfLines; private PrintStream console = System.out, err = System.err, fout; // To store lines sent to System.out or err private InputStream stdin; private String className; public TestStream(String className) { super(System.out, true); // Autoflush  System.setOut(this); System.setErr(this); stdin = System.in; // Save to restore in dispose() // Replace the default version with one that // automatically produces input on demand:  System.setIn(new BufferedInputStream(new InputStream() { char[] input = ("test\n").toCharArray(); int index = 0; publicint read() { return (int) input[index = (index + 1) % input.length]; } })); this.className = className; openOutputFile(); } // public PrintStream getConsole() { return console; } publicvoid dispose() { System.setOut(console); System.setErr(err); System.setIn(stdin); } // This will write over an old Output.txt file: publicvoid openOutputFile() { try { fout = new PrintStream(new FileOutputStream(newFile(className + "Output.txt"))); } catch (FileNotFoundException e) { thrownew RuntimeException(e); } } // Override all possible print/println methods to send // intercepted console output to both the console and // the Output.txt file: publicvoid print(boolean x) { console.print(x); fout.print(x); } publicvoid println(boolean x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(char x) { console.print(x); fout.print(x); } publicvoid println(char x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(int x) { console.print(x); fout.print(x); } publicvoid println(int x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(long x) { console.print(x); fout.print(x); } publicvoid println(long x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(float x) { console.print(x); fout.print(x); } publicvoid println(float x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(double x) { console.print(x); fout.print(x); } publicvoid println(double x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(char[] x) { console.print(x); fout.print(x); } publicvoid println(char[] x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(String x) { console.print(x); fout.print(x); } publicvoid println(String x) { numOfLines++; console.println(x); fout.println(x); } publicvoid print(Object x) { console.print(x); fout.print(x); } publicvoid println(Object x) { numOfLines++; console.println(x); fout.println(x); } publicvoid println() { if (false) console.print("println"); numOfLines++; console.println(); fout.println(); } publicvoid write(byte[] buffer, int offset, int length) { console.write(buffer, offset, length); fout.write(buffer, offset, length); } publicvoid write(int b) { console.write(b); fout.write(b); } } ///:~ class OutputVerifier { privatestaticvoid verifyLength(int output, int expected, int compare) { if ((compare == Test.EXACT && expected != output) || (compare == Test.AT_LEAST && output < expected)) thrownew NumOfLinesException(expected, output); } publicstaticvoid verify(List output, List expected) { verifyLength(output.size(), expected.size(), Test.EXACT); if (!expected.equals(output)) { //find the line of mismatch  ListIterator it1 = expected.listIterator(); ListIterator it2 = output.listIterator(); while (it1.hasNext() && it2.hasNext() && it1.next().equals(it2.next())) ; thrownew LineMismatchException(it1.nextIndex(), it1.previous() .toString(), it2.previous().toString()); } } publicstaticvoid verifyIgnoreOrder(List output, Object[] expected) { verifyLength(expected.length, output.size(), Test.EXACT); if (!(expected instanceof String[])) thrownew RuntimeException( "IGNORE_ORDER only works with String objects"); String[] out = new String[output.size()]; Iterator it = output.iterator(); for (int i = 0; i < out.length; i++) out[i] = it.next().toString(); Arrays.sort(out); Arrays.sort(expected); int i = 0; if (!Arrays.equals(expected, out)) { while (expected[i].equals(out[i])) { i++; } thrownew SimpleTestException(((String) out[i]).compareTo(expected[i]) < 0 ? "output: <" + out[i] + ">" : "expected: <" + expected[i] + ">"); } } publicstaticvoid verifyAtLeast(List output, List expected) { verifyLength(output.size(), expected.size(), Test.AT_LEAST); if (!output.containsAll(expected)) { ListIterator it = expected.listIterator(); while (output.contains(it.next())) { } thrownew SimpleTestException("expected: <" + it.previous().toString() + ">"); } } } ///:~ class SimpleTestException extends RuntimeException { public SimpleTestException(String msg) { super(msg); } } ///:~ class NumOfLinesException extends SimpleTestException { public NumOfLinesException(int exp, int out) { super("Number of lines of output and " + "expected output did not match.\n" + "expected: <" + exp + ">\n" + "output: <" + out + "> lines)"); } } ///:~ class LineMismatchException extends SimpleTestException { public LineMismatchException(int lineNum, String expected, String output) { super("line " + lineNum + " of output did not match expected output\n" + "expected: <" + expected + ">\n" + "output: <" + output + ">"); } } ///:~ 








Related examples in the same category

1.Set JUnit Test case fail information
2.JUnit assertEquals: Float With Delta
3.Assert equals: int
4.Assert equals: Long
5.JUnit assertEquals With Message
6.JUnit assertTrue
7.JUnit assertTrue: ObjectArray
8.Before annotation
9.JUnit BeforeClass
10.JUnit Extends TestCase
11.JUnit Ignore
12.Simple test with JUnit
13.JUnit Test Case With Expected Exception
14.JUnit Test Setup
15.Simple use of JUnit to test ArrayListSimple use of JUnit to test ArrayList
16.Debug frameDebug frame
17.Error HandlerError Handler
18.Redirect or reassign some standard descriptors
19.Utilities for debugging
20.Testing class ClassTesting class Class
21.Assertion tool for debugging
22.Simple DebuggingSimple Debugging
23.Random data for test
24.Demonstration of Design by Contract (DBC) combined with white-box unit testingDemonstration of Design by Contract (DBC) combined with white-box unit testing
close