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.internal.controller.impl;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.junit.Test;
24  import org.seasar.cubby.action.ActionErrors;
25  import org.seasar.cubby.action.FieldInfo;
26  import org.seasar.cubby.internal.controller.impl.ActionErrorsImpl;
27  
28  public class ActionErrorsImplTest {
29  
30  	private ActionErrors actionErrors = new ActionErrorsImpl();
31  
32  	@Test
33  	public void testIsEmpty1() {
34  		assertTrue(actionErrors.isEmpty());
35  		actionErrors.add("error1");
36  		assertFalse(actionErrors.isEmpty());
37  	}
38  
39  	@Test
40  	public void testAdd() {
41  		actionErrors.add("error1");
42  		assertEquals(1, actionErrors.getOthers().size());
43  		assertEquals("error1", actionErrors.getOthers().get(0));
44  		assertEquals(1, actionErrors.getAll().size());
45  		assertEquals("error1", actionErrors.getAll().get(0));
46  
47  		actionErrors.add("error2", new FieldInfo("field1"));
48  		assertFalse(actionErrors.getFields().get("field1").isEmpty());
49  		assertTrue(actionErrors.getFields().get("field2").isEmpty());
50  		assertEquals(1, actionErrors.getFields().get("field1").size());
51  		assertEquals("error2", actionErrors.getFields().get("field1").get(0));
52  		assertEquals(2, actionErrors.getAll().size());
53  		assertEquals("error1", actionErrors.getAll().get(0));
54  		assertEquals("error2", actionErrors.getAll().get(1));
55  
56  		actionErrors.add("error3");
57  		assertEquals(2, actionErrors.getOthers().size());
58  		assertEquals("error1", actionErrors.getOthers().get(0));
59  		assertEquals("error3", actionErrors.getOthers().get(1));
60  		assertEquals(3, actionErrors.getAll().size());
61  		assertEquals("error1", actionErrors.getAll().get(0));
62  		assertEquals("error2", actionErrors.getAll().get(1));
63  		assertEquals("error3", actionErrors.getAll().get(2));
64  
65  		actionErrors.add("error4", new FieldInfo("field1"));
66  		actionErrors.add("error5", new FieldInfo("field2", 0));
67  		assertFalse(actionErrors.getFields().get("field1").isEmpty());
68  		assertFalse(actionErrors.getFields().get("field2").isEmpty());
69  		assertEquals(2, actionErrors.getFields().get("field1").size());
70  		assertEquals("error2", actionErrors.getFields().get("field1").get(0));
71  		assertEquals("error4", actionErrors.getFields().get("field1").get(1));
72  		assertEquals(1, actionErrors.getFields().get("field2").size());
73  		assertEquals("error5", actionErrors.getFields().get("field2").get(0));
74  		assertEquals(1, actionErrors.getIndexedFields().get("field2").get(0)
75  				.size());
76  		assertTrue(actionErrors.getIndexedFields().get("field2").get(1)
77  				.isEmpty());
78  		assertEquals("error5", actionErrors.getIndexedFields().get("field2")
79  				.get(0).get(0));
80  		assertEquals(5, actionErrors.getAll().size());
81  		assertEquals("error1", actionErrors.getAll().get(0));
82  		assertEquals("error2", actionErrors.getAll().get(1));
83  		assertEquals("error3", actionErrors.getAll().get(2));
84  		assertEquals("error4", actionErrors.getAll().get(3));
85  		assertEquals("error5", actionErrors.getAll().get(4));
86  
87  		actionErrors.add("error6", (FieldInfo) null);
88  		assertEquals("error6", actionErrors.getAll().get(5));
89  		assertEquals(6, actionErrors.getAll().size());
90  
91  		actionErrors.clear();
92  		assertTrue(actionErrors.isEmpty());
93  	}
94  
95  }