Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.java.text;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.ObjectInputStream;
     22 import java.io.ObjectOutputStream;
     23 import java.text.DateFormatSymbols;
     24 import java.text.SimpleDateFormat;
     25 import java.util.Arrays;
     26 import java.util.Date;
     27 import java.util.HashMap;
     28 import java.util.Locale;
     29 import java.util.TimeZone;
     30 
     31 public class DateFormatSymbolsTest extends junit.framework.TestCase {
     32     private void assertLocaleIsEquivalentToRoot(Locale locale) {
     33         DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
     34         assertEquals(DateFormatSymbols.getInstance(Locale.ROOT), dfs);
     35     }
     36 
     37     /** http://b/3056586 */
     38     public void test_getInstance_unknown_locale() throws Exception {
     39         // TODO: we fail this test. on Android, the root locale uses GMT offsets as names.
     40         // see the invalid locale test below. on the RI, the root locale uses English names.
     41         assertLocaleIsEquivalentToRoot(new Locale("xx", "XX"));
     42     }
     43 
     44     public void test_getInstance_invalid_locale() throws Exception {
     45         assertLocaleIsEquivalentToRoot(new Locale("not exist language", "not exist country"));
     46     }
     47 
     48     public void testSerialization() throws Exception {
     49         // The Polish language needs stand-alone month and weekday names.
     50         Locale pl = new Locale("pl");
     51         DateFormatSymbols originalDfs = new DateFormatSymbols(pl);
     52 
     53         // Serialize...
     54         ByteArrayOutputStream out = new ByteArrayOutputStream();
     55         new ObjectOutputStream(out).writeObject(originalDfs);
     56         byte[] bytes = out.toByteArray();
     57 
     58         // Deserialize...
     59         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
     60         DateFormatSymbols deserializedDfs = (DateFormatSymbols) in.readObject();
     61         assertEquals(-1, in.read());
     62 
     63         // The two objects should claim to be equal, even though they aren't really.
     64         assertEquals(originalDfs, deserializedDfs);
     65 
     66         // The original differentiates between regular month names and stand-alone month names...
     67         assertEquals("stycznia", formatDate(pl, "MMMM", originalDfs));
     68         assertEquals("stycze\u0144", formatDate(pl, "LLLL", originalDfs));
     69 
     70         // Whereas the deserialized object can't, because it lost the strings...
     71         assertEquals("stycznia", formatDate(pl, "MMMM", deserializedDfs));
     72         assertEquals("stycznia", formatDate(pl, "LLLL", deserializedDfs));
     73     }
     74 
     75     private String formatDate(Locale l, String fmt, DateFormatSymbols dfs) {
     76         SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
     77         sdf.setDateFormatSymbols(dfs);
     78         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
     79         return sdf.format(new Date(0));
     80     }
     81 
     82     public void test_getZoneStrings_cloning() throws Exception {
     83         // Check that corrupting our array doesn't affect other callers.
     84 
     85         // Kill a row.
     86         {
     87             String[][] originalZoneStrings = DateFormatSymbols.getInstance(Locale.US).getZoneStrings();
     88             assertNotNull(originalZoneStrings[0]);
     89             originalZoneStrings[0] = null;
     90             String[][] currentZoneStrings = DateFormatSymbols.getInstance(Locale.US).getZoneStrings();
     91             assertNotNull(currentZoneStrings[0]);
     92         }
     93 
     94         // Kill an element.
     95         {
     96             String[][] originalZoneStrings = DateFormatSymbols.getInstance(Locale.US).getZoneStrings();
     97             assertNotNull(originalZoneStrings[0][0]);
     98             originalZoneStrings[0][0] = null;
     99             String[][] currentZoneStrings = DateFormatSymbols.getInstance(Locale.US).getZoneStrings();
    100             assertNotNull(currentZoneStrings[0][0]);
    101         }
    102     }
    103 
    104     public void test_getZoneStrings_UTC() throws Exception {
    105         HashMap<String, String[]> zoneStrings = new HashMap<String, String[]>();
    106         for (String[] row : DateFormatSymbols.getInstance(Locale.US).getZoneStrings()) {
    107             zoneStrings.put(row[0], row);
    108         }
    109 
    110         assertUtc(zoneStrings.get("Etc/UCT"));
    111         assertUtc(zoneStrings.get("Etc/UTC"));
    112         assertUtc(zoneStrings.get("Etc/Universal"));
    113         assertUtc(zoneStrings.get("Etc/Zulu"));
    114 
    115         assertUtc(zoneStrings.get("UCT"));
    116         assertUtc(zoneStrings.get("UTC"));
    117         assertUtc(zoneStrings.get("Universal"));
    118         assertUtc(zoneStrings.get("Zulu"));
    119     }
    120     private static void assertUtc(String[] row) {
    121         // Element 0 is the Olson id. The short names should be "UTC".
    122         // On the RI, the long names are localized. ICU doesn't have those, so we just use UTC.
    123         assertEquals(Arrays.toString(row), "UTC", row[2]);
    124         assertEquals(Arrays.toString(row), "UTC", row[4]);
    125     }
    126 }
    127