1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8145112
  27  * @summary newInstance(String,ClassLoader): java.lang.JAXBException should not be wrapped as expected according to spec
  28  * @run main/othervm JAXBContextWrapExceptionTest
  29  */
  30 import javax.xml.bind.JAXBContext;
  31 import javax.xml.bind.JAXBException;
  32 import java.util.Map;
  33 
  34 public class JAXBContextWrapExceptionTest {
  35 
  36     public static class Factory {
  37 
  38         public static JAXBContext createContext(Class[] classesToBeBound, Map<String, ?> properties) throws JAXBException {
  39             throw new JAXBException("test");
  40         }
  41 
  42         public static JAXBContext createContext(String contextPath, ClassLoader classLoader, Map<String, ?> properties)
  43                 throws JAXBException {
  44             throw new JAXBException("test");
  45         }
  46     }
  47 
  48     public void testContextPath() {
  49         try {
  50             JAXBContext.newInstance("whatever", ClassLoader.getSystemClassLoader());
  51             fail("This should fail");
  52         } catch (Throwable t) {
  53             assertEquals("test", t.getMessage());
  54             assertNull("Root cause must be null", t.getCause());
  55         }
  56     }
  57 
  58     public void testClasses() {
  59         try {
  60             JAXBContext.newInstance(new Class[0]);
  61             fail("This should fail");
  62         } catch (Throwable t) {
  63             assertEquals("test", t.getMessage());
  64             assertNull("Root cause must be null", t.getCause());
  65         }
  66     }
  67 
  68     public void setup() {
  69         System.setProperty("javax.xml.bind.JAXBContext", "JAXBContextWrapExceptionTest$Factory");
  70     }
  71 
  72     public static void main(String[] args) throws JAXBException {
  73         JAXBContextWrapExceptionTest tst = new JAXBContextWrapExceptionTest();
  74         tst.setup();
  75         tst.testContextPath();
  76         tst.testClasses();
  77     }
  78 
  79     private void fail(String msg) {
  80         throw new Error(msg);
  81     }
  82 
  83     private void assertNull(String msg, Object o) {
  84         if (o != null) fail(msg);
  85     }
  86 
  87     private void assertEquals(Object a, Object b) {
  88 
  89         if (a == null) {
  90             if (b != null) fail("AssertEquals failed -  expected: [" + a + "], actual: [" + b + "]");
  91 
  92         } else if (!a.equals(b)) {
  93             fail("AssertEquals failed -  expected: [" + a + "], actual: [" + b + "]");
  94         }
  95     }
  96 
  97 }