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.junit.Assert;
23  import org.junit.Test;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIOutput;
27  
28  public class LabelWithAccessKeyUnitTest {
29  
30    @Test
31    public void testSimple() {
32      UIComponent component = new UIOutput();
33      component.getAttributes().put("label", "Save");
34      LabelWithAccessKey label = new LabelWithAccessKey(component);
35      Assert.assertEquals("Save", label.getText());
36      Assert.assertEquals(-1, label.getPos());
37      Assert.assertEquals(null, label.getAccessKey());
38    }
39  
40    @Test
41    public void testWithKeyFirstLetter() {
42      UIComponent component = new UIOutput();
43      component.getAttributes().put("label", "Save_");
44      LabelWithAccessKey label = new LabelWithAccessKey(component);
45      Assert.assertEquals("Save", label.getText());
46      Assert.assertNull(label.getAccessKey());
47    }
48  
49    @Test
50    public void testWithKeyLastLetter() {
51      String result = "A_n_aly_ze";
52      UIComponent component = new UIOutput();
53      component.getAttributes().put("label", "A__n__a_ly__ze");
54      LabelWithAccessKey label = new LabelWithAccessKey(component);
55      Assert.assertEquals(result, label.getText());
56      Assert.assertEquals(5, label.getPos());
57      Assert.assertEquals(new Character('l'), label.getAccessKey());
58    }
59  
60    @Test
61    public void testAmpersand() {
62      UIComponent component = new UIOutput();
63      component.getAttributes().put("label", "_Save");
64      LabelWithAccessKey label = new LabelWithAccessKey(component);
65      Assert.assertEquals("Save", label.getText());
66      Assert.assertEquals(0, label.getPos());
67      Assert.assertEquals(new Character('S'), label.getAccessKey());
68    }
69  
70    @Test
71    public void testAmpersandAtEnd() {
72      UIComponent component = new UIOutput();
73      component.getAttributes().put("label", "Save_");
74      LabelWithAccessKey label = new LabelWithAccessKey(component);
75      Assert.assertEquals("Save", label.getText());
76      Assert.assertEquals(-1, label.getPos());
77      Assert.assertEquals(null, label.getAccessKey());
78    }
79  }