java_awt_CardLayout.java revision 16177:89ef4b822745
1/*
2 * Copyright (c) 2013, 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 * @bug 8007458
27 * @summary Tests CardLayout encoding
28 * @modules java.desktop/java.awt:open
29 * @author Sergey Malenkov
30 */
31
32import java.awt.CardLayout;
33import java.lang.reflect.Field;
34import java.util.Vector;
35import javax.swing.JLabel;
36
37public final class java_awt_CardLayout extends AbstractTest<CardLayout> {
38    private static final Field VECTOR = getField("java.awt.CardLayout.vector");
39    private static final Field NAME = getField("java.awt.CardLayout$Card.name");
40    private static final Field COMP = getField("java.awt.CardLayout$Card.comp");
41
42    public static void main(String[] args) throws Exception {
43        new java_awt_CardLayout().test(true);
44    }
45
46    @Override
47    protected CardLayout getObject() {
48        CardLayout layout = new CardLayout();
49        layout.addLayoutComponent(new JLabel("a"), "a");
50        layout.addLayoutComponent(new JLabel("b"), "b");
51        layout.addLayoutComponent(new JLabel("c"), "c");
52        return layout;
53    }
54
55    @Override
56    protected CardLayout getAnotherObject() {
57        CardLayout layout = new CardLayout();
58        layout.addLayoutComponent(new JLabel("a"), "a");
59        layout.addLayoutComponent(new JLabel("b"), "b");
60        layout.addLayoutComponent(new JLabel("c"), "c");
61        layout.addLayoutComponent(new JLabel("d"), "d");
62        return layout;
63    }
64
65    @Override
66    protected void validate(CardLayout before, CardLayout after) {
67        super.validate(before, after);
68        try {
69            Vector a = (Vector) VECTOR.get(after);
70            Vector b = (Vector) VECTOR.get(before);
71            int size = a.size();
72            if (size != b.size()) {
73                throw new Error("different content");
74            }
75            for (int i = 0; i < size; i++) {
76                super.validator.validate(NAME.get(a.get(i)), NAME.get(b.get(i)));
77                super.validator.validate(COMP.get(a.get(i)), COMP.get(b.get(i)));
78            }
79        }
80        catch (Exception exception) {
81            throw new Error(exception);
82        }
83    }
84}
85