001    // Copyright Aug 9, 2006 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    package org.apache.tapestry.listener;
015    
016    import org.apache.tapestry.BaseComponentTestCase;
017    import org.apache.tapestry.IRequestCycle;
018    import org.apache.tapestry.event.BrowserEvent;
019    import static org.easymock.EasyMock.expect;
020    import org.testng.annotations.Test;
021    
022    
023    /**
024     * Tests functionality of {@link ListenerMethodInvokerImpl}.
025     */
026    @Test(sequential=true)
027    public class ListenerMethodInvokerTest extends BaseComponentTestCase
028    {
029    
030        public void test_BrowserEvent_Parameter()
031        {
032            IRequestCycle cycle = newCycle();
033            
034            ListenerMethodHolder target = new ListenerMethodHolder();
035            
036            ListenerMethodInvoker invoker = 
037                new ListenerMethodInvokerImpl("bangbangClicked", target.getClass().getMethods());
038            
039            BrowserEvent event = new BrowserEvent("onClick", null);
040            Object[] parms = { 12, event };
041            
042            expect(cycle.getListenerParameters()).andReturn(parms);
043            
044            replay();
045            
046            invoker.invokeListenerMethod(target, cycle);
047            
048            verify();
049        }
050        
051        public void test_Type_Conversion_Parameter()
052        {
053            IRequestCycle cycle = newCycle();
054            
055            ListenerMethodHolder target = new ListenerMethodHolder();
056            
057            ListenerMethodInvoker invoker = 
058                new ListenerMethodInvokerImpl("bangbangClicked", target.getClass().getDeclaredMethods());
059    
060            BrowserEvent event = new BrowserEvent("onClick", null);
061            Object[] parms = { new Integer(12), event };
062            
063            expect(cycle.getListenerParameters()).andReturn(parms);
064            
065            replay();
066            
067            invoker.invokeListenerMethod(target, cycle);
068            
069            verify();
070        }
071    
072        public void test_Null_Parameter()
073        {
074            IRequestCycle cycle = newCycle();
075    
076            ListenerMethodHolder target = new ListenerMethodHolder();
077    
078            ListenerMethodInvoker invoker =
079                new ListenerMethodInvokerImpl("stringArg", target.getClass().getDeclaredMethods());
080    
081            Object[] parms = { null };
082    
083            expect(cycle.getListenerParameters()).andReturn(parms);
084    
085            replay();
086    
087            invoker.invokeListenerMethod(target, cycle);
088    
089            verify();
090    
091            assertEquals(target._stringArgCount, 1);
092        }
093    
094        public void test_No_Parameters_With_Method_Parameters()
095        {
096            IRequestCycle cycle = newCycle();
097    
098            ListenerMethodHolder target = new ListenerMethodHolder();
099    
100            ListenerMethodInvoker invoker =
101                new ListenerMethodInvokerImpl("stringArg", target.getClass().getDeclaredMethods());
102    
103            Object[] parms = new Object[0];
104    
105            expect(cycle.getListenerParameters()).andReturn(parms);
106    
107            replay();
108    
109            invoker.invokeListenerMethod(target, cycle);
110    
111            verify();
112    
113            assertEquals(target._stringArgCount, 1);
114        }
115    
116        public void test_To_String()
117        {
118            ListenerMethodInvoker invoker =
119                new ListenerMethodInvokerImpl("bangbangClicked", ListenerMethodHolder.class.getDeclaredMethods());
120    
121            
122            assertEquals(invoker.toString(), "ListenerMethodInvokerImpl[_name='bangbangClicked']");
123        }
124    }