001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: /**
019: * @version $Revision: 450758 $ $Date: 2006-09-28 01:40:18 -0700 $
020: */
021:
022: import junit.framework.TestCase;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.io.PrintStream;
027: import java.io.ObjectOutputStream;
028: import java.io.ByteArrayInputStream;
029: import java.io.ObjectInputStream;
030:
031: public class ThrowableArtifactTest extends TestCase {
032:
033: public void testThrowableArtifact() throws Throwable {
034: Throwable exception = new NullPointerException("ONE");
035: exception = throwCatchReturn(exception);
036:
037: exception = new IllegalArgumentException("TWO", exception);
038: exception = throwCatchReturn(exception);
039:
040: exception = new UnsupportedOperationException("THREE",
041: exception);
042: exception = throwCatchReturn(exception);
043:
044: exception = new IllegalStateException("FOUR", exception);
045: exception = throwCatchReturn(exception);
046:
047: exception = new BadException("FIVE", exception);
048: exception = throwCatchReturn(exception);
049:
050: String expectedStackTrace = getPrintedStackTrace(exception);
051:
052: ThrowableArtifact artifact = marshal(new ThrowableArtifact(
053: exception));
054: exception = throwCatchReturn(artifact.getThrowable().getCause());
055:
056: String actualStackTrace = getPrintedStackTrace(exception);
057:
058: assertEquals("stackTrace", expectedStackTrace, actualStackTrace);
059: }
060:
061: private String getPrintedStackTrace(Throwable exception) {
062: ByteArrayOutputStream baos = new ByteArrayOutputStream();
063: PrintStream printStream = new PrintStream(baos);
064: exception.printStackTrace(printStream);
065: printStream.flush();
066: String stackTrace = new String(baos.toByteArray());
067: return stackTrace;
068: }
069:
070: private Throwable throwCatchReturn(Throwable exception) {
071: try {
072: throw exception;
073: } catch (Throwable e) {
074: return exception;
075: }
076: }
077:
078: private ThrowableArtifact marshal(ThrowableArtifact artifact)
079: throws IOException, ClassNotFoundException {
080: ByteArrayOutputStream baos = new ByteArrayOutputStream();
081: ObjectOutputStream out = new ObjectOutputStream(baos);
082: out.writeObject(artifact);
083: out.close();
084:
085: ByteArrayInputStream bais = new ByteArrayInputStream(baos
086: .toByteArray());
087: ObjectInputStream in = new ObjectInputStream(bais);
088: return (ThrowableArtifact) in.readObject();
089: }
090:
091: public static class BadException extends Exception {
092: private final Object data = new NotSerializableObject();
093:
094: public BadException(String message, Throwable throwable) {
095: super (message, throwable);
096: }
097: }
098:
099: public static class NotSerializableObject {
100: }
101: }
|