View Javadoc

1   /*
2    * $Id: TestActionMessage.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2002-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  package org.apache.struts.action;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  /***
25   * Unit tests for the <code>org.apache.struts.action.ActionMessage</code>
26   * class.
27   *
28   * @version $Rev: 421119 $ $Date: 2005-05-19 23:50:06 -0400 (Thu, 19 May 2005)
29   *          $
30   */
31  public class TestActionMessage extends TestCase {
32      protected ActionMessage amWithNoValue = null;
33      protected ActionMessage amWithOneValue = null;
34      protected ActionMessage amWithTwoValues = null;
35      protected ActionMessage amWithThreeValues = null;
36      protected ActionMessage amWithFourValues = null;
37      protected ActionMessage amWithArrayValues = null;
38      protected ActionMessage amWithTwoIntegerValues = null;
39      protected ActionMessage amNoResource = null;
40      protected Object[] test_values =
41          new Object[] {
42              "stringValue1", "stringValue2", "stringValue3", "stringValue4"
43          };
44  
45      /***
46       * Defines the testcase name for JUnit.
47       *
48       * @param theName the testcase's name.
49       */
50      public TestActionMessage(String theName) {
51          super(theName);
52      }
53  
54      /***
55       * Start the tests.
56       *
57       * @param theArgs the arguments. Not used
58       */
59      public static void main(String[] theArgs) {
60          junit.awtui.TestRunner.main(new String[] {
61                  TestActionMessage.class.getName()
62              });
63      }
64  
65      /***
66       * @return a test suite (<code>TestSuite</code>) that includes all methods
67       *         starting with "test"
68       */
69      public static Test suite() {
70          // All methods starting with "test" will be executed in the test suite.
71          return new TestSuite(TestActionMessage.class);
72      }
73  
74      public void setUp() {
75          amWithNoValue = new ActionMessage("amWithNoValue");
76          amWithOneValue =
77              new ActionMessage("amWithOneValue", new String("stringValue"));
78          amWithTwoValues =
79              new ActionMessage("amWithTwoValues", new String("stringValue1"),
80                  new String("stringValue2"));
81          amWithThreeValues =
82              new ActionMessage("amWithThreeValues", new String("stringValue1"),
83                  new String("stringValue2"), new String("stringValue3"));
84          amWithFourValues =
85              new ActionMessage("amWithFourValues", new String("stringValue1"),
86                  new String("stringValue2"), new String("stringValue3"),
87                  new String("stringValue4"));
88          amWithArrayValues = new ActionMessage("amWithArrayValues", test_values);
89          amWithTwoIntegerValues =
90              new ActionMessage("amWithTwoIntegerValues", new Integer(5),
91                  new Integer(10));
92          amNoResource = new ActionMessage("amNoResource", false);
93      }
94  
95      public void tearDown() {
96          amWithNoValue = null;
97          amWithOneValue = null;
98          amWithTwoValues = null;
99          amWithThreeValues = null;
100         amWithFourValues = null;
101         amWithArrayValues = null;
102         amWithTwoIntegerValues = null;
103         amNoResource = null;
104     }
105 
106     public void testActionMessageWithNoValue() {
107         assertTrue(amWithNoValue.getValues() == null);
108         assertTrue(amWithNoValue.isResource());
109         assertTrue(amWithNoValue.getKey() == "amWithNoValue");
110         assertTrue(amWithNoValue.toString().equals("amWithNoValue[]"));
111     }
112 
113     public void testActionMessageWithAStringValue() {
114         Object[] values = amWithOneValue.getValues();
115 
116         assertTrue(values != null);
117         assertTrue(values.length == 1);
118         assertTrue(values[0].equals("stringValue"));
119         assertTrue(amWithOneValue.isResource());
120         assertTrue(amWithOneValue.getKey() == "amWithOneValue");
121         assertTrue(amWithOneValue.toString().equals("amWithOneValue[stringValue]"));
122     }
123 
124     public void testActionMessageWithTwoValues() {
125         Object[] values = amWithTwoValues.getValues();
126 
127         assertTrue(values != null);
128         assertTrue(values.length == 2);
129         assertTrue(values[0].equals("stringValue1"));
130         assertTrue(values[1].equals("stringValue2"));
131         assertTrue(amWithTwoValues.isResource());
132         assertTrue(amWithTwoValues.getKey() == "amWithTwoValues");
133         assertTrue(amWithTwoValues.toString().equals("amWithTwoValues[stringValue1, stringValue2]"));
134     }
135 
136     public void testActionMessageWithThreeValues() {
137         Object[] values = amWithThreeValues.getValues();
138 
139         assertTrue(values != null);
140         assertTrue(values.length == 3);
141         assertTrue(values[0].equals("stringValue1"));
142         assertTrue(values[1].equals("stringValue2"));
143         assertTrue(values[2].equals("stringValue3"));
144         assertTrue(amWithThreeValues.getKey() == "amWithThreeValues");
145         assertTrue(amWithThreeValues.isResource());
146         assertTrue(amWithThreeValues.toString().equals("amWithThreeValues[stringValue1, stringValue2, stringValue3]"));
147     }
148 
149     public void testActionMessageWithFourValues() {
150         Object[] values = amWithFourValues.getValues();
151 
152         assertTrue(values != null);
153         assertTrue(values.length == 4);
154         assertTrue(values[0].equals("stringValue1"));
155         assertTrue(values[1].equals("stringValue2"));
156         assertTrue(values[2].equals("stringValue3"));
157         assertTrue(values[3].equals("stringValue4"));
158         assertTrue(amWithFourValues.isResource());
159         assertTrue(amWithFourValues.getKey() == "amWithFourValues");
160         assertTrue(amWithFourValues.toString().equals("amWithFourValues[stringValue1, stringValue2, stringValue3, stringValue4]"));
161     }
162 
163     public void testActionMessageWithArrayValues() {
164         Object[] values = amWithArrayValues.getValues();
165 
166         assertTrue(values != null);
167         assertTrue(values.length == test_values.length);
168 
169         for (int i = 0; i < values.length; i++) {
170             assertTrue(values[i] == test_values[i]);
171         }
172 
173         assertTrue(amWithArrayValues.isResource());
174         assertTrue(amWithArrayValues.getKey() == "amWithArrayValues");
175         assertTrue(amWithArrayValues.toString().equals("amWithArrayValues[stringValue1, stringValue2, stringValue3, stringValue4]"));
176     }
177 
178     public void testActionWithTwoIntegers() {
179         Object[] values = amWithTwoIntegerValues.getValues();
180 
181         assertTrue(values != null);
182         assertTrue(values.length == 2);
183         assertTrue(values[0] instanceof Integer);
184         assertTrue(values[0].toString().equals("5"));
185         assertTrue(values[1] instanceof Integer);
186         assertTrue(values[1].toString().equals("10"));
187         assertTrue(amWithTwoIntegerValues.isResource());
188         assertTrue(amWithTwoIntegerValues.getKey() == "amWithTwoIntegerValues");
189         assertTrue(amWithTwoIntegerValues.toString().equals("amWithTwoIntegerValues[5, 10]"));
190     }
191 
192     public void testActionNoResource() {
193         assertTrue(amNoResource.getValues() == null);
194         assertTrue(amNoResource.isResource() == false);
195         assertTrue(amNoResource.getKey() == "amNoResource");
196         assertTrue(amNoResource.toString().equals("amNoResource[]"));
197     }
198 }