jdk/test/java/util/ResourceBundle/ReferencesTest.java
author rupashka
Mon, 17 May 2010 17:23:18 +0400
changeset 5581 9c2282c6f080
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6938481: 4906607 is not fixed for NIMBUS L&F Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
 * @bug 4405807
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @run main/othervm -Xms10m ReferencesTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that references from ResourceBundle cache don't prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * class loader reclamation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This test relies on the current behavior of the garbage collector and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * therefore no clear indicator of whether the fix for 4405807 works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * If the test fails, it might indicate a regression, or it might just mean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * that a less aggressive garbage collector is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class ReferencesTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int CLASS_LOADER_COUNT = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // These two parallel arrays have references to the same class loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // The weakLoaders array lets us track whether class loaders are being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // reclaimed after the references in the loaders array are nulled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static ClassLoader[] loaders = new ClassLoader[CLASS_LOADER_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static WeakReference[] weakLoaders = new WeakReference[CLASS_LOADER_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        URL testDirectory = new File(System.getProperty("test.classes", ".")).toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = 0; i < loaders.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            URL[] urls = { testDirectory };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            loaders[i] = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            weakLoaders[i] = new WeakReference(loaders[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // fill the ResourceBundle cache with entries for half the class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        loadBundles(0, CLASS_LOADER_COUNT / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        report("After loading resource bundles for first half of class loaders: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // release the first half of the class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for (int i = 0; i < CLASS_LOADER_COUNT / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            loaders[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        report("After releasing first half of class loaders: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // fill the ResourceBundle cache with entries for second half the class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        loadBundles(CLASS_LOADER_COUNT / 2, CLASS_LOADER_COUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        report("After loading resource bundles for second half of class loaders: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // release the second half of the class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i = CLASS_LOADER_COUNT / 2; i < CLASS_LOADER_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            loaders[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        report("After releasing second half of class loaders: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // The garbage collector in Tiger actually has reclaimed all class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // loaders at this point, but in order not to become too dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // on the current behavior, we only require that the first half
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // has been reclaimed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (countLoaders(0, CLASS_LOADER_COUNT / 2) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new RuntimeException("Too many class loaders not reclaimed yet.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static void report(String when) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int first = countLoaders(0, CLASS_LOADER_COUNT / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int second = countLoaders(CLASS_LOADER_COUNT / 2, CLASS_LOADER_COUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Class clazz = ResourceBundle.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Field cacheList = clazz.getDeclaredField("cacheList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        cacheList.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int cacheSize = ((Map)cacheList.get(clazz)).size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.println(when);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println("    " + first + " loaders alive in first half");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println("    " + second + " loaders alive in second half");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("    " + cacheSize + " entries in resource bundle cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static void loadBundles(int start, int end) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // There's no resource bundle for NonExistantBundle - this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // let's us test the case where a resource bundle is not found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // which in the past created a SoftReference on the value side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // of the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                ResourceBundle.getBundle("NonExistantBundle", Locale.US, loaders[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // There's a base resource bundle for ReferencesTestBundle - the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // normal case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ResourceBundle.getBundle("ReferencesTestBundle", Locale.US, loaders[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static int countLoaders(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (weakLoaders[i].get() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}