1 15 package org.apache.tapestry.portlet; 16 17 import java.util.Map ; 18 19 import javax.portlet.PortletMode; 20 import javax.portlet.PortletModeException; 21 import javax.portlet.PortletSecurityException; 22 import javax.portlet.PortletURL; 23 import javax.portlet.WindowState; 24 import javax.portlet.WindowStateException; 25 26 import org.apache.hivemind.test.HiveMindTestCase; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.engine.ILink; 29 import org.apache.tapestry.util.QueryParameterMap; 30 import org.easymock.MockControl; 31 32 38 public class TestPortletLink extends HiveMindTestCase 39 { 40 private static class PortletURLFixture implements PortletURL 41 { 42 private final String _toString; 43 44 public PortletURLFixture(String toString) 45 { 46 _toString = toString; 47 } 48 49 public String toString() 50 { 51 return _toString; 52 } 53 54 public void setWindowState(WindowState arg0) throws WindowStateException 55 { 56 } 57 58 public void setPortletMode(PortletMode arg0) throws PortletModeException 59 { 60 } 61 62 public void setParameter(String arg0, String arg1) 63 { 64 } 65 66 public void setParameter(String arg0, String [] arg1) 67 { 68 } 69 70 public void setParameters(Map arg0) 71 { 72 } 73 74 public void setSecure(boolean arg0) throws PortletSecurityException 75 { 76 } 77 78 } 79 80 private IRequestCycle newCycle() 81 { 82 return (IRequestCycle) newMock(IRequestCycle.class); 83 } 84 85 private PortletURL newPortletURL() 86 { 87 return (PortletURL) newMock(PortletURL.class); 88 } 89 90 private QueryParameterMap newParameters() 91 { 92 return (QueryParameterMap) newMock(QueryParameterMap.class); 93 } 94 95 public void testGetAbsoluteURL() 96 { 97 IRequestCycle cycle = newCycle(); 98 PortletURL url = newPortletURL(); 99 QueryParameterMap parameters = newParameters(); 100 101 replayControls(); 102 103 ILink link = new PortletLink(cycle, url, parameters, false); 104 105 try 106 { 107 link.getAbsoluteURL(); 108 unreachable(); 109 } 110 catch (UnsupportedOperationException ex) 111 { 112 } 114 115 try 116 { 117 link.getAbsoluteURL(null, null, -1, null, false); 118 unreachable(); 119 } 120 catch (UnsupportedOperationException ex) 121 { 122 } 124 125 verifyControls(); 126 } 127 128 public void testGetParameterNames() 129 { 130 IRequestCycle cycle = newCycle(); 131 PortletURL url = newPortletURL(); 132 133 MockControl control = newControl(QueryParameterMap.class); 134 QueryParameterMap parameters = (QueryParameterMap) control.getMock(); 135 136 String [] names = 137 { "Fred", "Barney" }; 138 139 parameters.getParameterNames(); 140 control.setReturnValue(names); 141 142 replayControls(); 143 144 ILink link = new PortletLink(cycle, url, parameters, false); 145 146 assertSame(names, link.getParameterNames()); 147 148 verifyControls(); 149 } 150 151 public void testGetParameterValues() 152 { 153 IRequestCycle cycle = newCycle(); 154 PortletURL url = newPortletURL(); 155 156 MockControl control = newControl(QueryParameterMap.class); 157 QueryParameterMap parameters = (QueryParameterMap) control.getMock(); 158 159 String [] values = 160 { "Fred", "Barney" }; 161 162 parameters.getParameterValues("bedrock"); 163 control.setReturnValue(values); 164 165 replayControls(); 166 167 ILink link = new PortletLink(cycle, url, parameters, false); 168 169 assertSame(values, link.getParameterValues("bedrock")); 170 171 verifyControls(); 172 } 173 174 public void testGetURL() 175 { 176 IRequestCycle cycle = newCycle(); 177 PortletURL url = newPortletURL(); 178 179 MockControl control = newControl(QueryParameterMap.class); 180 QueryParameterMap parameters = (QueryParameterMap) control.getMock(); 181 182 parameters.getParameterNames(); 183 control.setReturnValue(new String [0]); 184 185 replayControls(); 186 187 ILink link = new PortletLink(cycle, url, parameters, false); 188 189 assertEquals(url.toString(), link.getURL()); 190 191 verifyControls(); 192 } 193 194 public void testGetURLUnencoding() 195 { 196 IRequestCycle cycle = newCycle(); 197 PortletURL url = new PortletURLFixture("this=foo&that=bar"); 198 199 MockControl control = newControl(QueryParameterMap.class); 200 QueryParameterMap parameters = (QueryParameterMap) control.getMock(); 201 202 parameters.getParameterNames(); 203 control.setReturnValue(new String [0]); 204 205 replayControls(); 206 207 ILink link = new PortletLink(cycle, url, parameters, false); 208 209 assertEquals("this=foo&that=bar", link.getURL()); 210 211 verifyControls(); 212 } 213 214 public void testGetURLIncludeParameters() 215 { 216 IRequestCycle cycle = newCycle(); 217 PortletURL url = newPortletURL(); 218 219 MockControl control = newControl(QueryParameterMap.class); 220 QueryParameterMap parameters = (QueryParameterMap) control.getMock(); 221 222 String [] values = 223 { "Fred", "Barney" }; 224 225 parameters.getParameterNames(); 226 control.setReturnValue(new String [] 227 { "bedrock" }); 228 parameters.getParameterValues("bedrock"); 229 control.setReturnValue(values); 230 231 url.setParameter("bedrock", values); 232 233 replayControls(); 234 235 ILink link = new PortletLink(cycle, url, parameters, false); 236 237 assertEquals(url.toString(), link.getURL()); 238 239 verifyControls(); 240 } 241 242 public void testGetURLStatefulWithAnchor() 243 { 244 PortletURL url = newPortletURL(); 245 246 MockControl cyclec = newControl(IRequestCycle.class); 247 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 248 249 cycle.encodeURL(url.toString()); 250 cyclec.setReturnValue("/encoded-url"); 251 252 QueryParameterMap parameters = newParameters(); 253 254 replayControls(); 255 256 ILink link = new PortletLink(cycle, url, parameters, true); 257 258 assertEquals("/encoded-url#anchor", link.getURL("anchor", false)); 259 260 verifyControls(); 261 } 262 } | Popular Tags |