KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > ActionsTest


1 /*
2  * $Id: ActionsTest.java,v 1.2 2004/09/08 01:37:45 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup;
9
10 import java.awt.Component JavaDoc;
11
12 import java.util.logging.Level JavaDoc;
13
14 import javax.swing.*;
15
16 import java.net.URL JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18
19 import junit.framework.TestCase;
20
21 import org.jdesktop.jdnc.markup.ElementTypes;
22
23 import net.openmarkup.ObjectRealizer;
24 import net.openmarkup.Scribe;
25
26 import org.jdesktop.swing.actions.AbstractActionExt;
27 import org.jdesktop.swing.JXRootPane;
28
29 public class ActionsTest extends TestCase {
30
31     private ObjectRealizer realizer;
32
33     protected void setUp() {
34         realizer = RealizerUnitTest.createObjectRealizer();
35         realizer.add(ElementTypes.get());
36     }
37
38     protected void tearDown() {
39         realizer = null;
40     }
41
42     /**
43      * action1.xml is an action with values for all possible attributes.
44      */

45     public void testActionAttributes() throws Exception JavaDoc {
46         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/action1.xml");
47         assertNotNull(url);
48
49         Object JavaDoc obj = realizer.getObject(url);
50         assertNotNull(obj);
51         assertTrue(obj instanceof Action);
52
53         Action action = (Action)obj;
54
55         assertEquals("find", action.getValue(Action.ACTION_COMMAND_KEY));
56         assertEquals("Find", action.getValue(Action.NAME));
57         assertEquals("Find an item", action.getValue(Action.SHORT_DESCRIPTION));
58         assertEquals("Find an item", action.getValue(Action.LONG_DESCRIPTION));
59         assertTrue(((AbstractActionExt)action).isStateAction());
60         assertEquals("find-group", action.getValue(AbstractActionExt.GROUP));
61         assertEquals(new Integer JavaDoc('F'), action.getValue(Action.MNEMONIC_KEY));
62
63         assertNotNull(action.getValue(Action.SMALL_ICON));
64         assertTrue(action.getValue(Action.SMALL_ICON) instanceof Icon);
65         assertTrue(action.getValue(AbstractActionExt.LARGE_ICON) instanceof Icon);
66         assertTrue(action.getValue(Action.ACCELERATOR_KEY) instanceof KeyStroke);
67     }
68
69     /**
70      * action0.xml tests to ensure that the action icons are found as a relative
71      * url path to the document - like "images/Foo.gif"
72      */

73     public void testActionIcons() throws Exception JavaDoc {
74         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/action0.xml");
75         assertNotNull(url);
76
77         Object JavaDoc obj = realizer.getObject(url);
78         assertNotNull(obj);
79         assertTrue(obj instanceof Action);
80
81         Action action = (Action)obj;
82
83         Icon icon = (Icon)action.getValue(Action.SMALL_ICON);
84         assertNotNull("Icon should not be null: " + icon, icon);
85         assertTrue(icon instanceof Icon);
86
87         icon = (Icon)action.getValue(AbstractActionExt.LARGE_ICON);
88         assertNotNull("Icon should not be null: " + icon, icon);
89         assertTrue(icon instanceof Icon);
90
91     }
92
93     /**
94      * action6.xml: Both of the icons are bogus.
95      */

96     public void testActionIcons2() throws Exception JavaDoc {
97         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/action6.xml");
98         assertNotNull(url);
99
100         Object JavaDoc obj = realizer.getObject(url);
101         assertNotNull(obj);
102         assertTrue(obj instanceof Action);
103
104         Action action = (Action)obj;
105
106         Icon icon = (Icon)action.getValue(Action.SMALL_ICON);
107         assertNull("Icon should be null: " + icon, icon);
108         icon = (Icon)action.getValue(AbstractActionExt.LARGE_ICON);
109         assertNull("Icon should be null: " + icon, icon);
110     }
111
112     /**
113      * Simple test to ensure that a RootPane is created with a toolbar
114      * and 3 buttons. The last button should be a toggle button.
115      */

116     public void testToolBarCreate() throws Exception JavaDoc {
117         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/action2.xml");
118         assertNotNull(url);
119
120         Object JavaDoc obj = realizer.getObject(url);
121         assertNotNull(obj);
122         assertTrue(obj.getClass().toString() + " is not a JRootPane",
123                    obj instanceof JRootPane);
124
125         JXRootPane root = (JXRootPane)obj;
126         JToolBar toolbar = root.getToolBar();
127
128         assertTrue(toolbar.getComponentCount() == 4);
129
130         Component JavaDoc comp = toolbar.getComponent(3);
131         assertNotNull(comp);
132         assertTrue(comp.getClass().toString() + " is not a JToggleButton",
133                    comp instanceof JToggleButton);
134     }
135
136     /**
137      * Test to ensure that a rootpane with a menu bar has been created.
138      * This also tests that entity includes are working correctly.
139      */

140     public void testMenuBarCreate() throws Exception JavaDoc {
141         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/action4.xml");
142         assertNotNull(url);
143
144         Object JavaDoc obj = realizer.getObject(url);
145         assertNotNull(obj);
146         assertTrue(obj.getClass().toString() + " is not a JRootPane",
147                    obj instanceof JRootPane);
148
149         JXRootPane root = (JXRootPane)obj;
150         JMenuBar menubar = root.getJMenuBar();
151         assertNotNull(menubar);
152         assertTrue(menubar.getComponentCount() == 3);
153
154     }
155 }
156
Popular Tags