| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.lang.reflect.Method; |
| 36 | | | |
| 37 | | | import javax.servlet.ServletException; |
| 38 | | | import javax.servlet.UnavailableException; |
| 39 | | | import javax.xml.bind.JAXBException; |
| 40 | | | |
| 41 | | | import junit.framework.TestCase; |
| 42 | | | |
| 43 | | | import org.jaxen.JaxenException; |
| 44 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 45 | | (1) | import org.jcoderz.commons.Loggable; |
| 46 | | | import org.xml.sax.SAXException; |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | @author |
| 51 | | | |
| 52 | 100 | | public class ThrowableUtilTest |
| 53 | | | extends TestCase |
| 54 | | | { |
| 55 | | | |
| 56 | | | public void testFixChaining () |
| 57 | | | { |
| 58 | 0 | | final RuntimeException in = new RuntimeException("in"); |
| 59 | 0 | | final SAXException sax |
| 60 | | | = new org.xml.sax.SAXParseException("SAX", null, in); |
| 61 | 0 | | final JAXBException jaxb = new JAXBException(sax); |
| 62 | 0 | | final RuntimeException out = new RuntimeException("Outer", jaxb); |
| 63 | | | |
| 64 | 0 | | assertEquals("1st nesting level unexpected", jaxb, out.getCause()); |
| 65 | 0 | | assertEquals("2nd nesting level unexpected (pre chain)", |
| 66 | | | null, jaxb.getCause()); |
| 67 | 0 | | assertEquals("3rd nesting level unexpected (pre chain)", |
| 68 | | | null, sax.getCause()); |
| 69 | | | |
| 70 | 0 | | ThrowableUtil.fixChaining(out); |
| 71 | | | |
| 72 | 0 | | assertEquals("2nd nesting level unexpected", sax, jaxb.getCause()); |
| 73 | 0 | | assertEquals("3rd nesting level unexpected", in, sax.getCause()); |
| 74 | 0 | | } |
| 75 | | | |
| 76 | | | |
| 77 | | | public void testGetCauseDetection () |
| 78 | | | { |
| 79 | 50 | | getCauseDetectionTestHelper(SAXException.class, "getException"); |
| 80 | 0 | | getCauseDetectionTestHelper(ServletException.class, "getRootCause"); |
| 81 | 0 | | getCauseDetectionTestHelper(UnavailableException.class, "getRootCause"); |
| 82 | 0 | | getCauseDetectionTestHelper( |
| 83 | | | javax.resource.spi.UnavailableException.class, |
| 84 | | | "getLinkedException"); |
| 85 | 0 | | getCauseDetectionTestHelper(JaxenException.class, "getCause"); |
| 86 | 0 | | } |
| 87 | | | |
| 88 | | | {@link } |
| 89 | | | public void testCollectNestedData () |
| 90 | | | { |
| 91 | 100 | | final SAXException sax |
| 92 | | | = new org.xml.sax.SAXParseException( |
| 93 | | | "SAX", "public id", "system id", 5, 4); |
| 94 | 100 | | final ArgumentMalformedException ex |
| 95 | | | = new ArgumentMalformedException("TEST", "VALUE", "Hint", |
| 96 | | | sax); |
| 97 | 100 | | assertEquals("Parameter from nested Exception not found in list. " |
| 98 | | | + ex.getParameterNames(), "public id", |
| 99 | | | ex.getParameter( |
| 100 | | | "CAUSE_1_org.xml.sax.SAXParseException#PublicId").get(0)); |
| 101 | 100 | | } |
| 102 | | | |
| 103 | | | private void getCauseDetectionTestHelper (Class ex, String methodName) |
| 104 | | | { |
| 105 | 0 | | final Method m |
| 106 | | | = ThrowableUtil.findGetCauseMethod(ex.getMethods()); |
| 107 | 0 | | assertNotNull("Could not get getCause method for " + ex.getName(), m); |
| 108 | 0 | | assertEquals("Differen method expected for getCause in " + ex.getName(), |
| 109 | | | methodName, m.getName()); |
| 110 | 0 | | } |
| 111 | | | } |