View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit;
21  
22  import org.apache.myfaces.tobago.component.UILabel;
23  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class LabelWithAccessKeyUnitTest extends AbstractTobagoTestBase {
28  
29    @Test
30    public void testSimple() {
31      final UILabel component = new UILabel();
32      component.setValue("Save");
33      final LabelWithAccessKey label = new LabelWithAccessKey(component);
34      Assert.assertEquals("Save", label.getLabel());
35      Assert.assertEquals(-1, label.getPos());
36      Assert.assertEquals(null, label.getAccessKey());
37    }
38  
39    @Test
40    public void testWithKeyFirstLetter() {
41      final UILabel component = new UILabel();
42      component.setValue("Save_");
43      final LabelWithAccessKey label = new LabelWithAccessKey(component);
44      Assert.assertEquals("Save", label.getLabel());
45      Assert.assertEquals(-1, label.getPos());
46      Assert.assertNull(label.getAccessKey());
47    }
48  
49    @Test
50    public void testWithKeyLastLetter() {
51      final UILabel component = new UILabel();
52      component.setValue("A__n__a_ly__ze");
53      final LabelWithAccessKey label = new LabelWithAccessKey(component);
54      Assert.assertEquals("A_n_aly_ze", label.getLabel());
55      Assert.assertEquals(5, label.getPos());
56      Assert.assertEquals(Character.valueOf('l'), label.getAccessKey());
57    }
58  
59    @Test
60    public void testAmpersand() {
61      final UILabel component = new UILabel();
62      component.setValue("_Save");
63      final LabelWithAccessKey label = new LabelWithAccessKey(component);
64      Assert.assertEquals("Save", label.getLabel());
65      Assert.assertEquals(0, label.getPos());
66      Assert.assertEquals(Character.valueOf('s'), label.getAccessKey());
67    }
68  
69    @Test
70    public void testAmpersandAtEnd() {
71      final UILabel component = new UILabel();
72      component.setValue("Save_");
73      final LabelWithAccessKey label = new LabelWithAccessKey(component);
74      Assert.assertEquals("Save", label.getLabel());
75      Assert.assertEquals(-1, label.getPos());
76      Assert.assertEquals(null, label.getAccessKey());
77    }
78  
79    @Test
80    public void testUmlauts() {
81      // Umlauts are not supported as accessKey
82      final UILabel component = new UILabel();
83      component.setValue("Löschen");
84      final LabelWithAccessKey label = new LabelWithAccessKey(component);
85      Assert.assertEquals("Löschen", label.getLabel());
86      Assert.assertEquals(-1, label.getPos());
87      Assert.assertEquals(null, label.getAccessKey());
88    }
89  
90    @Test
91    public void testUmlauts2() {
92      // Umlauts are not supported as accessKey
93      final UILabel component = new UILabel();
94      component.setValue("L_öschen");
95      final LabelWithAccessKey label = new LabelWithAccessKey(component);
96      Assert.assertEquals("Löschen", label.getLabel());
97      Assert.assertEquals(-1, label.getPos());
98      Assert.assertEquals(null, label.getAccessKey());
99    }
100 
101   @Test
102   public void testKey() {
103     final UILabel component = new UILabel();
104     component.setValue("Save");
105     component.setAccessKey('a');
106     final LabelWithAccessKey label = new LabelWithAccessKey(component);
107     Assert.assertEquals("Save", label.getLabel());
108     Assert.assertEquals(1, label.getPos());
109     Assert.assertEquals((Character) 'a', label.getAccessKey());
110   }
111 
112   @Test
113   public void testKeyWithWongCase() {
114     final UILabel component = new UILabel();
115     component.setValue("Save");
116     component.setAccessKey('A');
117     final LabelWithAccessKey label = new LabelWithAccessKey(component);
118     Assert.assertEquals("Save", label.getLabel());
119     Assert.assertEquals(1, label.getPos());
120     Assert.assertEquals((Character) 'a', label.getAccessKey());
121   }
122 
123   @Test
124   public void testNumberKey() {
125     final UILabel component = new UILabel();
126     component.setValue("Save");
127     component.setAccessKey('5');
128     final LabelWithAccessKey label = new LabelWithAccessKey(component);
129     Assert.assertEquals("Save", label.getLabel());
130     Assert.assertEquals(-1, label.getPos());
131     Assert.assertEquals((Character) '5', label.getAccessKey());
132   }
133 
134   @Test
135   public void testForbiddenKey() {
136     final UILabel component = new UILabel();
137     component.setValue("Save");
138     component.setAccessKey('#');
139     final LabelWithAccessKey label = new LabelWithAccessKey(component);
140     Assert.assertEquals("Save", label.getLabel());
141     Assert.assertEquals(-1, label.getPos());
142     Assert.assertEquals(null, label.getAccessKey());
143   }
144 
145   @Test
146   public void testForbiddenKey2() {
147     final UILabel component = new UILabel();
148     component.setValue("Save");
149     component.setAccessKey('á');
150     final LabelWithAccessKey label = new LabelWithAccessKey(component);
151     Assert.assertEquals("Save", label.getLabel());
152     Assert.assertEquals(-1, label.getPos());
153     Assert.assertEquals(null, label.getAccessKey());
154   }
155 
156   @Test
157   public void testForbiddenKey3() {
158     final UILabel component = new UILabel();
159     component.setValue("Save");
160     component.setAccessKey('ä');
161     final LabelWithAccessKey label = new LabelWithAccessKey(component);
162     Assert.assertEquals("Save", label.getLabel());
163     Assert.assertEquals(-1, label.getPos());
164     Assert.assertEquals(null, label.getAccessKey());
165   }
166 
167   @Test
168   public void testUnderscores1() {
169     final UILabel component = new UILabel();
170     component.setValue("_");
171     final LabelWithAccessKey label = new LabelWithAccessKey(component);
172     Assert.assertEquals("", label.getLabel());
173     Assert.assertEquals(-1, label.getPos());
174     Assert.assertEquals(null, label.getAccessKey());
175   }
176 
177   @Test
178   public void testUnderscores2() {
179     final UILabel component = new UILabel();
180     component.setValue("__");
181     final LabelWithAccessKey label = new LabelWithAccessKey(component);
182     Assert.assertEquals("_", label.getLabel());
183     Assert.assertEquals(-1, label.getPos());
184     Assert.assertEquals(null, label.getAccessKey());
185   }
186 
187   @Test
188   public void testUnderscores3() {
189     final UILabel component = new UILabel();
190     component.setValue("___");
191     final LabelWithAccessKey label = new LabelWithAccessKey(component);
192     Assert.assertEquals("_", label.getLabel());
193     Assert.assertEquals(-1, label.getPos());
194     Assert.assertEquals(null, label.getAccessKey());
195   }
196 
197   @Test
198   public void testUnderscores4() {
199     final UILabel component = new UILabel();
200     component.setValue("____");
201     final LabelWithAccessKey label = new LabelWithAccessKey(component);
202     Assert.assertEquals("__", label.getLabel());
203     Assert.assertEquals(-1, label.getPos());
204     Assert.assertEquals(null, label.getAccessKey());
205   }
206 
207   @Test
208   public void testNull() {
209     final UILabel component = new UILabel();
210     final LabelWithAccessKey label = new LabelWithAccessKey(component);
211     Assert.assertEquals(null, label.getLabel());
212     Assert.assertEquals(-1, label.getPos());
213     Assert.assertEquals(null, label.getAccessKey());
214   }
215 }