Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4624506

ResourceBundle fails to fetch key from the default properties file

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.3.1
    • core-libs
    • None

      ResourceBundle fails to fetch key from the default properties file, instead
      an exception is thrown. This behavior is seen in both 1.3.1 and 1.4. In the
      following test program, test case 2 is the one which has problem. Test case 2
      is the same as test case 1 except that the test case 2 is accessing resource
      bundle for France and test case 1 is accessing resource bundle for US.

      Test.java
      ----------
      import java.util.*;

      public class Test {

         public ResourceBundle fetchResourceBundle(String language, String country){
            return(ResourceBundle.getBundle("MessagesBundle",
                                             new Locale(language, country)));

         }

         public void testCase1() {
            System.out.println("Test case 1");
            System.out.println("-----------");
            ResourceBundle rb = fetchResourceBundle("en", "US");

            if(rb.getString("greetings").compareTo("Hello") == 0)
               System.out.println("PASS: Match greeting string");
            else
               System.out.println("FAIL: Match greeting string");

            if(rb.getString("inquiry").compareTo("How are you?") == 0)
               System.out.println("PASS: Match inquiry string");
            else
               System.out.println("FAIL: Match inquiry string");

            if(rb.getString("farewell").compareTo("Goodbye") == 0)
               System.out.println("PASS: Match farewell string");
            else
               System.out.println("FAIL: Match farewell string");

            if(rb.getString("test1").compareTo("Test message 1") == 0)
               System.out.println("PASS: Match test1 string");
            else
               System.out.println("FAIL: Match test1 string");

            if(rb.getString("test2").compareTo("Test message 2") == 0)
               System.out.println("PASS: Match test2 string");
            else
               System.out.println("FAIL: Match test2 string");

            System.out.println();
         }

         public void testCase2() {
            System.out.println("Test case 2");
            System.out.println("-----------");
            ResourceBundle rb = fetchResourceBundle("fr", "FR");

            if(rb.getString("greetings").compareTo("Hello") == 0)
               System.out.println("PASS: Match greeting string");
            else
               System.out.println("FAIL: Match greeting string");

            if(rb.getString("inquiry").compareTo("How are you?") == 0)
               System.out.println("PASS: Match inquiry string");
            else
               System.out.println("FAIL: Match inquiry string");

            if(rb.getString("farewell").compareTo("Au revoir") == 0)
               System.out.println("PASS: Match farewell string");
            else
               System.out.println("FAIL: Match farewell string");

            if(rb.getString("test1").compareTo("Test message 1") == 0)
               System.out.println("PASS: Match test1 string");
            else
               System.out.println("FAIL: Match test1 string");

            if(rb.getString("test2").compareTo("Test message 2") == 0)
               System.out.println("PASS: Match test2 string");
            else
               System.out.println("FAIL: Match test2 string");

            System.out.println();
         }

         public void run() {
            testCase1();
            testCase2();
         }

         public static void main(String[] args) {
            Test test = new Test();

            test.run();
         }
      }

      MessagesBundle.properties
      -------------------------
      greetings = Hello
      farewell = Goodbye
      inquiry = How are you?

      MessagesBundle_fr_FR.properties
      -------------------------------
      farewell = Au revoir

      MessagesBundle_en_US.properties
      -------------------------------
      test1 = Test message 1

      MessagesBundle_en.properties
      ----------------------------
      test2 = Test message 2

      akila@agni3 > /import/java/jdk1.3.1/binaries/solsparc/bin/java Test
      Test case 1
      -----------
      PASS: Match greeting string
      PASS: Match inquiry string
      PASS: Match farewell string
      PASS: Match test1 string
      PASS: Match test2 string

      Test case 2
      -----------
      PASS: Match greeting string
      PASS: Match inquiry string
      PASS: Match farewell string
      Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key test1
              at java.util.ResourceBundle.getObject(ResourceBundle.java:382)
              at java.util.ResourceBundle.getObject(ResourceBundle.java:379)
              at java.util.ResourceBundle.getString(ResourceBundle.java:354)
              at Test.testCase2(Test.java:60)
              at Test.run(Test.java:75)
              at Test.main(Test.java:81)

      So if the resource bundle for US is called directly then the keys(test1 & test2)
      are found(test case 1), but if the resource bundle for US(default) is called
      indirectly then the keys(test1 & test2) are not found(test case2).


      ###@###.### 2002-01-16

            nlindenbsunw Norbert Lindenberg (Inactive)
            aksivaku Akila Sivakumar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: