KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > TestWebTestResultParser


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.client;
21
22 import junit.framework.TestCase;
23
24 import org.apache.cactus.internal.WebTestResult;
25
26 /**
27  * Unit tests of the <code>WebTestResultParser</code> class.
28  *
29  * @version $Id: TestWebTestResultParser.java,v 1.1 2004/05/22 11:34:50 vmassol Exp $
30  */

31 public class TestWebTestResultParser extends TestCase
32 {
33     /**
34      * Verify parsing when the test result contains no exception.
35      *
36      * @exception ParsingException if error
37      */

38     public void testParseNoException() throws ParsingException
39     {
40         WebTestResult initialResult = new WebTestResult();
41         WebTestResultParser parser = new WebTestResultParser();
42         WebTestResult result = parser.parse(initialResult.toXml());
43
44         assertNotNull(result);
45         assertTrue(!result.hasException());
46         assertNull(result.getExceptionClassName());
47         assertNull(result.getExceptionMessage());
48         assertNull(result.getExceptionStackTrace());
49     }
50
51     /**
52      * Verify parsing when the test result contains an exception.
53      *
54      * @exception ParsingException if error
55      */

56     public void testParseWithException() throws ParsingException
57     {
58         Exception JavaDoc e = new Exception JavaDoc("test exception");
59         WebTestResult initialResult = new WebTestResult(e);
60         WebTestResultParser parser = new WebTestResultParser();
61         WebTestResult result = parser.parse(initialResult.toXml());
62
63         assertNotNull(result);
64         assertTrue("There is no exception in the test result !",
65             result.hasException());
66         assertEquals("java.lang.Exception", result.getExceptionClassName());
67         assertEquals("test exception", result.getExceptionMessage());
68         assertTrue("Should not be empty",
69             result.getExceptionStackTrace().length() > 0);
70     }
71
72     /**
73      * Verify the correct parsing behaviour to extract the ROOT element when
74      * there is no exception returned in the test result.
75      *
76      * @exception ParsingException if error
77      */

78     public void testReadRootElementEmpty() throws ParsingException
79     {
80         WebTestResult initialResult = new WebTestResult();
81         WebTestResultParser parser = new WebTestResultParser();
82
83         String JavaDoc buffer = parser.readRootElement(initialResult.toXml());
84
85         assertEquals("", buffer);
86     }
87
88     /**
89      * Verify the correct parsing behaviour to extract the ROOT element when
90      * there is an exception returned in the test result.
91      *
92      * @exception ParsingException if error
93      */

94     public void testReadRootElementFull() throws ParsingException
95     {
96         String JavaDoc expectedStart = "<exception classname=\""
97             + "java.lang.Exception\"><message><![CDATA[test exception]]>"
98             + "</message><stacktrace><![CDATA[";
99         String JavaDoc expectedEnd = "]]></stacktrace></exception>";
100
101         Exception JavaDoc e = new Exception JavaDoc("test exception");
102         WebTestResult initialResult = new WebTestResult(e);
103         WebTestResultParser parser = new WebTestResultParser();
104
105         String JavaDoc buffer = parser.readRootElement(initialResult.toXml());
106
107         assertTrue("Should have started with [" + expectedStart + "]",
108             buffer.startsWith(expectedStart));
109         assertTrue("Should have ended with [" + expectedEnd + "]",
110             buffer.endsWith(expectedEnd));
111     }
112
113     /**
114      * Verify the correct parsing behaviour to extract the exception classname.
115      *
116      * @exception ParsingException if error
117      */

118     public void testReadExceptionClassName() throws ParsingException
119     {
120         String JavaDoc expectedStart = "<message><![CDATA[test exception]]>"
121             + "</message><stacktrace><![CDATA[";
122         String JavaDoc expectedEnd = "]]></stacktrace>";
123
124         Exception JavaDoc e = new Exception JavaDoc("test exception");
125         WebTestResult initialResult = new WebTestResult(e);
126         WebTestResultParser parser = new WebTestResultParser();
127         String JavaDoc buffer = parser.readRootElement(initialResult.toXml());
128
129         buffer = parser.readExceptionClassname(buffer);
130         assertEquals("java.lang.Exception", parser.exceptionClassname);
131         assertTrue("Should have started with [" + expectedStart + "]",
132             buffer.startsWith(expectedStart));
133         assertTrue("Should have ended with [" + expectedEnd + "]",
134             buffer.endsWith(expectedEnd));
135     }
136
137     /**
138      * Verify the correct parsing behaviour to extract the exception message.
139      *
140      * @exception ParsingException if error
141      */

142     public void testReadExceptionMessage() throws ParsingException
143     {
144         String JavaDoc expectedStart = "<stacktrace><![CDATA[";
145         String JavaDoc expectedEnd = "]]></stacktrace>";
146
147         Exception JavaDoc e = new Exception JavaDoc("test exception");
148         WebTestResult initialResult = new WebTestResult(e);
149         WebTestResultParser parser = new WebTestResultParser();
150         String JavaDoc buffer = parser.readRootElement(initialResult.toXml());
151
152         buffer = parser.readExceptionClassname(buffer);
153
154         buffer = parser.readExceptionMessage(buffer);
155         assertEquals("test exception", parser.exceptionMessage);
156         assertTrue("Should have started with [" + expectedStart + "]",
157             buffer.startsWith(expectedStart));
158         assertTrue("Should have ended with [" + expectedEnd + "]",
159             buffer.endsWith(expectedEnd));
160     }
161 }
162
Popular Tags