KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestServerSideExceptions


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.sample.servlet.unit;
21
22 import java.io.Serializable JavaDoc;
23
24 import org.apache.cactus.ServletTestCase;
25 import org.apache.cactus.internal.client.AssertionFailedErrorWrapper;
26 import org.apache.cactus.internal.client.ServletExceptionWrapper;
27 import org.apache.cactus.internal.util.JUnitVersionHelper;
28
29 import junit.framework.AssertionFailedError;
30 import junit.framework.ComparisonFailure;
31
32 /**
33  * Verifies the correct handling of exceptions that happen when running
34  * inside the server. Specifically verifies that serializable,
35  * non-serializable and {@link AssertionFailedError} exceptions are
36  * correctly propagated to the client side.
37  *
38  * @version $Id: TestServerSideExceptions.java,v 1.6 2004/04/10 16:11:26 vmassol Exp $
39  */

40 public class TestServerSideExceptions extends ServletTestCase
41 {
42     /**
43      * Not serializable exception.
44      */

45     public class NotSerializableException extends Exception JavaDoc
46     {
47         /**
48          * @param theMessage the exception message
49          */

50         public NotSerializableException(String JavaDoc theMessage)
51         {
52             super(theMessage);
53         }
54     }
55
56     /**
57      * Serializable exception.
58      */

59     public class SerializableException extends Exception JavaDoc
60         implements Serializable JavaDoc
61     {
62         /**
63          * @param theMessage the exception message
64          */

65         public SerializableException(String JavaDoc theMessage)
66         {
67             super(theMessage);
68         }
69     }
70
71     /**
72      * @param theTestName the test name to verify
73      * @return true if the test name to verify corresponds to the currently
74      * executing test
75      */

76     private boolean checkName(String JavaDoc theTestName)
77     {
78         return JUnitVersionHelper.getTestCaseName(this).equals(
79             theTestName);
80     }
81
82     /**
83      * Intercepts running test cases to check for normal exceptions.
84      *
85      * @exception Throwable on test failure
86      */

87     public void runBare() throws Throwable JavaDoc
88     {
89         try
90         {
91             super.runBare();
92         }
93         catch (AssertionFailedErrorWrapper e)
94         {
95             // If the test case is "testAssertionFailedError" and the exception
96
// is of type AssertionFailedError and contains the text
97
// "test assertion failed error", then the test is ok.
98
if (checkName("testAssertionFailedError"))
99             {
100                 assertEquals(AssertionFailedError.class.getName(),
101                     e.getWrappedClassName());
102                 assertEquals("test assertion failed error", e.getMessage());
103                 return;
104             }
105
106             // If the test case is "testComparisonFailure" and the exception
107
// is of type ComparisonFailure and contains the text
108
// "test comparison failure", then the test is ok.
109
else if (checkName("testComparisonFailure"))
110             {
111                 assertEquals(ComparisonFailure.class.getName(),
112                     e.getWrappedClassName());
113                 assertEquals("test comparison failure expected:<some...> "
114                     + "but was:<other...>", e.getMessage());
115                 return;
116             }
117         }
118         catch (ServletExceptionWrapper e)
119         {
120             // If the test case is "testExceptionNotSerializable" and the
121
// exception is of type
122
// TestServletTestCaseHelper1_ExceptionNotSerializable
123
// and contains the text "test non serializable exception", then
124
// the test is ok.
125
if (checkName("testExceptionNotSerializable"))
126             {
127                 assertEquals(NotSerializableException.class.getName(),
128                     e.getWrappedClassName());
129                 assertEquals("test non serializable exception",
130                     e.getMessage());
131                 return;
132             }
133
134             // If the test case is "testExceptionSerializable" and the exception
135
// is of type TestServletTestCaseHelper1_ExceptionSerializable
136
// and contains the text "test serializable exception", then
137
// the test is ok.
138
else if (checkName("testExceptionSerializable"))
139             {
140                 assertEquals(SerializableException.class.getName(),
141                     e.getWrappedClassName());
142                 assertEquals("test serializable exception", e.getMessage());
143                 return;
144             }
145         }
146
147         throw new AssertionFailedError("Unexpected test ["
148             + JUnitVersionHelper.getTestCaseName(this) + "]");
149     }
150
151     //-------------------------------------------------------------------------
152

153     /**
154      * Raises an <code>AssertionFailedError</code> exception. The exception is
155      * caught in
156      * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
157      * This is to verify that <code>AssertionFailedError</code> raised on the
158      * server side are properly propagated on the client side.
159      */

160     public void testAssertionFailedError()
161     {
162         throw new AssertionFailedError("test assertion failed error");
163     }
164
165     //-------------------------------------------------------------------------
166

167     /**
168      * Raises a non serializable exception. The exception is
169      * caught in
170      * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
171      * This is to verify that non serializable exceptions raised on the
172      * server side are properly propagated on the client side.
173      *
174      * @exception NotSerializableException the non serializable exception to
175      * throw
176      */

177     public void testExceptionNotSerializable()
178         throws NotSerializableException
179     {
180         throw new NotSerializableException("test non serializable exception");
181     }
182
183     //-------------------------------------------------------------------------
184

185     /**
186      * Raises a serializable exception. The exception is
187      * caught in
188      * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
189      * This is to verify that serializable exceptions raised on the
190      * server side are properly propagated on the client side.
191      *
192      * @exception SerializableException the serializable exception to throw
193      */

194     public void testExceptionSerializable() throws SerializableException
195     {
196         throw new SerializableException("test serializable exception");
197     }
198
199     //-------------------------------------------------------------------------
200

201     /**
202      * Verify that the new {@link ComparisonFailure} introduced in JUnit 3.8.1
203      * is correctly reported as a failure and not as an error in the Test
204      * Runner when Cactus is used.
205      */

206     public void testComparisonFailure()
207     {
208         throw new ComparisonFailure("test comparison failure", "some value",
209             "other value");
210     }
211
212 }
213
Popular Tags