View Javadoc

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.unit;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import junit.framework.Assert;
23  
24  import org.seasar.cubby.action.ActionResult;
25  import org.seasar.cubby.action.Forward;
26  import org.seasar.cubby.action.Redirect;
27  
28  /**
29   * アクションのテストを行うための検証メソッドの集合です。
30   * 
31   * @author someda
32   * @author baba
33   */
34  public class CubbyAssert {
35  
36  	/**
37  	 * デフォルトの文字エンコーディング。
38  	 */
39  	private static final String DEFAULT_CHARACTER_ENCODING = "UTF-8";
40  
41  	/**
42  	 * 指定された {@link ActionResult} の型とパスを検証します。
43  	 * 
44  	 * @param expectedType
45  	 *            期待する <code>ActionResult</code> の型
46  	 * @param expectedPath
47  	 *            期待する <code>ActionResult</code> のパス
48  	 * @param actual
49  	 *            実際の <code>ActionResult</code>
50  	 */
51  	public static void assertPathEquals(
52  			final Class<? extends ActionResult> expectedType,
53  			final String expectedPath, final ActionResult actual) {
54  		assertPathEquals(expectedType, expectedPath, actual,
55  				DEFAULT_CHARACTER_ENCODING);
56  	}
57  
58  	/**
59  	 * 指定された {@link ActionResult} の型とパスを検証します。
60  	 * 
61  	 * @param message
62  	 *            メッセージ
63  	 * @param expectedType
64  	 *            期待する <code>ActionResult</code> の型
65  	 * @param expectedPath
66  	 *            期待する <code>ActionResult</code> のパス
67  	 * @param actual
68  	 *            実際の <code>ActionResult</code>
69  	 * @since 2.0.2
70  	 */
71  	public static void assertPathEquals(final String message,
72  			final Class<? extends ActionResult> expectedType,
73  			final String expectedPath, final ActionResult actual) {
74  		assertPathEquals(message, expectedType, expectedPath, actual,
75  				DEFAULT_CHARACTER_ENCODING);
76  	}
77  
78  	/**
79  	 * 指定された {@link ActionResult} の型とパスを検証します。
80  	 * 
81  	 * @param expectedType
82  	 *            期待する <code>ActionResult</code> の型
83  	 * @param expectedPath
84  	 *            期待する <code>ActionResult</code> のパス
85  	 * @param actual
86  	 *            実際の <code>ActionResult</code>
87  	 * @param characterEncoding
88  	 *            URI のエンコーディング
89  	 */
90  	public static void assertPathEquals(
91  			final Class<? extends ActionResult> expectedType,
92  			final String expectedPath, final ActionResult actual,
93  			final String characterEncoding) {
94  
95  		assertPathEquals(null, expectedType, expectedPath, actual,
96  				characterEncoding);
97  	}
98  
99  	/**
100 	 * 指定された {@link ActionResult} の型とパスを検証します。
101 	 * 
102 	 * @param message
103 	 *            メッセージ
104 	 * @param expectedType
105 	 *            期待する <code>ActionResult</code> の型
106 	 * @param expectedPath
107 	 *            期待する <code>ActionResult</code> のパス
108 	 * @param actual
109 	 *            実際の <code>ActionResult</code>
110 	 * @param characterEncoding
111 	 *            URI のエンコーディング
112 	 * @since 2.0.2
113 	 */
114 	public static void assertPathEquals(final String message,
115 			final Class<? extends ActionResult> expectedType,
116 			final String expectedPath, final ActionResult actual,
117 			final String characterEncoding) {
118 
119 		final List<ActionResultAssert<?, String>> asserters = new ArrayList<ActionResultAssert<?, String>>();
120 		asserters.add(new ForwardAssert());
121 		asserters.add(new RedirectAssert());
122 		assertActionResult(message, expectedType, actual, asserters,
123 				expectedPath, characterEncoding);
124 	}
125 
126 	protected static <E> void assertActionResult(final String message,
127 			final Class<? extends ActionResult> expectedType,
128 			final ActionResult actualResult,
129 			final List<ActionResultAssert<?, E>> asserters, final E expected,
130 			final Object... args) {
131 		Assert.assertNotNull(message, actualResult);
132 		Assert.assertEquals(message, expectedType, actualResult.getClass());
133 		for (final ActionResultAssert<?, E> asserter : asserters) {
134 			asserter.assertType(message, actualResult, expected, args);
135 		}
136 	}
137 
138 	protected static abstract class ActionResultAssert<T extends ActionResult, E> {
139 
140 		private final Class<T> clazz;
141 
142 		ActionResultAssert(Class<T> clazz) {
143 			this.clazz = clazz;
144 		}
145 
146 		public void assertType(final String message,
147 				final ActionResult actualResult, final E expected,
148 				final Object... args) {
149 			if (clazz.isInstance(actualResult)) {
150 				doAssert(message, clazz.cast(actualResult), expected, args);
151 			}
152 		}
153 
154 		abstract void doAssert(String message, T actualResult, E expected,
155 				Object... args);
156 
157 	}
158 
159 	protected static abstract class PathAssert<T extends ActionResult> extends
160 			ActionResultAssert<T, String> {
161 
162 		PathAssert(final Class<T> clazz) {
163 			super(clazz);
164 		}
165 
166 		protected void doPathAssert(final String message,
167 				final String expectedPath, final String actualPath) {
168 			Assert.assertEquals(message, expectedPath, actualPath);
169 		}
170 	}
171 
172 	private static class ForwardAssert extends PathAssert<Forward> {
173 
174 		protected ForwardAssert() {
175 			super(Forward.class);
176 		}
177 
178 		@Override
179 		void doAssert(final String message, final Forward actualResult,
180 				final String expected, final Object... args) {
181 			doPathAssert(message, expected, actualResult.getPath(args[0]
182 					.toString()));
183 		}
184 
185 	}
186 
187 	private static class RedirectAssert extends PathAssert<Redirect> {
188 
189 		protected RedirectAssert() {
190 			super(Redirect.class);
191 		}
192 
193 		@Override
194 		void doAssert(final String message, final Redirect actualResult,
195 				final String expected, final Object... args) {
196 			doPathAssert(message, expected, actualResult.getPath(args[0]
197 					.toString()));
198 		}
199 
200 	}
201 
202 }