⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 portletutilstests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.portlet.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
import org.springframework.mock.web.portlet.MockActionRequest;
import org.springframework.mock.web.portlet.MockActionResponse;
import org.springframework.mock.web.portlet.MockPortletContext;
import org.springframework.mock.web.portlet.MockPortletRequest;
import org.springframework.mock.web.portlet.MockPortletSession;
import org.springframework.test.AssertThrows;
import org.springframework.web.util.WebUtils;

/**
 * @author Rick Evans
 */
public final class PortletUtilsTests extends TestCase {

	public void testGetTempDirWithNullPortletContext() throws Exception {
		new AssertThrows(IllegalArgumentException.class, "null PortletContext passed as argument") {
			public void test() throws Exception {
				PortletUtils.getTempDir(null);
			}
		}.runTest();
	}

	public void testGetTempDirSunnyDay() throws Exception {
		MockPortletContext ctx = new MockPortletContext();
		Object expectedTempDir = new File("doesn't exist but that's ok in the context of this test");
		ctx.setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, expectedTempDir);
		assertSame(expectedTempDir, PortletUtils.getTempDir(ctx));
	}

	public void testGetRealPathInterpretsLocationAsRelativeToWebAppRootIfPathDoesNotBeginWithALeadingSlash() throws Exception {
		final String originalPath = "web/foo";
		final String expectedRealPath = "/" + originalPath;
		new AbstractScalarMockTemplate(PortletContext.class) {
			public void setupExpectations(MockControl mockControl, Object mockObject) throws Exception {
				PortletContext ctx = (PortletContext) mockObject;
				ctx.getRealPath(expectedRealPath);
				mockControl.setReturnValue(expectedRealPath);
			}
			public void doTest(Object mockObject) throws Exception {
				PortletContext ctx = (PortletContext) mockObject;
				String actualRealPath = PortletUtils.getRealPath(ctx, originalPath);
				assertEquals(expectedRealPath, actualRealPath);
			}
		}.test();
	}

	public void testGetRealPathWithNullPortletContext() throws Exception {
		new AssertThrows(IllegalArgumentException.class) {
			public void test() throws Exception {
				PortletUtils.getRealPath(null, "/foo");
			}
		}.runTest();
	}

	public void testGetRealPathWithNullPath() throws Exception {
		new AssertThrows(NullPointerException.class) {
			public void test() throws Exception {
				PortletUtils.getRealPath(new MockPortletContext(), null);
			}
		}.runTest();
	}

	public void testGetRealPathWithPathThatCannotBeResolvedToFile() throws Exception {
		new AssertThrows(FileNotFoundException.class) {
			public void test() throws Exception {
				PortletUtils.getRealPath(new MockPortletContext() {
					public String getRealPath(String path) {
						return null;
					}
				}, "/rubbish");
			}
		}.runTest();
	}

	public void testPassAllParametersToRenderPhase() throws Exception {
		MockActionRequest request = new MockActionRequest();
		request.setParameter("William", "Baskerville");
		request.setParameter("Adso", "Melk");
		MockActionResponse response = new MockActionResponse();
		PortletUtils.passAllParametersToRenderPhase(request, response);
		assertEquals("The render parameters map is obviously not being populated with the request parameters.",
				request.getParameterMap().size(), response.getRenderParameterMap().size());
	}

	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));
	}

	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));
	}

	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"));
	}

	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"));
	}

	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());
	}

	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);
	}

	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);
	}

	public void testGetSubmitParameterWithNoParameterMatchJustReturnsNull() throws Exception {
		MockPortletRequest request = new MockPortletRequest();
		request.setParameter("Bill", "Baskerville");
		request.setParameter("Adso", "Melk");
		String submitParameter = PortletUtils.getSubmitParameter(request, "William");
		assertNull(submitParameter);
	}

	public void testGetSubmitParameterWithNullRequest() throws Exception {
		new AssertThrows(IllegalArgumentException.class) {
			public void test() throws Exception {
				final String targetSubmitParameter = "William";
				MockPortletRequest request = new MockPortletRequest();
				request.setParameter(targetSubmitParameter, "Baskerville");
				request.setParameter("Adso", "Melk");
				PortletUtils.getSubmitParameter(null, targetSubmitParameter);
			}
		}.runTest();
	}

	public void testPassAllParametersToRenderPhaseDoesNotPropagateExceptionIfRedirectAlreadySentAtTimeOfCall() throws Exception {
		MockActionRequest request = new MockActionRequest();
		request.setParameter("William", "Baskerville");
		request.setParameter("Adso", "Melk");
		MockActionResponse response = new MockActionResponse() {
			public void setRenderParameter(String key, String[] values) {
				throw new IllegalStateException();
			}
		};
		PortletUtils.passAllParametersToRenderPhase(request, response);
		assertEquals("The render parameters map must not be being populated with the request parameters (Action.sendRedirect(..) aleady called).",
				0, response.getRenderParameterMap().size());
	}

	public void testClearAllRenderParameters() throws Exception {
		MockActionResponse response = new MockActionResponse();
		response.setRenderParameter("William", "Baskerville");
		response.setRenderParameter("Adso", "Melk");
		PortletUtils.clearAllRenderParameters(response);
		assertEquals("The render parameters map is obviously not being cleared out.",
				0, response.getRenderParameterMap().size());
	}

	public void testClearAllRenderParametersDoesNotPropagateExceptionIfRedirectAlreadySentAtTimeOfCall() throws Exception {
		MockActionResponse response = new MockActionResponse() {
			public void setRenderParameters(Map parameters) {
				throw new IllegalStateException();
			}
		};
		response.setRenderParameter("William", "Baskerville");
		response.setRenderParameter("Adso", "Melk");
		PortletUtils.clearAllRenderParameters(response);
		assertEquals("The render parameters map must not be cleared if ActionResponse.sendRedirect() has been called (already).",
				2, response.getRenderParameterMap().size());
	}

	public void testHasSubmitParameterWithStraightNameMatch() throws Exception {
		final String targetSubmitParameter = "William";
		MockPortletRequest request = new MockPortletRequest();
		request.setParameter(targetSubmitParameter, "Baskerville");
		request.setParameter("Adso", "Melk");
		request.setParameter("dominican_Bernard", "Gui");
		assertTrue(PortletUtils.hasSubmitParameter(request, targetSubmitParameter));
	}

	public void testHasSubmitParameterWithPrefixedParameterMatch() 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");
		assertTrue(PortletUtils.hasSubmitParameter(request, bareParameterName));
	}

	public void testHasSubmitParameterWithNoParameterMatch() throws Exception {
		MockPortletRequest request = new MockPortletRequest();
		request.setParameter("Bill", "Baskerville");
		request.setParameter("Adso", "Melk");
		assertFalse(PortletUtils.hasSubmitParameter(request, "William"));
	}

	public void testHasSubmitParameterWithNullRequest() throws Exception {
		new AssertThrows(IllegalArgumentException.class) {
			public void test() throws Exception {
				PortletUtils.hasSubmitParameter(null, "bingo");
			}
		}.runTest();
	}

	public void testExposeRequestAttributesWithNullRequest() throws Exception {
		new AssertThrows(IllegalArgumentException.class) {
			public void test() throws Exception {
				PortletUtils.exposeRequestAttributes(null, Collections.EMPTY_MAP);
			}
		}.runTest();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -