jdk/test/java/util/ResourceBundle/Control/ExpirationTest.java
changeset 11303 5f48992867e6
parent 11302 a6305295d4d9
parent 11239 885050364691
child 11304 5d3d2bd1dfd1
equal deleted inserted replaced
11302:a6305295d4d9 11303:5f48992867e6
     1 /*
       
     2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 /*
       
    24  *
       
    25  */
       
    26 
       
    27 /*
       
    28  * This class is used by ExpirationTest.sh. See the timing information in
       
    29  * the shell script.
       
    30  */
       
    31 
       
    32 import java.util.*;
       
    33 
       
    34 public class ExpirationTest {
       
    35     static final Locale AUSTRIA = new Locale("de", "AT");
       
    36     static String format;
       
    37     static String fileType;
       
    38 
       
    39     public static void main(String[] args) {
       
    40         // If -latency is specified, try sleeping for 3 seconds 3
       
    41         // times to see its latency. If the latency is too large, then
       
    42         // the program exits with 2. (See sleep())
       
    43         if ("-latency".equals(args[0])) {
       
    44             System.out.print("Checking latency... ");
       
    45             for (int i = 0; i < 3; i++) {
       
    46                 sleep(3);
       
    47             }
       
    48             System.out.println("done");
       
    49             System.exit(0);
       
    50         }
       
    51 
       
    52         format = args[0];
       
    53         fileType = args[1];
       
    54 
       
    55         Locale loc = Locale.getDefault();
       
    56         try {
       
    57             Locale.setDefault(Locale.JAPAN);
       
    58             ResourceBundle.Control control = new TestControl();
       
    59             ResourceBundle rb = ResourceBundle.getBundle("ExpirationData", Locale.GERMAN,
       
    60                                                          control);
       
    61             check(rb.getString("data"), "German");
       
    62 
       
    63             rb = ResourceBundle.getBundle("ExpirationData", AUSTRIA, control);
       
    64             check(rb.getString("january"), "Januar");
       
    65 
       
    66             // Wait until the instance gets expired in the cache in 7 seconds.
       
    67             sleep(7);
       
    68 
       
    69             // At this point, it should be determined that reloading is not needed.
       
    70             rb = ResourceBundle.getBundle("ExpirationData", Locale.GERMAN, control);
       
    71             check(rb.getString("data"), "German");
       
    72 
       
    73             rb = ResourceBundle.getBundle("ExpirationData", AUSTRIA, control);
       
    74             check(rb.getString("january"), "Januar");
       
    75 
       
    76             // Wait until the instance in the cache gets expired again and
       
    77             // ExpirationData_de gets updated.
       
    78             // 33 = 40 - 7 (See the timing chart in ExpirationTest.sh)
       
    79             sleep(33);
       
    80 
       
    81             // At this point, getBundle must reload the updated
       
    82             // ExpirationData_de and ExpirationData_de_AT must be
       
    83             // avaible.
       
    84 
       
    85             rb = ResourceBundle.getBundle("ExpirationData", Locale.GERMAN, control);
       
    86             try {
       
    87                 check(rb.getString("data"), "Deutsch");
       
    88             } catch (RuntimeException e) {
       
    89                 if (format.equals("class")) {
       
    90                     // Class loader doesn't load updated classes.
       
    91                     System.out.println("Known class limitation: " + e.getMessage());
       
    92                 }
       
    93             }
       
    94 
       
    95             rb = ResourceBundle.getBundle("ExpirationData", AUSTRIA, control);
       
    96             try {
       
    97                 check(rb.getString("january"), "J\u00e4nner");
       
    98             } catch (RuntimeException e) {
       
    99                 if (fileType.equals("jar")) {
       
   100                     // Jar doesn't load new entries.
       
   101                     System.out.println("Known jar limitation: " + e.getMessage());
       
   102                 } else {
       
   103                     throw e;
       
   104                 }
       
   105             }
       
   106         } finally {
       
   107             Locale.setDefault(loc);
       
   108         }
       
   109     }
       
   110 
       
   111     private static void check(String s, String expected) {
       
   112         String time = getTime();
       
   113         if (!s.equals(expected)) {
       
   114             throw new RuntimeException("got '" + s + "', expected '" + expected + "' at "
       
   115                                        + time);
       
   116         }
       
   117         System.out.println("ExpirationTest: got '" + s + "' at " + time);
       
   118     }
       
   119 
       
   120     private static void sleep(int seconds) {
       
   121         long millis = seconds * 1000;
       
   122         long start = System.currentTimeMillis();
       
   123         try {
       
   124             Thread.sleep(millis);
       
   125         } catch (InterruptedException e) {
       
   126         }
       
   127         long end = System.currentTimeMillis();
       
   128         long latency = end - start - millis;
       
   129         // If the latecy is more than 1% of the requested sleep time,
       
   130         // then give up the testing.
       
   131         if (latency > millis/100) {
       
   132             System.err.printf("Latency is too large: slept for %d [ms], "
       
   133                               + "expected %d [ms] latency rate: %+.2f%% (expected not more than 1%%)%n"
       
   134                               + "exiting...%n",
       
   135                               end - start, millis, (double)latency*100.0/millis);
       
   136             System.exit(2);
       
   137         }
       
   138     }
       
   139 
       
   140     private static final String getTime() {
       
   141         return new Date().toString().substring(11, 19);
       
   142     }
       
   143 
       
   144     private static class TestControl extends ResourceBundle.Control {
       
   145         @Override
       
   146         public long getTimeToLive(String name, Locale loc) {
       
   147             return 5000; // 5 seconds
       
   148         }
       
   149 
       
   150         @Override
       
   151         public ResourceBundle newBundle(String name, Locale loc,
       
   152                                         String fmt, ClassLoader cl, boolean reload)
       
   153             throws IllegalAccessException, InstantiationException, java.io.IOException {
       
   154             ResourceBundle bundle = super.newBundle(name, loc, fmt, cl, reload);
       
   155             if (bundle != null) {
       
   156                 System.out.println("newBundle: " + (reload ? "**re" : "")
       
   157                                    + "loaded '" + toName(name, loc , fmt) + "' at " + getTime());
       
   158             }
       
   159             return bundle;
       
   160         }
       
   161 
       
   162         @Override
       
   163         public boolean needsReload(String name, Locale loc,
       
   164                                    String fmt, ClassLoader cl,
       
   165                                    ResourceBundle rb, long time) {
       
   166             boolean b = super.needsReload(name, loc, fmt, cl, rb, time);
       
   167             System.out.println("needsReload: '" + b + "' for " + toName(name, loc, fmt)
       
   168                                + " at " + getTime());
       
   169             return b;
       
   170         }
       
   171 
       
   172         private String toName(String name, Locale loc, String fmt) {
       
   173             return toResourceName(toBundleName(name, loc), fmt.substring(5));
       
   174         }
       
   175     }
       
   176 }