jdk/test/java/util/logging/modules/pkgs/p3/test/ResourceBundleTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 42338 a60f280f803c
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
package p3.test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.util.logging.Logger;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.util.MissingResourceException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.PropertyResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.ResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import p2.test.ModuleLoggerAccess;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 * Test Logger.getLogger + logger.getResourceBundle in unnamed/named module,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 * resources are in named and unnamed modules respectively.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
public class ResourceBundleTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
    private static final String RESOURCE_KEY = "OkKey";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    private static final String RESOURCE_VALUE = "OK";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
    private static final String HELP_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
            "Test that a class in a %s module %s obtain a logger " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
            "which uses a resource bundle %s bundled in " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
            "%s module. ( The package in which the resource is " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
            "contained is not exported by the module )";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    private static final String NAMED_POSITIVE_CLASSBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
            String.format(HELP_MSG, "named", "can", "class", "the same");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    private static final String UNNAMED_POSITIVE_CLASSBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
            String.format(HELP_MSG, "unnamed", "can", "class", "the same");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private static final String NAMED_POSITIVE_PROPERTYBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            String.format(HELP_MSG, "named", "can", "property", "the same");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private static final String UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            String.format(HELP_MSG, "unnamed", "can", "property", "the same");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private static final String NAMED_NEGATIVE_CLASSBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
            String.format(HELP_MSG, "named", "cannot", "class", "another");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    private static final String UNNAMED_NEGATIVE_CLASSBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            String.format(HELP_MSG, "unnamed", "cannot", "class", "another");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    private static final String NAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            String.format(HELP_MSG, "named", "cannot", "property", "another");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    private static final String UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            String.format(HELP_MSG, "unnamed", "cannot", "property", "another");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    public static void main(String[] args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        verifySetup();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        testLoggerRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        failToLoadRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    static void verifySetup() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        Module m = ResourceBundleTest.class.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        System.out.println("Module Name for ResourceBundleTest : " + m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        assertTrue(!m.isNamed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        m = ModuleLoggerAccess.class.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        System.out.println("Module Name for ModuleLoggerAccess : " + m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        assertTrue(m.isNamed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     * Positive tests :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     *  Should be able to access class/property resource bundle in current module,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     *  no matter named or unnamed module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    static void testLoggerRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        testLoggerClassRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        testLoggerPropertyRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    static void testLoggerClassRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        testLoggerResoureBundle(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                Logger.getLogger("mylogger.a", "p3.resource.ClassResource"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                p3.resource.ClassResource.class,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                UNNAMED_POSITIVE_CLASSBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        testLoggerResoureBundle(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                ModuleLoggerAccess.getLogger("mylogger.b", "p2.resource.ClassResource"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                ModuleLoggerAccess.getResourceClass(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                NAMED_POSITIVE_CLASSBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    static void testLoggerPropertyRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        testLoggerResoureBundle(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                Logger.getLogger("mylogger.c", "p3.resource.p"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                PropertyResourceBundle.class,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                UNNAMED_POSITIVE_PROPERTYBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        testLoggerResoureBundle(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                ModuleLoggerAccess.getLogger("mylogger.d", "p2.resource.p"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                PropertyResourceBundle.class,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                NAMED_POSITIVE_PROPERTYBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    static void testLoggerResoureBundle(Logger logger, Class<?> rbType, String helpMsg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        System.out.println(helpMsg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        ResourceBundle rb = logger.getResourceBundle();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        assertTrue(rbType.isInstance(rb));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        assertTrue(RESOURCE_VALUE.equals(rb.getString(RESOURCE_KEY)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * Negative tests :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     *  MissingResourceException should be thrown when access class/property resource bundle
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     *  from another module, no matter named or unnamed module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
    static void failToLoadRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        failToLoadClassRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        failToLoadPropertyRBs();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    static void failToLoadClassRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        // in an unnamed module, try to create a logger with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        // class resource bundle in named module m1 or m2.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        failToLoadResourceBundle("mylogger.e", "p1.resource.ClassResource",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        failToLoadResourceBundle("mylogger.f", "p2.resource.ClassResource",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                false, UNNAMED_NEGATIVE_CLASSBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        // in named module m2, try to create a logger with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        // class resource bundle in another named module m1.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        failToLoadResourceBundle("mylogger.g", "p1.resource.ClassResource",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                true, NAMED_NEGATIVE_CLASSBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    static void failToLoadPropertyRBs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        // in an unnamed module, try to create a logger with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        // property resource bundle in named module m1 or m2.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        failToLoadResourceBundle("mylogger.i", "p1.resource.p",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        failToLoadResourceBundle("mylogger.j", "p2.resource.p",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                false, UNNAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        // in named module m2, try to create a logger with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        // property resource bundle in another named module m1.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        failToLoadResourceBundle("mylogger.k", "p1.resource.p",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                true, NAMED_NEGATIVE_PROPERTYBUNDLE_MSG);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    static void failToLoadResourceBundle(String loggerName, String rbName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            boolean getLoggerInNamedModule, String helpMsg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        String msg = String.format(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                "Logger : %s. Expected exception is not thrown for ResourceBundle : %s.",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                loggerName, rbName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        System.out.println(helpMsg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            if(getLoggerInNamedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                ModuleLoggerAccess.getLogger(loggerName, rbName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                Logger.getLogger(loggerName, rbName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            throw new RuntimeException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        } catch (MissingResourceException expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            System.out.println("Get expected exception : " + expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    public static void assertTrue(boolean b) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        if (!b) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            throw new RuntimeException("Expect true, get false!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
}