1 15 package org.apache.tapestry.junit; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.apache.commons.codec.net.URLCodec; 21 import org.apache.tapestry.IRequestCycle; 22 import org.apache.tapestry.Tapestry; 23 import org.apache.tapestry.engine.EngineServiceLink; 24 import org.apache.tapestry.services.ServiceConstants; 25 import org.apache.tapestry.web.WebRequest; 26 import org.easymock.MockControl; 27 28 34 public class TestEngineServiceLink extends TapestryTestCase 35 { 36 private URLCodec _urlCodec = new URLCodec(); 37 38 private static final String ENCODING = "utf-8"; 39 40 private Map buildParameters(String serviceName, String [] serviceParameters) 41 { 42 Map result = new HashMap (); 43 44 result.put(ServiceConstants.SERVICE, serviceName); 45 result.put(ServiceConstants.PARAMETER, serviceParameters); 46 47 return result; 48 } 49 50 51 private WebRequest newRequest() 52 { 53 return (WebRequest) newMock(WebRequest.class); 54 } 55 56 57 public void testGetURLWithParameters() 58 { 59 WebRequest request = newRequest(); 60 MockControl control = newControl(IRequestCycle.class); 61 IRequestCycle rc = (IRequestCycle) control.getMock(); 62 63 rc.encodeURL("/context/servlet?service=myservice"); 64 control.setReturnValue("/context/servlet?service=myservice;encoded"); 65 66 EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec, 67 request, buildParameters("myservice", null), true); 68 69 replayControls(); 70 71 assertEquals("/context/servlet?service=myservice;encoded", l.getURL()); 72 73 verifyControls(); 74 75 checkList("parameterNames", new String [] 76 { Tapestry.SERVICE_QUERY_PARAMETER_NAME, Tapestry.PARAMETERS_QUERY_PARAMETER_NAME }, l 77 .getParameterNames()); 78 79 } 80 81 public void testGetURLWithServiceParameters() 82 { 83 WebRequest request = newRequest(); 84 85 IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class); 86 87 EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request, 88 buildParameters("foo", new String [] 89 { "godzilla", "frodo" }), false); 90 91 replayControls(); 92 93 assertEquals("/ctx/app?service=foo&sp=godzilla&sp=frodo", l.getURL()); 94 95 verifyControls(); 96 } 97 98 99 100 public void testGetURLSansParameters() 101 { 102 WebRequest request = newRequest(); 103 104 MockControl control = newControl(IRequestCycle.class); 105 IRequestCycle rc = (IRequestCycle) control.getMock(); 106 107 EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec, 108 request, buildParameters("myservice", null), true); 109 110 rc.encodeURL("/context/servlet"); 111 control.setReturnValue("/context/servlet;encoded"); 112 113 replayControls(); 114 115 assertEquals("/context/servlet;encoded", l.getURL(null, false)); 116 117 verifyControls(); 118 } 119 120 121 122 public void testGetURLWithAnchor() 123 { 124 WebRequest request = newRequest(); 125 IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class); 126 127 EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec, 128 request, buildParameters("myservice", null), false); 129 130 replayControls(); 131 132 assertEquals("/context/servlet#anchor", l.getURL("anchor", false)); 133 134 verifyControls(); 135 } 136 137 public void testGetURLWithAnchorAndParameters() throws Exception 138 { 139 WebRequest request = newRequest(); 140 IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class); 141 142 EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec, 143 request, buildParameters("myservice", null), false); 144 145 replayControls(); 146 147 assertEquals("/context/servlet?service=myservice#anchor", l.getURL("anchor", true)); 148 149 verifyControls(); 150 } 151 152 public void testGetAbsoluteURL() throws Exception 153 { 154 MockControl control = newControl(IRequestCycle.class); 155 IRequestCycle rc = (IRequestCycle) control.getMock(); 156 157 MockControl requestc = newControl(WebRequest.class); 158 WebRequest request = (WebRequest) requestc.getMock(); 159 160 EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request, 161 buildParameters("myservice", null), false); 162 163 request.getScheme(); 164 requestc.setReturnValue("HTTP"); 165 166 request.getServerName(); 167 requestc.setReturnValue("TESTSERVER.COM"); 168 169 request.getServerPort(); 170 requestc.setReturnValue(9187); 171 172 replayControls(); 173 174 assertEquals("HTTP://TESTSERVER.COM:9187/ctx/app?service=myservice", l.getAbsoluteURL()); 175 176 verifyControls(); 177 } 178 179 public void testGetAbsoluteURLWithOverrides() throws Exception 180 { 181 MockControl control = newControl(IRequestCycle.class); 182 IRequestCycle rc = (IRequestCycle) control.getMock(); 183 184 MockControl requestc = newControl(WebRequest.class); 185 WebRequest request = (WebRequest) requestc.getMock(); 186 187 EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request, 188 buildParameters("myservice", null), false); 189 190 replayControls(); 191 192 assertEquals("https://myserver.net:9100/ctx/app?service=myservice", l.getAbsoluteURL( 193 "https", 194 "myserver.net", 195 9100, 196 null, 197 true)); 198 199 verifyControls(); 200 } 201 } | Popular Tags |