J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4631471 |
| 27 | * @summary Tests DefaultTreeModel encoding |
| 28 | * @author Sergey Malenkov, Mark Davidson |
| 29 | */ |
| 30 | |
| 31 | import java.beans.XMLEncoder; |
| 32 | import javax.swing.JTree; |
| 33 | import javax.swing.tree.DefaultMutableTreeNode; |
| 34 | import javax.swing.tree.DefaultTreeModel; |
| 35 | import javax.swing.tree.TreeModel; |
| 36 | import javax.swing.tree.TreeNode; |
| 37 | |
| 38 | public abstract class Test4631471 extends AbstractTest { |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | // the DefaultMutableTreeNode will archive correctly |
| 41 | new Test4631471() { |
| 42 | protected Object getObject() { |
| 43 | return getRoot(); |
| 44 | } |
| 45 | }.test(false); |
| 46 | |
| 47 | // the DefaultTreeModel will also archive correctly |
| 48 | new Test4631471() { |
| 49 | protected Object getObject() { |
| 50 | return getModel(); |
| 51 | } |
| 52 | }.test(false); |
| 53 | |
| 54 | // create a new model from the root node |
| 55 | // this simulates the the MetaData ctor: |
| 56 | // registerConstructor("javax.swing.tree.DefaultTreeModel", new String[]{"root"}); |
| 57 | new Test4631471() { |
| 58 | protected Object getObject() { |
| 59 | return new DefaultTreeModel((TreeNode) getModel().getRoot()); |
| 60 | } |
| 61 | }.test(false); |
| 62 | |
| 63 | // the JTree will archive correctly too |
| 64 | new Test4631471() { |
| 65 | protected Object getObject() { |
| 66 | return getTree(); |
| 67 | } |
| 68 | }.test(false); |
| 69 | } |
| 70 | |
| 71 | protected final void validate(Object before, Object after) { |
| 72 | // do not any validation |
| 73 | } |
| 74 | |
| 75 | protected final void initialize(XMLEncoder encoder) { |
| 76 | encoder.setExceptionListener(this); |
| 77 | } |
| 78 | |
| 79 | public static TreeNode getRoot() { |
| 80 | DefaultMutableTreeNode node = new DefaultMutableTreeNode("root"); |
| 81 | DefaultMutableTreeNode first = new DefaultMutableTreeNode("first"); |
| 82 | DefaultMutableTreeNode second = new DefaultMutableTreeNode("second"); |
| 83 | DefaultMutableTreeNode third = new DefaultMutableTreeNode("third"); |
| 84 | |
| 85 | first.add(new DefaultMutableTreeNode("1.1")); |
| 86 | first.add(new DefaultMutableTreeNode("1.2")); |
| 87 | first.add(new DefaultMutableTreeNode("1.3")); |
| 88 | |
| 89 | second.add(new DefaultMutableTreeNode("2.1")); |
| 90 | second.add(new DefaultMutableTreeNode("2.2")); |
| 91 | second.add(new DefaultMutableTreeNode("2.3")); |
| 92 | |
| 93 | third.add(new DefaultMutableTreeNode("3.1")); |
| 94 | third.add(new DefaultMutableTreeNode("3.2")); |
| 95 | third.add(new DefaultMutableTreeNode("3.3")); |
| 96 | |
| 97 | node.add(first); |
| 98 | node.add(second); |
| 99 | node.add(third); |
| 100 | |
| 101 | return node; |
| 102 | } |
| 103 | |
| 104 | public static JTree getTree() { |
| 105 | return new JTree(getRoot()); |
| 106 | } |
| 107 | |
| 108 | public static TreeModel getModel() { |
| 109 | return getTree().getModel(); |
| 110 | } |
| 111 | } |