1 7 8 package org.jdesktop.swing.actions; 9 10 import junit.framework.TestCase; 11 12 17 public class TargetManagerTest extends TestCase { 18 19 protected void tearDown() { 20 TargetManager manager = TargetManager.getInstance(); 21 manager.reset(); 22 } 23 24 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 40 public void testAddRemoveTargets() { 41 FooTarget foo = new FooTarget(); 42 BarTarget bar = new BarTarget(); 43 44 TargetManager manager = TargetManager.getInstance(); 45 46 manager.addTarget(foo); 48 manager.addTarget(bar); 49 50 Targetable[] targets = manager.getTargets(); 51 assertNotNull(targets); 52 assertTrue(targets.length == 2); 53 assertTrue(targets[0] == foo); 55 assertTrue(targets[1] == bar); 56 57 manager.removeTarget(foo); 59 manager.removeTarget(bar); 60 61 targets = manager.getTargets(); 62 assertNotNull(targets); 63 assertTrue(targets.length == 0); 64 65 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 79 public void testDoCommand() { 80 FooTarget foo = new FooTarget(); 81 BarTarget bar = new BarTarget(); 82 TargetManager manager = TargetManager.getInstance(); 83 84 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 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 106 private class FooTarget implements Targetable { 107 108 public String FOO_ACTION = "foo"; 109 110 public boolean doCommand(Object command, Object value) { 111 return hasCommand(command); 112 } 113 114 public boolean hasCommand(Object command) { 115 if (command.equals(FOO_ACTION)) { 116 return true; 117 } 118 return false; 119 } 120 121 public Object [] getCommands() { 122 return new Object [] { FOO_ACTION }; 123 } 124 } 125 126 private class BarTarget implements Targetable { 127 public String BAR_ACTION = "bar"; 128 129 public boolean doCommand(Object command, Object value) { 130 return hasCommand(command); 131 } 132 133 public boolean hasCommand(Object command) { 134 if (command.equals(BAR_ACTION)) { 135 return true; 136 } 137 return false; 138 } 139 140 public Object [] getCommands() { 141 return new Object [] { BAR_ACTION }; 142 } 143 } 144 } 145 | Popular Tags |