1 /*
2  * Copyright (c) 2012, 2017, 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  * @test
25  * @bug 6959653 8172365
26  * @summary Test ResourceBundle.Control provided using SPI.
27  * @library test
28  * @build test/*
29  * @build com.foo.UserControlProvider
30  * @run main/othervm UserDefaultControlTest false
31  * @run main/othervm UserDefaultControlTest true
32  */
33 
34 import java.io.*;
35 import java.lang.reflect.*;
36 import java.net.*;
37 import java.nio.file.*;
38 import java.util.*;
39 
40 import jdk.test.*;
41 
42 public class UserDefaultControlTest {
main(String... args)43     public static void main(String... args) throws Exception {
44         boolean smExists = Boolean.valueOf(args[0]);
45         initServices();
46         if (smExists) {
47             System.out.println("test with security manager present:");
48             System.setSecurityManager(new SecurityManager());
49         } else {
50             System.out.println("test without security manager present:");
51         }
52 
53         test(smExists);
54     }
55 
initServices()56     private static void initServices() throws IOException {
57         Path testClasses = Paths.get(System.getProperty("test.classes"));
58         Path services = testClasses.resolve(Paths.get("META-INF", "services"));
59         Files.createDirectories(services);
60         Files.write(services.resolve("java.util.spi.ResourceBundleControlProvider"),
61                 List.of("com.foo.UserControlProvider"));
62         Path comfoo = testClasses.resolve(Paths.get("com", "foo"));
63         Path testSrcComFoo =
64             Paths.get(System.getProperty("test.src")).resolve(Paths.get("com", "foo"));
65         Files.copy(testSrcComFoo.resolve("XmlRB.xml"), comfoo.resolve("XmlRB.xml"),
66             StandardCopyOption.REPLACE_EXISTING);
67         Files.copy(testSrcComFoo.resolve("XmlRB_ja.xml"), comfoo.resolve("XmlRB_ja.xml"),
68             StandardCopyOption.REPLACE_EXISTING);
69     }
70 
test(boolean smExists)71     private static void test(boolean smExists) {
72         ResourceBundle rb;
73 
74         try {
75             rb = ResourceBundle.getBundle("com.foo.XmlRB", Locale.ROOT);
76             if (smExists) {
77                 throw new RuntimeException("getBundle did not throw " +
78                     "MissingResourceException with a security manager");
79             }
80         } catch (MissingResourceException e) {
81             if (smExists) {
82                 // failed successfully
83                 return;
84             } else {
85                 throw e;
86             }
87         }
88 
89         String type = rb.getString("type");
90         if (!type.equals("XML")) {
91             throw new RuntimeException("Root Locale: type: got " + type
92                                        + ", expected XML (ASCII)");
93         }
94 
95         rb = ResourceBundle.getBundle("com.foo.XmlRB", Locale.JAPAN);
96         type = rb.getString("type");
97         // Expect fullwidth "XML"
98         if (!type.equals("\uff38\uff2d\uff2c")) {
99             throw new RuntimeException("Locale.JAPAN: type: got " + type
100                                        + ", expected \uff38\uff2d\uff2c (fullwidth XML)");
101         }
102 
103         try {
104             rb = ResourceBundle.getBundle("com.bar.XmlRB", Locale.JAPAN);
105             throw new RuntimeException("com.bar.XmlRB test failed.");
106         } catch (MissingResourceException e) {
107             // OK
108         }
109 
110         // tests with named module. Only resource bundles on the classpath
111         // should be available, unless an unnamed module is explicitly
112         // specified.
113         rb = ResourceBundleDelegate.getBundle("simple", Locale.ROOT);
114         try {
115             rb = ResourceBundleDelegate.getBundle("com.foo.XmlRB", Locale.ROOT);
116             throw new RuntimeException("getBundle in a named module incorrectly loaded " +
117                                     "a resouce bundle through RBControlProvider");
118         } catch (MissingResourceException e) {
119             // OK
120         }
121 
122         Module unnamedModule = UserDefaultControlTest.class
123                                 .getClassLoader()
124                                 .getUnnamedModule();
125         rb = ResourceBundleDelegate.getBundle("com.foo.XmlRB", Locale.JAPAN, unnamedModule);
126         type = rb.getString("type");
127         // Expect fullwidth "XML"
128         if (!type.equals("\uff38\uff2d\uff2c")) {
129             throw new RuntimeException("getBundle called from named module for unnamed module."
130                                        + " Locale.JAPAN: type: got " + type
131                                        + ", expected \uff38\uff2d\uff2c (fullwidth XML)");
132         }
133     }
134 }
135