001    // Copyright 2004, 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.engine;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.Location;
022    import org.apache.tapestry.IExternalPage;
023    import org.apache.tapestry.IPage;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.services.LinkFactory;
026    import org.apache.tapestry.services.ResponseRenderer;
027    import org.apache.tapestry.services.ServiceConstants;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Tests for {@link org.apache.tapestry.engine.ExternalService}.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class ExternalServiceTest extends ServiceTestCase
038    {
039        public void test_Get_Link()
040        {
041            Object[] serviceParameters = new Object[0];
042            LinkFactory lf = newLinkFactory();
043            ILink link = newLink();
044    
045            Map parameters = new HashMap();
046            parameters.put(ServiceConstants.PAGE, "ActivePage");
047            parameters.put(ServiceConstants.PARAMETER, serviceParameters);
048    
049            ExternalService es = new ExternalService();
050            es.setLinkFactory(lf);
051    
052            trainConstructLink(lf, es, false, parameters, true, link);
053    
054            replay();
055    
056            ExternalServiceParameter p = new ExternalServiceParameter("ActivePage", serviceParameters);
057    
058            assertSame(link, es.getLink(false, p));
059    
060            verify();
061        }
062    
063        public void test_Service() throws Exception
064        {
065            IRequestCycle cycle = newCycle();
066            IExternalPage page = newMock(IExternalPage.class);
067            Object[] parameters = new Object[0];
068            LinkFactory lf = newLinkFactory();
069            ResponseRenderer rr = newResponseRenderer();
070            
071            trainGetParameter(cycle, ServiceConstants.PAGE, "ActivePage");
072            
073            trainGetPage(cycle, "ActivePage", page);
074            
075            trainExtractListenerParameters(lf, cycle, parameters);
076            
077            cycle.setListenerParameters(parameters);
078            cycle.activate(page);
079            
080            page.activateExternalPage(parameters, cycle);
081            
082            rr.renderResponse(cycle);
083    
084            replay();
085    
086            ExternalService es = new ExternalService();
087            es.setLinkFactory(lf);
088            es.setResponseRenderer(rr);
089    
090            es.service(cycle);
091    
092            verify();
093        }
094    
095        public void test_Service_Wrong_Type() throws Exception
096        {
097    
098            IRequestCycle cycle = newCycle();
099            IPage page = newPage();
100            Location l = newLocation();
101    
102            trainGetParameter(cycle, ServiceConstants.PAGE, "ActivePage");
103    
104            trainGetPage(cycle, "ActivePage", page);
105    
106            trainGetPageName(page, "ActivePage");
107            trainGetLocation(page, l);
108    
109            replay();
110    
111            ExternalService es = new ExternalService();
112    
113            try
114            {
115                es.service(cycle);
116                unreachable();
117            }
118            catch (ApplicationRuntimeException ex)
119            {
120                assertEquals(
121                        "Page ActivePage does not implement the org.apache.tapestry.IExternalPage interface.",
122                        ex.getMessage());
123                assertSame(l, ex.getLocation());
124                assertSame(page, ex.getComponent());
125            }
126    
127            verify();
128        }
129    }