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

Struts example source code file (ValidationInterceptorPrefixMethodInvocationTest.java)

This example Struts source code file (ValidationInterceptorPrefixMethodInvocationTest.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, exception, exception, hashmap, ianswer, ianswer, objectfactory, string, string, throwable, util, validateaction, validationinterceptor, validationinterceptor

The Struts ValidationInterceptorPrefixMethodInvocationTest.java source code

/*
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * 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 com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.validator.ValidationInterceptor;
import org.easymock.MockControl;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.easymock.IMocksControl;

import java.util.HashMap;

/**
 * Test ValidationInterceptor's prefix method invocation capabilities.
 * 
 * @author tm_jee
 * @version $Date: 2009-12-27 19:18:29 +0100 (Sun, 27 Dec 2009) $ $Id: ValidationInterceptorPrefixMethodInvocationTest.java 894090 2009-12-27 18:18:29Z martinc $
 */
public class ValidationInterceptorPrefixMethodInvocationTest extends XWorkTestCase {
    private ActionInvocation invocation;
    private ActionConfig config;
    private ActionProxy proxy;
    private ValidateAction action;
    private String result;
    private String method;

    public void testPrefixMethodInvocation1() throws Exception {
		method = "save";
		result = Action.INPUT;
		
		ValidationInterceptor interceptor = create();
		String result = interceptor.intercept(invocation);
		
		assertEquals(Action.INPUT, result);
	}
	
	public void testPrefixMethodInvocation2() throws Exception {
		method = "save";
		result = "okok";

		ValidationInterceptor interceptor = create();
		String result = interceptor.intercept(invocation);
		
		assertEquals("okok", result);
	}
	
	protected ValidationInterceptor create() {
	    ObjectFactory objectFactory = container.getInstance(ObjectFactory.class);
	    return (ValidationInterceptor) objectFactory.buildInterceptor(
                new InterceptorConfig.Builder("model", ValidationInterceptor.class.getName()).build(), new HashMap());
	}
	
	private interface ValidateAction extends Action, Validateable, ValidationAware {
		void validateDoSave();
		void validateSubmit();
		String submit();
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        config = new ActionConfig.Builder("", "action", "").build();
        invocation = EasyMock.createNiceMock(ActionInvocation.class);
        proxy = EasyMock.createNiceMock(ActionProxy.class);
        action = EasyMock.createNiceMock(ValidateAction.class);


        EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes();
        EasyMock.expect(invocation.getAction()).andReturn(action).anyTimes();
        EasyMock.expect(invocation.invoke()).andAnswer(new IAnswer<String>() {
            public String answer() throws Throwable {
                return result;
            }
        }).anyTimes();

        EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
        EasyMock.expect(proxy.getMethod()).andAnswer(new IAnswer<String>() {
            public String answer() throws Throwable {
                return method;
            }
        }).anyTimes();


        EasyMock.replay(invocation);
        EasyMock.replay(action);
        EasyMock.replay(proxy);

        ActionContext contex = new ActionContext(new HashMap<String, Object>());
        ActionContext.setContext(contex);
        contex.setActionInvocation(invocation);
    }
}

Other Struts examples (source code examples)

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