1 18 19 package org.apache.struts.action; 20 21 import java.util.Iterator ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 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 47 public TestActionMessages(String theName) { 48 super(theName); 49 } 50 51 56 public static void main(String [] theArgs) { 57 junit.awtui.TestRunner.main(new String [] { TestActionMessages.class.getName()}); 58 } 59 60 64 public static Test suite() { 65 return new TestSuite(TestActionMessages.class); 67 } 68 69 public void setUp() { 70 aMsgs = new ActionMessages(); 71 anMsgs = new ActionMessages(); 72 Object [] objs1 = new Object [] { "a", "b", "c", "d", "e" }; 73 Object [] objs2 = new Object [] { "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 it = aMsgs.get("myProp"); 119 assertTrue("iterator is not empty!", it.hasNext() == false); 120 } 121 122 public void testGetForAProperty() { 123 testSizeWithOneProperty(); 124 Iterator it = aMsgs.get("myProp"); 125 assertTrue("iterator is empty!", it.hasNext() == true); 126 } 127 128 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 Iterator 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 |