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

Spring Framework example source code file (GenericPortletBeanTests.java)

This example Spring Framework source code file (GenericPortletBeanTests.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, genericportletbean, genericportletbeantests, mockportletconfig, mockportletconfig, mockportletcontext, portletcontext, portletexception, string, string, testcase, testportletbean, testportletbean

The Spring Framework GenericPortletBeanTests.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;

import javax.portlet.PortletContext;
import javax.portlet.PortletException;

import junit.framework.TestCase;

import org.springframework.mock.web.portlet.MockPortletConfig;
import org.springframework.mock.web.portlet.MockPortletContext;

/**
 * @author Mark Fisher
 */
public class GenericPortletBeanTests extends TestCase {

	public void testInitParameterSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testValue = "testValue";
		portletConfig.addInitParameter("testParam", testValue);
		TestPortletBean portletBean = new TestPortletBean();
		assertNull(portletBean.getTestParam());
		portletBean.init(portletConfig);
		assertNotNull(portletBean.getTestParam());
		assertEquals(testValue, portletBean.getTestParam());
	}
	
	public void testInitParameterNotSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		TestPortletBean portletBean = new TestPortletBean();
		assertNull(portletBean.getTestParam());
		portletBean.init(portletConfig);
		assertNull(portletBean.getTestParam());
	}

	public void testMultipleInitParametersSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testValue = "testValue";
		String anotherValue = "anotherValue";
		portletConfig.addInitParameter("testParam", testValue);
		portletConfig.addInitParameter("anotherParam", anotherValue);
		portletConfig.addInitParameter("unknownParam", "unknownValue");
		TestPortletBean portletBean = new TestPortletBean();
		assertNull(portletBean.getTestParam());
		assertNull(portletBean.getAnotherParam());
		portletBean.init(portletConfig);
		assertNotNull(portletBean.getTestParam());
		assertNotNull(portletBean.getAnotherParam());
		assertEquals(testValue, portletBean.getTestParam());
		assertEquals(anotherValue, portletBean.getAnotherParam());
	}

	public void testMultipleInitParametersOnlyOneSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testValue = "testValue";
		portletConfig.addInitParameter("testParam", testValue);
		portletConfig.addInitParameter("unknownParam", "unknownValue");
		TestPortletBean portletBean = new TestPortletBean();
		assertNull(portletBean.getTestParam());
		assertNull(portletBean.getAnotherParam());
		portletBean.init(portletConfig);
		assertNotNull(portletBean.getTestParam());
		assertEquals(testValue, portletBean.getTestParam());
		assertNull(portletBean.getAnotherParam());
	}

	public void testRequiredInitParameterSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testParam = "testParam";
		String testValue = "testValue";
		portletConfig.addInitParameter(testParam, testValue);
		TestPortletBean portletBean = new TestPortletBean();
		portletBean.addRequiredProperty(testParam);
		assertNull(portletBean.getTestParam());
		portletBean.init(portletConfig);
		assertNotNull(portletBean.getTestParam());
		assertEquals(testValue, portletBean.getTestParam());
	}

	public void testRequiredInitParameterNotSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testParam = "testParam";
		TestPortletBean portletBean = new TestPortletBean();
		portletBean.addRequiredProperty(testParam);
		assertNull(portletBean.getTestParam());
		try {
			portletBean.init(portletConfig);
			fail("should have thrown PortletException");
		}
		catch (PortletException ex) {
			// expected
		}
	}
	
	public void testRequiredInitParameterNotSetOtherParameterNotSet() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testParam = "testParam";
		String testValue = "testValue";
		portletConfig.addInitParameter(testParam, testValue);
		TestPortletBean portletBean = new TestPortletBean();
		portletBean.addRequiredProperty("anotherParam");
		assertNull(portletBean.getTestParam());
		try {
			portletBean.init(portletConfig);
			fail("should have thrown PortletException");
		}
		catch (PortletException ex) {
			// expected
		}
		assertNull(portletBean.getTestParam());
	}

	public void testUnknownRequiredInitParameter() throws Exception {
		PortletContext portletContext = new MockPortletContext();
		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
		String testParam = "testParam";
		String testValue = "testValue";
		portletConfig.addInitParameter(testParam, testValue);
		TestPortletBean portletBean = new TestPortletBean();
		portletBean.addRequiredProperty("unknownParam");
		assertNull(portletBean.getTestParam());
		try {
			portletBean.init(portletConfig);
			fail("should have thrown PortletException");
		}
		catch (PortletException ex) {
			// expected
		}
		assertNull(portletBean.getTestParam());
	}


	private static class TestPortletBean extends GenericPortletBean {
		
		private String testParam; 
		private String anotherParam;
		
		public void setTestParam(String value) {
			this.testParam = value;
		}
		
		public String getTestParam() {
			return this.testParam;
		}
		
		public void setAnotherParam(String value) {
			this.anotherParam = value;
		}
		
		public String getAnotherParam() {
			return this.anotherParam;
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework GenericPortletBeanTests.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.