    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.mock;
    17
    18  import static org.seasar.cubby.action.RequestParameterBindingType.NONE;
    19  import static org.seasar.cubby.internal.util.LogMessages.format;
    20
    21  import java.lang.reflect.Method;
    22  import java.util.HashMap;
    23  import java.util.Map;
    24
    25  import org.seasar.cubby.action.ActionContext;
    26  import org.seasar.cubby.action.ActionErrors;
    27  import org.seasar.cubby.action.ActionException;
    28  import org.seasar.cubby.action.Form;
    29  import org.seasar.cubby.action.RequestParameterBindingType;
    30  import org.seasar.cubby.action.impl.ActionErrorsImpl;
    31  import org.seasar.cubby.internal.controller.ActionResultWrapper;
    32  import org.seasar.cubby.spi.beans.Attribute;
    33  import org.seasar.cubby.spi.beans.BeanDesc;
    34  import org.seasar.cubby.spi.beans.BeanDescFactory;
    35
    36  public class MockActionContext implements ActionContext {
    37
    38  	private Object action;
    39
    40  	private Class<?> actionClass;
    41
    42  	private Method actionMethod;
    43
    44  	private ActionErrors actionErrors = new ActionErrorsImpl();
    45
    46  	private Map<String, Object> flashMap = new HashMap<String, Object>();
    47
    48  	public MockActionContext(Object action, Class<?> actionClass,
    49  			Method actionMethod) {
    50  		this.initialize(action, actionClass, actionMethod,
    51  				new ActionErrorsImpl(), new HashMap<String, Object>());
    52  	}
    53
    54  	public MockActionContext() {
    55  	}
    56
    57  	public void initialize(Object action, Class<?> actionClass,
    58  			Method actionMethod, ActionErrors actionErrors,
    59  			Map<String, Object> flashMap) {
    60  		this.action = action;
    61  		this.actionClass = actionClass;
    62  		this.actionMethod = actionMethod;
    63  		this.actionErrors = actionErrors;
    64  		this.flashMap = flashMap;
    65  	}
    66
    67  	public Object getAction() {
    68  		return action;
    69  	}
    70
    71  	public Class<?> getActionClass() {
    72  		return actionClass;
    73  	}
    74
    75  	public Method getActionMethod() {
    76  		return actionMethod;
    77  	}
    78
    79  	/**
    80  	 * {@inheritDoc}
    81  	 */
    82  	public Object getFormBean() {
    83  		final Form form = getForm();
    84  		if (form == null) {
    85  			return action;
    86  		}
    87  		if (form.bindingType() == NONE) {
    88  			return null;
    89  		}
    90  		if (Form.THIS.equals(form.value())) {
    91  			return action;
    92  		}
    93
    94  		final String propertyName = form.value();
    95  		final BeanDesc beanDesc = BeanDescFactory
    96  				.getBeanDesc(action.getClass());
    97  		final Attribute attribute = beanDesc.getPropertyAttribute(propertyName);
    98  		final Object formBean = attribute.getValue(action);
    99  		if (formBean == null) {
    100 			throw new ActionException(format("ECUB0102", propertyName));
    101 		}
    102 		return formBean;
    103 	}
    104
    105 	private Form getForm() {
    106 		final Form form;
    107 		if (actionMethod.isAnnotationPresent(Form.class)) {
    108 			form = actionMethod.getAnnotation(Form.class);
    109 		} else {
    110 			form = actionClass.getAnnotation(Form.class);
    111 		}
    112 		return form;
    113 	}
    114
    115 	public boolean isBindRequestParameterToAllProperties() {
    116 		final Form form = this.getForm();
    117 		if (form == null) {
    118 			return false;
    119 		}
    120
    121 		final RequestParameterBindingType type = form.bindingType();
    122 		switch (type) {
    123 		case ALL_PROPERTIES:
    124 			return true;
    125 		case ONLY_SPECIFIED_PROPERTIES:
    126 			return false;
    127 		default:
    128 			throw new IllegalStateException(type.toString());
    129 		}
    130 	}
    131
    132 	/**
    133 	 * {@inheritDoc}
    134 	 */
    135 	public ActionResultWrapper invoke() throws Exception {
    136 		System.out.println("invoke");
    137 		return null;
    138 	}
    139
    140 	public void invokeInitializeMethod() {
    141 		this.initialized = true;
    142 	}
    143
    144 	public void invokePreRenderMethod() {
    145 		this.prerendered = true;
    146 	}
    147
    148 	public void invokePostRenderMethod() {
    149 		this.postrendered = true;
    150 	}
    151
    152 	public void clearFlash() {
    153 		flashMap.clear();
    154 	}
    155
    156 	private boolean initialized = false;
    157 	private boolean prerendered = false;
    158 	private boolean postrendered = false;
    159
    160 	public boolean isInitialized() {
    161 		return initialized;
    162 	}
    163
    164 	public boolean isPrerendered() {
    165 		return prerendered;
    166 	}
    167
    168 	public boolean isPostrendered() {
    169 		return postrendered;
    170 	}
    171
    172 	public ActionErrors getActionErrors() {
    173 		return actionErrors;
    174 	}
    175
    176 	public Map<String, Object> getFlashMap() {
    177 		return flashMap;
    178 	}
    179
    180 }