1 /* 2 * @test 3 * @bug 8009579 4 * @summary The initCause() incorrectly initialise the cause in 5 * XPathException class when used with XPathException(String) 6 * constructor. 7 * @run main XPathExceptionInitCause 8 * @author aleksej.efimov@oracle.com 9 */ 10 11 import javax.xml.xpath.XPathException; 12 import java.io.ByteArrayOutputStream; 13 import java.io.ByteArrayInputStream; 14 import java.io.ObjectOutputStream; 15 import java.io.ObjectInputStream; 16 import java.io.IOException; 17 import java.io.InvalidClassException; 18 19 20 21 22 public class XPathExceptionInitCause { 23 24 static byte [] twocauses = {-84,-19,0,5,115,114,0,30,106,97,118,97,120,46,120, 25 109,108,46,120,112,97,116,104,46,88,80,97,116,104,69,120,99,101,112,116, 26 105,111,110,-26,-127,97,60,-120,119,127,28,2,0,1,76,0,5,99,97,117,115,101, 27 116,0,21,76,106,97,118,97,47,108,97,110,103,47,84,104,114,111,119,97,98, 28 108,101,59,120,114,0,19,106,97,118,97,46,108,97,110,103,46,69,120,99,101, 29 112,116,105,111,110,-48,-3,31,62,26,59,28,-60,2,0,0,120,114,0,19,106,97, 30 118,97,46,108,97,110,103,46,84,104,114,111,119,97,98,108,101,-43,-58,53, 31 39,57,119,-72,-53,3,0,4,76,0,5,99,97,117,115,101,113,0,126,0,1,76,0,13, 32 100,101,116,97,105,108,77,101,115,115,97,103,101,116,0,18,76,106,97,118, 33 97,47,108,97,110,103,47,83,116,114,105,110,103,59,91,0,10,115,116,97,99, 34 107,84,114,97,99,101,116,0,30,91,76,106,97,118,97,47,108,97,110,103,47,83, 35 116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,59,76,0,20,115, 36 117,112,112,114,101,115,115,101,100,69,120,99,101,112,116,105,111,110,115, 37 116,0,16,76,106,97,118,97,47,117,116,105,108,47,76,105,115,116,59,120,112, 38 112,112,117,114,0,30,91,76,106,97,118,97,46,108,97,110,103,46,83,116,97,99, 39 107,84,114,97,99,101,69,108,101,109,101,110,116,59,2,70,42,60,60,-3,34,57, 40 2,0,0,120,112,0,0,0,1,115,114,0,27,106,97,118,97,46,108,97,110,103,46,83, 41 116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,97,9,-59,-102, 42 38,54,-35,-123,2,0,4,73,0,10,108,105,110,101,78,117,109,98,101,114,76,0, 43 14,100,101,99,108,97,114,105,110,103,67,108,97,115,115,113,0,126,0,4,76, 44 0,8,102,105,108,101,78,97,109,101,113,0,126,0,4,76,0,10,109,101,116,104, 45 111,100,78,97,109,101,113,0,126,0,4,120,112,0,0,0,31,116,0,23,88,80,97,116, 46 104,69,120,99,101,112,116,105,111,110,83,101,114,105,97,108,105,122,101, 47 116,0,28,88,80,97,116,104,69,120,99,101,112,116,105,111,110,83,101,114,105, 48 97,108,105,122,101,46,106,97,118,97,116,0,4,109,97,105,110,115,114,0,38, 49 106,97,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110, 50 115,36,85,110,109,111,100,105,102,105,97,98,108,101,76,105,115,116,-4,15, 51 37,49,-75,-20,-114,16,2,0,1,76,0,4,108,105,115,116,113,0,126,0,6,120,114, 52 0,44,106,97,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111, 53 110,115,36,85,110,109,111,100,105,102,105,97,98,108,101,67,111,108,108,101, 54 99,116,105,111,110,25,66,0,-128,-53,94,-9,30,2,0,1,76,0,1,99,116,0,22,76, 55 106,97,118,97,47,117,116,105,108,47,67,111,108,108,101,99,116,105,111,110, 56 59,120,112,115,114,0,19,106,97,118,97,46,117,116,105,108,46,65,114,114,97, 57 121,76,105,115,116,120,-127,-46,29,-103,-57,97,-99,3,0,1,73,0,4,115,105, 58 122,101,120,112,0,0,0,0,119,4,0,0,0,0,120,113,0,126,0,20,120,115,113,0,126, 59 0,2,113,0,126,0,21,112,117,113,0,126,0,8,0,0,0,1,115,113,0,126,0,10,0,0,0, 60 31,113,0,126,0,12,113,0,126,0,13,113,0,126,0,14,113,0,126,0,18,120 61 }; 62 63 static byte [] normaljdk7ser = {-84,-19,0,5,115,114,0,30,106,97,118,97,120, 64 46,120,109,108,46,120,112,97,116,104,46,88,80,97,116,104,69,120,99,101,112, 65 116,105,111,110,-26,-127,97,60,-120,119,127,28,2,0,1,76,0,5,99,97,117,115, 66 101,116,0,21,76,106,97,118,97,47,108,97,110,103,47,84,104,114,111,119,97, 67 98,108,101,59,120,114,0,19,106,97,118,97,46,108,97,110,103,46,69,120,99, 68 101,112,116,105,111,110,-48,-3,31,62,26,59,28,-60,2,0,0,120,114,0,19,106, 69 97,118,97,46,108,97,110,103,46,84,104,114,111,119,97,98,108,101,-43,-58, 70 53,39,57,119,-72,-53,3,0,4,76,0,5,99,97,117,115,101,113,0,126,0,1,76,0,13, 71 100,101,116,97,105,108,77,101,115,115,97,103,101,116,0,18,76,106,97,118, 72 97,47,108,97,110,103,47,83,116,114,105,110,103,59,91,0,10,115,116,97,99, 73 107,84,114,97,99,101,116,0,30,91,76,106,97,118,97,47,108,97,110,103,47,83, 74 116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,59,76,0,20,115, 75 117,112,112,114,101,115,115,101,100,69,120,99,101,112,116,105,111,110,115, 76 116,0,16,76,106,97,118,97,47,117,116,105,108,47,76,105,115,116,59,120,112, 77 115,113,0,126,0,3,113,0,126,0,8,116,0,9,109,101,115,115,97,103,101,32,49, 78 117,114,0,30,91,76,106,97,118,97,46,108,97,110,103,46,83,116,97,99,107,84, 79 114,97,99,101,69,108,101,109,101,110,116,59,2,70,42,60,60,-3,34,57,2,0,0, 80 120,112,0,0,0,1,115,114,0,27,106,97,118,97,46,108,97,110,103,46,83,116,97, 81 99,107,84,114,97,99,101,69,108,101,109,101,110,116,97,9,-59,-102,38,54,-35, 82 -123,2,0,4,73,0,10,108,105,110,101,78,117,109,98,101,114,76,0,14,100,101, 83 99,108,97,114,105,110,103,67,108,97,115,115,113,0,126,0,4,76,0,8,102,105, 84 108,101,78,97,109,101,113,0,126,0,4,76,0,10,109,101,116,104,111,100,78,97, 85 109,101,113,0,126,0,4,120,112,0,0,0,19,116,0,23,88,80,97,116,104,69,120, 86 99,101,112,116,105,111,110,83,101,114,105,97,108,105,122,101,116,0,28,88, 87 80,97,116,104,69,120,99,101,112,116,105,111,110,83,101,114,105,97,108,105, 88 122,101,46,106,97,118,97,116,0,4,109,97,105,110,115,114,0,38,106,97,118, 89 97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110,115,36,85, 90 110,109,111,100,105,102,105,97,98,108,101,76,105,115,116,-4,15,37,49,-75, 91 -20,-114,16,2,0,1,76,0,4,108,105,115,116,113,0,126,0,6,120,114,0,44,106, 92 97,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110,115, 93 36,85,110,109,111,100,105,102,105,97,98,108,101,67,111,108,108,101,99,116, 94 105,111,110,25,66,0,-128,-53,94,-9,30,2,0,1,76,0,1,99,116,0,22,76,106,97, 95 118,97,47,117,116,105,108,47,67,111,108,108,101,99,116,105,111,110,59,120, 96 112,115,114,0,19,106,97,118,97,46,117,116,105,108,46,65,114,114,97,121,76, 97 105,115,116,120,-127,-46,29,-103,-57,97,-99,3,0,1,73,0,4,115,105,122,101, 98 120,112,0,0,0,0,119,4,0,0,0,0,120,113,0,126,0,22,120,116,0,9,109,101,115, 99 115,97,103,101,32,50,117,113,0,126,0,10,0,0,0,1,115,113,0,126,0,12,0,0,0, 100 20,113,0,126,0,14,113,0,126,0,15,113,0,126,0,16,113,0,126,0,20,120,112 101 }; 102 103 //Serialize XPathException 104 static byte [] pickleXPE(XPathException xpe) throws IOException { 105 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 106 ObjectOutputStream xpeos = new ObjectOutputStream(bos); 107 xpeos.writeObject(xpe); 108 xpeos.close(); 109 return bos.toByteArray(); 110 } 111 112 //Deserialize XPathException with byte array as serial data source 113 static XPathException unpickleXPE(byte [] ser) 114 throws IOException, ClassNotFoundException { 115 XPathException xpe; 116 ByteArrayInputStream bis = new ByteArrayInputStream(ser); 117 ObjectInputStream xpeis = new ObjectInputStream(bis); 118 xpe = (XPathException) xpeis.readObject(); 119 xpeis.close(); 120 return xpe; 121 } 122 123 public static void main(String[] args) throws Exception { 124 Throwable cause = new Throwable("message 1"); 125 XPathException xpathexcep = new XPathException("message 2"); 126 127 //Test XPE initCause() method 128 xpathexcep.initCause(cause); 129 System.out.println("getCause() result: '" + xpathexcep.getCause() 130 + "' Cause itself: '" + cause + "'"); 131 if (!xpathexcep.getCause().toString().equals(cause.toString())) { 132 throw new Exception("Incorrect cause is set by initCause()"); 133 } 134 135 //Test serialization/deserialization of initialized XPE 136 byte [] xpeserial; 137 XPathException xpedeser; 138 xpeserial = pickleXPE(xpathexcep); 139 xpedeser = unpickleXPE(xpeserial); 140 System.out.println("Serialized XPE: message='" + xpathexcep.getMessage() 141 + "' cause='" + xpathexcep.getCause().toString() + "'"); 142 System.out.println("Deserialized XPE: message='" + xpedeser.getMessage() 143 + "' cause='" + xpedeser.getCause().toString()+"'"); 144 if(xpedeser.getCause() == null || 145 !xpedeser.getCause().toString().equals(cause.toString()) || 146 !xpedeser.getMessage().toString().equals("message 2") ) 147 throw new Exception("XPathException incorrectly serialized/deserialized"); 148 149 //Test serialization/deserialization of uninitialized cause in XPE 150 XPathException xpeuninit = new XPathException("uninitialized cause"); 151 xpeserial = pickleXPE(xpeuninit); 152 xpedeser = unpickleXPE(xpeserial); 153 System.out.println("Serialized XPE: message='" + xpeuninit.getMessage() 154 + "' cause='" + xpeuninit.getCause()+"'"); 155 System.out.println("Deserialized XPE: message='" + xpedeser.getMessage() 156 + "' cause='" + xpedeser.getCause()+"'"); 157 if(xpedeser.getCause() != null || 158 !xpedeser.getMessage().toString().equals("uninitialized cause") ) 159 throw new Exception("XPathException incorrectly serialized/deserialized"); 160 161 //Test deserialization of normal XPathException serialized by JDK7 162 XPathException xpejdk7 = unpickleXPE(normaljdk7ser); 163 if(xpejdk7 == null || xpejdk7.getCause() == null || 164 !xpejdk7.getMessage().equals("message 2") || 165 !xpejdk7.getCause().getMessage().equals("message 1")) 166 throw new Exception("XpathException serialized by JDK7 was " 167 + "incorrectly deserialized."); 168 169 //Test deserialization of XPathException with two causes from JDK7. 170 //The serialization are done for the following XPathException object: 171 // new XPathException(new Exception()).initCause(null) 172 try { 173 xpejdk7 = unpickleXPE(twocauses); 174 throw new Exception("Expected InvalidClassException but it wasn't" 175 + " observed"); 176 } catch(InvalidClassException e) { 177 System.out.println("InvalidClassException caught as expected."); 178 } 179 180 } 181 }