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.FileInputStream;
  17 import java.io.IOException;
  18 import java.io.InvalidClassException;
  19 
  20 public class XPathExceptionInitCause {
  21 
  22     //Serialize XPathException
  23     static byte [] pickleXPE(XPathException xpe) throws IOException {
  24         ByteArrayOutputStream bos =  new ByteArrayOutputStream();
  25         ObjectOutputStream xpeos = new ObjectOutputStream(bos);
  26         xpeos.writeObject(xpe);
  27         xpeos.close();
  28         return bos.toByteArray();
  29     }
  30 
  31     //Deserialize XPathException with byte array as serial data source
  32     static XPathException unpickleXPE(byte [] ser)
  33             throws IOException, ClassNotFoundException {
  34         XPathException xpe;
  35         ByteArrayInputStream bis = new ByteArrayInputStream(ser);
  36         ObjectInputStream xpeis = new ObjectInputStream(bis);
  37         xpe = (XPathException) xpeis.readObject();
  38         xpeis.close();
  39         return xpe;
  40     }
  41     
  42     //Deserialize XPathException with file as serial data source
  43     static XPathException unpickleXPE(String sfile)
  44             throws IOException, ClassNotFoundException {
  45         XPathException xpe;
  46         FileInputStream fis = new FileInputStream(sfile);
  47         ObjectInputStream xpeis = new ObjectInputStream(fis);
  48         xpe = (XPathException) xpeis.readObject();
  49         xpeis.close();
  50         return xpe;
  51     }
  52 
  53     public static void main(String[] args) throws Exception {
  54         Throwable cause = new Throwable("message 1");
  55         XPathException xpathexcep = new XPathException("message 2");
  56 
  57         //Test XPE initCause() method 
  58         xpathexcep.initCause(cause);
  59         System.out.println("getCause() result: '" + xpathexcep.getCause()
  60                 + "' Cause itself: '" + cause + "'");
  61         if (!xpathexcep.getCause().toString().equals(cause.toString())) {
  62             throw new Exception("Incorrect cause is set by initCause()");
  63         }
  64 
  65         //Test serialization/deserialization of initialized XPE
  66         byte [] xpeserial;
  67         XPathException xpedeser;
  68         xpeserial = pickleXPE(xpathexcep);
  69         xpedeser = unpickleXPE(xpeserial);
  70         System.out.println("Serialized XPE: message='" + xpathexcep.getMessage()
  71                 + "' cause='" + xpathexcep.getCause().toString() + "'");
  72         System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()
  73                 + "' cause='" + xpedeser.getCause().toString()+"'");
  74         if(xpedeser.getCause() == null || 
  75                 !xpedeser.getCause().toString().equals(cause.toString()) ||
  76                 !xpedeser.getMessage().toString().equals("message 2") )
  77             throw new Exception("XPathException incorrectly serialized/deserialized");
  78 
  79         //Test serialization/deserialization of uninitialized cause in XPE
  80         XPathException xpeuninit = new XPathException("uninitialized cause");
  81         xpeserial = pickleXPE(xpeuninit);
  82         xpedeser = unpickleXPE(xpeserial);
  83         System.out.println("Serialized XPE: message='" + xpeuninit.getMessage()
  84                 + "' cause='" + xpeuninit.getCause()+"'");
  85         System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()
  86                 + "' cause='" + xpedeser.getCause()+"'");
  87         if(xpedeser.getCause() != null ||
  88                 !xpedeser.getMessage().toString().equals("uninitialized cause") )
  89             throw new Exception("XPathException incorrectly serialized/deserialized");
  90 
  91         //Test deserialization of normal XPathException serialized by JDK7
  92         XPathException xpejdk7 = unpickleXPE(System.getProperty("test.src", ".")
  93                                + "/normal_jdk7.ser");
  94         if(xpejdk7 == null || xpejdk7.getCause() == null || 
  95                 !xpejdk7.getMessage().equals("message 2") ||
  96                 !xpejdk7.getCause().getMessage().equals("message 1"))
  97             throw new Exception("XpathException serialized by JDK7 was "
  98                     + "incorrectly deserialized.");
  99 
 100         //Test deserialization of XPathException with two causes from JDK7.
 101         //The serialization are done for the following XPathException object:
 102         // new XPathException(new Exception()).initCause(null)
 103         try {
 104             xpejdk7 = unpickleXPE(System.getProperty("test.src", ".") 
 105                     + "/twocauses_jdk7.ser");
 106             throw new Exception("Expected InvalidClassException but it wasn't"
 107                     + " observed");
 108         } catch(InvalidClassException e) {
 109             System.out.println("InvalidClassException caught as expected.");
 110         }
 111 
 112     }
 113 }