alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (PortletRequestAttributesTests.java)

This example Spring Framework source code file (PortletRequestAttributesTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

exception, exception, illegalstateexception, io, mockcontrol, mockportletrequest, mockportletrequest, mockportletsession, object, portletrequest, portletrequestattributes, portletrequestattributes, serializable, value, value

The Spring Framework PortletRequestAttributesTests.java source code

/*
 * 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.context;

import java.io.Serializable;

import javax.portlet.PortletRequest;

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

import org.springframework.mock.web.portlet.MockPortletRequest;
import org.springframework.mock.web.portlet.MockPortletSession;
import org.springframework.test.AssertThrows;
import org.springframework.web.context.request.RequestAttributes;

/**
 * @author Rick Evans
 * @author Juergen Hoeller
 */
public final class PortletRequestAttributesTests extends TestCase {

	private static final String KEY = "ThatThingThatThing";


	private static final Serializable VALUE = new Serializable() {
	};


	public void testCtorRejectsNullArg() throws Exception {
		new AssertThrows(IllegalArgumentException.class) {
			public void test() throws Exception {
				new PortletRequestAttributes(null);
			}
		}.runTest();
	}

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

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

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

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

	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);
		request.close();
		attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
		Object value = session.getAttribute(KEY);
		assertSame(VALUE, value);
	}

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

	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);
		request.close();
		attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION);
		Object value = session.getAttribute(KEY);
		assertSame(VALUE, value);
	}

	public void testGetSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
		MockControl mockRequest = MockControl.createControl(PortletRequest.class);
		PortletRequest request = (PortletRequest) mockRequest.getMock();
		request.getPortletSession(false);
		mockRequest.setReturnValue(null, 2);
		mockRequest.replay();

		PortletRequestAttributes attrs = new PortletRequestAttributes(request);
		Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
		assertNull(value);

		mockRequest.verify();
	}

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

	public void testRemoveSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
		MockControl mockRequest = MockControl.createControl(PortletRequest.class);
		PortletRequest request = (PortletRequest) mockRequest.getMock();
		request.getPortletSession(false);
		mockRequest.setReturnValue(null, 2);
		mockRequest.replay();

		PortletRequestAttributes attrs = new PortletRequestAttributes(request);
		attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);

		mockRequest.verify();
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework PortletRequestAttributesTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.