Author: dblevins Date: Wed Dec 7 01:14:14 2011 New Revision: 1211251 URL: http://svn.apache.org/viewvc?rev=1211251&view=rev Log: Test case Added: geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/EJBExceptionTest.java Modified: geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/pom.xml geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/main/java/javax/ejb/EJBException.java Modified: geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/pom.xml URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/pom.xml?rev=1211251&r1=1211250&r2=1211251&view=diff ============================================================================== --- geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/pom.xml (original) +++ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/pom.xml Wed Dec 7 01:14:14 2011 @@ -63,6 +63,13 @@ 1.0 provided + + + junit + junit + 4.8.1 + test + Modified: geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/main/java/javax/ejb/EJBException.java URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/main/java/javax/ejb/EJBException.java?rev=1211251&r1=1211250&r2=1211251&view=diff ============================================================================== --- geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/main/java/javax/ejb/EJBException.java (original) +++ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/main/java/javax/ejb/EJBException.java Wed Dec 7 01:14:14 2011 @@ -58,12 +58,12 @@ public class EJBException extends Runtim @Override public Throwable getCause() { - return super.getCause() != null? super.getCause(): getCausedByException(); + return super.getCause() != null ? super.getCause() : getCausedByException(); } public String getMessage() { - if (causeException == null || causeException == super.getCause()) return super.getMessage(); + if (causeException == null) return super.getMessage(); StringBuilder sb = new StringBuilder(); @@ -88,10 +88,6 @@ public class EJBException extends Runtim } } - public void printStackTrace() { - printStackTrace(System.err); - } - public void printStackTrace(PrintWriter pw) { if (causeException == null || causeException == super.getCause()) { super.printStackTrace(pw); @@ -101,4 +97,8 @@ public class EJBException extends Runtim super.printStackTrace(pw); } } + + public void printStackTrace() { + printStackTrace(System.err); + } } Added: geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/EJBExceptionTest.java URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/EJBExceptionTest.java?rev=1211251&view=auto ============================================================================== --- geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/EJBExceptionTest.java (added) +++ geronimo/specs/trunk/geronimo-ejb_3.1_spec-alt/src/test/java/javax/ejb/EJBExceptionTest.java Wed Dec 7 01:14:14 2011 @@ -0,0 +1,259 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// This source code implements specifications defined by the Java +// Community Process. In order to remain compliant with the specification +// DO NOT add / change / or delete method signatures! +// +package javax.ejb; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; + +public class EJBExceptionTest { + + private static final EJBException exceptionDefaultConstructor = new EJBException(); + private static final EJBException exceptionWithNullMessage = new EJBException((String) null); + private static final EJBException exceptionWithMessage = new EJBException("msg"); + + private static final EJBException exceptionWithCausedBy = createException(true, false); + private static final EJBException exceptionWithInitCause = createException(false, true); + private static final EJBException exceptionWithCausedByAndInitCause = createException(true, true); + + private static EJBException createException(boolean causedBy, boolean initCause) { + + try { + + throw new Exception("cause"); + + } catch (Exception ex) { + + EJBException ejbException = causedBy ? new EJBException("msg", ex) : new EJBException("msg"); + + if (initCause) { + + ejbException.initCause(ex); + + } + + return ejbException; + } + } + + @Test + public void testGetMessage() { + + Assert.assertEquals(null, exceptionDefaultConstructor.getMessage()); + + Assert.assertEquals(null, exceptionWithNullMessage.getMessage()); + + Assert.assertEquals("msg", exceptionWithMessage.getMessage()); + + Assert.assertEquals("msg; nested exception is: java.lang.Exception: cause", exceptionWithCausedBy.getMessage()); + + Assert.assertEquals("msg", exceptionWithInitCause.getMessage()); + + Assert.assertEquals("msg; nested exception is: java.lang.Exception: cause", exceptionWithCausedByAndInitCause.getMessage()); + } + + @Test + public void testGetCause() { + + Assert.assertNull(exceptionDefaultConstructor.getCause()); + + Assert.assertNull(exceptionWithNullMessage.getCause()); + + Assert.assertNull(exceptionWithMessage.getCause()); + + Assert.assertNotNull(exceptionWithCausedBy.getCause()); + + Assert.assertNotNull(exceptionWithInitCause.getCause()); + } + + @Test + public void testGetCausedByException() { + + Assert.assertNull(exceptionDefaultConstructor.getCausedByException()); + + Assert.assertNull(exceptionWithNullMessage.getCausedByException()); + + Assert.assertNull(exceptionWithMessage.getCausedByException()); + + Assert.assertSame(exceptionWithCausedBy.getCause(), exceptionWithCausedBy.getCausedByException()); + + Assert.assertNull(exceptionWithInitCause.getCausedByException()); + } + + @Test + public void testInitCause() { + + Exception cause1 = new Exception("cause1"); + + Exception cause2 = new Exception("cause2"); + + + EJBException ejbEx = new EJBException(cause1); + + Assert.assertSame(cause1, ejbEx.getCausedByException()); + + Assert.assertSame(cause1, ejbEx.getCause()); + + ejbEx.initCause(cause2); + + Assert.assertSame(cause1, ejbEx.getCausedByException()); + + Assert.assertSame(cause2, ejbEx.getCause()); + } + + private static final String[] printStackTraces(Throwable t) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + t.printStackTrace(new PrintStream(baos)); + + + StringWriter sw = new StringWriter(); + + t.printStackTrace(new PrintWriter(sw)); + + + return new String[]{new String(baos.toByteArray()), sw.toString()}; + } + + private static String highlight(String s) { + + return ">>>" + s + "<<<"; + } + + private void assertStackTrace(String s, String... pieces) throws IOException { + + BufferedReader br = new BufferedReader(new StringReader(s)); + + int pos = 0; + + + for (String line; (line = br.readLine()) != null; ) { + + if (line.startsWith("\tat") && (pos > 0 && pieces[pos - 1].equals("\tat"))) { + + continue; + + } + + + if (pos == pieces.length) { + + Assert.fail("unexpected lines" + System.getProperty("line.separator") + highlight(s)); + + } + + + if (!line.startsWith(pieces[pos])) { + + Assert.fail(highlight(line) + " does not start with piece " + + + (pos + 1) + " " + highlight(pieces[pos]) + + + System.getProperty("line.separator") + highlight(s)); + + } + + + pos++; + } + + + if (pos != pieces.length) { + + Assert.fail("expected piece " + (pos + 1) + System.getProperty("line.separator") + highlight(s)); + } + } + + @Test + public void _testPrintStackTrace() throws Exception { + + for (String s : printStackTraces(exceptionDefaultConstructor)) { + + assertStackTrace(s, "javax.ejb.EJBException", "\tat"); + } + + + for (String s : printStackTraces(exceptionWithNullMessage)) { + + assertStackTrace(s, "javax.ejb.EJBException", "\tat"); + } + + + for (String s : printStackTraces(exceptionWithMessage)) { + + assertStackTrace(s, "javax.ejb.EJBException", "\tat"); + } + + + for (String s : printStackTraces(exceptionWithCausedBy)) { + + assertStackTrace(s, "javax.ejb.EJBException: msg; nested exception", + "\tat", + "Caused by", + "\tat", + "\t..."); + } + + + for (String s : printStackTraces(exceptionWithInitCause)) { + + assertStackTrace(s, + "javax.ejb.EJBException", + "\tat", + "Caused by", + "\tat", + "\t..."); + } + + + for (String s : printStackTraces(exceptionWithCausedByAndInitCause)) { + + assertStackTrace(s, + "javax.ejb.EJBException: msg; nested exception", + "\tat", + "Caused by", + "\tat", + "\t..."); + } + + + for (String s : printStackTraces(new EJBException(new Exception("cause1")).initCause(new Exception("cause2")))) { + + assertStackTrace(s, + "javax.ejb.EJBException: nested exception is: java.lang.Exception: cause1", + "java.lang.Exception: cause1", + "\tat", + "javax.ejb.EJBException: nested exception is: java.lang.Exception: cause1", + "\tat", + "Caused by: java.lang.Exception: cause2", + "\t..."); + } + } +} \ No newline at end of file