001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.listener; 016 017 import java.util.ArrayList; 018 import java.util.HashMap; 019 import java.util.Map; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.tapestry.BaseComponentTestCase; 023 import org.apache.tapestry.IActionListener; 024 import org.apache.tapestry.IComponent; 025 import org.apache.tapestry.IRequestCycle; 026 import static org.easymock.EasyMock.expect; 027 import org.testng.annotations.Test; 028 029 /** 030 * Tests for {@link org.apache.tapestry.listener.ListenerMapImpl}. 031 * 032 * @author Howard M. Lewis Ship 033 * @since 4.0 034 */ 035 @Test 036 public class TestListenerMap extends BaseComponentTestCase 037 { 038 public void test_Get_Listener() 039 { 040 Object target = new Object(); 041 IRequestCycle cycle = newCycle(); 042 ListenerMethodInvoker invoker = newInvoker(); 043 Map map = newMap("method", invoker); 044 045 invoker.invokeListenerMethod(target, cycle); 046 047 replay(); 048 049 ListenerMap lm = new ListenerMapImpl(target, map); 050 051 IActionListener l1 = lm.getListener("method"); 052 053 l1.actionTriggered(null, cycle); 054 055 verify(); 056 057 IActionListener l2 = lm.getListener("method"); 058 059 assertSame(l1, l2); 060 } 061 062 public void test_GetListener_Names() 063 { 064 Object target = new Object(); 065 ListenerMethodInvoker invoker = newInvoker(); 066 Map map = newMap("method", invoker); 067 068 replay(); 069 070 ListenerMap lm = new ListenerMapImpl(target, map); 071 072 // Copy both collections into ArrayLists for comparison purposes. 073 074 assertEquals(new ArrayList(map.keySet()), new ArrayList(lm.getListenerNames())); 075 076 verify(); 077 078 try 079 { 080 lm.getListenerNames().clear(); 081 unreachable(); 082 } 083 catch (UnsupportedOperationException ex) 084 { 085 // Ignore. Expected result. 086 } 087 } 088 089 public void test_Can_Provide_Listener() 090 { 091 Object target = new Object(); 092 ListenerMethodInvoker invoker = newInvoker(); 093 Map map = newMap("method", invoker); 094 095 replay(); 096 097 ListenerMap lm = new ListenerMapImpl(target, map); 098 099 assertEquals(true, lm.canProvideListener("method")); 100 assertEquals(false, lm.canProvideListener("foobar")); 101 102 verify(); 103 } 104 105 public void test_Missing_Listener() 106 { 107 Object target = "*TARGET*"; 108 ListenerMethodInvoker invoker = newInvoker(); 109 Map map = newMap("method", invoker); 110 111 replay(); 112 113 ListenerMap lm = new ListenerMapImpl(target, map); 114 115 try 116 { 117 lm.getListener("foobar"); 118 unreachable(); 119 } 120 catch (ApplicationRuntimeException ex) 121 { 122 assertEquals("Object *TARGET* does not implement a listener method named 'foobar'.", ex 123 .getMessage()); 124 assertSame(target, ex.getComponent()); 125 } 126 127 verify(); 128 } 129 130 public void test_Get_Implicit_Listener() 131 { 132 Object target = new Object(); 133 IRequestCycle cycle = newCycle(); 134 ListenerMethodInvoker invoker = newInvoker(); 135 IComponent component = newMock(IComponent.class); 136 expect(component.getId()).andReturn("action").times(2); 137 Map map = newMap("doAction", invoker); 138 139 invoker.invokeListenerMethod(target, cycle); 140 141 replay(); 142 143 ListenerMap lm = new ListenerMapImpl(target, map); 144 145 IActionListener l1 = lm.getImplicitListener(component); 146 147 l1.actionTriggered(null, cycle); 148 149 IActionListener l2 = lm.getImplicitListener(component); 150 151 verify(); 152 153 assertSame(l1, l2); 154 } 155 156 public void test_Missing_Implicit_Listener() 157 { 158 ListenerMethodInvoker invoker = newInvoker(); 159 IComponent component = newMock(IComponent.class); 160 expect(component.getLocation()).andReturn(null); 161 expect(component.getId()).andReturn("action"); 162 Map map = newMap("method", invoker); 163 164 replay(); 165 166 ListenerMap lm = new ListenerMapImpl("test", map); 167 168 try 169 { 170 lm.getImplicitListener(component); 171 unreachable(); 172 } 173 catch (ApplicationRuntimeException ex) 174 { 175 assertEquals("No implicit listener method named 'doAction' found in " + component, ex 176 .getMessage()); 177 assertSame(component, ex.getComponent()); 178 } 179 180 verify(); 181 } 182 183 private Map newMap(Object key, Object value) 184 { 185 Map result = new HashMap(); 186 187 result.put(key, value); 188 189 return result; 190 } 191 192 private ListenerMethodInvoker newInvoker() 193 { 194 return newMock(ListenerMethodInvoker.class); 195 } 196 }