001: /*
002: * $Id: TestActionMessages.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.action;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import java.util.Iterator;
028:
029: /**
030: * Unit tests for the <code>org.apache.struts.action.ActionMessages</code>
031: * class.
032: *
033: * @version $Rev: 471754 $ $Date: 2004-10-16 12:09:25 -0400 (Sat, 16 Oct 2004)
034: * $
035: */
036: public class TestActionMessages extends TestCase {
037: protected ActionMessages aMsgs = null;
038: protected ActionMessages anMsgs = null;
039: protected ActionMessage msg1 = null;
040: protected ActionMessage msg2 = null;
041: protected ActionMessage msg3 = null;
042: protected ActionMessage msg4 = null;
043: protected ActionMessage msg5 = null;
044:
045: /**
046: * Defines the testcase name for JUnit.
047: *
048: * @param theName the testcase's name.
049: */
050: public TestActionMessages(String theName) {
051: super (theName);
052: }
053:
054: /**
055: * Start the tests.
056: *
057: * @param theArgs the arguments. Not used
058: */
059: public static void main(String[] theArgs) {
060: junit.awtui.TestRunner
061: .main(new String[] { TestActionMessages.class.getName() });
062: }
063:
064: /**
065: * @return a test suite (<code>TestSuite</code>) that includes all methods
066: * starting with "test"
067: */
068: public static Test suite() {
069: // All methods starting with "test" will be executed in the test suite.
070: return new TestSuite(TestActionMessages.class);
071: }
072:
073: public void setUp() {
074: aMsgs = new ActionMessages();
075: anMsgs = new ActionMessages();
076:
077: Object[] objs1 = new Object[] { "a", "b", "c", "d", "e" };
078: Object[] objs2 = new Object[] { "f", "g", "h", "i", "j" };
079:
080: msg1 = new ActionMessage("aMessage", objs1);
081: msg2 = new ActionMessage("anMessage", objs2);
082: msg3 = new ActionMessage("msg3", "value1");
083: msg4 = new ActionMessage("msg4", "value2");
084: msg5 = new ActionMessage("msg5", "value3", "value4");
085: }
086:
087: public void tearDown() {
088: aMsgs = null;
089: }
090:
091: public void testEmpty() {
092: assertTrue("aMsgs is not empty!", aMsgs.isEmpty());
093: }
094:
095: public void testNotEmpty() {
096: aMsgs.add("myProp", msg1);
097: assertTrue("aMsgs is empty!", aMsgs.isEmpty() == false);
098: }
099:
100: public void testSizeWithOneProperty() {
101: aMsgs.add("myProp", msg1);
102: aMsgs.add("myProp", msg2);
103: assertTrue("number of mesages is not 2",
104: aMsgs.size("myProp") == 2);
105: }
106:
107: public void testSizeWithManyProperties() {
108: aMsgs.add("myProp1", msg1);
109: aMsgs.add("myProp2", msg2);
110: aMsgs.add("myProp3", msg3);
111: aMsgs.add("myProp3", msg4);
112: aMsgs.add("myProp4", msg5);
113: assertTrue("number of messages for myProp1 is not 1", aMsgs
114: .size("myProp1") == 1);
115: assertTrue("number of messages", aMsgs.size() == 5);
116: }
117:
118: public void testSizeAndEmptyAfterClear() {
119: testSizeWithOneProperty();
120: aMsgs.clear();
121: testEmpty();
122: assertTrue("number of meesages is not 0",
123: aMsgs.size("myProp") == 0);
124: }
125:
126: public void testGetWithNoProperty() {
127: Iterator it = aMsgs.get("myProp");
128:
129: assertTrue("iterator is not empty!", it.hasNext() == false);
130: }
131:
132: public void testGetForAProperty() {
133: testSizeWithOneProperty();
134:
135: Iterator it = aMsgs.get("myProp");
136:
137: assertTrue("iterator is empty!", it.hasNext() == true);
138: }
139:
140: /**
141: * Tests adding an ActionMessages object to an ActionMessages object.
142: */
143: public void testAddMessages() {
144: ActionMessage msg1 = new ActionMessage("key");
145: ActionMessage msg2 = new ActionMessage("key2");
146: ActionMessage msg3 = new ActionMessage("key3");
147: ActionMessages msgs = new ActionMessages();
148: ActionMessages add = new ActionMessages();
149:
150: msgs.add("prop1", msg1);
151: add.add("prop1", msg2);
152: add.add("prop3", msg3);
153:
154: msgs.add(add);
155: assertTrue(msgs.size() == 3);
156: assertTrue(msgs.size("prop1") == 2);
157:
158: // test message order
159: Iterator props = msgs.get();
160: int count = 1;
161:
162: while (props.hasNext()) {
163: ActionMessage msg = (ActionMessage) props.next();
164:
165: if (count == 1) {
166: assertTrue(msg.getKey().equals("key"));
167: } else if (count == 2) {
168: assertTrue(msg.getKey().equals("key2"));
169: } else {
170: assertTrue(msg.getKey().equals("key3"));
171: }
172:
173: count++;
174: }
175: }
176: }
|