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.sample.servlet.unit;
021:
022: import java.io.Serializable;
023:
024: import org.apache.cactus.ServletTestCase;
025: import org.apache.cactus.internal.client.AssertionFailedErrorWrapper;
026: import org.apache.cactus.internal.client.ServletExceptionWrapper;
027: import org.apache.cactus.internal.util.JUnitVersionHelper;
028:
029: import junit.framework.AssertionFailedError;
030: import junit.framework.ComparisonFailure;
031:
032: /**
033: * Verifies the correct handling of exceptions that happen when running
034: * inside the server. Specifically verifies that serializable,
035: * non-serializable and {@link AssertionFailedError} exceptions are
036: * correctly propagated to the client side.
037: *
038: * @version $Id: TestServerSideExceptions.java 238900 2004-04-10 16:11:26Z vmassol $
039: */
040: public class TestServerSideExceptions extends ServletTestCase {
041: /**
042: * Not serializable exception.
043: */
044: public class NotSerializableException extends Exception {
045: /**
046: * @param theMessage the exception message
047: */
048: public NotSerializableException(String theMessage) {
049: super (theMessage);
050: }
051: }
052:
053: /**
054: * Serializable exception.
055: */
056: public class SerializableException extends Exception implements
057: Serializable {
058: /**
059: * @param theMessage the exception message
060: */
061: public SerializableException(String theMessage) {
062: super (theMessage);
063: }
064: }
065:
066: /**
067: * @param theTestName the test name to verify
068: * @return true if the test name to verify corresponds to the currently
069: * executing test
070: */
071: private boolean checkName(String theTestName) {
072: return JUnitVersionHelper.getTestCaseName(this ).equals(
073: theTestName);
074: }
075:
076: /**
077: * Intercepts running test cases to check for normal exceptions.
078: *
079: * @exception Throwable on test failure
080: */
081: public void runBare() throws Throwable {
082: try {
083: super .runBare();
084: } catch (AssertionFailedErrorWrapper e) {
085: // If the test case is "testAssertionFailedError" and the exception
086: // is of type AssertionFailedError and contains the text
087: // "test assertion failed error", then the test is ok.
088: if (checkName("testAssertionFailedError")) {
089: assertEquals(AssertionFailedError.class.getName(), e
090: .getWrappedClassName());
091: assertEquals("test assertion failed error", e
092: .getMessage());
093: return;
094: }
095:
096: // If the test case is "testComparisonFailure" and the exception
097: // is of type ComparisonFailure and contains the text
098: // "test comparison failure", then the test is ok.
099: else if (checkName("testComparisonFailure")) {
100: assertEquals(ComparisonFailure.class.getName(), e
101: .getWrappedClassName());
102: assertEquals(
103: "test comparison failure expected:<some...> "
104: + "but was:<other...>", e.getMessage());
105: return;
106: }
107: } catch (ServletExceptionWrapper e) {
108: // If the test case is "testExceptionNotSerializable" and the
109: // exception is of type
110: // TestServletTestCaseHelper1_ExceptionNotSerializable
111: // and contains the text "test non serializable exception", then
112: // the test is ok.
113: if (checkName("testExceptionNotSerializable")) {
114: assertEquals(NotSerializableException.class.getName(),
115: e.getWrappedClassName());
116: assertEquals("test non serializable exception", e
117: .getMessage());
118: return;
119: }
120:
121: // If the test case is "testExceptionSerializable" and the exception
122: // is of type TestServletTestCaseHelper1_ExceptionSerializable
123: // and contains the text "test serializable exception", then
124: // the test is ok.
125: else if (checkName("testExceptionSerializable")) {
126: assertEquals(SerializableException.class.getName(), e
127: .getWrappedClassName());
128: assertEquals("test serializable exception", e
129: .getMessage());
130: return;
131: }
132: }
133:
134: throw new AssertionFailedError("Unexpected test ["
135: + JUnitVersionHelper.getTestCaseName(this ) + "]");
136: }
137:
138: //-------------------------------------------------------------------------
139:
140: /**
141: * Raises an <code>AssertionFailedError</code> exception. The exception is
142: * caught in
143: * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
144: * This is to verify that <code>AssertionFailedError</code> raised on the
145: * server side are properly propagated on the client side.
146: */
147: public void testAssertionFailedError() {
148: throw new AssertionFailedError("test assertion failed error");
149: }
150:
151: //-------------------------------------------------------------------------
152:
153: /**
154: * Raises a non serializable exception. The exception is
155: * caught in
156: * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
157: * This is to verify that non serializable exceptions raised on the
158: * server side are properly propagated on the client side.
159: *
160: * @exception NotSerializableException the non serializable exception to
161: * throw
162: */
163: public void testExceptionNotSerializable()
164: throws NotSerializableException {
165: throw new NotSerializableException(
166: "test non serializable exception");
167: }
168:
169: //-------------------------------------------------------------------------
170:
171: /**
172: * Raises a serializable exception. The exception is
173: * caught in
174: * <code>TestServletTestCase_InterceptorServletTestCase.runTest()</code>.
175: * This is to verify that serializable exceptions raised on the
176: * server side are properly propagated on the client side.
177: *
178: * @exception SerializableException the serializable exception to throw
179: */
180: public void testExceptionSerializable()
181: throws SerializableException {
182: throw new SerializableException("test serializable exception");
183: }
184:
185: //-------------------------------------------------------------------------
186:
187: /**
188: * Verify that the new {@link ComparisonFailure} introduced in JUnit 3.8.1
189: * is correctly reported as a failure and not as an error in the Test
190: * Runner when Cactus is used.
191: */
192: public void testComparisonFailure() {
193: throw new ComparisonFailure("test comparison failure",
194: "some value", "other value");
195: }
196:
197: }
|