1 12 package org.displaytag.portlet; 13 14 import java.util.Map ; 15 16 import junit.framework.TestCase; 17 18 import org.springframework.mock.web.MockPageContext; 19 import org.springframework.mock.web.portlet.MockPortletRequest; 20 import org.springframework.mock.web.portlet.MockRenderResponse; 21 22 23 27 public class PortletRequestHelperTest extends TestCase 28 { 29 30 33 public String getName() 34 { 35 return "PortletRequestHelper Test"; 36 } 37 38 public void testNullPageContext() 39 { 40 try 41 { 42 new PortletRequestHelper(null); 43 fail("IllegalArgumentException should have been thrown"); 44 } 45 catch (IllegalArgumentException iae) 46 { 47 } 49 } 50 51 public void testEmptyPageContext() 52 { 53 final MockPageContext pageContext = new MockPageContext(); 54 55 try 56 { 57 new PortletRequestHelper(pageContext); 58 fail("IllegalStateException should have been thrown"); 59 } 60 catch (IllegalStateException ise) 61 { 62 } 64 65 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest()); 66 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, null); 67 try 68 { 69 new PortletRequestHelper(pageContext); 70 fail("IllegalStateException should have been thrown"); 71 } 72 catch (IllegalStateException ise) 73 { 74 } 76 77 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, null); 78 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 79 try 80 { 81 new PortletRequestHelper(pageContext); 82 fail("IllegalStateException should have been thrown"); 83 } 84 catch (IllegalStateException ise) 85 { 86 } 88 } 89 90 public void testBasicPageContext() 91 { 92 final MockPageContext pageContext = new MockPageContext(); 93 94 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest()); 95 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 96 97 new PortletRequestHelper(pageContext); 98 } 99 100 public void testRequestParameters() 101 { 102 final MockPageContext pageContext = new MockPageContext(); 103 final MockPortletRequest request = new MockPortletRequest(); 104 105 request.setParameter("STRING_PARAM", "STRING_VALUE"); 106 request.setParameter("INTEGER_PARAM", "31337"); 107 108 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request); 109 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 110 111 final PortletRequestHelper helper = new PortletRequestHelper(pageContext); 112 113 final String strVal = helper.getParameter("STRING_PARAM"); 114 assertEquals("STRING_VALUE", strVal); 115 116 final Integer intVal = helper.getIntParameter("INTEGER_PARAM"); 117 assertEquals(new Integer (31337), intVal); 118 119 final Integer nullIntVal = helper.getIntParameter("STRING_PARAM"); 120 assertNull(nullIntVal); 121 122 final Map params = helper.getParameterMap(); 123 assertEquals(2, params.size()); 124 125 final String [] expextedStrArryVal = new String []{"STRING_VALUE"}; 126 final String [] strArryVal = (String []) params.get("STRING_PARAM"); 127 assertEquals(expextedStrArryVal.length, strArryVal.length); 128 assertEquals(expextedStrArryVal[0], strArryVal[0]); 129 130 final String [] expextedIntArryVal = new String []{"31337"}; 131 final String [] intArryVal = (String []) params.get("INTEGER_PARAM"); 132 assertEquals(expextedIntArryVal.length, intArryVal.length); 133 assertEquals(expextedIntArryVal[0], intArryVal[0]); 134 } 135 136 public void testCreateEmptyHref() 137 { 138 final MockPageContext pageContext = new MockPageContext(); 139 140 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest()); 141 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 142 143 final PortletRequestHelper helper = new PortletRequestHelper(pageContext); 144 145 final PortletHref ref = (PortletHref) helper.getHref(); 146 147 final Map params = ref.getParameterMap(); 148 assertEquals(0, params.size()); 149 150 assertNull(ref.getAnchor()); 151 assertNull(ref.getRequestedMode()); 152 assertNull(ref.getRequestedState()); 153 154 assertFalse(ref.isRequestedSecure()); 155 } 156 157 public void testCreateSecureHref() 158 { 159 final MockPageContext pageContext = new MockPageContext(); 160 final MockPortletRequest request = new MockPortletRequest(); 161 162 request.setSecure(true); 163 164 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request); 165 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 166 167 final PortletRequestHelper helper = new PortletRequestHelper(pageContext); 168 169 final PortletHref ref = (PortletHref) helper.getHref(); 170 171 final Map params = ref.getParameterMap(); 172 assertEquals(0, params.size()); 173 174 assertNull(ref.getAnchor()); 175 assertNull(ref.getRequestedMode()); 176 assertNull(ref.getRequestedState()); 177 178 assertTrue(ref.isRequestedSecure()); 179 } 180 181 public void testParameterizedHref() 182 { 183 final MockPageContext pageContext = new MockPageContext(); 184 final MockPortletRequest request = new MockPortletRequest(); 185 186 request.setParameter("STRING_PARAM", "STRING_VALUE"); 187 request.setParameter("INTEGER_PARAM", "31337"); 188 189 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request); 190 pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); 191 192 final PortletRequestHelper helper = new PortletRequestHelper(pageContext); 193 194 final PortletHref ref = (PortletHref) helper.getHref(); 195 196 final Map params = ref.getParameterMap(); 197 assertEquals(2, params.size()); 198 199 final String [] expextedStrArryVal = new String []{"STRING_VALUE"}; 200 final String [] strArryVal = (String []) params.get("STRING_PARAM"); 201 assertEquals(expextedStrArryVal.length, strArryVal.length); 202 assertEquals(expextedStrArryVal[0], strArryVal[0]); 203 204 final String [] expextedIntArryVal = new String []{"31337"}; 205 final String [] intArryVal = (String []) params.get("INTEGER_PARAM"); 206 assertEquals(expextedIntArryVal.length, intArryVal.length); 207 assertEquals(expextedIntArryVal[0], intArryVal[0]); 208 209 assertNull(ref.getAnchor()); 210 assertNull(ref.getRequestedMode()); 211 assertNull(ref.getRequestedState()); 212 213 assertFalse(ref.isRequestedSecure()); 214 } 215 } 216 | Popular Tags |