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 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import java.io.ByteArrayInputStream;
     21 import java.io.CharArrayReader;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.io.Reader;
     25 import java.io.UnsupportedEncodingException;
     26 import java.nio.ByteBuffer;
     27 import java.nio.CharBuffer;
     28 import java.nio.charset.Charset;
     29 import java.util.Enumeration;
     30 import java.util.MissingResourceException;
     31 import java.util.PropertyResourceBundle;
     32 import java.util.ResourceBundle;
     33 import java.util.Vector;
     34 
     35 public class PropertyResourceBundleTest extends junit.framework.TestCase {
     36 
     37     static PropertyResourceBundle prb;
     38 
     39     /**
     40      * @throws IOException
     41      * java.util.PropertyResourceBundle#PropertyResourceBundle(java.io.InputStream)
     42      */
     43     @SuppressWarnings("nls")
     44     public void test_ConstructorLjava_io_InputStream() throws IOException {
     45         InputStream propertiesStream = new ByteArrayInputStream(
     46                 "p1=one\ncharset=iso-8859-1".getBytes("ISO-8859-1"));
     47         prb = new PropertyResourceBundle(propertiesStream);
     48         assertEquals(2, prb.keySet().size());
     49         assertEquals("one", prb.getString("p1"));
     50         assertEquals("iso-8859-1", prb.getString("charset"));
     51 
     52         propertiesStream = new ByteArrayInputStream("p1=one\ncharset=UTF-8"
     53                 .getBytes("UTF-8"));
     54         prb = new PropertyResourceBundle(propertiesStream);
     55         assertEquals(2, prb.keySet().size());
     56         assertEquals("UTF-8", prb.getString("charset"));
     57 
     58         try {
     59             new PropertyResourceBundle((InputStream) null);
     60             fail("Should throw NullPointerException");
     61         } catch (NullPointerException e) {
     62             // expected
     63         }
     64     }
     65 
     66     /**
     67      * @throws IOException
     68      * {@link java.util.PropertyResourceBundle#PropertyResourceBundle(java.io.Reader)}
     69      * @since 1.6
     70      */
     71     @SuppressWarnings("nls")
     72     public void test_ConstructorLjava_io_Reader() throws IOException {
     73         Charset charset = Charset.forName("ISO-8859-1");
     74         String content = "p1=one\nfeature=good_feature";
     75         CharBuffer cbuffer = charset.decode(ByteBuffer.wrap(content
     76                 .getBytes("ISO-8859-1")));
     77         char[] chars = new char[cbuffer.limit()];
     78         cbuffer.get(chars);
     79 
     80         prb = new PropertyResourceBundle(new CharArrayReader(chars));
     81         assertEquals(2, prb.keySet().size());
     82         assertEquals("one", prb.getString("p1"));
     83         assertEquals("good_feature", prb.getString("feature"));
     84 
     85         charset = Charset.forName("UTF-8");
     86         cbuffer = charset.decode(ByteBuffer.wrap(content.getBytes("UTF-8")));
     87         chars = new char[cbuffer.limit()];
     88         cbuffer.get(chars);
     89 
     90         prb = new PropertyResourceBundle(new CharArrayReader(chars));
     91         assertEquals(2, prb.keySet().size());
     92         assertEquals("one", prb.getString("p1"));
     93         assertEquals("good_feature", prb.getString("feature"));
     94 
     95         try {
     96             new PropertyResourceBundle((Reader) null);
     97             fail("Should throw NullPointerException");
     98         } catch (NullPointerException e) {
     99             // expected
    100         }
    101     }
    102 
    103     /**
    104      * java.util.PropertyResourceBundle#getKeys()
    105      */
    106     public void test_getKeys() {
    107         Enumeration keyEnum = prb.getKeys();
    108         Vector<Object> test = new Vector<Object>();
    109         int keyCount = 0;
    110         while (keyEnum.hasMoreElements()) {
    111             test.addElement(keyEnum.nextElement());
    112             keyCount++;
    113         }
    114 
    115         assertEquals("Returned the wrong number of keys", 2, keyCount);
    116         assertTrue("Returned the wrong keys", test.contains("p1")
    117                 && test.contains("p2"));
    118     }
    119 
    120     /**
    121      * java.util.PropertyResourceBundle#handleGetObject(java.lang.String)
    122      */
    123     public void test_handleGetObjectLjava_lang_String() {
    124         // Test for method java.lang.Object
    125         // java.util.PropertyResourceBundle.handleGetObject(java.lang.String)
    126         try {
    127             assertTrue("Returned incorrect objects", prb.getObject("p1")
    128                     .equals("one")
    129                     && prb.getObject("p2").equals("two"));
    130         } catch (MissingResourceException e) {
    131             fail(
    132                     "Threw MisingResourceException for a key contained in the bundle");
    133         }
    134         try {
    135             prb.getObject("Not in the bundle");
    136         } catch (MissingResourceException e) {
    137             return;
    138         }
    139         fail(
    140                 "Failed to throw MissingResourceException for object not in the bundle");
    141     }
    142 
    143     /**
    144      * Sets up the fixture, for example, open a network connection. This method
    145      * is called before a test is executed.
    146      *
    147      * @throws UnsupportedEncodingException
    148      */
    149     protected void setUp() throws UnsupportedEncodingException {
    150         InputStream propertiesStream = new ByteArrayInputStream(
    151                 "p1=one\np2=two".getBytes("ISO-8859-1"));
    152         try {
    153             prb = new PropertyResourceBundle(propertiesStream);
    154         } catch (java.io.IOException e) {
    155             fail(
    156                     "Construction of PropertyResourceBundle threw IOException");
    157         }
    158     }
    159 
    160     /**
    161      * Tears down the fixture, for example, close a network connection. This
    162      * method is called after a test is executed.
    163      */
    164     protected void tearDown() {
    165     }
    166 
    167     /**
    168      * {@link java.util.PropertyResourceBundle#Enumeration}
    169      */
    170     public void test_access$0_Enumeration() throws IOException {
    171         class MockResourceBundle extends PropertyResourceBundle {
    172             MockResourceBundle(java.io.InputStream stream) throws IOException {
    173                 super(stream);
    174             }
    175 
    176             @Override
    177             protected void setParent(ResourceBundle bundle) {
    178                 super.setParent(bundle);
    179             }
    180         }
    181 
    182         java.io.InputStream localStream = new java.io.ByteArrayInputStream(
    183                 "p3=three\np4=four".getBytes());
    184         MockResourceBundle localPrb = new MockResourceBundle(localStream);
    185         localPrb.setParent(prb);
    186         Enumeration<String> keys = localPrb.getKeys();
    187         Vector<String> contents = new Vector<String>();
    188         while (keys.hasMoreElements()) {
    189             contents.add(keys.nextElement());
    190         }
    191 
    192         assertEquals("did not get the right number of properties", 4, contents
    193                 .size());
    194         assertTrue("did not get the parent property p1", contents
    195                 .contains("p1"));
    196         assertTrue("did not get the parent property p2", contents
    197                 .contains("p2"));
    198         assertTrue("did not get the local property p3", contents.contains("p3"));
    199         assertTrue("did not get the local property p4", contents.contains("p4"));
    200     }
    201 }
    202