1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.action;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import java.util.Iterator;
25
26 /***
27 * Unit tests for the <code>org.apache.struts.action.ActionMessages</code>
28 * class.
29 *
30 * @version $Rev: 421119 $ $Date: 2004-10-16 12:09:25 -0400 (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 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[] theArgs) {
57 junit.awtui.TestRunner.main(new String[] {
58 TestActionMessages.class.getName()
59 });
60 }
61
62 /***
63 * @return a test suite (<code>TestSuite</code>) that includes all methods
64 * starting with "test"
65 */
66 public static Test suite() {
67
68 return new TestSuite(TestActionMessages.class);
69 }
70
71 public void setUp() {
72 aMsgs = new ActionMessages();
73 anMsgs = new ActionMessages();
74
75 Object[] objs1 = new Object[] { "a", "b", "c", "d", "e" };
76 Object[] objs2 = new Object[] { "f", "g", "h", "i", "j" };
77
78 msg1 = new ActionMessage("aMessage", objs1);
79 msg2 = new ActionMessage("anMessage", objs2);
80 msg3 = new ActionMessage("msg3", "value1");
81 msg4 = new ActionMessage("msg4", "value2");
82 msg5 = new ActionMessage("msg5", "value3", "value4");
83 }
84
85 public void tearDown() {
86 aMsgs = null;
87 }
88
89 public void testEmpty() {
90 assertTrue("aMsgs is not empty!", aMsgs.isEmpty());
91 }
92
93 public void testNotEmpty() {
94 aMsgs.add("myProp", msg1);
95 assertTrue("aMsgs is empty!", aMsgs.isEmpty() == false);
96 }
97
98 public void testSizeWithOneProperty() {
99 aMsgs.add("myProp", msg1);
100 aMsgs.add("myProp", msg2);
101 assertTrue("number of mesages is not 2", aMsgs.size("myProp") == 2);
102 }
103
104 public void testSizeWithManyProperties() {
105 aMsgs.add("myProp1", msg1);
106 aMsgs.add("myProp2", msg2);
107 aMsgs.add("myProp3", msg3);
108 aMsgs.add("myProp3", msg4);
109 aMsgs.add("myProp4", msg5);
110 assertTrue("number of messages for myProp1 is not 1",
111 aMsgs.size("myProp1") == 1);
112 assertTrue("number of messages", aMsgs.size() == 5);
113 }
114
115 public void testSizeAndEmptyAfterClear() {
116 testSizeWithOneProperty();
117 aMsgs.clear();
118 testEmpty();
119 assertTrue("number of meesages is not 0", aMsgs.size("myProp") == 0);
120 }
121
122 public void testGetWithNoProperty() {
123 Iterator it = aMsgs.get("myProp");
124
125 assertTrue("iterator is not empty!", it.hasNext() == false);
126 }
127
128 public void testGetForAProperty() {
129 testSizeWithOneProperty();
130
131 Iterator it = aMsgs.get("myProp");
132
133 assertTrue("iterator is empty!", it.hasNext() == true);
134 }
135
136 /***
137 * Tests adding an ActionMessages object to an ActionMessages object.
138 */
139 public void testAddMessages() {
140 ActionMessage msg1 = new ActionMessage("key");
141 ActionMessage msg2 = new ActionMessage("key2");
142 ActionMessage msg3 = new ActionMessage("key3");
143 ActionMessages msgs = new ActionMessages();
144 ActionMessages add = new ActionMessages();
145
146 msgs.add("prop1", msg1);
147 add.add("prop1", msg2);
148 add.add("prop3", msg3);
149
150 msgs.add(add);
151 assertTrue(msgs.size() == 3);
152 assertTrue(msgs.size("prop1") == 2);
153
154
155 Iterator props = msgs.get();
156 int count = 1;
157
158 while (props.hasNext()) {
159 ActionMessage msg = (ActionMessage) props.next();
160
161 if (count == 1) {
162 assertTrue(msg.getKey().equals("key"));
163 } else if (count == 2) {
164 assertTrue(msg.getKey().equals("key2"));
165 } else {
166 assertTrue(msg.getKey().equals("key3"));
167 }
168
169 count++;
170 }
171 }
172 }