1 15 package org.apache.tapestry.engine; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.Location; 22 import org.apache.tapestry.IExternalPage; 23 import org.apache.tapestry.IPage; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.Tapestry; 26 import org.apache.tapestry.services.LinkFactory; 27 import org.apache.tapestry.services.ResponseRenderer; 28 import org.apache.tapestry.services.ServiceConstants; 29 import org.easymock.MockControl; 30 31 37 public class TestExternalService extends ServiceTestCase 38 { 39 private LinkFactory newLinkFactory(IRequestCycle cycle, Object [] serviceParameters) 40 { 41 MockControl control = newControl(LinkFactory.class); 42 LinkFactory lf = (LinkFactory) control.getMock(); 43 44 lf.extractListenerParameters(cycle); 45 control.setReturnValue(serviceParameters); 46 47 return lf; 48 } 49 50 public void testGetLink() 51 { 52 Object [] serviceParameters = new Object [0]; 53 54 Map parameters = new HashMap (); 55 parameters.put(ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE); 56 parameters.put(ServiceConstants.PAGE, "ActivePage"); 57 parameters.put(ServiceConstants.PARAMETER, serviceParameters); 58 59 MockControl lfc = newControl(LinkFactory.class); 60 LinkFactory lf = (LinkFactory) lfc.getMock(); 61 62 ILink link = (ILink) newMock(ILink.class); 63 64 IRequestCycle cycle = (IRequestCycle) newMock(IRequestCycle.class); 65 66 lf.constructLink(cycle, parameters, true); 67 lfc.setReturnValue(link); 68 69 replayControls(); 70 71 ExternalService es = new ExternalService(); 72 es.setLinkFactory(lf); 73 74 ExternalServiceParameter p = new ExternalServiceParameter("ActivePage", serviceParameters); 75 76 assertSame(link, es.getLink(cycle, p)); 77 78 verifyControls(); 79 } 80 81 public void testService() throws Exception 82 { 83 MockControl cyclec = newControl(IRequestCycle.class); 84 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 85 86 IExternalPage page = (IExternalPage) newMock(IExternalPage.class); 87 88 Object [] parameters = new Object [0]; 89 90 cycle.getParameter(ServiceConstants.PAGE); 91 cyclec.setReturnValue("ActivePage"); 92 93 cycle.getPage("ActivePage"); 94 cyclec.setReturnValue(page); 95 96 LinkFactory lf = newLinkFactory(cycle, parameters); 97 98 cycle.setListenerParameters(parameters); 99 cycle.activate(page); 100 page.activateExternalPage(parameters, cycle); 101 102 ResponseRenderer rr = (ResponseRenderer) newMock(ResponseRenderer.class); 103 104 rr.renderResponse(cycle); 105 106 replayControls(); 107 108 ExternalService es = new ExternalService(); 109 es.setLinkFactory(lf); 110 es.setResponseRenderer(rr); 111 112 es.service(cycle); 113 114 verifyControls(); 115 } 116 117 public void testServiceWrongType() throws Exception 118 { 119 MockControl cyclec = newControl(IRequestCycle.class); 120 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 121 122 MockControl pagec = newControl(IPage.class); 123 IPage page = (IPage) pagec.getMock(); 124 125 cycle.getParameter(ServiceConstants.PAGE); 126 cyclec.setReturnValue("ActivePage"); 127 128 cycle.getPage("ActivePage"); 129 cyclec.setReturnValue(page); 130 131 page.getPageName(); 132 pagec.setReturnValue("ActivePage"); 133 134 Location l = fabricateLocation(17); 135 136 page.getLocation(); 137 pagec.setReturnValue(l); 138 139 replayControls(); 140 141 ExternalService es = new ExternalService(); 142 143 try 144 { 145 es.service(cycle); 146 unreachable(); 147 } 148 catch (ApplicationRuntimeException ex) 149 { 150 assertEquals( 151 "Page ActivePage does not implement the org.apache.tapestry.IExternalPage interface.", 152 ex.getMessage()); 153 assertSame(l, ex.getLocation()); 154 assertSame(page, ex.getComponent()); 155 } 156 157 verifyControls(); 158 } 159 } | Popular Tags |