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.callback;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.apache.tapestry.IComponent;
023    import org.apache.tapestry.IDirect;
024    import org.apache.tapestry.IPage;
025    import org.apache.tapestry.IRequestCycle;
026    import org.testng.annotations.Test;
027    
028    /**
029     * @author Howard M. Lewis Ship
030     */
031    @Test
032    public class TestDirectCallback extends BaseComponentTestCase
033    {
034        public void testNoParams()
035        {
036            IPage page = newMock(IPage.class);
037            IDirect component = newMock(IDirect.class);
038    
039            expect(component.getPage()).andReturn(page);
040    
041            expect(page.getPageName()).andReturn("Fred");
042    
043            expect(component.getIdPath()).andReturn("foo.bar");
044    
045            replay();
046    
047            DirectCallback callback = new DirectCallback(component, null);
048    
049            assertEquals("DirectCallback[Fred/foo.bar]", callback.toString());
050    
051            verify();
052    
053            IRequestCycle cycle = newCycleGetPage("Fred", page);
054    
055            expect(page.getNestedComponent("foo.bar")).andReturn(component);
056    
057            cycle.setListenerParameters(null);
058    
059            component.trigger(cycle);
060    
061            replay();
062    
063            callback.performCallback(cycle);
064    
065            verify();
066        }
067    
068        public void testWithParams()
069        {
070            Object[] params = new Object[]
071            { "p1", "p2" };
072    
073            IPage page = newMock(IPage.class);
074            IDirect component = newMock(IDirect.class);
075    
076            expect(component.getPage()).andReturn(page);
077    
078            expect(page.getPageName()).andReturn("Barney");
079    
080            expect(component.getIdPath()).andReturn("foo.bar");
081    
082            replay();
083    
084            DirectCallback callback = new DirectCallback(component, params);
085    
086            assertEquals("DirectCallback[Barney/foo.bar p1, p2]", callback.toString());
087    
088            verify();
089    
090            IRequestCycle cycle = newCycleGetPage("Barney", page);
091    
092            expect(page.getNestedComponent("foo.bar")).andReturn(component);
093    
094            cycle.setListenerParameters(params);
095    
096            component.trigger(cycle);
097    
098            replay();
099    
100            callback.performCallback(cycle);
101    
102            verify();
103        }
104    
105        public void testNotDirect()
106        {
107            IPage page = newMock(IPage.class);
108            IDirect component = newMock(IDirect.class);
109    
110            expect(component.getPage()).andReturn(page);
111    
112            expect(page.getPageName()).andReturn("Fred");
113            
114            expect(component.getIdPath()).andReturn("foo.bar");
115    
116            replay();
117    
118            DirectCallback callback = new DirectCallback(component, null);
119    
120            assertEquals("DirectCallback[Fred/foo.bar]", callback.toString());
121    
122            verify();
123    
124            IRequestCycle cycle = newCycleGetPage("Fred", page);
125            
126            Location l = newLocation();
127            IComponent component2 = newComponent("Fred/foo.bar", l);
128            
129            expect(page.getNestedComponent("foo.bar")).andReturn(component2);
130    
131            replay();
132    
133            try
134            {
135                callback.performCallback(cycle);
136            }
137            catch (ApplicationRuntimeException ex)
138            {
139                assertEquals("Component Fred/foo.bar does not implement the IDirect interface.", ex
140                        .getMessage());
141                assertSame(component2, ex.getComponent());
142                assertSame(l, ex.getLocation());
143            }
144    
145            verify();
146        }
147    }