001: package com.sun.portal.wireless.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
013: * of any line in the test results, or </li>
014: * <li> any line in the unexpected reference file is found as a substring of
015: * any line in the test results </li>
016: * </ul>
017: */
018: public class Compare {
019: public TestCase test = null;
020: public PrintStream out = null;
021:
022: /**
023: * Creates a <code>Compare</code> class with error messages going to
024: * stdout.
025: *
026: * @param test the test case calling this class
027: */
028: public Compare(TestCase test) {
029: this (test, System.out);
030: }
031:
032: /**
033: * Creates a <code>Compare</code> class.
034: *
035: * @param test the test case calling this class
036: * @param out where error messages will be sent
037: */
038: public Compare(TestCase test, PrintStream out) {
039: this .test = test;
040: this .out = out;
041: }
042:
043: /**
044: * Checks the given test results against expected and unexpected output.
045: *
046: * @param result the test results
047: * @param expected the filename for expected output
048: * @param unexpected the filename for unexpected output
049: */
050: public void check(String result, String expected, String unexpected) {
051: check(result, expected, true);
052: check(result, unexpected, false);
053: }
054:
055: /**
056: * Checks the given test results against expected output.
057: *
058: * @param result the test results
059: * @param filename the filename for expected output
060: */
061: public void expected(String result, String filename) {
062: check(result, filename, true);
063: }
064:
065: /**
066: * Checks the given test results against unexpected output.
067: *
068: * @param result the test results
069: * @param filename the filename for unexpected output
070: */
071: public void unexpected(String result, String filename) {
072: check(result, filename, false);
073: }
074:
075: private void check(String result, String filename, boolean expected) {
076: // check for null result
077: if (result == null) {
078: out.println("null result");
079: return;
080: }
081:
082: // open the reference file and tokenize
083: File file = new File(filename);
084: String refStr = null;
085: if (file.exists()) {
086: try {
087: refStr = TestUtils.getString(new FileInputStream(file));
088: } catch (Exception e) {
089: }
090: } else {
091: refStr = "";
092: }
093: Enumeration reference = new StringTokenizer(refStr, "\n");
094:
095: // loop through reference strings looking for (un)expected strings
096: // in the result
097: boolean failed = false;
098: StringBuffer failure = new StringBuffer();
099: while (reference.hasMoreElements()) {
100: String next = (String) reference.nextElement();
101: next = next.trim();
102: boolean found = (result.indexOf(next.trim()) != -1);
103: if ((expected && !found) || (!expected && found)) {
104: if (failed) {
105: failure.append("\n");
106: } else {
107: failed = true;
108: }
109: failure.append(next);
110: }
111: }
112:
113: if (failed) {
114: fail(result, failure.toString(), expected);
115: }
116: }
117:
118: private void fail(String result, String failure, boolean expected) {
119: StringBuffer msg = new StringBuffer();
120: msg.append("\n");
121: msg.append("----- result:\n");
122: msg.append(result);
123: msg.append("\n");
124: if (expected) {
125: msg.append("----- expected:\n");
126: } else {
127: msg.append("----- unexpected:\n");
128: }
129: msg.append(failure);
130:
131: test.fail(msg.toString());
132: }
133:
134: }
|