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