001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.client;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.cactus.internal.WebTestResult;
025:
026: /**
027: * Unit tests of the <code>WebTestResultParser</code> class.
028: *
029: * @version $Id: TestWebTestResultParser.java 238991 2004-05-22 11:34:50Z vmassol $
030: */
031: public class TestWebTestResultParser extends TestCase {
032: /**
033: * Verify parsing when the test result contains no exception.
034: *
035: * @exception ParsingException if error
036: */
037: public void testParseNoException() throws ParsingException {
038: WebTestResult initialResult = new WebTestResult();
039: WebTestResultParser parser = new WebTestResultParser();
040: WebTestResult result = parser.parse(initialResult.toXml());
041:
042: assertNotNull(result);
043: assertTrue(!result.hasException());
044: assertNull(result.getExceptionClassName());
045: assertNull(result.getExceptionMessage());
046: assertNull(result.getExceptionStackTrace());
047: }
048:
049: /**
050: * Verify parsing when the test result contains an exception.
051: *
052: * @exception ParsingException if error
053: */
054: public void testParseWithException() throws ParsingException {
055: Exception e = new Exception("test exception");
056: WebTestResult initialResult = new WebTestResult(e);
057: WebTestResultParser parser = new WebTestResultParser();
058: WebTestResult result = parser.parse(initialResult.toXml());
059:
060: assertNotNull(result);
061: assertTrue("There is no exception in the test result !", result
062: .hasException());
063: assertEquals("java.lang.Exception", result
064: .getExceptionClassName());
065: assertEquals("test exception", result.getExceptionMessage());
066: assertTrue("Should not be empty", result
067: .getExceptionStackTrace().length() > 0);
068: }
069:
070: /**
071: * Verify the correct parsing behaviour to extract the ROOT element when
072: * there is no exception returned in the test result.
073: *
074: * @exception ParsingException if error
075: */
076: public void testReadRootElementEmpty() throws ParsingException {
077: WebTestResult initialResult = new WebTestResult();
078: WebTestResultParser parser = new WebTestResultParser();
079:
080: String buffer = parser.readRootElement(initialResult.toXml());
081:
082: assertEquals("", buffer);
083: }
084:
085: /**
086: * Verify the correct parsing behaviour to extract the ROOT element when
087: * there is an exception returned in the test result.
088: *
089: * @exception ParsingException if error
090: */
091: public void testReadRootElementFull() throws ParsingException {
092: String expectedStart = "<exception classname=\""
093: + "java.lang.Exception\"><message><![CDATA[test exception]]>"
094: + "</message><stacktrace><![CDATA[";
095: String expectedEnd = "]]></stacktrace></exception>";
096:
097: Exception e = new Exception("test exception");
098: WebTestResult initialResult = new WebTestResult(e);
099: WebTestResultParser parser = new WebTestResultParser();
100:
101: String buffer = parser.readRootElement(initialResult.toXml());
102:
103: assertTrue("Should have started with [" + expectedStart + "]",
104: buffer.startsWith(expectedStart));
105: assertTrue("Should have ended with [" + expectedEnd + "]",
106: buffer.endsWith(expectedEnd));
107: }
108:
109: /**
110: * Verify the correct parsing behaviour to extract the exception classname.
111: *
112: * @exception ParsingException if error
113: */
114: public void testReadExceptionClassName() throws ParsingException {
115: String expectedStart = "<message><![CDATA[test exception]]>"
116: + "</message><stacktrace><![CDATA[";
117: String expectedEnd = "]]></stacktrace>";
118:
119: Exception e = new Exception("test exception");
120: WebTestResult initialResult = new WebTestResult(e);
121: WebTestResultParser parser = new WebTestResultParser();
122: String buffer = parser.readRootElement(initialResult.toXml());
123:
124: buffer = parser.readExceptionClassname(buffer);
125: assertEquals("java.lang.Exception", parser.exceptionClassname);
126: assertTrue("Should have started with [" + expectedStart + "]",
127: buffer.startsWith(expectedStart));
128: assertTrue("Should have ended with [" + expectedEnd + "]",
129: buffer.endsWith(expectedEnd));
130: }
131:
132: /**
133: * Verify the correct parsing behaviour to extract the exception message.
134: *
135: * @exception ParsingException if error
136: */
137: public void testReadExceptionMessage() throws ParsingException {
138: String expectedStart = "<stacktrace><![CDATA[";
139: String expectedEnd = "]]></stacktrace>";
140:
141: Exception e = new Exception("test exception");
142: WebTestResult initialResult = new WebTestResult(e);
143: WebTestResultParser parser = new WebTestResultParser();
144: String buffer = parser.readRootElement(initialResult.toXml());
145:
146: buffer = parser.readExceptionClassname(buffer);
147:
148: buffer = parser.readExceptionMessage(buffer);
149: assertEquals("test exception", parser.exceptionMessage);
150: assertTrue("Should have started with [" + expectedStart + "]",
151: buffer.startsWith(expectedStart));
152: assertTrue("Should have ended with [" + expectedEnd + "]",
153: buffer.endsWith(expectedEnd));
154: }
155: }
|