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