1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @summary Tests <new> element
27 * @author Sergey Malenkov
28 */
29
30import java.beans.XMLDecoder;
31import java.util.ArrayList;
32import java.util.List;
33
34public final class TestNew extends AbstractTest {
35    public static final String XML
36            = "<java>\n"
37            + " <new class=\"TestNew\"/>\n"
38            + " <new class=\"TestNew\">\n"
39            + "  <null/>\n"
40            + " </new>\n"
41            + " <new class=\"TestNew\">\n"
42            + "  <string>single</string>\n"
43            + " </new>\n"
44            + " <new class=\"TestNew\">\n"
45            + "  <string>first</string>\n"
46            + "  <string>second</string>\n"
47            + "  <string>third</string>\n"
48            + " </new>\n"
49            + "</java>";
50
51    public static void main(String[] args) {
52        new TestNew().test(true);
53    }
54
55    private List<String> list;
56
57    public TestNew(String...messages) {
58        if (messages != null) {
59            this.list = new ArrayList<String>();
60            for (String message : messages) {
61                this.list.add(message);
62            }
63        }
64    }
65
66    @Override
67    public boolean equals(Object object) {
68        if (object instanceof TestNew) {
69            TestNew test = (TestNew) object;
70            return (test.list == null)
71                    ? this.list == null
72                    : test.list.equals(this.list);
73        }
74        return false;
75    }
76
77    @Override
78    protected void validate(XMLDecoder decoder) {
79        validate(decoder.readObject());
80        validate(decoder.readObject(), null);
81        validate(decoder.readObject(), "single");
82        validate(decoder.readObject(), "first", "second", "third");
83    }
84
85    private static void validate(Object object, String...messages) {
86        if (!object.equals(new TestNew(messages))) {
87            throw new Error("expected object");
88        }
89    }
90}
91