001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.core;
017    
018    import java.awt.event.KeyEvent;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import javax.swing.JLabel;
024    import javax.swing.JPanel;
025    import javax.swing.JTextField;
026    
027    
028    import junit.framework.Assert;
029    import junit.framework.TestCase;
030    
031    
032    /**
033     * Provides a suite of unit tests for the {@link LabelInfo} class.
034     * 
035     * @author Peter De Bruycker
036     * @author Kevin Stembridge
037     */
038    public class LabelInfoTests extends TestCase {
039        
040        private static final int DEFAULT_MNEMONIC_INDEX = -1;
041        
042        private Map invalidLabelDescriptors;
043        
044        /**
045         * Creates a new {@code LabelInfoTests}.
046         */
047        public LabelInfoTests() {
048            this.invalidLabelDescriptors = new HashMap();
049            this.invalidLabelDescriptors.put("&", "A mnemonic indicator must be followed by a character.");
050            this.invalidLabelDescriptors.put("\\", "A backslash must be followed by an escapable character.");
051            this.invalidLabelDescriptors.put("abcd& abcd", "A space character is not a valid mnemonic character.");
052            this.invalidLabelDescriptors.put("&ab&cd", "A label descriptor can only contain a single non-escaped &");
053            this.invalidLabelDescriptors.put("abcd&", "A mnemonic indicator must be followed by a character.");
054            this.invalidLabelDescriptors.put("\\abcd", "Only an ampersand or a backslash can be escaped.");
055        }
056        
057        /**
058         * Confirms that null or empty input will result in a LabelInfo instance with a blank 
059         * text label and no specified mnemonic.
060         */
061        public void testForNullOrEmptyInput() {
062    
063            LabelInfo info = LabelInfo.valueOf(null);
064    
065            Assert.assertEquals("", info.getText());
066            Assert.assertEquals(0, info.getMnemonic());
067            Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
068            
069            info = LabelInfo.valueOf("");
070    
071            Assert.assertEquals("", info.getText());
072            Assert.assertEquals(0, info.getMnemonic());
073            Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
074            
075        }
076    
077        /**
078         * Confirms that a label descriptor with various special characters produces a LabelInfo 
079         * with expected values for mnemonic and mnemonicIndex.
080         */
081        public void testValueOfWithValidSyntax() {
082            LabelInfo info = LabelInfo.valueOf("Save As");
083    
084            Assert.assertEquals("Save As", info.getText());
085            Assert.assertEquals(0, info.getMnemonic());
086            Assert.assertEquals(DEFAULT_MNEMONIC_INDEX, info.getMnemonicIndex());
087            
088            info = LabelInfo.valueOf("S\\&ave @&as");
089            Assert.assertEquals("S&ave @as", info.getText());
090            Assert.assertEquals(KeyEvent.VK_A, info.getMnemonic());
091            Assert.assertEquals(7, info.getMnemonicIndex());
092            
093        }
094        
095        /**
096         * Confirms that exceptions are thrown for label descriptors that violate the syntax rules.
097         */
098        public void testInvalidSyntax() {
099            
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    }