1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.action;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertSame;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.lang.reflect.Method;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.junit.Test;
28  import org.seasar.cubby.mock.MockActionErrors;
29  
30  public class ActionTest {
31  
32  	private final ActionImpl action = new ActionImpl();
33  
34  	@Test
35  	public void errors() {
36  		ActionErrors errors = new MockActionErrors();
37  		action.setErrors(errors);
38  		assertSame(errors, action.errors);
39  		assertSame(errors, action.getErrors());
40  	}
41  
42  	@Test
43  	public void flash() {
44  		Map<String, Object> flash = new HashMap<String, Object>();
45  		action.setFlash(flash);
46  		assertSame(flash, action.flash);
47  		assertSame(flash, action.getFlash());
48  	}
49  
50  	@Test
51  	public void noAnnotationInititalize() throws Exception {
52  		Method method = ActionImpl.class.getMethod("noannotation");
53  		action.invokeInitializeMethod(method);
54  		assertTrue(action.initialized1);
55  		assertFalse(action.initialized2);
56  		assertFalse(action.prerendered1);
57  		assertFalse(action.prerendered2);
58  		assertFalse(action.postrendered1);
59  		assertFalse(action.postrendered2);
60  	}
61  
62  	@Test
63  	public void annotationInititalize() throws Exception {
64  		Method method = ActionImpl.class.getMethod("annotation");
65  		action.invokeInitializeMethod(method);
66  		assertFalse(action.initialized1);
67  		assertTrue(action.initialized2);
68  		assertFalse(action.prerendered1);
69  		assertFalse(action.prerendered2);
70  		assertFalse(action.postrendered1);
71  		assertFalse(action.postrendered2);
72  	}
73  
74  	@Test
75  	public void noAnnotationPrerender() throws Exception {
76  		Method method = ActionImpl.class.getMethod("noannotation");
77  		action.invokePreRenderMethod(method);
78  		assertFalse(action.initialized1);
79  		assertFalse(action.initialized2);
80  		assertTrue(action.prerendered1);
81  		assertFalse(action.prerendered2);
82  		assertFalse(action.postrendered1);
83  		assertFalse(action.postrendered2);
84  	}
85  
86  	@Test
87  	public void annotationPrerender() throws Exception {
88  		Method method = ActionImpl.class.getMethod("annotation");
89  		action.invokePreRenderMethod(method);
90  		assertFalse(action.initialized1);
91  		assertFalse(action.initialized2);
92  		assertFalse(action.prerendered1);
93  		assertTrue(action.prerendered2);
94  		assertFalse(action.postrendered1);
95  		assertFalse(action.postrendered2);
96  	}
97  
98  	@Test
99  	public void noAnnotationPostrender() throws Exception {
100 		Method method = ActionImpl.class.getMethod("noannotation");
101 		action.invokePostRenderMethod(method);
102 		assertFalse(action.initialized1);
103 		assertFalse(action.initialized2);
104 		assertFalse(action.prerendered1);
105 		assertFalse(action.prerendered2);
106 		assertTrue(action.postrendered1);
107 		assertFalse(action.postrendered2);
108 	}
109 
110 	@Test
111 	public void annotationPostrender() throws Exception {
112 		Method method = ActionImpl.class.getMethod("annotation");
113 		action.invokePostRenderMethod(method);
114 		assertFalse(action.initialized1);
115 		assertFalse(action.initialized2);
116 		assertFalse(action.prerendered1);
117 		assertFalse(action.prerendered2);
118 		assertFalse(action.postrendered1);
119 		assertTrue(action.postrendered2);
120 	}
121 
122 	public static class ActionImpl extends Action {
123 
124 		boolean initialized1 = false;
125 		boolean initialized2 = false;
126 		boolean prerendered1 = false;
127 		boolean prerendered2 = false;
128 		boolean postrendered1 = false;
129 		boolean postrendered2 = false;
130 
131 		public ActionResult noannotation() {
132 			return null;
133 		}
134 
135 		@InitializeMethod("initialize2")
136 		@PreRenderMethod("prerender2")
137 		@PostRenderMethod("postrender2")
138 		public ActionResult annotation() {
139 			return null;
140 		}
141 
142 		@Override
143 		protected void initialize() {
144 			super.initialize();
145 			initialized1 = true;
146 		}
147 
148 		public void initialize2() {
149 			initialized2 = true;
150 		}
151 
152 		@Override
153 		protected void prerender() {
154 			super.prerender();
155 			prerendered1 = true;
156 		}
157 
158 		public void prerender2() {
159 			prerendered2 = true;
160 		}
161 
162 		@Override
163 		protected void postrender() {
164 			super.postrender();
165 			postrendered1 = true;
166 		}
167 
168 		public void postrender2() {
169 			postrendered2 = true;
170 		}
171 
172 	}
173 
174 }