KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > TargetManagerTest


1 /*
2  * $Id: TargetManagerTest.java,v 1.1 2004/09/30 00:05:52 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.swing.actions;
9
10 import junit.framework.TestCase;
11
12 /**
13  * Tests registration and command execution of the target manager.
14  *
15  * @author Mark Davidson
16  */

17 public class TargetManagerTest extends TestCase {
18
19     protected void tearDown() {
20         TargetManager manager = TargetManager.getInstance();
21         manager.reset();
22     }
23
24     /**
25      * By default, there are no targets.
26      */

27     public void testGetTargets() {
28         TargetManager manager = TargetManager.getInstance();
29
30         assertNull(manager.getTarget());
31         Targetable[] targets = manager.getTargets();
32
33         assertNotNull(targets);
34         assertTrue(targets.length == 0);
35     }
36
37     /**
38      * Test Target registration methods.
39      */

40     public void testAddRemoveTargets() {
41         FooTarget foo = new FooTarget();
42         BarTarget bar = new BarTarget();
43
44         TargetManager manager = TargetManager.getInstance();
45
46         // append targets
47
manager.addTarget(foo);
48         manager.addTarget(bar);
49
50         Targetable[] targets = manager.getTargets();
51         assertNotNull(targets);
52         assertTrue(targets.length == 2);
53         // by default, targets are appended to the list.
54
assertTrue(targets[0] == foo);
55         assertTrue(targets[1] == bar);
56
57         // Test removal
58
manager.removeTarget(foo);
59         manager.removeTarget(bar);
60
61         targets = manager.getTargets();
62         assertNotNull(targets);
63         assertTrue(targets.length == 0);
64
65         // add targets, prepend bar
66
manager.addTarget(foo);
67         manager.addTarget(bar, true);
68
69         targets = manager.getTargets();
70         assertNotNull(targets);
71         assertTrue(targets.length == 2);
72         assertTrue(targets[0] == bar);
73         assertTrue(targets[1] == foo);
74     }
75
76     /**
77      * Tests doCommand for both explicit targets and the targets in the list.
78      */

79     public void testDoCommand() {
80         FooTarget foo = new FooTarget();
81         BarTarget bar = new BarTarget();
82         TargetManager manager = TargetManager.getInstance();
83
84         // set explicit targets
85
manager.setTarget(foo);
86         assertTrue(manager.doCommand("foo", null));
87         assertFalse(manager.doCommand("bar", null));
88
89         manager.setTarget(bar);
90         assertTrue(manager.doCommand("bar", null));
91         assertFalse(manager.doCommand("foo", null));
92
93         // Add both of the targets all commands are valid
94
manager.setTarget(null);
95         manager.addTarget(foo);
96         manager.addTarget(bar);
97
98         assertTrue(manager.doCommand("bar", null));
99         assertTrue(manager.doCommand("foo", null));
100     }
101
102     //
103
// Some examples of Targets
104
//
105

106     private class FooTarget implements Targetable {
107
108         public String JavaDoc FOO_ACTION = "foo";
109
110         public boolean doCommand(Object JavaDoc command, Object JavaDoc value) {
111             return hasCommand(command);
112         }
113
114         public boolean hasCommand(Object JavaDoc command) {
115             if (command.equals(FOO_ACTION)) {
116                 return true;
117             }
118             return false;
119         }
120
121         public Object JavaDoc[] getCommands() {
122             return new Object JavaDoc[] { FOO_ACTION };
123         }
124     }
125
126     private class BarTarget implements Targetable {
127         public String JavaDoc BAR_ACTION = "bar";
128
129         public boolean doCommand(Object JavaDoc command, Object JavaDoc value) {
130             return hasCommand(command);
131         }
132
133         public boolean hasCommand(Object JavaDoc command) {
134             if (command.equals(BAR_ACTION)) {
135                 return true;
136             }
137             return false;
138         }
139
140         public Object JavaDoc[] getCommands() {
141             return new Object JavaDoc[] { BAR_ACTION };
142         }
143     }
144 }
145
Popular Tags