KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > action > TestActionMessages


1 /*
2  * $Id: TestActionMessages.java 54927 2004-10-16 16:09:25Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.action;
20
21 import java.util.Iterator JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * Unit tests for the <code>org.apache.struts.action.ActionMessages</code> class.
29  *
30  * @version $Rev: 54927 $ $Date: 2004-10-16 17:09:25 +0100 (Sat, 16 Oct 2004) $
31  */

32
33 public class TestActionMessages extends TestCase {
34     protected ActionMessages aMsgs = null;
35     protected ActionMessages anMsgs = null;
36     protected ActionMessage msg1 = null;
37     protected ActionMessage msg2 = null;
38     protected ActionMessage msg3 = null;
39     protected ActionMessage msg4 = null;
40     protected ActionMessage msg5 = null;
41
42     /**
43      * Defines the testcase name for JUnit.
44      *
45      * @param theName the testcase's name.
46      */

47     public TestActionMessages(String JavaDoc theName) {
48         super(theName);
49     }
50
51     /**
52      * Start the tests.
53      *
54      * @param theArgs the arguments. Not used
55      */

56     public static void main(String JavaDoc[] theArgs) {
57         junit.awtui.TestRunner.main(new String JavaDoc[] { TestActionMessages.class.getName()});
58     }
59
60     /**
61      * @return a test suite (<code>TestSuite</code>) that includes all methods
62      * starting with "test"
63      */

64     public static Test suite() {
65         // All methods starting with "test" will be executed in the test suite.
66
return new TestSuite(TestActionMessages.class);
67     }
68
69     public void setUp() {
70         aMsgs = new ActionMessages();
71         anMsgs = new ActionMessages();
72         Object JavaDoc[] objs1 = new Object JavaDoc[] { "a", "b", "c", "d", "e" };
73         Object JavaDoc[] objs2 = new Object JavaDoc[] { "f", "g", "h", "i", "j" };
74         msg1 = new ActionMessage("aMessage", objs1);
75         msg2 = new ActionMessage("anMessage", objs2);
76         msg3 = new ActionMessage("msg3", "value1");
77         msg4 = new ActionMessage("msg4", "value2");
78         msg5 = new ActionMessage("msg5", "value3", "value4");
79     }
80
81     public void tearDown() {
82         aMsgs = null;
83     }
84
85     public void testEmpty() {
86         assertTrue("aMsgs is not empty!", aMsgs.isEmpty());
87     }
88
89     public void testNotEmpty() {
90         aMsgs.add("myProp", msg1);
91         assertTrue("aMsgs is empty!", aMsgs.isEmpty() == false);
92     }
93
94     public void testSizeWithOneProperty() {
95         aMsgs.add("myProp", msg1);
96         aMsgs.add("myProp", msg2);
97         assertTrue("number of mesages is not 2", aMsgs.size("myProp") == 2);
98     }
99
100     public void testSizeWithManyProperties() {
101         aMsgs.add("myProp1", msg1);
102         aMsgs.add("myProp2", msg2);
103         aMsgs.add("myProp3", msg3);
104         aMsgs.add("myProp3", msg4);
105         aMsgs.add("myProp4", msg5);
106         assertTrue("number of messages for myProp1 is not 1", aMsgs.size("myProp1") == 1);
107         assertTrue("number of messages", aMsgs.size() == 5);
108     }
109
110     public void testSizeAndEmptyAfterClear() {
111         testSizeWithOneProperty();
112         aMsgs.clear();
113         testEmpty();
114         assertTrue("number of meesages is not 0", aMsgs.size("myProp") == 0);
115     }
116
117     public void testGetWithNoProperty() {
118         Iterator JavaDoc it = aMsgs.get("myProp");
119         assertTrue("iterator is not empty!", it.hasNext() == false);
120     }
121
122     public void testGetForAProperty() {
123         testSizeWithOneProperty();
124         Iterator JavaDoc it = aMsgs.get("myProp");
125         assertTrue("iterator is empty!", it.hasNext() == true);
126     }
127
128     /**
129      * Tests adding an ActionMessages object to an ActionMessages object.
130      */

131     public void testAddMessages() {
132         ActionMessage msg1 = new ActionMessage("key");
133         ActionMessage msg2 = new ActionMessage("key2");
134         ActionMessage msg3 = new ActionMessage("key3");
135         ActionMessages msgs = new ActionMessages();
136         ActionMessages add = new ActionMessages();
137
138         msgs.add("prop1", msg1);
139         add.add("prop1", msg2);
140         add.add("prop3", msg3);
141
142         msgs.add(add);
143         assertTrue(msgs.size() == 3);
144         assertTrue(msgs.size("prop1") == 2);
145
146         // test message order
147
Iterator JavaDoc props = msgs.get();
148         int count = 1;
149         while (props.hasNext()) {
150             ActionMessage msg = (ActionMessage) props.next();
151             if (count == 1) {
152                 assertTrue(msg.getKey().equals("key"));
153             } else if (count == 2) {
154                 assertTrue(msg.getKey().equals("key2"));
155             } else {
156                 assertTrue(msg.getKey().equals("key3"));
157             }
158
159             count++;
160         }
161     }
162 }
163
Popular Tags