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

Struts example source code file (AnnotationParameterFilterUnitTest.java)

This example Struts source code file (AnnotationParameterFilterUnitTest.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 - Struts tags/keywords

actioncontext, actioninvocation, actioninvocation, annotationparameterfilterintereptor, annotationparameterfilterintereptor, baker, exception, hashmap, hashmap, map, map, martin, mock, paramter, util

The Struts AnnotationParameterFilterUnitTest.java source code

package com.opensymphony.xwork2.interceptor.annotations;

import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.StubValueStack;
import com.opensymphony.xwork2.util.ValueStack;
import junit.framework.TestCase;

import java.util.HashMap;
import java.util.Map;

/**
 * @author martin.gilday
 * @author jafl
 *
 */
public class AnnotationParameterFilterUnitTest extends TestCase {

	ValueStack stack;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		stack = new StubValueStack();
	}

	/**
	 * Only "name" should remain in the parameter map.  All others
	 * should be removed
	 * @throws Exception
	 */
	public void testBlockingByDefault() throws Exception {
		
		Map contextMap = new HashMap();
		Map parameterMap = new HashMap();
		
		parameterMap.put("job", "Baker");
		parameterMap.put("name", "Martin");
		
		contextMap.put(ActionContext.PARAMETERS, parameterMap);
		
		Action action = new BlockingByDefaultAction();
		stack.push(action);
		
		Mock mockInvocation = new Mock(ActionInvocation.class);
		mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap));
		mockInvocation.matchAndReturn("getAction", action);
		mockInvocation.matchAndReturn("getStack", stack);
		mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
		
		ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy();
		
		AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor();
		intereptor.intercept(invocation);
		
		assertEquals("Paramter map should contain one entry", 1, parameterMap.size());
		assertNull(parameterMap.get("job"));
		assertNotNull(parameterMap.get("name"));
		
	}

	/**
	 * "name" should be removed from the map, as it is blocked.
	 * All other parameters should remain
	 * @throws Exception
	 */
	public void testAllowingByDefault() throws Exception {
		
		Map contextMap = new HashMap();
		Map parameterMap = new HashMap();
		
		parameterMap.put("job", "Baker");
		parameterMap.put("name", "Martin");
		
		contextMap.put(ActionContext.PARAMETERS, parameterMap);
		
		Action action = new AllowingByDefaultAction();
		stack.push(action);
		
		Mock mockInvocation = new Mock(ActionInvocation.class);
		mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap));
		mockInvocation.matchAndReturn("getAction", action);
		mockInvocation.matchAndReturn("getStack", stack);
		mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
		
		ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy();
		
		AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor();
		intereptor.intercept(invocation);
		
		assertEquals("Paramter map should contain one entry", 1, parameterMap.size());
		assertNotNull(parameterMap.get("job"));
		assertNull(parameterMap.get("name"));
		
	}

	/**
	 * Only "name" should remain in the parameter map.  All others
	 * should be removed
	 * @throws Exception
	 */
	public void testBlockingByDefaultWithModel() throws Exception {
		
		Map contextMap = new HashMap();
		Map parameterMap = new HashMap();
		
		parameterMap.put("job", "Baker");
		parameterMap.put("name", "Martin");
		parameterMap.put("m1", "s1");
		parameterMap.put("m2", "s2");
		
		contextMap.put(ActionContext.PARAMETERS, parameterMap);
		stack.push(new BlockingByDefaultModel());
		
		Mock mockInvocation = new Mock(ActionInvocation.class);
		mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap));
		mockInvocation.matchAndReturn("getAction", new BlockingByDefaultAction());
		mockInvocation.matchAndReturn("getStack", stack);
		mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
		
		ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy();
		
		AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor();
		intereptor.intercept(invocation);
		
		assertEquals("Paramter map should contain two entries", 2, parameterMap.size());
		assertNull(parameterMap.get("job"));
		assertNotNull(parameterMap.get("name"));
		assertNotNull(parameterMap.get("m1"));
		assertNull(parameterMap.get("m2"));
		
	}

	/**
	 * "name" should be removed from the map, as it is blocked.
	 * All other parameters should remain
	 * @throws Exception
	 */
	public void testAllowingByDefaultWithModel() throws Exception {
		
		Map contextMap = new HashMap();
		Map parameterMap = new HashMap();
		
		parameterMap.put("job", "Baker");
		parameterMap.put("name", "Martin");
		parameterMap.put("m1", "s1");
		parameterMap.put("m2", "s2");
		
		contextMap.put(ActionContext.PARAMETERS, parameterMap);
		stack.push(new AllowingByDefaultModel());
		
		Mock mockInvocation = new Mock(ActionInvocation.class);
		mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap));
		mockInvocation.matchAndReturn("getAction", new AllowingByDefaultAction());
		mockInvocation.matchAndReturn("getStack", stack);
		mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
		
		ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy();
		
		AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor();
		intereptor.intercept(invocation);
		
		assertEquals("Paramter map should contain two entries", 2, parameterMap.size());
		assertNotNull(parameterMap.get("job"));
		assertNull(parameterMap.get("name"));
		assertNull(parameterMap.get("m1"));
		assertNotNull(parameterMap.get("m2"));
		
	}
	
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts AnnotationParameterFilterUnitTest.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.