Path: blob/master/test/jdk/javax/xml/jaxp/XPath/8009579/XPathExceptionInitCause.java
41155 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800957926* @summary The initCause() incorrectly initialise the cause in27* XPathException class when used with XPathException(String)28* constructor.29* @run main XPathExceptionInitCause30* @author [email protected]31*/3233import javax.xml.xpath.XPathException;34import java.io.ByteArrayOutputStream;35import java.io.ByteArrayInputStream;36import java.io.ObjectOutputStream;37import java.io.ObjectInputStream;38import java.io.IOException;39import java.io.InvalidClassException;404142public class XPathExceptionInitCause {4344/* This is a serial form of XPathException with two causes serialized45* by JDK7 code:46*47* ByteArrayOutputStream fser = new ByteArrayOutputStream();48* ObjectOutputStream oos = new ObjectOutputStream(fser);49* oos.writeObject(new XPathException(new Exception()).initCause(null));50* oos.close();51*/52static final byte [] TWOCAUSES = {-84,-19,0,5,115,114,0,30,106,97,118,97,120,46,120,53109,108,46,120,112,97,116,104,46,88,80,97,116,104,69,120,99,101,112,116,54105,111,110,-26,-127,97,60,-120,119,127,28,2,0,1,76,0,5,99,97,117,115,101,55116,0,21,76,106,97,118,97,47,108,97,110,103,47,84,104,114,111,119,97,98,56108,101,59,120,114,0,19,106,97,118,97,46,108,97,110,103,46,69,120,99,101,57112,116,105,111,110,-48,-3,31,62,26,59,28,-60,2,0,0,120,114,0,19,106,97,58118,97,46,108,97,110,103,46,84,104,114,111,119,97,98,108,101,-43,-58,53,5939,57,119,-72,-53,3,0,4,76,0,5,99,97,117,115,101,113,0,126,0,1,76,0,13,60100,101,116,97,105,108,77,101,115,115,97,103,101,116,0,18,76,106,97,118,6197,47,108,97,110,103,47,83,116,114,105,110,103,59,91,0,10,115,116,97,99,62107,84,114,97,99,101,116,0,30,91,76,106,97,118,97,47,108,97,110,103,47,83,63116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,59,76,0,20,115,64117,112,112,114,101,115,115,101,100,69,120,99,101,112,116,105,111,110,115,65116,0,16,76,106,97,118,97,47,117,116,105,108,47,76,105,115,116,59,120,112,66112,112,117,114,0,30,91,76,106,97,118,97,46,108,97,110,103,46,83,116,97,99,67107,84,114,97,99,101,69,108,101,109,101,110,116,59,2,70,42,60,60,-3,34,57,682,0,0,120,112,0,0,0,1,115,114,0,27,106,97,118,97,46,108,97,110,103,46,83,69116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,97,9,-59,-102,7038,54,-35,-123,2,0,4,73,0,10,108,105,110,101,78,117,109,98,101,114,76,0,7114,100,101,99,108,97,114,105,110,103,67,108,97,115,115,113,0,126,0,4,76,720,8,102,105,108,101,78,97,109,101,113,0,126,0,4,76,0,10,109,101,116,104,73111,100,78,97,109,101,113,0,126,0,4,120,112,0,0,0,31,116,0,23,88,80,97,116,74104,69,120,99,101,112,116,105,111,110,83,101,114,105,97,108,105,122,101,75116,0,28,88,80,97,116,104,69,120,99,101,112,116,105,111,110,83,101,114,105,7697,108,105,122,101,46,106,97,118,97,116,0,4,109,97,105,110,115,114,0,38,77106,97,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110,78115,36,85,110,109,111,100,105,102,105,97,98,108,101,76,105,115,116,-4,15,7937,49,-75,-20,-114,16,2,0,1,76,0,4,108,105,115,116,113,0,126,0,6,120,114,800,44,106,97,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,81110,115,36,85,110,109,111,100,105,102,105,97,98,108,101,67,111,108,108,101,8299,116,105,111,110,25,66,0,-128,-53,94,-9,30,2,0,1,76,0,1,99,116,0,22,76,83106,97,118,97,47,117,116,105,108,47,67,111,108,108,101,99,116,105,111,110,8459,120,112,115,114,0,19,106,97,118,97,46,117,116,105,108,46,65,114,114,97,85121,76,105,115,116,120,-127,-46,29,-103,-57,97,-99,3,0,1,73,0,4,115,105,86122,101,120,112,0,0,0,0,119,4,0,0,0,0,120,113,0,126,0,20,120,115,113,0,126,870,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,8831,113,0,126,0,12,113,0,126,0,13,113,0,126,0,14,113,0,126,0,18,12089};9091/* This is a serial form of ordinary XPathException serialized by JDK7 code:92*93* Throwable cause = new Throwable( "message 1" );94* XPathException xpathexcep = new XPathException( "message 2" );95* xpathexcep.initCause( cause );96* ByteArrayOutputStream fser = new ByteArrayOutputStream();97* ObjectOutputStream oos = new ObjectOutputStream(fser);98* oos.writeObject(xpathexcep);99* oos.close();100*/101static final byte [] NORMALJDK7SER = {-84,-19,0,5,115,114,0,30,106,97,118,97,120,10246,120,109,108,46,120,112,97,116,104,46,88,80,97,116,104,69,120,99,101,112,103116,105,111,110,-26,-127,97,60,-120,119,127,28,2,0,1,76,0,5,99,97,117,115,104101,116,0,21,76,106,97,118,97,47,108,97,110,103,47,84,104,114,111,119,97,10598,108,101,59,120,114,0,19,106,97,118,97,46,108,97,110,103,46,69,120,99,106101,112,116,105,111,110,-48,-3,31,62,26,59,28,-60,2,0,0,120,114,0,19,106,10797,118,97,46,108,97,110,103,46,84,104,114,111,119,97,98,108,101,-43,-58,10853,39,57,119,-72,-53,3,0,4,76,0,5,99,97,117,115,101,113,0,126,0,1,76,0,13,109100,101,116,97,105,108,77,101,115,115,97,103,101,116,0,18,76,106,97,118,11097,47,108,97,110,103,47,83,116,114,105,110,103,59,91,0,10,115,116,97,99,111107,84,114,97,99,101,116,0,30,91,76,106,97,118,97,47,108,97,110,103,47,83,112116,97,99,107,84,114,97,99,101,69,108,101,109,101,110,116,59,76,0,20,115,113117,112,112,114,101,115,115,101,100,69,120,99,101,112,116,105,111,110,115,114116,0,16,76,106,97,118,97,47,117,116,105,108,47,76,105,115,116,59,120,112,115115,113,0,126,0,3,113,0,126,0,8,116,0,9,109,101,115,115,97,103,101,32,49,116117,114,0,30,91,76,106,97,118,97,46,108,97,110,103,46,83,116,97,99,107,84,117114,97,99,101,69,108,101,109,101,110,116,59,2,70,42,60,60,-3,34,57,2,0,0,118120,112,0,0,0,1,115,114,0,27,106,97,118,97,46,108,97,110,103,46,83,116,97,11999,107,84,114,97,99,101,69,108,101,109,101,110,116,97,9,-59,-102,38,54,-35,120-123,2,0,4,73,0,10,108,105,110,101,78,117,109,98,101,114,76,0,14,100,101,12199,108,97,114,105,110,103,67,108,97,115,115,113,0,126,0,4,76,0,8,102,105,122108,101,78,97,109,101,113,0,126,0,4,76,0,10,109,101,116,104,111,100,78,97,123109,101,113,0,126,0,4,120,112,0,0,0,19,116,0,23,88,80,97,116,104,69,120,12499,101,112,116,105,111,110,83,101,114,105,97,108,105,122,101,116,0,28,88,12580,97,116,104,69,120,99,101,112,116,105,111,110,83,101,114,105,97,108,105,126122,101,46,106,97,118,97,116,0,4,109,97,105,110,115,114,0,38,106,97,118,12797,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110,115,36,85,128110,109,111,100,105,102,105,97,98,108,101,76,105,115,116,-4,15,37,49,-75,129-20,-114,16,2,0,1,76,0,4,108,105,115,116,113,0,126,0,6,120,114,0,44,106,13097,118,97,46,117,116,105,108,46,67,111,108,108,101,99,116,105,111,110,115,13136,85,110,109,111,100,105,102,105,97,98,108,101,67,111,108,108,101,99,116,132105,111,110,25,66,0,-128,-53,94,-9,30,2,0,1,76,0,1,99,116,0,22,76,106,97,133118,97,47,117,116,105,108,47,67,111,108,108,101,99,116,105,111,110,59,120,134112,115,114,0,19,106,97,118,97,46,117,116,105,108,46,65,114,114,97,121,76,135105,115,116,120,-127,-46,29,-103,-57,97,-99,3,0,1,73,0,4,115,105,122,101,136120,112,0,0,0,0,119,4,0,0,0,0,120,113,0,126,0,22,120,116,0,9,109,101,115,137115,97,103,101,32,50,117,113,0,126,0,10,0,0,0,1,115,113,0,126,0,12,0,0,0,13820,113,0,126,0,14,113,0,126,0,15,113,0,126,0,16,113,0,126,0,20,120,112139};140141//Serialize XPathException142static byte [] pickleXPE(XPathException xpe) throws IOException {143ByteArrayOutputStream bos = new ByteArrayOutputStream();144ObjectOutputStream xpeos = new ObjectOutputStream(bos);145xpeos.writeObject(xpe);146xpeos.close();147return bos.toByteArray();148}149150//Deserialize XPathException with byte array as serial data source151static XPathException unpickleXPE(byte [] ser)152throws IOException, ClassNotFoundException {153XPathException xpe;154ByteArrayInputStream bis = new ByteArrayInputStream(ser);155ObjectInputStream xpeis = new ObjectInputStream(bis);156xpe = (XPathException) xpeis.readObject();157xpeis.close();158return xpe;159}160161public static void main(String[] args) throws Exception {162Throwable cause = new Throwable("message 1");163XPathException xpathexcep = new XPathException("message 2");164165//Test XPE initCause() method166xpathexcep.initCause(cause);167System.out.println("getCause() result: '" + xpathexcep.getCause()168+ "' Cause itself: '" + cause + "'");169if (!xpathexcep.getCause().toString().equals(cause.toString())) {170throw new Exception("Incorrect cause is set by initCause()");171}172173//Test serialization/deserialization of initialized XPE174byte [] xpeserial;175XPathException xpedeser;176xpeserial = pickleXPE(xpathexcep);177xpedeser = unpickleXPE(xpeserial);178System.out.println("Serialized XPE: message='" + xpathexcep.getMessage()179+ "' cause='" + xpathexcep.getCause().toString() + "'");180System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()181+ "' cause='" + xpedeser.getCause().toString()+"'");182if(xpedeser.getCause() == null ||183!xpedeser.getCause().toString().equals(cause.toString()) ||184!xpedeser.getMessage().toString().equals("message 2") )185throw new Exception("XPathException incorrectly serialized/deserialized");186187//Test serialization/deserialization of uninitialized cause in XPE188XPathException xpeuninit = new XPathException("uninitialized cause");189xpeserial = pickleXPE(xpeuninit);190xpedeser = unpickleXPE(xpeserial);191System.out.println("Serialized XPE: message='" + xpeuninit.getMessage()192+ "' cause='" + xpeuninit.getCause()+"'");193System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()194+ "' cause='" + xpedeser.getCause()+"'");195if(xpedeser.getCause() != null ||196!xpedeser.getMessage().toString().equals("uninitialized cause") )197throw new Exception("XPathException incorrectly serialized/deserialized");198199//Test deserialization of normal XPathException serialized by JDK7200XPathException xpejdk7 = unpickleXPE(NORMALJDK7SER);201if(xpejdk7 == null || xpejdk7.getCause() == null ||202!xpejdk7.getMessage().equals("message 2") ||203!xpejdk7.getCause().getMessage().equals("message 1"))204throw new Exception("XpathException serialized by JDK7 was "205+ "incorrectly deserialized.");206207//Test deserialization of XPathException with two causes from JDK7.208//The serialization are done for the following XPathException object:209// new XPathException(new Exception()).initCause(null)210try {211xpejdk7 = unpickleXPE(TWOCAUSES);212throw new Exception("Expected InvalidClassException but it wasn't"213+ " observed");214} catch(InvalidClassException e) {215System.out.println("InvalidClassException caught as expected.");216}217218}219}220221222