1   /*
2    * Copyright 2004-2008 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.impl;
17  
18  import javax.servlet.http.HttpServletRequest;
19  
20  import org.seasar.cubby.action.ActionErrors;
21  import org.seasar.cubby.action.FieldInfo;
22  import org.seasar.extension.unit.S2TestCase;
23  
24  public class ActionErrorsImplTest extends S2TestCase {
25  
26  	public ActionErrors actionErrors;
27  
28  	@Override
29  	protected void setUp() throws Exception {
30  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
31  	}
32  
33  	public void testRequestAttributes() {
34  		HttpServletRequest request = this.getRequest();
35  		assertSame(actionErrors, request.getAttribute("errors"));
36  	}
37  
38  	public void testIsEmpty1() {
39  		assertTrue(actionErrors.isEmpty());
40  		actionErrors.add("error1");
41  		assertFalse(actionErrors.isEmpty());
42  	}
43  
44  	public void testAdd() {
45  		actionErrors.add("error1");
46  		assertEquals(1, actionErrors.getOthers().size());
47  		assertEquals("error1", actionErrors.getOthers().get(0));
48  		assertEquals(1, actionErrors.getAll().size());
49  		assertEquals("error1", actionErrors.getAll().get(0));
50  
51  		actionErrors.add("error2", new FieldInfo("field1"));
52  		assertFalse(actionErrors.getFields().get("field1").isEmpty());
53  		assertTrue(actionErrors.getFields().get("field2").isEmpty());
54  		assertEquals(1, actionErrors.getFields().get("field1").size());
55  		assertEquals("error2", actionErrors.getFields().get("field1").get(0));
56  		assertEquals(2, actionErrors.getAll().size());
57  		assertEquals("error1", actionErrors.getAll().get(0));
58  		assertEquals("error2", actionErrors.getAll().get(1));
59  
60  		actionErrors.add("error3");
61  		assertEquals(2, actionErrors.getOthers().size());
62  		assertEquals("error1", actionErrors.getOthers().get(0));
63  		assertEquals("error3", actionErrors.getOthers().get(1));
64  		assertEquals(3, actionErrors.getAll().size());
65  		assertEquals("error1", actionErrors.getAll().get(0));
66  		assertEquals("error2", actionErrors.getAll().get(1));
67  		assertEquals("error3", actionErrors.getAll().get(2));
68  
69  		actionErrors.add("error4", new FieldInfo("field1"));
70  		actionErrors.add("error5", new FieldInfo("field2", 0));
71  		assertFalse(actionErrors.getFields().get("field1").isEmpty());
72  		assertFalse(actionErrors.getFields().get("field2").isEmpty());
73  		assertEquals(2, actionErrors.getFields().get("field1").size());
74  		assertEquals("error2", actionErrors.getFields().get("field1").get(0));
75  		assertEquals("error4", actionErrors.getFields().get("field1").get(1));
76  		assertEquals(1, actionErrors.getFields().get("field2").size());
77  		assertEquals("error5", actionErrors.getFields().get("field2").get(0));
78  		assertEquals(1, actionErrors.getIndexedFields().get("field2").get(0).size());
79  		assertTrue(actionErrors.getIndexedFields().get("field2").get(1).isEmpty());
80  		assertEquals("error5", actionErrors.getIndexedFields().get("field2").get(0).get(0));
81  		assertEquals(5, actionErrors.getAll().size());
82  		assertEquals("error1", actionErrors.getAll().get(0));
83  		assertEquals("error2", actionErrors.getAll().get(1));
84  		assertEquals("error3", actionErrors.getAll().get(2));
85  		assertEquals("error4", actionErrors.getAll().get(3));
86  		assertEquals("error5", actionErrors.getAll().get(4));
87  	}
88  }