1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.core;
17  
18  import java.awt.event.KeyEvent;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import javax.swing.JLabel;
24  import javax.swing.JPanel;
25  import javax.swing.JTextField;
26  
27  
28  import junit.framework.Assert;
29  import junit.framework.TestCase;
30  
31  
32  /**
33   * Provides a suite of unit tests for the {@link LabelInfo} class.
34   * 
35   * @author Peter De Bruycker
36   * @author Kevin Stembridge
37   */
38  public class LabelInfoTests extends TestCase {
39      
40      private static final int DEFAULT_MNEMONIC_INDEX = -1;
41      
42      private Map invalidLabelDescriptors;
43      
44      /**
45       * Creates a new {@code LabelInfoTests}.
46       */
47      public LabelInfoTests() {
48          this.invalidLabelDescriptors = new HashMap();
49          this.invalidLabelDescriptors.put("&", "A mnemonic indicator must be followed by a character.");
50          this.invalidLabelDescriptors.put("\\", "A backslash must be followed by an escapable character.");
51          this.invalidLabelDescriptors.put("abcd& abcd", "A space character is not a valid mnemonic character.");
52          this.invalidLabelDescriptors.put("&ab&cd", "A label descriptor can only contain a single non-escaped &");
53          this.invalidLabelDescriptors.put("abcd&", "A mnemonic indicator must be followed by a character.");
54          this.invalidLabelDescriptors.put("\\abcd", "Only an ampersand or a backslash can be escaped.");
55      }
56      
57      /**
58       * Confirms that null or empty input will result in a LabelInfo instance with a blank 
59       * text label and no specified mnemonic.
60       */
61      public void testForNullOrEmptyInput() {
62  
63          LabelInfo info = LabelInfo.valueOf(null);
64  
65          Assert.assertEquals("", info.getText());
66          Assert.assertEquals(0, info.getMnemonic());
67          Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
68          
69          info = LabelInfo.valueOf("");
70  
71          Assert.assertEquals("", info.getText());
72          Assert.assertEquals(0, info.getMnemonic());
73          Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
74          
75      }
76  
77      /**
78       * Confirms that a label descriptor with various special characters produces a LabelInfo 
79       * with expected values for mnemonic and mnemonicIndex.
80       */
81      public void testValueOfWithValidSyntax() {
82          LabelInfo info = LabelInfo.valueOf("Save As");
83  
84          Assert.assertEquals("Save As", info.getText());
85          Assert.assertEquals(0, info.getMnemonic());
86          Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
87          
88          info = LabelInfo.valueOf("S\\&ave @&as");
89          Assert.assertEquals("S&ave @as", info.getText());
90          Assert.assertEquals(KeyEvent.VK_A, info.getMnemonic());
91          Assert.assertEquals(7, info.getMnemonicIndex());
92          
93      }
94      
95      /**
96       * Confirms that exceptions are thrown for label descriptors that violate the syntax rules.
97       */
98      public void testInvalidSyntax() {
99          
100         Iterator entryIterator = this.invalidLabelDescriptors.entrySet().iterator();
101         
102         while (entryIterator.hasNext()) {
103             
104             Map.Entry entry = (Map.Entry) entryIterator.next();
105             
106             try {
107                 LabelInfo.valueOf((String) entry.getKey());
108                 Assert.fail("Should have thrown an IllegalArgumentException for label descriptor [" 
109                             + entry.getKey()
110                             + "] due to "
111                             + entry.getValue());
112             }
113             catch (IllegalArgumentException e) {
114                 //do nothing, test succeeded
115             }
116             
117         }
118         
119     }
120     
121     /**
122      * Confirms that any ampersands escaped with a backslash character appear as text in the label.
123      *
124      */
125     public void testForEscapedAmpersands() {
126         
127         LabelInfo info = LabelInfo.valueOf("&Save \\& Run");
128         
129         Assert.assertEquals(0, info.getMnemonicIndex());
130         Assert.assertEquals(KeyEvent.VK_S, info.getMnemonic());
131         Assert.assertEquals("Save & Run", info.getText());
132         
133     }
134     
135     /**
136      * Confirms that any backslashes escaped with a backslash character appear as text in the label.
137      *
138      */
139     public void testForEscapedBackslashes() {
140         
141         LabelInfo info = LabelInfo.valueOf("This is a backslash (\\\\)");
142         Assert.assertEquals("This is a backslash (\\)", info.getText());
143         
144     }
145     
146     /**
147      * Confirms that any @ symbols, used by the CommandButtonLabelInfo, will not be given special 
148      * treatment by a LabelInfo.
149      */
150     public void testForAtSymbols() {
151         
152         LabelInfo info = LabelInfo.valueOf("Something with an @ in it");
153         Assert.assertEquals("Something with an @ in it", info.getText());
154         info = LabelInfo.valueOf("S\\&ave with an @ &as");
155         Assert.assertEquals("S&ave with an @ as", info.getText());
156         Assert.assertEquals(KeyEvent.VK_A, info.getMnemonic());
157         Assert.assertEquals(16, info.getMnemonicIndex());
158         
159     }
160     
161     public void testConstructor() {
162         LabelInfo info = new LabelInfo("test");
163         assertEquals("test", info.getText());
164         assertEquals(0, info.getMnemonic());
165         assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
166 
167         info = new LabelInfo("test", KeyEvent.VK_T);
168         assertEquals("test", info.getText());
169         assertEquals(KeyEvent.VK_T, info.getMnemonic());
170         assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
171 
172         info = new LabelInfo("test", KeyEvent.VK_T, 3);
173         assertEquals("test", info.getText());
174         assertEquals(KeyEvent.VK_T, info.getMnemonic());
175         assertEquals(3, info.getMnemonicIndex());
176     }
177     
178     public void testEquals() throws Exception {
179         LabelInfo info1 = new LabelInfo("test", 0, 0);
180         LabelInfo info2 = new LabelInfo("test", 0, 0);
181         assertTrue(info1.equals(info2));
182         info2 = new LabelInfo("test", 1, 0);
183         assertFalse(info1.equals(info2));
184         info2 = new LabelInfo("test", 0, 1);
185         assertFalse(info1.equals(info2));
186         info2 = new LabelInfo("test2", 0, 0);
187         assertFalse(info1.equals(info2));
188         assertFalse(info1.equals(null));
189     }
190 
191     public void testHashCode() throws Exception {
192         LabelInfo info1 = new LabelInfo("test", 0, 0);
193         LabelInfo info2 = new LabelInfo("test", 0, 0);
194         assertTrue(info1.hashCode() == info2.hashCode());
195         info2 = new LabelInfo("test", 1, 0);
196         assertFalse(info1.hashCode() == info2.hashCode());
197         LabelInfo info3 = new LabelInfo("test", 0, 1);
198         assertFalse(info1.hashCode() == info2.hashCode());
199         assertFalse(info2.hashCode() == info3.hashCode());
200         info2 = new LabelInfo("test2", 0, 0);
201         assertFalse(info1.hashCode() == info2.hashCode());
202     }
203 
204     public void testConstructorEmptyText() {
205         LabelInfo info = new LabelInfo("", KeyEvent.VK_A, -1);
206         assertEquals("", info.getText());
207         assertEquals(KeyEvent.VK_A, info.getMnemonic());
208         assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
209     }
210 
211     public void testConstructorNullText() {
212         try {
213             new LabelInfo(null);
214             fail("no null text");
215         }
216         catch (IllegalArgumentException e) {
217             pass();
218         }
219     }
220 
221     public void testConstructorNegativeMnemonic() {
222         try {
223             new LabelInfo("test", -5);
224             fail("No negative mnemonics");
225         }
226         catch (IllegalArgumentException e) {
227             pass();
228         }
229     }
230 
231     public void testConfigureLabel() {
232         JLabel label = new JLabel();
233         LabelInfo info = new LabelInfo("Save As", 'A', 5);
234         info.configureLabel(label);
235 
236         assertEquals("Save As", label.getText());
237         assertEquals('A', label.getDisplayedMnemonic());
238         assertEquals(5, label.getDisplayedMnemonicIndex());
239     }
240 
241     public void testConfigureLabelNull() {
242         LabelInfo info = new LabelInfo("Test");
243         
244         try {
245             info.configureLabel(null);
246             Assert.fail("Should have thrown an IllegalArgumentException");
247         }
248         catch (IllegalArgumentException e) {
249             //do nothing, test succeeded
250         }
251         
252     }
253 
254     public void testConfigureLabelFor() {
255         JTextField field = new JTextField();
256         JLabel label = new JLabel();
257         LabelInfo info = new LabelInfo("Name", 'N');
258         info.configureLabelFor(label, field);
259 
260         assertEquals("LabelInfo must add colon if none present", "Name:", label.getText());
261         assertEquals('N', label.getDisplayedMnemonic());
262         assertEquals(field, label.getLabelFor());
263     }
264 
265     public void testConfigureLabelForWithColon() {
266         JTextField field = new JTextField();
267         JLabel label = new JLabel();
268         LabelInfo info = new LabelInfo("Name:", KeyEvent.VK_N);
269         info.configureLabelFor(label, field);
270 
271         assertEquals("Name:", label.getText());
272         assertEquals(KeyEvent.VK_N, label.getDisplayedMnemonic());
273         assertEquals(field, label.getLabelFor());
274     }
275 
276     public void testConfigureLabelForJPanel() {
277         JPanel panel = new JPanel();
278         JLabel label = new JLabel();
279         LabelInfo info = new LabelInfo("Name", KeyEvent.VK_N);
280         info.configureLabelFor(label, panel);
281 
282         assertEquals("No colon for panel", "Name", label.getText());
283         assertEquals(KeyEvent.VK_N, label.getDisplayedMnemonic());
284         assertEquals(panel, label.getLabelFor());
285     }
286 
287     public void testConstructorMnemonicIndexGreaterThanLength() {
288         try {
289             new LabelInfo("test", KeyEvent.VK_T, 4);
290             fail("Mnemonic index must be < text.length()");
291         }
292         catch (IllegalArgumentException e) {
293             pass();
294         }
295     }
296 
297     public void testConstructorNegativeMnemonicIndex() {
298         try {
299             new LabelInfo("test", KeyEvent.VK_T, -2);
300             fail("index must be >= -1");
301         }
302         catch (IllegalArgumentException e) {
303             pass();
304         }
305     }
306 
307     public static final void pass() {
308         // test passes
309     }
310         
311 }