1 /* 2 * Copyright (c) 2016, 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 import java.security.Permission; 24 import java.security.Policy; 25 import java.security.ProtectionDomain; 26 import java.util.Map; 27 import javax.xml.bind.JAXBContext; 28 import javax.xml.bind.JAXBContextFactory; 29 import javax.xml.bind.JAXBException; 30 import javax.xml.bind.Marshaller; 31 import javax.xml.bind.Unmarshaller; 32 import javax.xml.bind.Validator; 33 34 /** 35 * @test 36 * @bug 8150173 37 * @summary Verifies that a factory which inherit its createContext method 38 * from a concrete subclass of JAXBContextFactory is be instantiated. 39 * @compile -addmods java.xml.bind JAXBContextWithSubclassedFactory.java 40 * @run main/othervm -addmods java.xml.bind JAXBContextWithSubclassedFactory 41 */ 42 public class JAXBContextWithSubclassedFactory { 43 static JAXBContext tmp; 44 45 public static class JAXBContextImpl extends JAXBContext { 46 public final Class<?> creator; 47 JAXBContextImpl(Class<?> creator) { 48 this.creator = creator; 49 } 50 51 @Override 52 public Unmarshaller createUnmarshaller() throws JAXBException { 53 return tmp.createUnmarshaller(); 54 } 55 56 @Override 57 public Marshaller createMarshaller() throws JAXBException { 58 return tmp.createMarshaller(); 59 } 60 61 @Override 62 public Validator createValidator() throws JAXBException { 63 return tmp.createValidator(); 64 } 65 } 66 67 public static class FactoryBase implements JAXBContextFactory { 68 69 @Override 70 public JAXBContext createContext(Class<?>[] classesToBeBound, 71 Map<String, ?> properties) throws JAXBException { 72 return new JAXBContextImpl(this.getClass()); 73 } 74 75 @Override 76 public JAXBContext createContext(String contextPath, 77 ClassLoader classLoader, Map<String, ?> properties) 78 throws JAXBException { 79 return new JAXBContextImpl(this.getClass()); 80 } 81 } 82 83 public static class NonFactoryBase { 84 85 public JAXBContext createContext(Class<?>[] classesToBeBound, 86 Map<String, ?> properties) throws JAXBException { 87 return new JAXBContextImpl(this.getClass()); 88 } 89 90 public JAXBContext createContext(String contextPath, 91 ClassLoader classLoader, Map<String, ?> properties) 92 throws JAXBException { 93 return new JAXBContextImpl(this.getClass()); 94 } 95 } 96 97 public static class Factory1 extends FactoryBase {} 98 public static class Factory2 extends NonFactoryBase implements JAXBContextFactory {} 99 100 // test both without and then with a security manager as the code path 101 // can be different when System.getSecurityManager() != null; 102 public static void main(String[] args) throws JAXBException { 103 System.out.println("\nWithout security manager\n"); 104 test(FactoryBase.class); 105 test(Factory1.class); 106 test(Factory2.class); 107 108 System.out.println("\nWith security manager\n"); 109 Policy.setPolicy(new Policy() { 110 @Override 111 public boolean implies(ProtectionDomain domain, Permission permission) { 112 return true; // allow all 113 } 114 }); 115 System.setSecurityManager(new SecurityManager()); 116 test(FactoryBase.class); 117 test(Factory1.class); 118 test(Factory2.class); 119 } 120 121 public static void test(Class<?> factoryClass) throws JAXBException { 122 System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY); 123 System.out.println("** Testing with Factory Class: " + factoryClass.getName()); 124 System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = " 125 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY, "")); 126 System.out.println("Calling " 127 + "JAXBContext.newInstance(JAXBContextWithSubclassedFactory.class)"); 128 tmp = JAXBContext.newInstance(JAXBContextWithSubclassedFactory.class); 129 System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY, 130 factoryClass.getName()); 131 System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = " 132 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY)); 133 System.out.println("Calling " 134 + "JAXBContext.newInstance(JAXBContextWithSubclassedFactory.class)"); 135 JAXBContext ctxt = JAXBContext.newInstance(JAXBContextWithSubclassedFactory.class); 136 System.out.println("Successfully loaded JAXBcontext: " + 137 System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName()); 138 if (ctxt.getClass() != JAXBContextImpl.class) { 139 throw new RuntimeException("Wrong JAXBContext class" 140 + "\n\texpected: " 141 + System.identityHashCode(tmp) + "@" + JAXBContextImpl.class.getName() 142 + "\n\tactual: " 143 + System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName()); 144 } 145 if (((JAXBContextImpl)ctxt).creator != factoryClass) { 146 throw new RuntimeException("Wrong Factory class" 147 + "\n\texpected: " 148 + System.identityHashCode(tmp) + "@" + factoryClass.getName() 149 + "\n\tactual: " 150 + System.identityHashCode(ctxt) + "@" + ((JAXBContextImpl)ctxt).creator.getName()); 151 } 152 } 153 }