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 
  31 /**
  32  * @test
  33  * @bug 8150173
  34  * @summary Verifies that a factory which inherit its createContext method
  35  *          from an abstract subclass of JAXBContextFactory can be instantiated.
  36  * @modules java.xml.bind
  37  * @run main/othervm JAXBContextWithAbstractFactory
  38  */
  39 public class JAXBContextWithAbstractFactory {
  40     private static JAXBContext tmp;
  41 
  42     public static abstract class FactoryBase implements JAXBContextFactory {
  43         @Override
  44         public JAXBContext createContext(Class<?>[] classesToBeBound,
  45                 Map<String, ?> properties) throws JAXBException {
  46             return tmp;
  47         }
  48 
  49         @Override
  50         public JAXBContext createContext(String contextPath,
  51                 ClassLoader classLoader, Map<String, ?> properties)
  52                 throws JAXBException {
  53             return tmp;
  54         }
  55     }
  56 
  57     public static class Factory extends FactoryBase {}
  58 
  59     // test both without and then with a security manager as the code path
  60     // can be different when System.getSecurityManager() != null;
  61     public static void main(String[] args) throws JAXBException {
  62         System.out.println("\nWithout security manager\n");
  63         test();
  64 
  65         System.out.println("\nWith security manager\n");
  66         Policy.setPolicy(new Policy() {
  67             @Override
  68             public boolean implies(ProtectionDomain domain, Permission permission) {
  69                 return true; // allow all
  70             }
  71         });
  72         System.setSecurityManager(new SecurityManager());
  73         test();
  74     }
  75 
  76     public static void test() throws JAXBException {
  77         System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
  78         System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = "
  79                 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY, ""));
  80         System.out.println("Calling "
  81                 + "JAXBContext.newInstance(JAXBContextWithAbstractFactory.class)");
  82         tmp = JAXBContext.newInstance(JAXBContextWithAbstractFactory.class);
  83         System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY,
  84                 "JAXBContextWithAbstractFactory$Factory");
  85         System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = "
  86                 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY));
  87         System.out.println("Calling "
  88                 + "JAXBContext.newInstance(JAXBContextWithAbstractFactory.class)");
  89         JAXBContext ctxt = JAXBContext.newInstance(JAXBContextWithAbstractFactory.class);
  90         System.out.println("Successfully loaded JAXBcontext: " +
  91                 System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName());
  92         if (ctxt != tmp) {
  93             throw new RuntimeException("Wrong JAXBContext instance"
  94                 + "\n\texpected: "
  95                 + System.identityHashCode(tmp) + "@" + tmp.getClass().getName()
  96                 + "\n\tactual:   "
  97                 + System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName());
  98         }
  99     }
 100 }