jdk/test/java/util/ResourceBundle/Bug4168625Test.java
author naoto
Fri, 22 Oct 2010 11:32:26 -0700
changeset 7008 0bac31837ad2
parent 5801 8008ed6e4a37
child 7253 58ff073169cb
permissions -rw-r--r--
6993339: Bug4168625Test.java fails Reviewed-by: peytoia
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
    @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
    @summary test Resource Bundle for bug 4168625
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
    @build Bug4168625Class Bug4168625Getter Bug4168625Resource Bug4168625Resource3 Bug4168625Resource3_en Bug4168625Resource3_en_CA Bug4168625Resource3_en_IE Bug4168625Resource3_en_US Bug4168625Resource2_en_US Bug4168625Resource2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    @run main/timeout=600 Bug4168625Test
7008
0bac31837ad2 6993339: Bug4168625Test.java fails
naoto
parents: 5801
diff changeset
    28
    @bug 4168625 6993339
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * (C) Copyright IBM Corp. 1999 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This software is the confidential and proprietary information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of Sun Microsystems, Inc. ("Confidential Information").  You
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * shall not disclose such Confidential Information and shall use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * it only in accordance with the terms of the license agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * you entered into with Sun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * copyrighted and owned by IBM. These materials are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * under terms of a License Agreement between IBM and Sun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * patents. This notice and attribution to IBM may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
7008
0bac31837ad2 6993339: Bug4168625Test.java fails
naoto
parents: 5801
diff changeset
    53
 *  This test tries to correct two efficiency problems with the caching
