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.Action;
26  import org.seasar.cubby.action.ActionContext;
27  import org.seasar.cubby.action.ActionErrors;
28  import org.seasar.cubby.action.ActionException;
29  import org.seasar.cubby.action.Form;
30  import org.seasar.cubby.action.RequestParameterBindingType;
31  import org.seasar.cubby.internal.action.impl.ActionErrorsImpl;
32  import org.seasar.cubby.internal.controller.ActionResultWrapper;
33  import org.seasar.cubby.spi.beans.BeanDesc;
34  import org.seasar.cubby.spi.beans.BeanDescFactory;
35  import org.seasar.cubby.spi.beans.PropertyDesc;
36  
37  public class MockActionContext implements ActionContext {
38  
39  	private final Action action;
40  
41  	private final Class<? extends Action> actionClass;
42  
43  	private final Method actionMethod;
44  
45  	private final ActionErrors actionErrors = new ActionErrorsImpl();
46  
47  	private final Map<String, Object> flashMap = new HashMap<String, Object>();
48  
49  	public MockActionContext(Action action,
50  			Class<? extends Action> actionClass, Method actionMethod) {
51  		this.action = action;
52  		this.actionClass = actionClass;
53  		this.actionMethod = actionMethod;
54  	}
55  
56  	public Action getAction() {
57  		return action;
58  	}
59  
60  	public Class<? extends Action> getActionClass() {
61  		return actionClass;
62  	}
63  
64  	public Method getActionMethod() {
65  		return actionMethod;
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public Object getFormBean() {
72  		final Form form = getForm();
73  		if (form == null) {
74  			return action;
75  		}
76  		if (form.bindingType() == NONE) {
77  			return null;
78  		}
79  		if (Form.THIS.equals(form.value())) {
80  			return action;
81  		}
82  
83  		final String propertyName = form.value();
84  		final BeanDesc beanDesc = BeanDescFactory
85  				.getBeanDesc(action.getClass());
86  		final PropertyDesc propertyDesc = beanDesc
87  				.getPropertyDesc(propertyName);
88  		final Object formBean = propertyDesc.getValue(action);
89  		if (formBean == null) {
90  			throw new ActionException(format("ECUB0102", propertyName));
91  		}
92  		return formBean;
93  	}
94  
95  	private Form getForm() {
96  		final Form form;
97  		if (actionMethod.isAnnotationPresent(Form.class)) {
98  			form = actionMethod.getAnnotation(Form.class);
99  		} else {
100 			form = actionClass.getAnnotation(Form.class);
101 		}
102 		return form;
103 	}
104 
105 	public boolean isBindRequestParameterToAllProperties() {
106 		final Form form = this.getForm();
107 		if (form == null) {
108 			return false;
109 		}
110 
111 		final RequestParameterBindingType type = form.bindingType();
112 		switch (type) {
113 		case ALL_PROPERTIES:
114 			return true;
115 		case ONLY_SPECIFIED_PROPERTIES:
116 			return false;
117 		default:
118 			throw new IllegalStateException(type.toString());
119 		}
120 	}
121 
122 	/**
123 	 * {@inheritDoc}
124 	 */
125 	public ActionResultWrapper invoke() throws Exception {
126 		System.out.println("invoke");
127 		return null;
128 	}
129 
130 	public void invokeInitializeMethod() {
131 		this.initialized = true;
132 	}
133 
134 	public void invokePreRenderMethod() {
135 		this.prerendered = true;
136 	}
137 
138 	public void invokePostRenderMethod() {
139 		this.postrendered = true;
140 	}
141 
142 	public void clearFlash() {
143 		flashMap.clear();
144 	}
145 
146 	private boolean initialized = false;
147 	private boolean prerendered = false;
148 	private boolean postrendered = false;
149 
150 	public boolean isInitialized() {
151 		return initialized;
152 	}
153 
154 	public boolean isPrerendered() {
155 		return prerendered;
156 	}
157 
158 	public boolean isPostrendered() {
159 		return postrendered;
160 	}
161 
162 	public ActionErrors getActionErrors() {
163 		return actionErrors;
164 	}
165 
166 	public Map<String, Object> getFlashMap() {
167 		return flashMap;
168 	}
169 
170 }