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