001: package com.sun.portal.samples.asc.tests;
002:
003: import java.io.*;
004: import java.util.*;
005: import junit.framework.*;
006:
007: /**
008: * Compares test results with expected and unexpected reference output. Each
009: * line of the reference file(s) is compared against the test results. A test
010: * failure is generated if:
011: * <ul>
012: * <li> any line in the expected reference file is not found as a substring of
013: * any line in the test results, or </li>
014: * <li> any line in the unexpected reference file is found as a substring of any
015: * line in the test results </li>
016: * </ul>
017: */
018: public class Compare {
019: public TestCase test = null;
020:
021: public PrintStream out = null;
022:
023: /**
024: * Creates a <code>Compare</code> class with error messages going to
025: * stdout.
026: *
027: * @param test
028: * the test case calling this class
029: */
030: public Compare(TestCase test) {
031: this (test, System.out);
032: }
033:
034: /**
035: * Creates a <code>Compare</code> class.
036: *
037: * @param test
038: * the test case calling this class
039: * @param out
040: * where error messages will be sent
041: */
042: public Compare(TestCase test, PrintStream out) {
043: this .test = test;
044: this .out = out;
045: }
046:
047: /**
048: * Checks the given test results against expected and unexpected output.
049: *
050: * @param result
051: * the test results
052: * @param expected
053: * the filename for expected output
054: * @param unexpected
055: * the filename for unexpected output
056: */
057: public void check(String result, String expected, String unexpected) {
058: check(result, expected, true);
059: check(result, unexpected, false);
060: }
061:
062: /**
063: * Checks the given test results against expected output.
064: *
065: * @param result
066: * the test results
067: * @param filename
068: * the filename for expected output
069: */
070: public void expected(String result, String filename) {
071: check(result, filename, true);
072: }
073:
074: /**
075: * Checks the given test results against unexpected output.
076: *
077: * @param result
078: * the test results
079: * @param filename
080: * the filename for unexpected output
081: */
082: public void unexpected(String result, String filename) {
083: check(result, filename, false);
084: }
085:
086: private void check(String result, String filename, boolean expected) {
087: // check for null result
088: if (result == null) {
089: TestCase.fail("Result was null!");
090: }
091:
092: // open the reference file and tokenize
093: File file = new File(filename);
094: String refStr = null;
095: if (file.exists()) {
096: try {
097: refStr = TestUtils.getString(new FileInputStream(file));
098: } catch (Exception e) {
099: }
100: } else {
101: // I guess Luu assumed missing files meant no failure
102: // refStr = "";
103: TestCase.fail("Reference file " + filename + " missing!");
104: }
105: Enumeration reference = new StringTokenizer(refStr, "\n");
106:
107: // loop through reference strings looking for (un)expected strings
108: // in the result
109: boolean failed = false;
110: StringBuffer failure = new StringBuffer();
111: while (reference.hasMoreElements()) {
112: String next = (String) reference.nextElement();
113: // leading & trailing white space ignored
114: next = next.trim();
115: // compare is irrespective of order
116: boolean found = (result.indexOf(next.trim()) != -1);
117: if ((expected && !found) || (!expected && found)) {
118: if (failed) {
119: failure.append("\n");
120: } else {
121: failed = true;
122: }
123: failure.append(next);
124: }
125: }
126:
127: if (failed) {
128: fail(result, failure.toString(), expected);
129: }
130: }
131:
132: private void fail(String result, String failure, boolean expected) {
133: StringBuffer msg = new StringBuffer();
134: msg.append("\n");
135: msg.append("----- result:\n");
136: msg.append(result);
137: msg.append("\n");
138: if (expected) {
139: msg.append("----- expected:\n");
140: } else {
141: msg.append("----- unexpected:\n");
142: }
143: msg.append(failure);
144:
145: TestCase.fail(msg.toString());
146: }
147:
148: }
|