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
17package libcore.java.text;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.ObjectInputStream;
22import java.io.ObjectOutputStream;
23import java.text.DecimalFormatSymbols;
24import java.util.Locale;
25
26public class DecimalFormatSymbolsTest extends junit.framework.TestCase {
27    private void checkLocaleIsEquivalentToRoot(Locale locale) {
28        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
29        assertEquals(DecimalFormatSymbols.getInstance(Locale.ROOT), dfs);
30    }
31    public void test_getInstance_unknown_or_invalid_locale() throws Exception {
32        // TODO: we fail these tests because ROOT has "INF" for infinity but 'dfs' has "\u221e".
33        // On the RI, ROOT has "\u221e" too, but DecimalFormatSymbols.equals appears to be broken;
34        // it returns false for objects that -- if you compare their externally visible state --
35        // are equal. It could be that they're accidentally checking the Locale.
36        checkLocaleIsEquivalentToRoot(new Locale("xx", "XX"));
37        checkLocaleIsEquivalentToRoot(new Locale("not exist language", "not exist country"));
38    }
39
40    // http://code.google.com/p/android/issues/detail?id=14495
41    public void testSerialization() throws Exception {
42        DecimalFormatSymbols originalDfs = DecimalFormatSymbols.getInstance(Locale.GERMANY);
43
44        // Serialize...
45        ByteArrayOutputStream out = new ByteArrayOutputStream();
46        new ObjectOutputStream(out).writeObject(originalDfs);
47        byte[] bytes = out.toByteArray();
48
49        // Deserialize...
50        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
51        DecimalFormatSymbols deserializedDfs = (DecimalFormatSymbols) in.readObject();
52        assertEquals(-1, in.read());
53
54        // The two objects should claim to be equal.
55        assertEquals(originalDfs, deserializedDfs);
56    }
57}
58