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
24package p3.test;
25
26import java.util.logging.Logger;
27import java.util.MissingResourceException;
28import java.util.PropertyResourceBundle;
29import java.util.ResourceBundle;
30
31import p2.test.ModuleLoggerAccess;
32
33/*
34 * Test Logger.getLogger + logger.getResourceBundle in unnamed/named module,
35 * resources are in named and unnamed modules respectively.
36 */
37public class ResourceBundleTest {
38    private static final String RESOURCE_KEY = "OkKey";
39    private static final String RESOURCE_VALUE = "OK";
40
41    private static final String HELP_MSG =
42            "Test that a class in a %s module %s obtain a logger " +
43            "which uses a resource bundle %s bundled in " +
44            "%s module. ( The package in which the resource is " +
45            "contained is not exported by the module )";
46    private static final String NAMED_POSITIVE_CLASSBUNDLE_MSG =
47            String.format(HELP_MSG, "named", "can", "class", "the same");
48    private static final String UNNAMED_POSITIVE_CLASSBUNDLE_MSG =
49            String.format(HELP_MSG, "unnamed", "can", "class", "the same");
50    private static final String NAMED_POSITIVE_PROPERTYBUNDLE_MSG =
51            String.format(HELP_MSG, "named", "can", "property", "the same");
52    private static final String UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG =
53            String.format(HELP_MSG, "unnamed", "can", "property", "the same");
54    private static final String NAMED_NEGATIVE_CLASSBUNDLE_MSG =
55            String.format(HELP_MSG, "named", "cannot", "class", "another");
56    private static final String UNNAMED_NEGATIVE_CLASSBUNDLE_MSG =
57            String.format(HELP_MSG, "unnamed", "cannot", "class", "another");
58    private static final String NAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
59            String.format(HELP_MSG, "named", "cannot", "property", "another");
60    private static final String UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
61            String.format(HELP_MSG, "unnamed", "cannot", "property", "another");
62
63    public static void main(String[] args) {
64        verifySetup();
65        testLoggerRBs();
66        failToLoadRBs();
67    }
68
69    static void verifySetup() {
70        Module m = ResourceBundleTest.class.getModule();
71        System.out.println("Module Name for ResourceBundleTest : " + m.getName());
72        assertTrue(!m.isNamed());
73        m = ModuleLoggerAccess.class.getModule();
74        System.out.println("Module Name for ModuleLoggerAccess : " + m.getName());
75        assertTrue(m.isNamed());
76    }
77
78    /*
79     * Positive tests :
80     *  Should be able to access class/property resource bundle in current module,
81     *  no matter named or unnamed module.
82     */
83    static void testLoggerRBs() {
84        testLoggerClassRBs();
85        testLoggerPropertyRBs();
86    }
87
88    static void testLoggerClassRBs() {
89        testLoggerResoureBundle(
90                Logger.getLogger("mylogger.a", "p3.resource.ClassResource"),
91                p3.resource.ClassResource.class,
92                UNNAMED_POSITIVE_CLASSBUNDLE_MSG);
93        testLoggerResoureBundle(
94                ModuleLoggerAccess.getLogger("mylogger.b", "p2.resource.ClassResource"),
95                ModuleLoggerAccess.getResourceClass(),
96                NAMED_POSITIVE_CLASSBUNDLE_MSG);
97    }
98
99    static void testLoggerPropertyRBs() {
100        testLoggerResoureBundle(
101                Logger.getLogger("mylogger.c", "p3.resource.p"),
102                PropertyResourceBundle.class,
103                UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG);
104        testLoggerResoureBundle(
105                ModuleLoggerAccess.getLogger("mylogger.d", "p2.resource.p"),
106                PropertyResourceBundle.class,
107                NAMED_POSITIVE_PROPERTYBUNDLE_MSG);
108    }
109
110    static void testLoggerResoureBundle(Logger logger, Class<?> rbType, String helpMsg) {
111        System.out.println(helpMsg);
112        ResourceBundle rb = logger.getResourceBundle();
113        assertTrue(rbType.isInstance(rb));
114        assertTrue(RESOURCE_VALUE.equals(rb.getString(RESOURCE_KEY)));
115    }
116
117    /*
118     * Negative tests :
119     *  MissingResourceException should be thrown when access class/property resource bundle
120     *  from another module, no matter named or unnamed module.
121     */
122    static void failToLoadRBs() {
123        failToLoadClassRBs();
124        failToLoadPropertyRBs();
125    }
126
127    static void failToLoadClassRBs() {
128        // in an unnamed module, try to create a logger with
129        // class resource bundle in named module m1 or m2.
130        failToLoadResourceBundle("mylogger.e", "p1.resource.ClassResource",
131                false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
132        failToLoadResourceBundle("mylogger.f", "p2.resource.ClassResource",
133                false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
134        // in named module m2, try to create a logger with
135        // class resource bundle in another named module m1.
136        failToLoadResourceBundle("mylogger.g", "p1.resource.ClassResource",
137                true, NAMED_NEGATIVE_CLASSBUNDLE_MSG);
138    }
139
140    static void failToLoadPropertyRBs() {
141        // in an unnamed module, try to create a logger with
142        // property resource bundle in named module m1 or m2.
143        failToLoadResourceBundle("mylogger.i", "p1.resource.p",
144                false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
145        failToLoadResourceBundle("mylogger.j", "p2.resource.p",
146                false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
147        // in named module m2, try to create a logger with
148        // property resource bundle in another named module m1.
149        failToLoadResourceBundle("mylogger.k", "p1.resource.p",
150                true, NAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
151    }
152
153    static void failToLoadResourceBundle(String loggerName, String rbName,
154            boolean getLoggerInNamedModule, String helpMsg) {
155        String msg = String.format(
156                "Logger : %s. Expected exception is not thrown for ResourceBundle : %s.",
157                loggerName, rbName);
158        System.out.println(helpMsg);
159        try {
160            if(getLoggerInNamedModule) {
161                ModuleLoggerAccess.getLogger(loggerName, rbName);
162            } else {
163                Logger.getLogger(loggerName, rbName);
164            }
165            throw new RuntimeException(msg);
166        } catch (MissingResourceException expected) {
167            System.out.println("Get expected exception : " + expected);
168            return;
169        }
170    }
171
172    public static void assertTrue(boolean b) {
173        if (!b) {
174            throw new RuntimeException("Expect true, get false!");
175        }
176    }
177}
178