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 */
17package org.apache.harmony.text.tests.java.text;
18
19import java.io.File;
20import java.net.URL;
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
25import java.text.DateFormatSymbols;
26import java.util.Arrays;
27import java.util.Locale;
28import java.util.ServiceConfigurationError;
29
30import org.apache.harmony.text.tests.java.text.MockedDateFormatSymbolsProvider.MockedDateFormatSymbols;
31
32public class DateFormatSymbolsTest extends junit.framework.TestCase {
33
34    private DateFormatSymbols dfs;
35
36    /**
37     * @tests java.text.DateFormatSymbols#DateFormatSymbols()
38     */
39    public void test_Constructor() {
40        // Test for method java.text.DateFormatSymbols()
41        // Used in tests
42        new DateFormatSymbols();
43    }
44
45    /**
46     * @tests java.text.DateFormatSymbols#DateFormatSymbols(java.util.Locale)
47     */
48    public void test_ConstructorLjava_util_Locale() {
49        // Test for method java.text.DateFormatSymbols(java.util.Locale)
50        new DateFormatSymbols(new Locale("en", "us"));
51    }
52
53    /**
54     * @tests java.text.DateFormatSymbols#getAvailableLocales()
55     */
56    public void test_getAvailableLocales_no_provider() throws Exception {
57        Locale[] locales = DateFormatSymbols.getAvailableLocales();
58        assertNotNull(locales);
59        // must contain Locale.US
60        boolean flag = false;
61        for (Locale locale : locales) {
62            if (locale.equals(Locale.US)) {
63                flag = true;
64                break;
65            }
66        }
67        assertTrue(flag);
68    }
69
70    /**
71     * @tests java.text.DateFormatSymbols#getAvailableLocales()
72     */
73    public void test_getAvailableLocales_correct_provider() throws Exception {
74        URL path1 = new File("src/test/resources/provider/correct").toURL();
75        URL path2 = new File("resources/provider/correct").toURL();
76        LoadLocaleProviderTestHelper helper = new LoadLocaleProviderTestHelper(
77                new URL[] { path1, path2 }) {
78            @Override
79            public void test() {
80                Locale[] locales = DateFormatSymbols.getAvailableLocales();
81                assertNotNull(locales);
82                // must contain mock Locale
83                boolean flag = false;
84                for (Locale locale : locales) {
85                    if (locale.getLanguage().equals("mock")) {
86                        flag = true;
87                        break;
88                    }
89                }
90                assertTrue(flag);
91            }
92
93        };
94
95        if (helper.getThrowable() != null) {
96            throw new Exception(helper.getThrowable());
97        }
98    }
99
100    /**
101     * @tests java.text.DateFormatSymbols#getAvailableLocales()
102     */
103    public void test_getAvailableLocales_wrong_provider() throws Exception {
104        URL path1 = new File("src/test/resources/provider/wrong").toURL();
105        URL path2 = new File("resources/provider/wrong").toURL();
106        LoadLocaleProviderTestHelper helper = new LoadLocaleProviderTestHelper(
107                new URL[] { path1, path2 }) {
108            @Override
109            public void test() {
110                try {
111                    DateFormatSymbols.getAvailableLocales();
112                    fail("Should throw ServiceConfigurationError");
113                } catch (ServiceConfigurationError e) {
114                    // expected
115                }
116            }
117        };
118
119        if (helper.getThrowable() != null) {
120            throw new Exception(helper.getThrowable());
121        }
122    }
123
124    /**
125     * @tests java.text.DateFormatSymbols#getInstance()
126     */
127    public void test_getInstance() {
128        DateFormatSymbols.getInstance();
129        assertEquals(new DateFormatSymbols(), DateFormatSymbols.getInstance());
130        assertEquals(new DateFormatSymbols(Locale.getDefault()),
131                DateFormatSymbols.getInstance());
132
133        assertNotSame(DateFormatSymbols.getInstance(), DateFormatSymbols.getInstance());
134    }
135
136    public void test_getInstanceLjava_util_Locale() {
137        try {
138            DateFormatSymbols.getInstance(null);
139            fail("Should throw NullPointerException");
140        } catch (NullPointerException e) {
141            // expected
142        }
143
144        assertEquals(new DateFormatSymbols(Locale.GERMANY), DateFormatSymbols
145                .getInstance(Locale.GERMANY));
146
147        Locale locale = new Locale("not exist language", "not exist country");
148        DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
149        assertNotNull(symbols);
150        assertEquals(DateFormatSymbols.getInstance(), symbols);
151    }
152
153    /**
154     * @tests java.text.DateFormatSymbols#getInstance(Locale)
155     */
156    public void test_getInstanceLjava_util_Locale_no_provider() {
157        try {
158            DateFormatSymbols.getInstance(null);
159            fail("Should throw NullPointerException");
160        } catch (NullPointerException e) {
161            // expected
162        }
163
164        assertEquals(new DateFormatSymbols(Locale.GERMANY), DateFormatSymbols
165                .getInstance(Locale.GERMANY));
166
167        Locale locale = new Locale("not exist language", "not exist country");
168        DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
169        assertNotNull(symbols);
170        assertEquals(DateFormatSymbols.getInstance(), symbols);
171    }
172
173    /**
174     * @tests java.text.DateFormatSymbols#getInstance(Locale)
175     */
176    public void test_getInstanceLjava_util_Locale_correct_provider()
177            throws Exception {
178        URL path1 = new File("src/test/resources/provider/correct").toURL();
179        URL path2 = new File("resources/provider/correct").toURL();
180        LoadLocaleProviderTestHelper helper = new LoadLocaleProviderTestHelper(
181                new URL[] { path1, path2 }) {
182            @Override
183            public void test() {
184                DateFormatSymbols symbols = DateFormatSymbols
185                        .getInstance(new Locale("Mock"));
186                assertTrue(symbols instanceof MockedDateFormatSymbols);
187
188            }
189        };
190
191        if (helper.getThrowable() != null) {
192            throw new Exception(helper.getThrowable());
193        }
194    }
195
196    /**
197     * @tests java.text.DateFormatSymbols#getInstance(Locale)
198     */
199    public void test_getInstanceLjava_util_Locale_wrong_provider()
200            throws Exception {
201        URL path1 = new File("src/test/resources/provider/wrong").toURL();
202        URL path2 = new File("resources/provider/wrong").toURL();
203        LoadLocaleProviderTestHelper helper = new LoadLocaleProviderTestHelper(
204                new URL[] { path1, path2 }) {
205            @Override
206            public void test() {
207                try {
208                    DateFormatSymbols.getInstance(new Locale("Mock"));
209                    fail("Should throw ServiceConfigurationError");
210                } catch (ServiceConfigurationError e) {
211                    // expected
212                }
213            }
214        };
215
216        if (helper.getThrowable() != null) {
217            throw new Exception(helper.getThrowable());
218        }
219    }
220
221    /**
222     * @tests java.text.DateFormatSymbols#clone()
223     */
224    public void test_clone() {
225        // Test for method java.lang.Object java.text.DateFormatSymbols.clone()
226        DateFormatSymbols symbols = new DateFormatSymbols();
227        DateFormatSymbols clone = (DateFormatSymbols) symbols.clone();
228        assertTrue("Not equal", symbols.equals(clone));
229    }
230
231    /**
232     * @tests java.text.DateFormatSymbols#equals(java.lang.Object)
233     */
234    public void test_equalsLjava_lang_Object() {
235        // Test for method boolean
236        // java.text.DateFormatSymbols.equals(java.lang.Object)
237        assertTrue("Equal object returned true", dfs.equals(dfs.clone()));
238        dfs.setLocalPatternChars("KKKKKKKKK");
239        assertTrue("Un-Equal objects returned false", !dfs
240                .equals(new DateFormatSymbols()));
241    }
242
243    /**
244     * @tests java.text.DateFormatSymbols#getAmPmStrings()
245     */
246    public void test_getAmPmStrings() {
247        // Test for method java.lang.String []
248        // java.text.DateFormatSymbols.getAmPmStrings()
249        String[] retVal = dfs.getAmPmStrings();
250        String[] val = { "AM", "PM" };
251        if (retVal.length != val.length)
252            fail("Returned wrong array");
253        for (int i = 0; i < val.length; i++)
254            assertTrue("Array values do not match", retVal[i].equals(val[i]));
255    }
256
257    /**
258     * @tests java.text.DateFormatSymbols#getEras()
259     */
260    public void test_getEras() {
261        // Test for method java.lang.String []
262        // java.text.DateFormatSymbols.getEras()
263        String[] retVal = dfs.getEras();
264        String[] val = { "BC", "AD" };
265        if (retVal.length != val.length)
266            fail("Returned wrong array");
267        for (int i = 0; i < val.length; i++)
268            assertTrue("Array values do not match", retVal[i].equals(val[i]));
269    }
270
271    /**
272     * @tests java.text.DateFormatSymbols#getLocalPatternChars()
273     */
274    public void test_getLocalPatternChars() {
275        // Test for method java.lang.String
276        // java.text.DateFormatSymbols.getLocalPatternChars()
277        String retVal = dfs.getLocalPatternChars();
278
279        String val = "GyMdkHmsSEDFwWahKzYeugAZvcLQqV";
280
281        assertEquals("Returned incorrect pattern string", val, retVal);
282    }
283
284    /**
285     * @tests java.text.DateFormatSymbols#getMonths()
286     */
287    public void test_getMonths() {
288        // Test for method java.lang.String []
289        // java.text.DateFormatSymbols.getMonths()
290        String[] retVal = dfs.getMonths();
291        String[] val = { "January", "February", "March", "April", "May",
292                "June", "July", "August", "September", "October", "November",
293                "December"};
294        assertEquals("Returned wrong array: ", val.length, retVal.length);
295        for (int i = 0; i < val.length; i++)
296            assertTrue("Array values do not match", retVal[i].equals(val[i]));
297    }
298
299    /**
300     * @tests java.text.DateFormatSymbols#getShortMonths()
301     */
302    public void test_getShortMonths() {
303        // Test for method java.lang.String []
304        // java.text.DateFormatSymbols.getShortMonths()
305        String[] retVal = dfs.getShortMonths();
306        String[] val = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
307                "Aug", "Sep", "Oct", "Nov", "Dec"};
308        assertEquals("Returned wrong array: ", val.length, retVal.length);
309        for (int i = 0; i < val.length; i++)
310            assertTrue("Array values do not match", retVal[i].equals(val[i]));
311    }
312
313    /**
314     * @tests java.text.DateFormatSymbols#getShortWeekdays()
315     */
316    public void test_getShortWeekdays() {
317        // Test for method java.lang.String []
318        // java.text.DateFormatSymbols.getShortWeekdays()
319        String[] retVal = dfs.getShortWeekdays();
320        String[] val = { "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
321        if (retVal.length != val.length)
322            fail("Returned wrong array");
323        for (int i = 0; i < val.length; i++)
324            assertTrue("Array values do not match", retVal[i].equals(val[i]));
325    }
326
327    /**
328     * @tests java.text.DateFormatSymbols#getWeekdays()
329     */
330    public void test_getWeekdays() {
331        // Test for method java.lang.String []
332        // java.text.DateFormatSymbols.getWeekdays()
333        String[] retVal = dfs.getWeekdays();
334        String[] val = { "", "Sunday", "Monday", "Tuesday", "Wednesday",
335                "Thursday", "Friday", "Saturday" };
336        if (retVal.length != val.length)
337            fail("Returned wrong array");
338        for (int i = 0; i < val.length; i++)
339            assertTrue("Array values do not match", retVal[i].equals(val[i]));
340    }
341
342    /**
343     * @tests java.text.DateFormatSymbols#getZoneStrings()
344     */
345    public void test_getZoneStrings() {
346        // Test for method java.lang.String [][]
347        // java.text.DateFormatSymbols.getZoneStrings()
348        String[][] val = { { "XX", "XX", "XX", "XX", "XX" },
349                { "YY", "YY", "YY", "YY", "YY" } };
350        dfs.setZoneStrings(val);
351        String[][] retVal = dfs.getZoneStrings();
352        if (retVal.length != val.length)
353            fail("Returned wrong array");
354        for (int i = 0; i < val.length; i++)
355            assertTrue("Failed to set strings", Arrays
356                    .equals(retVal[i], val[i]));
357    }
358
359    /**
360     * @tests java.text.DateFormatSymbols#hashCode()
361     */
362    public void test_hashCode() {
363        // Test for method int java.text.DateFormatSymbols.hashCode()
364        int hc1 = dfs.hashCode();
365        int hc2 = dfs.hashCode();
366        assertTrue("hashCode() returned inconsistent number : " + hc1 + " - " + hc2, hc1 == hc2);
367
368        assertTrue("hashCode() returns different values for equal() objects",
369                            dfs.hashCode() == dfs.clone().hashCode());
370    }
371
372    /**
373     * @tests java.text.DateFormatSymbols#setAmPmStrings(java.lang.String[])
374     */
375    public void test_setAmPmStrings$Ljava_lang_String() {
376        // Test for method void
377        // java.text.DateFormatSymbols.setAmPmStrings(java.lang.String [])
378        String[] val = { "XX", "YY" };
379        dfs.setAmPmStrings(val);
380        String[] retVal = dfs.getAmPmStrings();
381        if (retVal.length != val.length)
382            fail("Returned wrong array");
383        for (int i = 0; i < val.length; i++)
384            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
385    }
386
387    /**
388     * @tests java.text.DateFormatSymbols#setEras(java.lang.String[])
389     */
390    public void test_setEras$Ljava_lang_String() {
391        // Test for method void
392        // java.text.DateFormatSymbols.setEras(java.lang.String [])
393        String[] val = { "XX", "YY" };
394        dfs.setEras(val);
395        String[] retVal = dfs.getEras();
396        if (retVal.length != val.length)
397            fail("Returned wrong array");
398        for (int i = 0; i < val.length; i++)
399            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
400    }
401
402    /**
403     * @tests java.text.DateFormatSymbols#setLocalPatternChars(java.lang.String)
404     */
405    public void test_setLocalPatternCharsLjava_lang_String() {
406        // Test for method void
407        // java.text.DateFormatSymbols.setLocalPatternChars(java.lang.String)
408        dfs.setLocalPatternChars("GyMZZkHmsSEHHFwWahKz");
409        String retVal = dfs.getLocalPatternChars();
410        String val = "GyMZZkHmsSEHHFwWahKz";
411        assertTrue("Returned incorrect pattern string", retVal.equals(val));
412
413        try {
414            // Regression for HARMONY-466
415            new DateFormatSymbols().setLocalPatternChars(null);
416            fail("NullPointerException expected");
417        } catch (NullPointerException e) {
418            // expected
419        }
420    }
421
422    /**
423     * @tests java.text.DateFormatSymbols#setMonths(java.lang.String[])
424     */
425    public void test_setMonths$Ljava_lang_String() {
426        // Test for method void
427        // java.text.DateFormatSymbols.setMonths(java.lang.String [])
428        String[] val = { "XX", "YY" };
429        dfs.setMonths(val);
430        String[] retVal = dfs.getMonths();
431        assertTrue("Return is identical", retVal != dfs.getMonths());
432        if (retVal.length != val.length)
433            fail("Returned wrong array");
434        for (int i = 0; i < val.length; i++)
435            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
436    }
437
438    /**
439     * @tests java.text.DateFormatSymbols#setShortMonths(java.lang.String[])
440     */
441    public void test_setShortMonths$Ljava_lang_String() {
442        // Test for method void
443        // java.text.DateFormatSymbols.setShortMonths(java.lang.String [])
444        String[] val = { "XX", "YY" };
445        dfs.setShortMonths(val);
446        String[] retVal = dfs.getShortMonths();
447        assertTrue("Return is identical", retVal != dfs.getShortMonths());
448        if (retVal.length != val.length)
449            fail("Returned wrong array");
450        for (int i = 0; i < val.length; i++)
451            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
452    }
453
454    /**
455     * @tests java.text.DateFormatSymbols#setShortWeekdays(java.lang.String[])
456     */
457    public void test_setShortWeekdays$Ljava_lang_String() {
458        // Test for method void
459        // java.text.DateFormatSymbols.setShortWeekdays(java.lang.String [])
460        String[] val = { "XX", "YY" };
461        dfs.setShortWeekdays(val);
462        String[] retVal = dfs.getShortWeekdays();
463        assertTrue("Return is identical", retVal != dfs.getShortWeekdays());
464        if (retVal.length != val.length)
465            fail("Returned wrong array");
466        for (int i = 0; i < val.length; i++)
467            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
468    }
469
470    /**
471     * @tests java.text.DateFormatSymbols#setWeekdays(java.lang.String[])
472     */
473    public void test_setWeekdays$Ljava_lang_String() {
474        // Test for method void
475        // java.text.DateFormatSymbols.setWeekdays(java.lang.String [])
476        String[] val = { "XX", "YY" };
477        dfs.setWeekdays(val);
478        String[] retVal = dfs.getWeekdays();
479        assertTrue("Return is identical", retVal != dfs.getWeekdays());
480        if (retVal.length != val.length)
481            fail("Returned wrong array");
482        for (int i = 0; i < val.length; i++)
483            assertTrue("Failed to set strings", retVal[i].equals(val[i]));
484    }
485
486    /**
487     * @tests java.text.DateFormatSymbols#setZoneStrings(java.lang.String[][])
488     */
489    public void test_setZoneStrings$$Ljava_lang_String() {
490        // Test for method void
491        // java.text.DateFormatSymbols.setZoneStrings(java.lang.String [][])
492        String[][] val = { { "XX", "XX", "XX", "XX", "XX" },
493                        { "YY", "YY", "YY", "YY", "YY" } };
494        dfs.setZoneStrings(val);
495        String[][] retVal = dfs.getZoneStrings();
496        assertTrue("get returns identical", retVal != dfs.getZoneStrings());
497        assertTrue("get[0] returns identical", retVal[0] != dfs
498                .getZoneStrings()[0]);
499        assertTrue("get returned identical", retVal != val);
500        assertEquals("Returned wrong array", val.length, retVal.length);
501        for (int i = 0; i < val.length; i++)
502            assertTrue("Failed to set strings: " + retVal[i], Arrays.equals(
503                    retVal[i], val[i]));
504    }
505
506    /**
507     * @tests java.text.DateFormatSymbols#setZoneStrings(java.lang.String[][])
508     *
509     * Tests setting zone strings to invalid values
510     * Regression for HARMONY-6337
511     */
512    public void test_setZoneStrings_invalid() {
513        // failing cases
514        String[][] val1 = null;
515        try {
516            dfs.setZoneStrings(val1);
517            fail("Attempt to set zone strings a null array should throw NullPointerException");
518        } catch (NullPointerException e) {
519            // expected
520        }
521
522        String[][] val2 = { { "XX", "XX" }, { "YY", "YY" } };
523        try {
524            dfs.setZoneStrings(val2);
525            fail("Attempt to set zone strings to a 2D array that contains one or more "
526                 + "rows of length less than 5 should throw IllegalArgumentException");
527        } catch (IllegalArgumentException e) {
528            // expected because each subarray has length < 5
529        }
530
531        String[][] val3 = { { "a", "b", "c", "d", "e" },
532                { "a", "b", "c", "d", "e" },
533                { "a", "b", "c", "d" },
534                { "a", "b", "c", "d", "e" } };
535        try {
536            dfs.setZoneStrings(val3);
537            fail("Attempt to set zone strings to a 2D array that contains one or more "
538                 + "rows of length less than 5 should throw IllegalArgumentException");
539        } catch (IllegalArgumentException e) {
540            // expected because each subarray has length < 5
541        }
542    }
543
544    /**
545     * Sets up the fixture, for example, open a network connection. This method
546     * is called before a test is executed.
547     */
548    protected void setUp() {
549        dfs = new DateFormatSymbols(new Locale("en", "us"));
550    }
551
552    /**
553     * Tears down the fixture, for example, close a network connection. This
554     * method is called after a test is executed.
555     */
556    protected void tearDown() {
557    }
558
559    // Test serialization mechanism of DateFormatSymbols
560    public void test_serialization() throws Exception {
561        DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRANCE);
562        String[][] zoneStrings = symbols.getZoneStrings();
563        assertNotNull(zoneStrings);
564
565        // serialize
566        ByteArrayOutputStream byteOStream = new ByteArrayOutputStream();
567        ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream);
568        objectOStream.writeObject(symbols);
569
570        // and deserialize
571        ObjectInputStream objectIStream = new ObjectInputStream(
572                new ByteArrayInputStream(byteOStream.toByteArray()));
573        DateFormatSymbols symbolsD = (DateFormatSymbols) objectIStream
574                .readObject();
575
576        // The associated currency will not persist
577        String[][] zoneStringsD = symbolsD.getZoneStrings();
578        assertNotNull(zoneStringsD);
579        assertEquals(symbols, symbolsD);
580    }
581}
582