1 15 package org.apache.tapestry.portlet; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.List ; 20 21 import javax.portlet.PortletMode; 22 import javax.portlet.PortletRequest; 23 import javax.portlet.WindowState; 24 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.apache.tapestry.IPage; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.PageNotFoundException; 29 import org.easymock.MockControl; 30 31 37 public class TestMatchingPortletPageResolver extends HiveMindTestCase 38 { 39 private IRequestCycle newCycle() 40 { 41 return (IRequestCycle) newMock(IRequestCycle.class); 42 } 43 44 private IPage newPage() 45 { 46 return (IPage) newMock(IPage.class); 47 } 48 49 private IRequestCycle newCycle(String pageName) 50 { 51 IPage page = newPage(); 52 53 MockControl control = newControl(IRequestCycle.class); 54 IRequestCycle cycle = (IRequestCycle) control.getMock(); 55 56 cycle.getPage(pageName); 57 control.setReturnValue(page); 58 59 return cycle; 60 } 61 62 private PageResolverContribution newContribution(String mimeType, String portletMode, 63 String windowState, String pageName) 64 { 65 PageResolverContribution result = new PageResolverContribution(); 66 result.setMimeType(mimeType); 67 result.setPortletMode(portletMode); 68 result.setWindowState(windowState); 69 result.setPageName(pageName); 70 71 return result; 72 } 73 74 public void testNoMatchOnContentType() 75 { 76 IRequestCycle cycle = newCycle(); 77 78 MockControl requestc = newControl(PortletRequest.class); 79 PortletRequest request = (PortletRequest) requestc.getMock(); 80 81 trainContentType(requestc, request, "text/xml"); 82 83 List l = Collections.singletonList(newContribution("text/html", null, "wierd", "Wierd")); 84 85 replayControls(); 86 87 MatchingPortletPageResolver resolver = new MatchingPortletPageResolver(); 88 resolver.setContributions(l); 89 resolver.initializeService(); 90 resolver.setRequest(request); 91 92 assertNull(resolver.getPageNameForRequest(cycle)); 93 94 verifyControls(); 95 } 96 97 public void testNoMatchOnPortletMode() 98 { 99 IRequestCycle cycle = newCycle(); 100 101 MockControl requestc = newControl(PortletRequest.class); 102 PortletRequest request = (PortletRequest) requestc.getMock(); 103 104 trainPortletMode(requestc, request, "edit"); 105 106 List l = Collections.singletonList(newContribution(null, "view", null, "Wierd")); 107 108 replayControls(); 109 110 MatchingPortletPageResolver resolver = new MatchingPortletPageResolver(); 111 resolver.setContributions(l); 112 resolver.initializeService(); 113 resolver.setRequest(request); 114 115 assertNull(resolver.getPageNameForRequest(cycle)); 116 117 verifyControls(); 118 } 119 120 public void testNoMatchOnWindowState() 121 { 122 IRequestCycle cycle = newCycle(); 123 124 MockControl requestc = newControl(PortletRequest.class); 125 PortletRequest request = (PortletRequest) requestc.getMock(); 126 127 trainWindowState(requestc, request, "huge"); 128 129 List l = Collections.singletonList(newContribution(null, null, "tiny", "Wierd")); 130 131 replayControls(); 132 133 MatchingPortletPageResolver resolver = new MatchingPortletPageResolver(); 134 resolver.setContributions(l); 135 resolver.initializeService(); 136 resolver.setRequest(request); 137 138 assertNull(resolver.getPageNameForRequest(cycle)); 139 140 verifyControls(); 141 } 142 143 public void testSortingCounts() 144 { 145 IRequestCycle cycle = newCycle("EditHuge"); 146 147 MockControl requestc = newControl(PortletRequest.class); 148 PortletRequest request = (PortletRequest) requestc.getMock(); 149 150 List l = new ArrayList (); 151 l.add(newContribution(null, "edit", null, "EditNormal")); 152 l.add(newContribution(null, "edit", "huge", "EditHuge")); 153 154 trainPortletMode(requestc, request, "edit"); 155 trainWindowState(requestc, request, "huge"); 156 157 replayControls(); 158 159 MatchingPortletPageResolver resolver = new MatchingPortletPageResolver(); 160 resolver.setContributions(l); 161 resolver.initializeService(); 162 resolver.setRequest(request); 163 164 assertEquals("EditHuge", resolver.getPageNameForRequest(cycle)); 165 166 verifyControls(); 167 } 168 169 public void testPageMissing() 170 { 171 IPage page = newPage(); 172 173 MockControl cyclec = newControl(IRequestCycle.class); 174 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 175 176 MockControl requestc = newControl(PortletRequest.class); 177 PortletRequest request = (PortletRequest) requestc.getMock(); 178 179 List l = new ArrayList (); 180 l.add(newContribution(null, "edit", null, "EditNormal")); 181 l.add(newContribution(null, "edit", "huge", "EditHuge")); 182 183 trainPortletMode(requestc, request, "edit"); 184 trainWindowState(requestc, request, "huge"); 185 186 cycle.getPage("EditHuge"); 187 cyclec.setThrowable(new PageNotFoundException("missing!")); 188 189 trainPortletMode(requestc, request, "edit"); 190 191 cycle.getPage("EditNormal"); 192 cyclec.setReturnValue(page); 193 194 replayControls(); 195 196 MatchingPortletPageResolver resolver = new MatchingPortletPageResolver(); 197 resolver.setContributions(l); 198 resolver.initializeService(); 199 resolver.setRequest(request); 200 201 assertEquals("EditNormal", resolver.getPageNameForRequest(cycle)); 202 203 verifyControls(); 204 } 205 206 private void trainContentType(MockControl control, PortletRequest request, String contentType) 207 { 208 request.getResponseContentType(); 209 control.setReturnValue(contentType); 210 } 211 212 private void trainPortletMode(MockControl control, PortletRequest request, String modeName) 213 { 214 PortletMode mode = new PortletMode(modeName); 215 216 request.getPortletMode(); 217 control.setReturnValue(mode); 218 } 219 220 private void trainWindowState(MockControl control, PortletRequest request, String stateName) 221 { 222 WindowState state = new WindowState(stateName); 223 224 request.getWindowState(); 225 control.setReturnValue(state); 226 } 227 } | Popular Tags |