Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.harmony.luni.tests.java.util;
     18 
     19 import static java.util.ResourceBundle.Control.FORMAT_CLASS;
     20 import static java.util.ResourceBundle.Control.FORMAT_DEFAULT;
     21 import static java.util.ResourceBundle.Control.FORMAT_PROPERTIES;
     22 import static java.util.ResourceBundle.Control.TTL_DONT_CACHE;
     23 import static java.util.ResourceBundle.Control.TTL_NO_EXPIRATION_CONTROL;
     24 
     25 import java.io.File;
     26 import java.io.FileNotFoundException;
     27 import java.io.FileWriter;
     28 import java.io.IOException;
     29 import java.io.InputStreamReader;
     30 import java.io.PrintWriter;
     31 import java.io.Reader;
     32 import java.io.Writer;
     33 import java.lang.reflect.Member;
     34 import java.net.URL;
     35 import java.security.Permission;
     36 import java.util.ArrayList;
     37 import java.util.Collections;
     38 import java.util.List;
     39 import java.util.ListResourceBundle;
     40 import java.util.Locale;
     41 import java.util.PropertyResourceBundle;
     42 import java.util.ResourceBundle;
     43 import java.util.Scanner;
     44 import java.util.ResourceBundle.Control;
     45 
     46 import junit.framework.TestCase;
     47 import tests.support.resource.Support_Resources;
     48 
     49 /**
     50  * Test cases for java.util.ResourceBundle.Control
     51  *
     52  * @since 1.6
     53  */
     54 public class ControlTest extends TestCase {
     55 
     56     /**
     57      * Control with format:FORMAT_PROPERTIES
     58      */
     59     private Control controlP;
     60 
     61     /**
     62      * Control with format:FORMAT_CLASS
     63      */
     64     private Control controlC;
     65 
     66     /**
     67      * Control with format:FORMAT_DEFAULT
     68      */
     69     private Control control;
     70 
     71     /**
     72      * @tests {@link java.util.ResourceBundle.Control#Control()}.
     73      */
     74     @SuppressWarnings("nls")
     75     public void test_Constructor() {
     76 
     77         class SubControl extends Control {
     78             SubControl() {
     79                 super();
     80             }
     81         }
     82         Control subControl = new SubControl();
     83         assertEquals(FORMAT_DEFAULT, subControl.getFormats(""));
     84         assertFalse(control.equals(subControl));
     85     }
     86 
     87     /**
     88      * Test for all the public constants.
     89      *
     90      * @tests {@link java.util.ResourceBundle.Control#FORMAT_CLASS}
     91      * @tests {@link java.util.ResourceBundle.Control#FORMAT_DEFAULT}
     92      * @tests {@link java.util.ResourceBundle.Control#FORMAT_PROPERTIES}
     93      * @tests {@link java.util.ResourceBundle.Control#TTL_DONT_CACHE}
     94      * @tests {@link java.util.ResourceBundle.Control#TTL_NO_EXPIRATION_CONTROL}
     95      */
     96     @SuppressWarnings("nls")
     97     public void test_Constants() {
     98         List<String> list = FORMAT_CLASS;
     99         assertEquals(1, list.size());
    100         assertEquals("java.class", list.get(0));
    101         list = FORMAT_PROPERTIES;
    102         assertEquals(1, list.size());
    103         assertEquals("java.properties", list.get(0));
    104         list = FORMAT_DEFAULT;
    105         assertEquals(2, list.size());
    106         assertEquals("java.class", list.get(0));
    107         assertEquals("java.properties", list.get(1));
    108         try {
    109             FORMAT_CLASS.add("");
    110             fail("Should throw UnsupportedOperationException");
    111         } catch (UnsupportedOperationException e) {
    112             // expected
    113         }
    114         try {
    115             FORMAT_DEFAULT.add("");
    116             fail("Should throw UnsupportedOperationException");
    117         } catch (UnsupportedOperationException e) {
    118             // expected
    119         }
    120         try {
    121             FORMAT_PROPERTIES.add("");
    122             fail("Should throw UnsupportedOperationException");
    123         } catch (UnsupportedOperationException e) {
    124             // expected
    125         }
    126         Class<?> unmodifiableListClass = Collections.unmodifiableList(
    127                 new ArrayList<String>()).getClass();
    128         assertEquals(FORMAT_CLASS.getClass(), unmodifiableListClass);
    129         assertEquals(FORMAT_DEFAULT.getClass(), unmodifiableListClass);
    130         assertEquals(FORMAT_PROPERTIES.getClass(), unmodifiableListClass);
    131         assertEquals(-1L, TTL_DONT_CACHE);
    132         assertEquals(-2L, TTL_NO_EXPIRATION_CONTROL);
    133     }
    134 
    135     /**
    136      * @tests {@link java.util.ResourceBundle.Control#getControl(java.util.List)}.
    137      */
    138     @SuppressWarnings("nls")
    139     public void test_getControl_LList() {
    140         // singleton
    141         assertSame(control, Control.getControl(FORMAT_DEFAULT));
    142         assertSame(controlC, Control.getControl(FORMAT_CLASS));
    143         assertSame(controlP, Control.getControl(FORMAT_PROPERTIES));
    144 
    145         // class
    146         assertTrue(control.getClass() == Control.class);
    147         assertTrue(controlC.getClass() != Control.class);
    148         assertTrue(controlP.getClass() != Control.class);
    149 
    150         // formats: need not same, just need equal
    151         List<String> list = new ArrayList<String>(FORMAT_CLASS);
    152         assertSame(controlC, Control.getControl(list));
    153         // can add
    154         list.add(FORMAT_PROPERTIES.get(0));
    155         assertSame(control, Control.getControl(list));
    156 
    157         // exceptions
    158         try {
    159             Control.getControl(null);
    160             fail("Should throw NullPointerException");
    161         } catch (NullPointerException e) {
    162             // expected
    163         }
    164         list = new ArrayList<String>();
    165         try {
    166             Control.getControl(list);
    167             fail("Should throw IllegalArgumentException");
    168         } catch (IllegalArgumentException e) {
    169             // expected
    170         }
    171         list = new ArrayList<String>(FORMAT_CLASS);
    172         // java.class -> JAVA.CLASS
    173         list.set(0, list.get(0).toUpperCase());
    174         try {
    175             Control.getControl(list);
    176             fail("Should throw IllegalArgumentException");
    177         } catch (IllegalArgumentException e) {
    178             // expected
    179         }
    180         list = new ArrayList<String>(FORMAT_CLASS);
    181         list.add("");
    182         try {
    183             Control.getControl(list);
    184             fail("Should throw IllegalArgumentException");
    185         } catch (IllegalArgumentException e) {
    186             // expected
    187         }
    188     }
    189 
    190     /**
    191      * @tests {@link java.util.ResourceBundle.Control#getNoFallbackControl(java.util.List)}.
    192      */
    193     @SuppressWarnings("nls")
    194     public void test_getNoFallbackControl_LList() {
    195         assertNotSame(control, Control.getNoFallbackControl(FORMAT_DEFAULT));
    196         assertNotSame(controlC, Control.getNoFallbackControl(FORMAT_CLASS));
    197         assertNotSame(controlP, Control.getNoFallbackControl(FORMAT_PROPERTIES));
    198         controlP = Control.getNoFallbackControl(FORMAT_PROPERTIES);
    199         controlC = Control.getNoFallbackControl(FORMAT_CLASS);
    200         control = Control.getNoFallbackControl(FORMAT_DEFAULT);
    201         // singleton
    202         assertSame(control, Control.getNoFallbackControl(FORMAT_DEFAULT));
    203         assertSame(controlC, Control.getNoFallbackControl(FORMAT_CLASS));
    204         assertSame(controlP, Control.getNoFallbackControl(FORMAT_PROPERTIES));
    205 
    206         // class
    207         assertTrue(control.getClass() != Control.class);
    208         assertTrue(controlC.getClass() != Control.class);
    209         assertTrue(controlP.getClass() != Control.class);
    210 
    211         // format
    212         assertEquals(FORMAT_CLASS, controlC.getFormats(""));
    213         assertEquals(FORMAT_DEFAULT, control.getFormats(""));
    214         assertEquals(FORMAT_PROPERTIES, controlP.getFormats(""));
    215 
    216         // no fall back locale
    217         Locale defaultLocale = Locale.getDefault();
    218         Locale.setDefault(new Locale("TestLanguage", "TestCountry", "Var"));
    219         assertNull(control.getFallbackLocale("message", Locale.US));
    220         try {
    221             control.getFallbackLocale("message", null);
    222             fail("Should throw NullPointerException");
    223         } catch (NullPointerException e) {
    224             // expected
    225         }
    226         try {
    227             control.getFallbackLocale(null, Locale.US);
    228             fail("Should throw NullPointerException");
    229         } catch (NullPointerException e) {
    230             // expected
    231         }
    232         Locale.setDefault(defaultLocale);
    233 
    234         // formats: need not same, just need equal
    235         List<String> list = new ArrayList<String>(FORMAT_CLASS);
    236         assertSame(controlC, Control.getNoFallbackControl(list));
    237         // can add
    238         list.add(FORMAT_PROPERTIES.get(0));
    239         assertSame(control, Control.getNoFallbackControl(list));
    240 
    241         // exceptions
    242         try {
    243             Control.getNoFallbackControl(null);
    244             fail("Should throw NullPointerException");
    245         } catch (NullPointerException e) {
    246             // expected
    247         }
    248         list = new ArrayList<String>();
    249         try {
    250             Control.getNoFallbackControl(list);
    251             fail("Should throw IllegalArgumentException");
    252         } catch (IllegalArgumentException e) {
    253             // expected
    254         }
    255         list = new ArrayList<String>(FORMAT_CLASS);
    256         // java.class -> JAVA.CLASS
    257         list.set(0, list.get(0).toUpperCase());
    258         try {
    259             Control.getNoFallbackControl(list);
    260             fail("Should throw IllegalArgumentException");
    261         } catch (IllegalArgumentException e) {
    262             // expected
    263         }
    264         list = new ArrayList<String>(FORMAT_CLASS);
    265         list.add("");
    266         try {
    267             Control.getNoFallbackControl(list);
    268             fail("Should throw IllegalArgumentException");
    269         } catch (IllegalArgumentException e) {
    270             // expected
    271         }
    272     }
    273 
    274     /**
    275      * @tests {@link java.util.ResourceBundle.Control#getFormats(java.lang.String)}.
    276      */
    277     @SuppressWarnings("nls")
    278     public void test_getFormats_LString() {
    279         assertEquals(FORMAT_DEFAULT, control.getFormats(""));
    280         assertEquals(FORMAT_PROPERTIES, controlP.getFormats(""));
    281         assertEquals(FORMAT_CLASS, controlC.getFormats(""));
    282         try {
    283             controlC.getFormats(null);
    284             fail("Should throw NullPointerException");
    285         } catch (NullPointerException e) {
    286             // expected
    287         }
    288     }
    289 
    290     /**
    291      * @tests {@link java.util.ResourceBundle.Control#getCandidateLocales(java.lang.String, java.util.Locale)}.
    292      */
    293     @SuppressWarnings("nls")
    294     public void test_getCandidateLocales_LStringLLocale() {
    295         // the ResourceBundle for this baseName and Locale does not exists
    296         List<Locale> result = control.getCandidateLocales("baseName",
    297                 new Locale("one", "two", "three"));
    298         assertEquals(4, result.size());
    299         Locale locale = result.get(0);
    300         assertEquals("one", locale.getLanguage());
    301         assertEquals("TWO", locale.getCountry());
    302         assertEquals("three", locale.getVariant());
    303         assertEquals(new Locale("one", "TWO"), result.get(1));
    304         assertEquals(new Locale("one"), result.get(2));
    305         assertSame(Locale.ROOT, result.get(3));
    306         // ArrayList is not immutable
    307         assertTrue(ArrayList.class == result.getClass());
    308 
    309         result = control.getCandidateLocales("baseName", new Locale("one",
    310                 "two", ""));
    311         assertEquals(new Locale("one", "TWO"), result.get(0));
    312         assertEquals(new Locale("one"), result.get(1));
    313         assertSame(Locale.ROOT, result.get(2));
    314 
    315         result = control.getCandidateLocales("baseName", new Locale("one", "",
    316                 "three"));
    317         assertEquals(new Locale("one", "", "three"), result.get(0));
    318         assertEquals(new Locale("one"), result.get(1));
    319         assertSame(Locale.ROOT, result.get(2));
    320 
    321         result = control.getCandidateLocales("baseName", new Locale("", "two",
    322                 "three"));
    323         assertEquals(new Locale("", "TWO", "three"), result.get(0));
    324         assertEquals(new Locale("", "TWO"), result.get(1));
    325         assertSame(Locale.ROOT, result.get(2));
    326 
    327         result = control.getCandidateLocales("baseName", new Locale("", "",
    328                 "three"));
    329         assertEquals(new Locale("", "", "three"), result.get(0));
    330         assertSame(Locale.ROOT, result.get(1));
    331 
    332         result = control.getCandidateLocales("baseName", new Locale("", "two",
    333                 ""));
    334         assertEquals(new Locale("", "TWO"), result.get(0));
    335         assertSame(Locale.ROOT, result.get(1));
    336 
    337         result = control.getCandidateLocales("baseName", Locale.ROOT);
    338         assertSame(Locale.ROOT, result.get(0));
    339 
    340         try {
    341             control.getCandidateLocales(null, Locale.US);
    342             fail("Should throw NullPointerException");
    343         } catch (NullPointerException e) {
    344             // expected
    345         }
    346 
    347         try {
    348             control.getCandidateLocales("baseName", null);
    349             fail("Should throw NullPointerException");
    350         } catch (NullPointerException e) {
    351             // expected
    352         }
    353     }
    354 
    355     /**
    356      * @tests {@link java.util.ResourceBundle.Control#getFallbackLocale(java.lang.String, java.util.Locale)}.
    357      */
    358     @SuppressWarnings("nls")
    359     public void test_getFallbackLocale_LStringLLocale() {
    360         Locale defaultLocale = Locale.getDefault();
    361         Locale testLocale = new Locale("TestLanguage", "TestCountry", "Var");
    362         Locale.setDefault(testLocale);
    363         assertSame(testLocale, control.getFallbackLocale("baseName",
    364                 Locale.ROOT));
    365         assertSame(testLocale, control.getFallbackLocale("baseName", Locale.US));
    366         assertSame(null, control.getFallbackLocale("baseName", testLocale));
    367         try {
    368             control.getFallbackLocale(null, Locale.US);
    369             fail("Should throw NullPointerException");
    370         } catch (NullPointerException e) {
    371             // expected
    372         }
    373 
    374         try {
    375             control.getFallbackLocale("baseName", null);
    376             fail("Should throw NullPointerException");
    377         } catch (NullPointerException e) {
    378             // expected
    379         }
    380         // restore
    381         Locale.setDefault(defaultLocale);
    382     }
    383 
    384     @SuppressWarnings("nls")
    385     private void newBundleTester() throws IllegalAccessException,
    386             InstantiationException, IOException, FileNotFoundException {
    387         String className = "tests.support.Support_TestResource";
    388         String propertiesName = Support_Resources.RESOURCE_PACKAGE_NAME
    389                 + ".hyts_resource";
    390         String propertiesNameCopy = "hyts_resource_copy";
    391         String CLASS = "java.class";
    392         String PROPERTIES = "java.properties";
    393         Locale frFR = new Locale("fr", "FR");
    394         // the locale does not exist, but its parent does
    395         Locale enUSVAR = new Locale("en", "US", "VAR");
    396         ClassLoader URLLoader = ResourceBundleTest.getURLClassLoader();
    397         ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
    398         ResourceBundle bundle = null;
    399         final URL srcFile = URLLoader.getResource(control
    400                 .toResourceName(control.toBundleName(propertiesName,
    401                         Locale.ROOT), "properties"));
    402         final File copyFile = copyFile(srcFile);
    403 
    404         // 1. with format of "java.class"
    405         bundle = control.newBundle(className, frFR, CLASS, systemLoader, false);
    406         assertEquals("frFRChildValue1", bundle.getString("child1"));
    407 
    408         bundle = control.newBundle(className, frFR, CLASS, systemLoader, true);
    409         assertEquals("frFRChildValue1", bundle.getString("child1"));
    410 
    411         bundle = control.newBundle(className, Locale.ROOT, CLASS, systemLoader,
    412                 false);
    413         assertEquals("parentValue1", bundle.getString("parent1"));
    414 
    415         assertNull(control.newBundle(className, frFR, CLASS, URLLoader, false));
    416         assertNull(control.newBundle(className, enUSVAR, CLASS, systemLoader,
    417                 false));
    418         assertNull(control.newBundle("wrongName", frFR, CLASS, systemLoader,
    419                 false));
    420         assertNull(control.newBundle(className, Locale.ROOT, PROPERTIES,
    421                 systemLoader, false));
    422 
    423         // 2. with format of "java.properties"
    424         // if copy file exists, run the "changing" test
    425         if (null != URLLoader
    426                 .getResourceAsStream("hyts_resource_copy.properties")) {
    427             ResourceBundle.clearCache(URLLoader);
    428             bundle = control.newBundle(propertiesNameCopy, Locale.ROOT,
    429                     PROPERTIES, URLLoader, false);
    430             assertTrue(bundle.getClass() == PropertyResourceBundle.class);
    431             assertEquals("resource", bundle.getString("property"));
    432             // change the file
    433             changeProperties(copyFile);
    434             assertEquals("resource", bundle.getString("property"));
    435             bundle = control.newBundle(propertiesNameCopy, Locale.ROOT,
    436                     PROPERTIES, URLLoader, false);
    437             assertEquals("changedValue", bundle.getString("property"));
    438             bundle = control.newBundle(propertiesNameCopy, Locale.ROOT,
    439                     PROPERTIES, URLLoader, true);
    440             assertEquals("changedValue", bundle.getString("property"));
    441         } else {
    442             System.err
    443                     .println("Can not find the test file, some code of this test 'newBundleTester' did not run.");
    444         }
    445 
    446         // class loader
    447         bundle = control.newBundle(propertiesName, Locale.ROOT, PROPERTIES,
    448                 URLLoader, true);
    449         assertEquals("resource", bundle.getString("property"));
    450         bundle = control.newBundle(propertiesName, Locale.ROOT, PROPERTIES,
    451                 systemLoader, true);
    452         assertEquals("parent", bundle.getString("property"));
    453 
    454         // locale
    455         bundle = control.newBundle(propertiesName, frFR, PROPERTIES, URLLoader,
    456                 false);
    457         assertEquals("fr_FR_resource", bundle.getString("property"));
    458         assertNull(control.newBundle(propertiesName, frFR, PROPERTIES,
    459                 systemLoader, false));
    460         assertNull(control.newBundle(propertiesName, enUSVAR, PROPERTIES,
    461                 systemLoader, true));
    462         assertNull(control.newBundle(propertiesName, enUSVAR, PROPERTIES,
    463                 URLLoader, true));
    464         assertNull(control.newBundle("wrongName", frFR, PROPERTIES, URLLoader,
    465                 false));
    466 
    467         // exceptions
    468         try {
    469             control.newBundle(null, frFR, PROPERTIES, URLLoader, false);
    470             fail("Should throw NullPointerException");
    471         } catch (NullPointerException e) {
    472             // expected
    473         }
    474         try {
    475             control.newBundle(propertiesName, null, PROPERTIES, URLLoader,
    476                     false);
    477             fail("Should throw NullPointerException");
    478         } catch (NullPointerException e) {
    479             // expected
    480         }
    481         try {
    482             control.newBundle(propertiesName, frFR, null, URLLoader, false);
    483             fail("Should throw NullPointerException");
    484         } catch (NullPointerException e) {
    485             // expected
    486         }
    487         try {
    488             control.newBundle(propertiesName, frFR, PROPERTIES, null, false);
    489             fail("Should throw NullPointerException");
    490         } catch (NullPointerException e) {
    491             // expected
    492         }
    493         // null is returned by toBundleName method
    494         try {
    495             new Control() {
    496                 @Override
    497                 public String toBundleName(@SuppressWarnings("unused")
    498                 String baseName, @SuppressWarnings("unused")
    499                 Locale locale) {
    500                     return null;
    501                 }
    502             }.newBundle(propertiesName, frFR, PROPERTIES, URLLoader, false);
    503             fail("Should throw NullPointerException");
    504         } catch (NullPointerException e) {
    505             // expected
    506         }
    507         try {
    508             control.newBundle(propertiesName, frFR, "JAVA.CLASS", URLLoader,
    509                     false);
    510             fail("Should throw IllegalArgumentException");
    511         } catch (IllegalArgumentException e) {
    512             // expected
    513         }
    514         try {
    515             control.newBundle(this.getClass().getName(), Locale.ROOT, CLASS,
    516                     systemLoader, false);
    517             fail("Should throw ClassCastException");
    518         } catch (ClassCastException e) {
    519             // expected
    520         }
    521         try {
    522             control.newBundle(SubRBStaticPrivate.class.getName(), Locale.ROOT,
    523                     CLASS, systemLoader, false);
    524             fail("Should throw IllegalAccessException");
    525         } catch (IllegalAccessException e) {
    526             // expected
    527         }
    528         class SubRBNonStaticPrivate extends ListResourceBundle {
    529             private SubRBNonStaticPrivate() {
    530                 super();
    531             }
    532 
    533             @Override
    534             protected Object[][] getContents() {
    535                 return null;
    536             }
    537         }
    538         class SubRBNonStaticPublic extends ListResourceBundle {
    539             public SubRBNonStaticPublic() {
    540                 super();
    541             }
    542 
    543             @Override
    544             protected Object[][] getContents() {
    545                 return null;
    546             }
    547         }
    548     }
    549 
    550     @SuppressWarnings("nls")
    551     static File copyFile(final URL src) throws IOException {
    552         String tail = src.getFile().split("hyts_resource")[1];
    553         String tmpdir = System.getProperty("java.io.tmpdir");
    554         if (null == tmpdir) {
    555             return null;
    556         }
    557         String copyName = tmpdir + File.separator + "hyts_resource_copy" + tail;
    558         File copy = new File(copyName);
    559         if (copy.exists()) {
    560             copy.delete();
    561         }
    562         copy.createNewFile();
    563         copy.deleteOnExit();
    564 
    565         Reader in = new InputStreamReader(src.openStream());
    566         Writer out = new FileWriter(copy);
    567         int c;
    568         while ((c = in.read()) != -1) {
    569             out.write(c);
    570         }
    571         in.close();
    572         out.close();
    573         return copy;
    574     }
    575 
    576     static class SubRBStaticPrivate extends ListResourceBundle {
    577         private SubRBStaticPrivate() {
    578             super();
    579         }
    580 
    581         @Override
    582         protected Object[][] getContents() {
    583             return null;
    584         }
    585     }
    586 
    587     /*
    588      * change the value in the .properties file
    589      */
    590     @SuppressWarnings("nls")
    591     static void changeProperties(File file) throws FileNotFoundException {
    592         String newValue = "property=changedValue";
    593         PrintWriter writer = new PrintWriter(file);
    594         writer.write(newValue);
    595         writer.flush();
    596         writer.close();
    597         Scanner scanner = new Scanner(file);
    598         assertEquals(newValue, scanner.nextLine());
    599         scanner.close();
    600     }
    601 
    602     /**
    603      * @tests {@link java.util.ResourceBundle.Control#getTimeToLive(java.lang.String, java.util.Locale)}.
    604      */
    605     @SuppressWarnings("nls")
    606     public void test_getTimeToLive_LStringLLocale() {
    607         assertEquals(TTL_NO_EXPIRATION_CONTROL, control.getTimeToLive(
    608                 "baseName", Locale.US));
    609         try {
    610             control.getTimeToLive(null, Locale.US);
    611             fail("Should throw NullPointerException");
    612         } catch (NullPointerException e) {
    613             // expected
    614         }
    615         try {
    616             control.getTimeToLive("baseName", null);
    617             fail("Should throw NullPointerException");
    618         } catch (NullPointerException e) {
    619             // expected
    620         }
    621     }
    622 
    623     /**
    624      * @throws Exception
    625      * @tests {@link java.util.ResourceBundle.Control#needsReload(java.lang.String, java.util.Locale, java.lang.String, java.lang.ClassLoader, java.util.ResourceBundle, long)}.
    626      */
    627     @SuppressWarnings("nls")
    628     public void test_needsReload_LStringLLocaleLStringLClassLoaderResourceBundleJ()
    629             throws Exception {
    630         String className = "tests.support.Support_TestResource";
    631         String propertiesName = Support_Resources.RESOURCE_PACKAGE_NAME
    632                 + ".hyts_resource";
    633         String propertiesNameCopy = "hyts_resource_copy";
    634         String CLASS = "java.class";
    635         String PROPERTIES = "java.properties";
    636         Locale frFR = new Locale("fr", "FR");
    637         ClassLoader URLLoader = ResourceBundleTest.getURLClassLoader();
    638         ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
    639         ResourceBundle bundle = null;
    640         long time = 0L;
    641         final URL srcFile = URLLoader.getResource(control.toResourceName(
    642                 control.toBundleName(propertiesName, frFR), "properties"));
    643         final File copyFile = copyFile(srcFile);
    644 
    645         // 1. format = "java.properties"
    646         if (null != URLLoader
    647                 .getResourceAsStream("hyts_resource_copy_fr_FR.properties")) {
    648             Thread.sleep(1000);
    649             bundle = control.newBundle(propertiesNameCopy, frFR, PROPERTIES,
    650                     URLLoader, false);
    651             time = System.currentTimeMillis();
    652             assertTrue(bundle.getClass() == PropertyResourceBundle.class);
    653             assertEquals("fr_FR_resource", bundle.getString("property"));
    654             assertFalse(control.needsReload(propertiesNameCopy, frFR,
    655                     PROPERTIES, URLLoader, bundle, time));
    656             // change the file
    657             Thread.sleep(2000);
    658             changeProperties(copyFile);
    659             assertTrue(control.needsReload(propertiesNameCopy, frFR,
    660                     PROPERTIES, URLLoader, bundle, time));
    661             // detect again
    662             assertTrue(control.needsReload(propertiesNameCopy, frFR,
    663                     PROPERTIES, URLLoader, bundle, time));
    664             // long long ago
    665             assertTrue(control.needsReload(propertiesNameCopy, frFR,
    666                     PROPERTIES, URLLoader, bundle, 2006L));
    667             // other loader
    668             assertFalse(control.needsReload(propertiesNameCopy, frFR,
    669                     PROPERTIES, systemLoader, bundle, time));
    670             // other bundle
    671             ResourceBundle otherBundle = control.newBundle(propertiesName,
    672                     Locale.ROOT, PROPERTIES, systemLoader, false);
    673             assertEquals("parent", otherBundle.getString("property"));
    674             assertTrue(control.needsReload(propertiesNameCopy, frFR,
    675                     PROPERTIES, URLLoader, otherBundle, time));
    676             otherBundle = control.newBundle(propertiesName, Locale.ROOT,
    677                     PROPERTIES, URLLoader, false);
    678             assertEquals("resource", otherBundle.getString("property"));
    679             assertTrue(control.needsReload(propertiesNameCopy, frFR,
    680                     PROPERTIES, URLLoader, otherBundle, time));
    681             // other time
    682             assertFalse(control.needsReload(propertiesNameCopy, frFR,
    683                     PROPERTIES, URLLoader, bundle, System.currentTimeMillis()));
    684         } else {
    685             System.err
    686                     .println("Can not find the test file, some code of this test 'test_needsReload_LStringLLocaleLStringLClassLoaderResourceBundleJ' did not run.");
    687 
    688         }
    689 
    690         // 2. format = "java.class"
    691         bundle = control.newBundle(className, frFR, CLASS, systemLoader, false);
    692         time = System.currentTimeMillis();
    693         assertEquals("frFRValue3", bundle.getString("parent3"));
    694         assertFalse(control.needsReload(className, frFR, CLASS, systemLoader,
    695                 bundle, time));
    696         // exceptions
    697         control.needsReload(propertiesName, frFR, PROPERTIES, URLLoader,
    698                 bundle, time);
    699         try {
    700             control
    701                     .needsReload(null, frFR, PROPERTIES, URLLoader, bundle,
    702                             time);
    703             fail("Should throw NullPointerException");
    704         } catch (NullPointerException e) {
    705             // expected
    706         }
    707         try {
    708             control.needsReload(propertiesName, null, PROPERTIES, URLLoader,
    709                     bundle, time);
    710             fail("Should throw NullPointerException");
    711         } catch (NullPointerException e) {
    712             // expected
    713         }
    714         try {
    715             control.needsReload(propertiesName, frFR, null, URLLoader, bundle,
    716                     time);
    717             fail("Should throw NullPointerException");
    718         } catch (NullPointerException e) {
    719             // expected
    720         }
    721         try {
    722             control.needsReload(propertiesName, frFR, PROPERTIES, null, bundle,
    723                     time);
    724             fail("Should throw NullPointerException");
    725         } catch (NullPointerException e) {
    726             // expected
    727         }
    728         try {
    729             control.needsReload(propertiesName, frFR, PROPERTIES, URLLoader,
    730                     null, time);
    731             fail("Should throw NullPointerException");
    732         } catch (NullPointerException e) {
    733             // expected
    734         }
    735     }
    736 
    737     /**
    738      * @tests {@link java.util.ResourceBundle.Control#toBundleName(java.lang.String, java.util.Locale)}.
    739      */
    740     @SuppressWarnings("nls")
    741     public void test_toBundleName_LStringLLocale() {
    742         assertEquals("baseName_one_TWO_three", control.toBundleName("baseName",
    743                 new Locale("one", "two", "three")));
    744         assertEquals("baseName_one_TWO", control.toBundleName("baseName",
    745                 new Locale("one", "two")));
    746         assertEquals("baseName_one__three", control.toBundleName("baseName",
    747                 new Locale("one", "", "three")));
    748         assertEquals("baseName__TWO_three", control.toBundleName("baseName",
    749                 new Locale("", "two", "three")));
    750         assertEquals("baseName_one", control.toBundleName("baseName",
    751                 new Locale("one", "", "")));
    752         assertEquals("baseName___three", control.toBundleName("baseName",
    753                 new Locale("", "", "three")));
    754         assertEquals("baseName__TWO", control.toBundleName("baseName",
    755                 new Locale("", "two", "")));
    756         assertEquals("baseName", control.toBundleName("baseName", new Locale(
    757                 "", "", "")));
    758         assertEquals("baseName", control.toBundleName("baseName", Locale.ROOT));
    759         assertEquals("_one_TWO_three", control.toBundleName("", new Locale(
    760                 "one", "two", "three")));
    761         assertEquals("", control.toBundleName("", Locale.ROOT));
    762 
    763         assertEquals("does.not.exists_one_TWO_three", control.toBundleName(
    764                 "does.not.exists", new Locale("one", "two", "three")));
    765         assertEquals("does/not/exists_one_TWO_three", control.toBundleName(
    766                 "does/not/exists", new Locale("one", "two", "three")));
    767         assertEquals("does_not_exists__one_TWO_three", control.toBundleName(
    768                 "does_not_exists_", new Locale("one", "two", "three")));
    769 
    770         assertEquals("...", control.toBundleName("...", Locale.ROOT));
    771         assertEquals("s/./\\//g", control
    772                 .toBundleName("s/./\\//g", Locale.ROOT));
    773         assertEquals("123_one", control.toBundleName("123", new Locale("one")));
    774 
    775         try {
    776             control.toBundleName(null, Locale.US);
    777             fail("Should throw NullPointerException");
    778         } catch (NullPointerException e) {
    779             // expected
    780         }
    781 
    782         try {
    783             control.toBundleName("baseName", null);
    784             fail("Should throw NullPointerException");
    785         } catch (NullPointerException e) {
    786             // expected
    787         }
    788     }
    789 
    790     /**
    791      * @tests {@link java.util.ResourceBundle.Control#toResourceName(java.lang.String, java.lang.String)}.
    792      */
    793     @SuppressWarnings("nls")
    794     public void test_toResourceNameLStringLString() {
    795         assertEquals("does/not/exists_language_country.someSuffix", control
    796                 .toResourceName("does.not.exists_language_country",
    797                         "someSuffix"));
    798         assertEquals("does/not/exists_language_country.someSuffix", control
    799                 .toResourceName("does/not/exists_language_country",
    800                         "someSuffix"));
    801         assertEquals("does///not//exists_language/country.someSuffix", control
    802                 .toResourceName("does...not..exists_language.country",
    803                         "someSuffix"));
    804         assertEquals("does\\not\\exists_language_country.someSuffix", control
    805                 .toResourceName("does\\not\\exists_language_country",
    806                         "someSuffix"));
    807         assertEquals("does/not/exists_language_country/.someSuffix", control
    808                 .toResourceName("does.not.exists_language_country.",
    809                         "someSuffix"));
    810         assertEquals("does/not/exists_language_country../someSuffix", control
    811                 .toResourceName("does.not.exists_language_country",
    812                         "./someSuffix"));
    813 
    814         assertEquals("///.//", control.toResourceName("...", "//"));
    815         assertEquals("///...", control.toResourceName("///", ".."));
    816         assertEquals("123...", control.toResourceName("123", ".."));
    817         assertEquals("base.", control.toResourceName("base", ""));
    818         assertEquals(".suffix", control.toResourceName("", "suffix"));
    819         assertEquals(".", control.toResourceName("", ""));
    820 
    821         try {
    822             control.toResourceName(null, "suffix");
    823             fail("Should throw NullPointerException");
    824         } catch (NullPointerException e) {
    825             // expected
    826         }
    827 
    828         try {
    829             control.toResourceName("bundleName", null);
    830             fail("Should throw NullPointerException");
    831         } catch (NullPointerException e) {
    832             // expected
    833         }
    834 
    835     }
    836 
    837     /**
    838      * @throws java.lang.Exception
    839      */
    840     @Override
    841     protected void tearDown() throws Exception {
    842         super.tearDown();
    843     }
    844 
    845     /**
    846      * @throws java.lang.Exception
    847      */
    848     @Override
    849     protected void setUp() throws Exception {
    850         super.setUp();
    851         controlP = Control.getControl(FORMAT_PROPERTIES);
    852         controlC = Control.getControl(FORMAT_CLASS);
    853         control = Control.getControl(FORMAT_DEFAULT);
    854     }
    855 
    856 }
    857