@Test public void testParameters() { MockPortletRequest portletRequest = new MockPortletRequest(); portletRequest.addParameter("param1", "value1"); portletRequest.addParameter("param2", "value2"); portletRequest.addParameter("param2", "value2a"); PortletWebRequest request = new PortletWebRequest(portletRequest); assertEquals("value1", request.getParameter("param1")); assertEquals(1, request.getParameterValues("param1").length); assertEquals("value1", request.getParameterValues("param1")[0]); assertEquals("value2", request.getParameter("param2")); assertEquals(2, request.getParameterValues("param2").length); assertEquals("value2", request.getParameterValues("param2")[0]); assertEquals("value2a", request.getParameterValues("param2")[1]); Map<String, String[]> paramMap = request.getParameterMap(); assertEquals(2, paramMap.size()); assertEquals(1, paramMap.get("param1").length); assertEquals("value1", paramMap.get("param1")[0]); assertEquals(2, paramMap.get("param2").length); assertEquals("value2", paramMap.get("param2")[0]); assertEquals("value2a", paramMap.get("param2")[1]); }
@Test public void testGetParametersStartingWith() throws Exception { final String targetPrefix = "francisan_"; final String badKey = "dominican_Bernard"; MockPortletRequest request = new MockPortletRequest(); request.setParameter(targetPrefix + "William", "Baskerville"); request.setParameter(targetPrefix + "Adso", "Melk"); request.setParameter(badKey, "Gui"); Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix); assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters); assertEquals("Obviously not finding all of the correct parameters", 2, actualParameters.size()); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("Adso")); assertFalse("Obviously not finding all of the correct parameters (is returning a parameter whose name does not start with the desired prefix", actualParameters.containsKey(badKey)); }
@Test public void testGetParametersStartingWithUnpicksScalarParameterValues() throws Exception { final String targetPrefix = "francisan_"; final String badKey = "dominican_Bernard"; MockPortletRequest request = new MockPortletRequest(); request.setParameter(targetPrefix + "William", "Baskerville"); request.setParameter(targetPrefix + "Adso", new String[]{"Melk", "Of Melk"}); request.setParameter(badKey, "Gui"); Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix); assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters); assertEquals("Obviously not finding all of the correct parameters", 2, actualParameters.size()); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William")); assertEquals("Not picking scalar parameter value out correctly", "Baskerville", actualParameters.get("William")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("Adso")); assertFalse("Obviously not finding all of the correct parameters (is returning a parameter whose name does not start with the desired prefix", actualParameters.containsKey(badKey)); }
@Test public void testIntParameters() throws PortletRequestBindingException { MockPortletRequest request = new MockPortletRequest(); request.addParameter("param", new String[] {"1", "2", "3"}); request.addParameter("param2", "1"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); int[] array = new int[] { 1, 2, 3 }; int[] values = PortletRequestUtils.getRequiredIntParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } try { PortletRequestUtils.getRequiredIntParameters(request, "param2"); fail("Should have thrown PortletRequestBindingException"); } catch (PortletRequestBindingException ex) { // expected } }
@Test public void testFloatParameters() throws PortletRequestBindingException { MockPortletRequest request = new MockPortletRequest(); request.addParameter("param", new String[] {"1.5", "2.5", "3"}); request.addParameter("param2", "1.5"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); float[] array = new float[] { 1.5F, 2.5F, 3 }; float[] values = PortletRequestUtils.getRequiredFloatParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i], 0); } try { PortletRequestUtils.getRequiredFloatParameters(request, "param2"); fail("Should have thrown PortletRequestBindingException"); } catch (PortletRequestBindingException ex) { // expected } }
@Test public void testDoubleParameters() throws PortletRequestBindingException { MockPortletRequest request = new MockPortletRequest(); request.addParameter("param", new String[] {"1.5", "2.5", "3"}); request.addParameter("param2", "1.5"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); double[] array = new double[] { 1.5, 2.5, 3 }; double[] values = PortletRequestUtils.getRequiredDoubleParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i], 0); } try { PortletRequestUtils.getRequiredDoubleParameters(request, "param2"); fail("Should have thrown PortletRequestBindingException"); } catch (PortletRequestBindingException ex) { // expected } }
@Test public void testBooleanParameters() throws PortletRequestBindingException { MockPortletRequest request = new MockPortletRequest(); request.addParameter("param", new String[] {"true", "yes", "off", "1", "bogus"}); request.addParameter("param2", "false"); request.addParameter("param2", "true"); request.addParameter("param2", ""); boolean[] array = new boolean[] { true, true, false, true, false }; boolean[] values = PortletRequestUtils.getRequiredBooleanParameters(request, "param"); assertEquals(5, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } array = new boolean[] { false, true, false }; values = PortletRequestUtils.getRequiredBooleanParameters(request, "param2"); assertEquals(array.length, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } }
@Test public void testStringParameter() throws PortletRequestBindingException { MockPortletRequest request = new MockPortletRequest(); request.addParameter("param1", "str"); request.addParameter("paramEmpty", ""); assertEquals("str", PortletRequestUtils.getStringParameter(request, "param1")); assertEquals("str", PortletRequestUtils.getStringParameter(request, "param1", "string")); assertEquals("str", PortletRequestUtils.getRequiredStringParameter(request, "param1")); assertEquals(null, PortletRequestUtils.getStringParameter(request, "param3")); assertEquals("string", PortletRequestUtils.getStringParameter(request, "param3", "string")); try { PortletRequestUtils.getRequiredStringParameter(request, "param3"); fail("Should have thrown PortletRequestBindingException"); } catch (PortletRequestBindingException ex) { // expected } assertEquals("", PortletRequestUtils.getStringParameter(request, "paramEmpty")); assertEquals("", PortletRequestUtils.getRequiredStringParameter(request, "paramEmpty")); }
@Test public void testParameters() { MockPortletRequest portletRequest = new MockPortletRequest(); portletRequest.addParameter("param1", "value1"); portletRequest.addParameter("param2", "value2"); portletRequest.addParameter("param2", "value2a"); PortletWebRequest request = new PortletWebRequest(portletRequest); assertEquals("value1", request.getParameter("param1")); assertEquals(1, request.getParameterValues("param1").length); assertEquals("value1", request.getParameterValues("param1")[0]); assertEquals("value2", request.getParameter("param2")); assertEquals(2, request.getParameterValues("param2").length); assertEquals("value2", request.getParameterValues("param2")[0]); assertEquals("value2a", request.getParameterValues("param2")[1]); Map paramMap = request.getParameterMap(); assertEquals(2, paramMap.size()); assertEquals(1, ((String[]) paramMap.get("param1")).length); assertEquals("value1", ((String[]) paramMap.get("param1"))[0]); assertEquals(2, ((String[]) paramMap.get("param2")).length); assertEquals("value2", ((String[]) paramMap.get("param2"))[0]); assertEquals("value2a", ((String[]) paramMap.get("param2"))[1]); }
public void testPortletModeViewWithParameter() throws Exception { HandlerMapping hm = (HandlerMapping)pac.getBean("handlerMapping"); MockPortletRequest addRequest = new MockPortletRequest(); addRequest.setPortletMode(PortletMode.VIEW); addRequest.setParameter("action", "add"); MockPortletRequest removeRequest = new MockPortletRequest(); removeRequest.setPortletMode(PortletMode.VIEW); removeRequest.setParameter("action", "remove"); Object addHandler = hm.getHandler(addRequest).getHandler(); Object removeHandler = hm.getHandler(removeRequest).getHandler(); assertEquals(pac.getBean("addItemHandler"), addHandler); assertEquals(pac.getBean("removeItemHandler"), removeHandler); }
public void testNestedBindWithPropertyEditor() { TestBean bean = new TestBean(); PortletRequestDataBinder binder = new PortletRequestDataBinder(bean); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean(text)); } }); MockPortletRequest request = new MockPortletRequest(); request.addParameter("spouse", "test"); request.addParameter("spouse.age", "32"); binder.bind(request); assertNotNull(bean.getSpouse()); assertEquals("test", bean.getSpouse().getName()); assertEquals(32, bean.getSpouse().getAge()); }
@Test public void testSecureParameters() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, Boolean.TRUE.toString()); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "true"); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "TRUE"); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "yes"); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, null); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "false"); Assert.assertFalse(href.isRequestedSecure()); }
@Test public void testAddParameters() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter(PortletHref.PARAM_MODE, "help"); href.addParameter(PortletHref.PARAM_STATE, "maximized"); href.addParameter(PortletHref.PARAM_SECURE, "true"); href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION); href.addParameter("SINGLE_PARAM", "VAL1"); href.addParameter("INT_PARAM", 31337); Assert.assertTrue(href.isRequestedSecure()); Assert.assertEquals(new PortletMode("help"), href.getRequestedMode()); Assert.assertEquals(new WindowState("maximized"), href.getRequestedState()); Assert.assertTrue(href.isAction()); final Map<String, String[]> actualParams = href.getParameterMap(); Assert.assertEquals(2, actualParams.size()); Assert.assertArrayEquals(new String[]{"VAL1"}, actualParams.get("SINGLE_PARAM")); Assert.assertArrayEquals(new String[]{"31337"}, actualParams.get("INT_PARAM")); }
@Test public void testRemoveParameter() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter("SINGLE_PARAM", "VAL1"); final Map<String, String[]> actualParams = href.getParameterMap(); Assert.assertEquals(1, actualParams.size()); Assert.assertArrayEquals(new String[]{"VAL1"}, actualParams.get("SINGLE_PARAM")); href.removeParameter("SINGLE_PARAM"); final Map<String, String[]> actualParams2 = href.getParameterMap(); Assert.assertEquals(0, actualParams2.size()); Assert.assertNull(actualParams2.get("SINGLE_PARAM")); }
@Test public void testBaseUrl() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter(PortletHref.PARAM_MODE, "help"); href.addParameter(PortletHref.PARAM_STATE, "maximized"); href.addParameter(PortletHref.PARAM_SECURE, "true"); href.addParameter("SINGLE_PARAM", "VAL1"); final String baseRenderUrl = href.getBaseUrl(); Assert.assertEquals("http://localhost/mockportlet?urlType=render", baseRenderUrl); href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION); final String baseActionUrl = href.getBaseUrl(); Assert.assertEquals("http://localhost/mockportlet?urlType=action", baseActionUrl); }
@Test public void testCreateEmptyHref() { final MockPageContext pageContext = new MockPageContext(); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest()); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); final PortletRequestHelper helper = new PortletRequestHelper(pageContext); final PortletHref ref = (PortletHref) helper.getHref(); final Map<String, String[]> params = ref.getParameterMap(); Assert.assertEquals(0, params.size()); Assert.assertNull(ref.getAnchor()); Assert.assertNull(ref.getRequestedMode()); Assert.assertNull(ref.getRequestedState()); Assert.assertFalse(ref.isRequestedSecure()); }
@Test public void testCreateSecureHref() { final MockPageContext pageContext = new MockPageContext(); final MockPortletRequest request = new MockPortletRequest(); request.setSecure(true); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); final PortletRequestHelper helper = new PortletRequestHelper(pageContext); final PortletHref ref = (PortletHref) helper.getHref(); final Map<String, String[]> params = ref.getParameterMap(); Assert.assertEquals(0, params.size()); Assert.assertNull(ref.getAnchor()); Assert.assertNull(ref.getRequestedMode()); Assert.assertNull(ref.getRequestedState()); Assert.assertTrue(ref.isRequestedSecure()); }
@Test public void testLocale() { MockPortletRequest portletRequest = new MockPortletRequest(); portletRequest.addPreferredLocale(Locale.UK); PortletWebRequest request = new PortletWebRequest(portletRequest); assertEquals(Locale.UK, request.getLocale()); }
@Test public void testUpdateAccessedAttributes() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION); assertSame(VALUE, value); attrs.requestCompleted(); }
@Test public void testSetRequestScopedAttribute() throws Exception { MockPortletRequest request = new MockPortletRequest(); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST); Object value = request.getAttribute(KEY); assertSame(VALUE, value); }
@Test public void testSetRequestScopedAttributeAfterCompletion() throws Exception { MockPortletRequest request = new MockPortletRequest(); PortletRequestAttributes attrs = new PortletRequestAttributes(request); request.close(); try { attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Test public void testSetSessionScopedAttribute() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION); Object value = session.getAttribute(KEY); assertSame(VALUE, value); }
@Test public void testSetSessionScopedAttributeAfterCompletion() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.requestCompleted(); request.close(); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION); Object value = session.getAttribute(KEY); assertSame(VALUE, value); }
@Test public void testSetGlobalSessionScopedAttribute() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION); Object value = session.getAttribute(KEY); assertSame(VALUE, value); }
@Test public void testSetGlobalSessionScopedAttributeAfterCompletion() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.requestCompleted(); request.close(); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION); Object value = session.getAttribute(KEY); assertSame(VALUE, value); }
@Test public void testRemoveSessionScopedAttribute() throws Exception { MockPortletSession session = new MockPortletSession(); session.setAttribute(KEY, VALUE); MockPortletRequest request = new MockPortletRequest(); request.setSession(session); PortletRequestAttributes attrs = new PortletRequestAttributes(request); attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION); Object value = session.getAttribute(KEY); assertNull(value); }
@Test public void portletModeViewWithParameter() throws Exception { MockPortletRequest addRequest = request; addRequest.setPortletMode(PortletMode.VIEW); addRequest.setParameter("action", "add"); Object addHandler = hm.getHandler(addRequest).getHandler(); assertEquals(pac.getBean("addItemHandler"), addHandler); MockPortletRequest removeRequest = new MockPortletRequest(); removeRequest.setPortletMode(PortletMode.VIEW); removeRequest.setParameter("action", "remove"); Object removeHandler = hm.getHandler(removeRequest).getHandler(); assertEquals(pac.getBean("removeItemHandler"), removeHandler); }
@Test public void parameterMapping() throws Exception { MockPortletRequest addRequest = request; addRequest.addParameter("action", "add"); Object addHandler = hm.getHandler(addRequest).getHandler(); assertEquals(pac.getBean("addItemHandler"), addHandler); MockPortletRequest removeRequest = new MockPortletRequest(); removeRequest.addParameter("action", "remove"); Object removeHandler = hm.getHandler(removeRequest).getHandler(); assertEquals(pac.getBean("removeItemHandler"), removeHandler); }
@Test public void testGetParametersStartingWithYieldsEverythingIfTargetPrefixIsNull() throws Exception { MockPortletRequest request = new MockPortletRequest(); request.setParameter("William", "Baskerville"); request.setParameter("Adso", "Melk"); request.setParameter("dominican_Bernard", "Gui"); Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, null); assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters); assertEquals("Obviously not finding all of the correct parameters", request.getParameterMap().size(), actualParameters.size()); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("Adso")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("dominican_Bernard")); }
@Test public void testGetParametersStartingWithYieldsEverythingIfTargetPrefixIsTheEmptyString() throws Exception { MockPortletRequest request = new MockPortletRequest(); request.setParameter("William", "Baskerville"); request.setParameter("Adso", "Melk"); request.setParameter("dominican_Bernard", "Gui"); Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, ""); assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters); assertEquals("Obviously not finding all of the correct parameters", request.getParameterMap().size(), actualParameters.size()); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("Adso")); assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("dominican_Bernard")); }
@Test public void testGetParametersStartingWithYieldsEmptyNonNullMapWhenNoParamaterExistInRequest() throws Exception { MockPortletRequest request = new MockPortletRequest(); Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, null); assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters); assertEquals("Obviously finding some parameters from somewhere (incorrectly)", request.getParameterMap().size(), actualParameters.size()); }
@Test public void testGetSubmitParameterWithStraightNameMatch() throws Exception { final String targetSubmitParameter = "William"; MockPortletRequest request = new MockPortletRequest(); request.setParameter(targetSubmitParameter, "Baskerville"); request.setParameter("Adso", "Melk"); request.setParameter("dominican_Bernard", "Gui"); String submitParameter = PortletUtils.getSubmitParameter(request, targetSubmitParameter); assertNotNull(submitParameter); assertEquals(targetSubmitParameter, submitParameter); }
@Test public void testGetSubmitParameterWithPrefixedParameterMatch() throws Exception { final String bareParameterName = "William"; final String targetParameterName = bareParameterName + WebUtils.SUBMIT_IMAGE_SUFFIXES[0]; MockPortletRequest request = new MockPortletRequest(); request.setParameter(targetParameterName, "Baskerville"); request.setParameter("Adso", "Melk"); String submitParameter = PortletUtils.getSubmitParameter(request, bareParameterName); assertNotNull(submitParameter); assertEquals(targetParameterName, submitParameter); }