View Javadoc

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