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 import javax.xml.soap.MessageFactory;
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;
  30 import java.nio.file.StandardOpenOption;
  31 
  32 /*
  33  * @test
  34  * @bug 8131334
  35  * @summary SAAJ Plugability Layer: using java.util.ServiceLoader
  36  *
  37  * There are unsafe scenarios not to be run within jdk build (relying on global jdk confguration)
  38  *
  39  * unsafe; not running:
  40  *
  41  * run main/othervm SAAJFactoryTest saaj.factory.Valid -
  42  *      scenario1 javax.xml.soap.MessageFactory=saaj.factory.Valid -
  43  * run main/othervm SAAJFactoryTest - javax.xml.soap.SOAPException
  44  *      scenario3 javax.xml.soap.MessageFactory=non.existing.FactoryClass -
  45  * run main/othervm SAAJFactoryTest - javax.xml.soap.SOAPException
  46  *      scenario4 javax.xml.soap.MessageFactory=saaj.factory.Invalid -
  47  * run main/othervm -Djavax.xml.soap.MessageFactory=saaj.factory.Valid3 SAAJFactoryTest saaj.factory.Valid3 -
  48  *      scenario13 javax.xml.soap.MessageFactory=saaj.factory.Valid saaj.factory.Valid2
  49  * run main/othervm SAAJFactoryTest saaj.factory.Valid -
  50  *      scenario14 javax.xml.soap.MessageFactory=saaj.factory.Valid saaj.factory.Valid2 -
  51  *
  52  * @compile -addmods java.xml.ws saaj/factory/Invalid.java saaj/factory/Valid.java
  53  *     saaj/factory/Valid2.java saaj/factory/Valid3.java SAAJFactoryTest.java
  54  *
  55  * @run main/othervm -addmods java.xml.ws
  56  *      SAAJFactoryTest com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl - scenario2 - -
  57  * @run main/othervm -addmods java.xml.ws -Djavax.xml.soap.MessageFactory=saaj.factory.Valid
  58  *      SAAJFactoryTest saaj.factory.Valid - scenario5 - -
  59  * @run main/othervm -addmods java.xml.ws -Djavax.xml.soap.MessageFactory=saaj.factory.NonExisting
  60  *      SAAJFactoryTest - javax.xml.soap.SOAPException scenario6 - -
  61  * @run main/othervm -addmods java.xml.ws -Djavax.xml.soap.MessageFactory=saaj.factory.Invalid
  62  *      SAAJFactoryTest - javax.xml.soap.SOAPException scenario7 - -
  63  * @run main/othervm  -addmods java.xml.ws
  64  *      SAAJFactoryTest saaj.factory.Valid - scenario8 - saaj.factory.Valid
  65  * @run main/othervm -addmods java.xml.ws
  66  *      SAAJFactoryTest saaj.factory.Valid - scenario9 - saaj.factory.Valid
  67  * @run main/othervm -addmods java.xml.ws
  68  *      SAAJFactoryTest - javax.xml.soap.SOAPException scenario10 - saaj.factory.NonExisting
  69  * @run main/othervm -addmods java.xml.ws
  70  *      SAAJFactoryTest - javax.xml.soap.SOAPException scenario11 - saaj.factory.Invalid scenario11 - saaj.factory.Invalid
  71  * @run main/othervm  -addmods java.xml.ws
  72  *      SAAJFactoryTest com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl - scenario12 - -
  73  * @run main/othervm -addmods java.xml.ws
  74  *      SAAJFactoryTest saaj.factory.Valid - scenario15 - saaj.factory.Valid
  75  */
  76 public class SAAJFactoryTest {
  77 
  78     // scenario name - just for logging
  79     static String scenario;
  80 
  81     // configuration to be created by the test
  82     static Path providersDir = Paths.get(System.getProperty("test.classes"), "META-INF", "services");
  83     static Path providersFile = providersDir.resolve("javax.xml.soap.MessageFactory");
  84 
  85     // configuration to be created by the test
  86     static Path jdkDir = Paths.get(System.getProperty("java.home"), "conf");
  87     static Path jdkFile = jdkDir.resolve("jaxm.properties");
  88 
  89     // java policy file for testing w/security manager
  90     static String policy = System.getProperty("test.src", ".") + File.separator + "test.policy";
  91 
  92 
  93     protected MessageFactory factory() throws Throwable {
  94         try {
  95             MessageFactory factory = MessageFactory.newInstance();
  96             System.out.println("     TEST: factory class = [" + factory.getClass().getName() + "]\n");
  97             return factory;
  98         } catch (Throwable t) {
  99             System.out.println("     TEST: Throwable [" + t.getClass().getName() + "] thrown.\n");
 100             t.printStackTrace();
 101             throw t;
 102         }
 103     }
 104 
 105     protected void test(String[] args) {
 106         if (args.length < 5) throw new IllegalArgumentException("Incorrect test setup. Required 5 arguments: \n" +
 107                 "   1. expected factory class (if any)\n" +
 108                 "   2. expected \n" +
 109                 "   3. scenario name\n" +
 110                 "   4. jdk/conf configured provider class name\n" +
 111                 "   5. service loader provider class name");
 112 
 113         scenario = args[2]; // scenario name
 114         prepare(args[3], args[4]); // jdk/conf class, service loader class
 115 
 116         try {
 117             MessageFactory factory = factory();
 118             assertTrue(factory != null, "No factory found.");
 119             String className = factory.getClass().getName();
 120             assertTrue(args[0].equals(className), "Incorrect factory: [" + className +
 121                     "], Expected: [" + args[0] + "]");
 122 
 123         } catch (Throwable throwable) {
 124             String expectedExceptionClass = args[1];
 125             String throwableClass = throwable.getClass().getName();
 126             boolean correctException = throwableClass.equals(expectedExceptionClass);
 127             if (!correctException) {
 128                 throwable.printStackTrace();
 129             }
 130             assertTrue(correctException, "Got unexpected exception: [" +
 131                     throwableClass + "], expected: [" + expectedExceptionClass + "]");
 132         } finally {
 133             cleanResource(providersFile);
 134             cleanResource(providersDir);
 135 
 136             // unsafe; not running:
 137             // cleanResource(jdkFile);
 138         }
 139     }
 140 
 141     private void cleanResource(Path resource) {
 142         try {
 143             Files.deleteIfExists(resource);
 144         } catch (IOException ignored) {
 145             ignored.printStackTrace();
 146         }
 147     }
 148 
 149     private void prepare(String propertiesClassName, String providerClassName) {
 150 
 151         try {
 152             log("providerClassName = " + providerClassName);
 153             log("propertiesClassName = " + propertiesClassName);
 154 
 155             setupFile(providersFile, providersDir, providerClassName);
 156 
 157             // unsafe; not running:
 158             //setupFile(jdkFile, jdkDir, propertiesClassName);
 159 
 160             log(" SETUP OK.");
 161 
 162         } catch (IOException e) {
 163             log(" SETUP FAILED.");
 164             e.printStackTrace();
 165         }
 166     }
 167 
 168     private void setupFile(Path file, Path dir, String value) throws IOException {
 169         cleanResource(file);
 170         if (!"-".equals(value)) {
 171             log("writing configuration [" + value + "] into file [" + file.toAbsolutePath() + "]");
 172             Files.createDirectories(dir);
 173             Files.write(
 174                     file,
 175                     value.getBytes(),
 176                     StandardOpenOption.CREATE);
 177         }
 178     }
 179 
 180     private static void assertTrue(boolean condition, String msg) {
 181         if (!condition) {
 182             log(" FAILED -  ERROR: " + msg);
 183             throw new RuntimeException("[" + scenario + "] " + msg);
 184         } else {
 185             log(" PASSED.");
 186         }
 187     }
 188 
 189     private static void log(String msg) {
 190         System.out.println("[" + scenario + "] " + msg);
 191     }
 192 
 193 
 194     public static void main(String[] args) {
 195         // no security manager
 196         new SAAJFactoryTest().test(args);
 197 
 198         System.out.println("Policy file: " + policy);
 199         System.setProperty("java.security.policy", policy);
 200 
 201         System.out.println("Install security manager...");
 202         System.setSecurityManager(new SecurityManager());
 203 
 204         // security manager enabled
 205         new SAAJFactoryTest().test(args);
 206     }
 207 
 208 }
 209