0bac31837ad2 6993339: Bug4168625Test.java fails
naoto
parents: 5801
diff changeset
    54
 *  mechanism of ResourceBundle.  It also allows concurrent loads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *  of resource bundles to be performed if the bundles are unrelated (ex. a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  load of a local system resource by one thread while another thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  doing a slow load over a network).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class Bug4168625Test extends RBTestFmwk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        new Bug4168625Test().run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Verify that getBundle will do something reasonable when part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * resource hierarchy is missing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void testMissingParent() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        final Locale oldDefault = Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Locale.setDefault(new Locale("en", "US"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            final Locale loc = new Locale("jf", "jf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ResourceBundle bundle = ResourceBundle.getBundle("Bug4168625Resource2", loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            final String s1 = bundle.getString("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (!s1.equals("Bug4168625Resource2_en_US")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                errln("getBundle did not find leaf bundle: "+bundle.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            final String s2 = bundle.getString("baseName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (!s2.equals("Bug4168625Resource2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                errln("getBundle did not set up proper inheritance chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Locale.setDefault(oldDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *  Previous versions of ResourceBundle have had the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *  caching behavior.  Assume the classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *  Bug4168625Resource_fr_FR, Bug4168625Resource_fr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  Bug4168625Resource_en_US, and Bug4168625Resource_en don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *  exist.  The class Bug4168625Resource does.  Assume the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *  locale is en_US.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *  getBundle("Bug4168625Resource", new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *      -->try to load Bug4168625Resource_fr_FR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *      -->try to load Bug4168625Resource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *      -->try to load Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *      -->try to load Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *      -->load Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *      -->cache Bug4168625Resource as Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *      -->cache Bug4168625Resource as Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *      -->cache Bug4168625Resource as Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *      -->return Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *  getBundle("Bug4168625Resource", new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *      -->try to load Bug4168625Resource_fr_FR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *      -->try to load Bug4168625Resource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *      -->find cached Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *      -->return Bug4168625Resource_en_US (which is realy Bug4168625Resource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *  The second call causes two loads for Bug4168625Resource_fr_FR and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  Bug4168625Resource_en which have already been tried and failed.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *  two loads should have been cached as Bug4168625Resource by the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *  call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *  The following, more efficient behavior is desired:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *  getBundle("Bug4168625Resource", new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *      -->try to load Bug4168625Resource_fr_FR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *      -->try to load Bug4168625Resource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *      -->try to load Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *      -->try to load Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *      -->load Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *      -->cache Bug4168625Resource as Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *      -->cache Bug4168625Resource as Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *      -->cache Bug4168625Resource as Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *      -->cache Bug4168625Resource as Bug4168625Resource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *      -->cache Bug4168625Resource as Bug4168625Resource_fr_FR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *      -->return Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *  getBundle("Bug4168625Resource", new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *      -->find cached Bug4168625Resource_fr_FR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *      -->return Bug4168625Resource_en_US (which is realy Bug4168625Resource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void testCacheFailures() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        checkResourceLoading("Bug4168625Resource", new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *  Previous versions of ResourceBundle have had the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *  caching behavior.  Assume the current locale is locale is en_US.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *  The classes Bug4168625Resource_en_US, and Bug4168625Resource_en don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *  exist.  The class Bug4168625Resource does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *  getBundle("Bug4168625Resource", new Locale("en", "US"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *      -->try to load Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *      -->try to load Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *      -->try to load Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *      -->try to load Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *      -->load Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *      -->cache Bug4168625Resource as Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *      -->cache Bug4168625Resource as Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *      -->cache Bug4168625Resource as Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *      -->return Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  The redundant loads of Bug4168625Resource_en_US and Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  should not occur.  The desired behavior is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  getBundle("Bug4168625Resource", new Locale("en", "US"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *      -->try to load Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *      -->try to load Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *      -->load Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *      -->cache Bug4168625Resource as Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *      -->cache Bug4168625Resource as Bug4168625Resource_en
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *      -->cache Bug4168625Resource as Bug4168625Resource_en_US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *      -->return Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void testRedundantLoads() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        checkResourceLoading("Bug4168625Resource", Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Ensure that resources are only loaded once and are cached correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private void checkResourceLoading(String resName, Locale l) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        final Loader loader = new Loader( new String[] { "Bug4168625Class" }, new String[] { "Bug4168625Resource3_en_US", "Bug4168625Resource3_en_CA" });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        final Class c = loader.loadClass("Bug4168625Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Bug4168625Getter test = (Bug4168625Getter)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        final String resClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (l.toString().length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            resClassName = resName+"_"+l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            resClassName = resName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Object bundle = test.getResourceBundle(resName, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        loader.logClasses("Initial lookup of "+resClassName+" generated the following loads:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        final Vector lastLoad = new Vector(loader.loadedClasses.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        boolean dups = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i = loader.loadedClasses.size() - 1; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            final Object item = loader.loadedClasses.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            loader.loadedClasses.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (loader.loadedClasses.contains(item)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                logln("Resource loaded more than once: "+item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                dups = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                lastLoad.addElement(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (dups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            errln("ResourceBundle loaded some classes multiple times");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        loader.loadedClasses.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        bundle = test.getResourceBundle(resName, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        loader.logClasses("Second lookup of "+resClassName+" generated the following loads:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        dups = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        for (int i = 0; i < loader.loadedClasses.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            Object item = loader.loadedClasses.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (lastLoad.contains(item)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                logln("ResourceBundle did not cache "+item+" correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                dups = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (dups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            errln("Resource bundle not caching some classes properly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private class ConcurrentLoadingThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public Object bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        private Bug4168625Getter test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        private Locale locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        private String resourceName = "Bug4168625Resource3";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public ConcurrentLoadingThread(Loader loader, Bug4168625Getter test, Locale l, String resourceName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            this.test = test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            this.locale = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            this.resourceName = resourceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public ConcurrentLoadingThread(Loader loader, Bug4168625Getter test, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.test = test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            this.locale = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                logln(">>"+threadName()+">run");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                bundle = test.getResourceBundle(resourceName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                errln("TEST CAUGHT UNEXPECTED EXCEPTION: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                logln("<<"+threadName()+"<run");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public synchronized void waitUntilPinged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            logln(">>"+threadName()+">waitUntilPinged");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            loader.notifyEveryone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                wait(30000);    //wait 30 seconds max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                logln("Test deadlocked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            logln("<<"+threadName()+"<waitUntilPinged");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public synchronized void ping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            logln(">>"+threadName()+">ping "+threadName(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            logln("<<"+threadName()+"<ping "+threadName(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * This test ensures that multiple resources can be loading at the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * time as long as they don't depend on each other in some way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
7008
0bac31837ad2 6993339: Bug4168625Test.java fails
naoto
parents: 5801
diff changeset
   280
    public void testConcurrentLoading() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        final Loader loader = new Loader( new String[] { "Bug4168625Class" }, new String[] { "Bug4168625Resource3_en_US", "Bug4168625Resource3_en_CA" });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        final Class c = loader.loadClass("Bug4168625Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        final Bug4168625Getter test = (Bug4168625Getter)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        ConcurrentLoadingThread thread1 = new ConcurrentLoadingThread(loader, test, new Locale("en", "CA"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        ConcurrentLoadingThread thread2 = new ConcurrentLoadingThread(loader, test, new Locale("en", "IE"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        thread1.start();            //start thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        loader.waitForNotify(1);    //wait for thread1 to do getBundle & block in loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        thread2.start();            //start second thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        thread2.join(1000);         //wait until thread2 blocks somewhere in getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            //Thread1 should be blocked inside getBundle at the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            //Thread2 should have completed its getBundle call and terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!thread1.isAlive() || thread2.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            errln("ResourceBundle.getBundle not allowing legal concurrent loads");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        thread1.ping();             //continue thread1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        thread1.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        thread2.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * This test ensures that a resource loads correctly (with all its parents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * when memory is very low (ex. the cache gets purged during a load).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void testLowMemoryLoad() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        final String[] classToLoad = { "Bug4168625Class" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        final String[] classToWait = { "Bug4168625Resource3_en_US","Bug4168625Resource3_en","Bug4168625Resource3" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        final Loader loader = new Loader(classToLoad, classToWait);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        final Class c = loader.loadClass("Bug4168625Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        final Bug4168625Getter test = (Bug4168625Getter)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        causeResourceBundleCacheFlush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        ConcurrentLoadingThread thread1 = new ConcurrentLoadingThread(loader, test, new Locale("en", "US"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        thread1.start();            //start thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        loader.waitForNotify(1);    //wait for thread1 to do getBundle(en_US) & block in loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        causeResourceBundleCacheFlush();    //cause a cache flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        thread1.ping();             //kick thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        loader.waitForNotify(2);    //wait for thread1 to do getBundle(en) & block in loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        causeResourceBundleCacheFlush();    //cause a cache flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        thread1.ping();             //kick thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        loader.waitForNotify(3);    //wait for thread1 to do getBundle(en) & block in loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        causeResourceBundleCacheFlush();    //cause a cache flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        thread1.ping();             //kick thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        thread1.ping();             //kick thread 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        thread1.join(1000);         //wait until thread2 blocks somewhere in getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        ResourceBundle bundle = (ResourceBundle)thread1.bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        String s1 = bundle.getString("Bug4168625Resource3_en_US");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String s2 = bundle.getString("Bug4168625Resource3_en");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        String s3 = bundle.getString("Bug4168625Resource3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if ((s1 == null) || (s2 == null) || (s3 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            errln("Bundle not constructed correctly.  The parent chain is incorrect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * A simple class loader that loads classes from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * working directory.  The loader will block the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * of execution before it returns when it tries to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * the class "Bug4168625Resource3_en_US".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static final String CLASS_PREFIX = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private static final String CLASS_SUFFIX = ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private static final class SimpleLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        private boolean network = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public SimpleLoader() {
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   352
            super(SimpleLoader.class.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            this.network = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public SimpleLoader(boolean simulateNetworkLoad) {
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   356
            super(SimpleLoader.class.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            this.network = simulateNetworkLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public Class loadClass(final String className, final boolean resolveIt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            Class result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                result = findLoadedClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    if (network) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                             Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        } catch (java.lang.InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    }
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   371
                    result = getParent().loadClass(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if ((result != null) && resolveIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        resolveClass(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private final class Loader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public final Vector loadedClasses = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        private String[] classesToLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        private String[] classesToWaitFor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        public Loader() {
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   387
            super(Loader.class.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            classesToLoad = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            classesToWaitFor = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public Loader(final String[] classesToLoadIn, final String[] classesToWaitForIn) {
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   393
            super(Loader.class.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            classesToLoad = classesToLoadIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            classesToWaitFor = classesToWaitForIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * Load a class.  Files we can load take preference over ones the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * can load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        private byte[] getClassData(final String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            boolean shouldLoad = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            for (int i = classesToLoad.length-1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                if (className.equals(classesToLoad[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    shouldLoad = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (shouldLoad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                final String name = CLASS_PREFIX+className+CLASS_SUFFIX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    final InputStream fi = this.getClass().getClassLoader().getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    final byte[] result = new byte[fi.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    fi.read(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    logln("Error loading test class: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    logln(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * Load a class.  Files we can load take preference over ones the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * can load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        public Class loadClass(final String className, final boolean resolveIt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            Class result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                logln(">>"+threadName()+">load "+className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                loadedClasses.addElement(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                result = findLoadedClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    final byte[] classData = getClassData(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    if (classData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        //we don't have a local copy of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        logln("Loading system class: "+className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        result = loadFromSystem(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        result = defineClass(classData, 0, classData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            //there was an error defining the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            result = loadFromSystem(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    if ((result != null) && resolveIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        resolveClass(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            for (int i = classesToWaitFor.length-1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                if (className.equals(classesToWaitFor[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    rendezvous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            logln("<<"+threadName()+"<load "+className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        /**
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   469
         * Delegate loading to its parent class loader that loads the test classes.
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   470
         * In othervm mode, the parent class loader is the system class loader;
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   471
         * in samevm mode, the parent class loader is the jtreg URLClassLoader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        private Class loadFromSystem(String className) throws ClassNotFoundException {
5797
c2641f6791be 6961506: TEST_BUG: ResourceBundle/Bug4168625Test.java and TestBug4179766.java fails in samevm mode
mchung
parents: 5506
diff changeset
   474
            return getParent().loadClass(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        public void logClasses(String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            logln(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            for (int i = 0; i < loadedClasses.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                logln("    "+loadedClasses.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            logln("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        public int notifyCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public int waitForNotify(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return waitForNotify(count, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        public synchronized int waitForNotify(int count, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            logln(">>"+threadName()+">waitForNotify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (count > notifyCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    wait(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                logln("  count("+count+") > notifyCount("+notifyCount+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            logln("<<"+threadName()+"<waitForNotify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return notifyCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        private synchronized void notifyEveryone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            logln(">>"+threadName()+">notifyEveryone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            notifyCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            logln("<<"+threadName()+"<notifyEveryone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        private void rendezvous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            final Thread current = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (current instanceof ConcurrentLoadingThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                ((ConcurrentLoadingThread)current).waitUntilPinged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private static String threadName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return threadName(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static String threadName(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        String temp = t.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int ndx = temp.indexOf("Thread[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        temp = temp.substring(ndx + "Thread[".length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        ndx = temp.indexOf(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        temp = temp.substring(0, ndx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /** Fill memory to force all SoftReferences to be GCed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private void causeResourceBundleCacheFlush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        logln("Filling memory...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int allocationSize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        Vector memoryHog = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                memoryHog.addElement(new byte[allocationSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                allocationSize *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            logln("Caught "+e+" filling memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        } finally{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            memoryHog = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        logln("last allocation size: " + allocationSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *  NOTE: this problem is not externally testable and can only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *  verified through code inspection unless special code to force
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *  a task switch is inserted into ResourceBundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *  The class Bug4168625Resource_sp exists.  It's parent bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *  (Bug4168625Resource) contains a resource string with the tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *  "language" but Bug4168625Resource_sp does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *  Assume two threads are executing, ThreadA and ThreadB and they both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *  load a resource Bug4168625Resource with from sp locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *  ResourceBundle.getBundle adds a bundle to the bundle cache (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *  findBundle) before it sets the bundle's parent (in getBundle after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *  returning from findBundle).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *  ThreadA.getBundle("Bug4168625Resource", new Locale("sp"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *      A-->load Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *      A-->find cached Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *      A-->cache Bug4168625Resource_sp as Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *  ThreadB.getBundle("Bug4168625Resource", new Locale("sp"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *      B-->find cached Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *      B-->return Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *  ThreadB.bundle.getString("language");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *      B-->try to find "language" in Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *      B-->Bug4168625Resource_sp does not have a parent, so return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *  ThreadB.System.out.println("Some unknown country");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *      A-->set parent of Bug4168625Resource_sp to Bug4168625Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *      A-->return Bug4168625Resource_sp (the same bundle ThreadB got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *  ThreadA.bundle.getString("language");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *      A-->try to find "language" in Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *      A-->try to find "language" in Bug4168625Resource (parent of Bug4168625Resource_sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *      A-->return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *  ThreadA.System.out.println("Langauge = "+country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *  ThreadB.bundle.getString("language");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *      B-->try to find "language" in Bug4168625Resource_sp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *      B-->try to find "language" in Bug4168625Resource (parent of Bug4168625Resource_sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *      B-->return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *  ThreadB.System.out.println("Langauge = "+country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *  Note that the first call to getString() by ThreadB returns null, but the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *  returns a value.  Thus to ThreadB, the bundle appears to change.  ThreadA gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *  the expected results right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}