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 libcore.java.text;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.IOException;
22import java.io.InvalidObjectException;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
25import java.text.MessageFormat;
26import junit.framework.TestCase;
27
28public class OldMessageFormatFieldTest extends TestCase {
29
30    public void test_ConstructorLjava_lang_String() {
31        // protected constructor
32        String name = "new Message format";
33        MyMessageFormat field = new MyMessageFormat(name);
34        assertEquals("field has wrong name", name, field.getName());
35
36        field = new MyMessageFormat(null);
37        assertEquals("field has wrong name", null, field.getName());
38    }
39
40    public void test_readResolve() {
41        // test for method java.lang.Object readResolve()
42
43        // see serialization stress tests:
44        // implemented in
45        // SerializationStressTest4.test_writeObject_MessageFormat_Field()
46        ObjectOutputStream out = null;
47        ObjectInputStream in = null;
48        try {
49            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
50            out = new ObjectOutputStream(bytes);
51
52            MessageFormat.Field mfield, mfield2;
53            MyMessageFormat field;
54
55            mfield = MessageFormat.Field.ARGUMENT;
56
57            field = new MyMessageFormat(null);
58
59            out.writeObject(mfield);
60            out.writeObject(field);
61
62            in = new ObjectInputStream(new ByteArrayInputStream(bytes
63                    .toByteArray()));
64
65            try {
66                mfield2 = (MessageFormat.Field) in.readObject();
67                assertSame("resolved incorrectly", mfield, mfield2);
68            } catch (IllegalArgumentException e) {
69                fail("Unexpected IllegalArgumentException: " + e);
70            }
71
72            try {
73                in.readObject();
74                fail("Expected InvalidObjectException for subclass instance with null name");
75            } catch (InvalidObjectException e) {
76            }
77
78        } catch (IOException e) {
79            fail("unexpected IOException" + e);
80        } catch (ClassNotFoundException e) {
81            fail("unexpected ClassNotFoundException" + e);
82        } finally {
83            try {
84                if (out != null)
85                    out.close();
86                if (in != null)
87                    in.close();
88            } catch (IOException e) {
89            }
90        }
91    }
92
93    static class MyMessageFormat extends MessageFormat.Field {
94        static final long serialVersionUID = 1L;
95
96        protected MyMessageFormat(String attr) {
97            super(attr);
98        }
99
100        protected String getName() {
101            return super.getName();
102        }
103    }
104}
105