1 15 package org.apache.tapestry.listener; 16 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.test.HiveMindTestCase; 23 import org.apache.tapestry.IActionListener; 24 import org.apache.tapestry.IRequestCycle; 25 26 32 public class TestListenerMap extends HiveMindTestCase 33 { 34 public void testGetListener() 35 { 36 Object target = new Object (); 37 IRequestCycle cycle = newCycle(); 38 ListenerMethodInvoker invoker = newInvoker(); 39 Map map = newMap("method", invoker); 40 41 invoker.invokeListenerMethod(target, cycle); 42 43 replayControls(); 44 45 ListenerMap lm = new ListenerMapImpl(target, map); 46 47 IActionListener l1 = lm.getListener("method"); 48 49 l1.actionTriggered(null, cycle); 50 51 verifyControls(); 52 53 IActionListener l2 = lm.getListener("method"); 54 55 assertSame(l1, l2); 56 } 57 58 public void testGetListenerNames() 59 { 60 Object target = new Object (); 61 ListenerMethodInvoker invoker = newInvoker(); 62 Map map = newMap("method", invoker); 63 64 replayControls(); 65 66 ListenerMap lm = new ListenerMapImpl(target, map); 67 68 70 assertEquals(new ArrayList (map.keySet()), new ArrayList (lm.getListenerNames())); 71 72 verifyControls(); 73 74 try 75 { 76 lm.getListenerNames().clear(); 77 unreachable(); 78 } 79 catch (UnsupportedOperationException ex) 80 { 81 } 83 } 84 85 public void testCanProvideListener() 86 { 87 Object target = new Object (); 88 ListenerMethodInvoker invoker = newInvoker(); 89 Map map = newMap("method", invoker); 90 91 replayControls(); 92 93 ListenerMap lm = new ListenerMapImpl(target, map); 94 95 assertEquals(true, lm.canProvideListener("method")); 96 assertEquals(false, lm.canProvideListener("foobar")); 97 98 verifyControls(); 99 } 100 101 public void testMissingListener() 102 { 103 Object target = "*TARGET*"; 104 ListenerMethodInvoker invoker = newInvoker(); 105 Map map = newMap("method", invoker); 106 107 replayControls(); 108 109 ListenerMap lm = new ListenerMapImpl(target, map); 110 111 try 112 { 113 lm.getListener("foobar"); 114 unreachable(); 115 } 116 catch (ApplicationRuntimeException ex) 117 { 118 assertEquals("Object *TARGET* does not implement a listener method named 'foobar'.", ex 119 .getMessage()); 120 assertSame(target, ex.getComponent()); 121 } 122 123 verifyControls(); 124 } 125 126 private Map newMap(Object key, Object value) 127 { 128 Map result = new HashMap (); 129 130 result.put(key, value); 131 132 return result; 133 } 134 135 private ListenerMethodInvoker newInvoker() 136 { 137 return (ListenerMethodInvoker) newMock(ListenerMethodInvoker.class); 138 } 139 140 private IRequestCycle newCycle() 141 { 142 return (IRequestCycle) newMock(IRequestCycle.class); 143 } 144 } | Popular Tags |