View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.component;
17  
18  /**
19   * various tests for {@link org.kuali.rice.krad.uif.component.ComponentBase}
20   *
21   @author Kuali Rice Team (rice.collab@kuali.org)
22   */
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.kuali.rice.krad.uif.control.FileControl;
27  import org.kuali.rice.krad.uif.control.TextAreaControl;
28  import org.kuali.rice.krad.uif.control.TextControl;
29  import org.kuali.rice.krad.uif.control.UserControl;
30  import org.kuali.rice.krad.uif.element.Action;
31  import org.kuali.rice.krad.uif.element.Image;
32  import org.kuali.rice.krad.uif.element.Message;
33  import org.kuali.rice.krad.uif.field.LinkField;
34  
35  import java.util.TreeMap;
36  
37  import static junit.framework.Assert.assertFalse;
38  import static junit.framework.Assert.assertTrue;
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertNotNull;
41  
42  /**
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ComponentBaseTest {
46      private Component component;
47      private TreeMap<String, String> dataAttributes;
48  
49      @Before
50      public void setUp() throws Exception {
51          // use an action field, since ComponentBase is abstract
52          component = new Action();
53          component.setId("action1");
54          // used a TreeMap since it makes specific guarantees as to the order of entries
55          dataAttributes = new TreeMap<String, String>();
56          // set data attributes - for testing purposes only - they do not have any functional significance
57          dataAttributes.put("iconTemplateName", "cool-icon-%s.png");
58          dataAttributes.put("transitions", "3");
59          dataAttributes.put("growing-seasons", "{summer:'hot', winter:'cold'}");
60          dataAttributes.put("intervals", "{short:2, medium:5, long:13}");
61          component.setDataAttributes(dataAttributes);
62      }
63  
64      @Test
65      /**
66       * test that complex date attributes are converted into a jquery script ok
67       */
68      public void testGetComplexDataAttributesJs() throws Exception {
69          assertNotNull(component.getComplexDataAttributesJs());
70          String expected = "jQuery('#action1').data('growing-seasons', {summer:'hot', winter:'cold'});"
71                  + "jQuery('#action1').data('intervals', {short:2, medium:5, long:13});";
72          assertEquals("complex attributes JS string did not match", expected, component.getComplexDataAttributesJs());
73      }
74  
75      @Test
76      /**
77       * test that simple data attributes are converted into inline attributes ok
78       */
79      public void testGetSimpleDataAttributes() throws Exception {
80          assertNotNull(component.getSimpleDataAttributes());
81          String expected = " data-iconTemplateName=\"cool-icon-%s.png\" data-transitions=\"3\"";
82          assertEquals("simple attributes did not match", expected, component.getSimpleDataAttributes());
83      }
84  
85      @Test
86      /**
87       * test that get all attributes js works ok even when the data attributes are null
88       */
89      public void testGetAllDataAttributesJs() throws Exception {
90          assertNotNull(component.getAllDataAttributesJs());
91          String expected = "jQuery('#action1').data('growing-seasons', {summer:'hot', winter:'cold'});"
92                  + "jQuery('#action1').data('iconTemplateName', 'cool-icon-%s.png');"
93                  + "jQuery('#action1').data('intervals', {short:2, medium:5, long:13});"
94                  + "jQuery('#action1').data('transitions', 3);";
95          assertEquals("all attributes JS string did not match", expected, component.getAllDataAttributesJs());
96      }
97  
98      @Test
99      /**
100      *  test that complex date attributes are converted into a jquery script ok even when data attributes are null
101      */
102     public  void testGetComplexAttributesJSWhenNull () throws Exception{
103         component.setDataAttributes(null);
104         assertEquals("complex attributes JS string did not match", "", component.getComplexDataAttributesJs());
105     }
106 
107     @Test
108     /**
109      * test that simple data attributes are converted into inline attributes ok  when data attributes are null
110      */
111     public void testGetSimpleDataAttributesWhenNull() throws Exception {
112         component.setDataAttributes(null);
113         assertEquals("simple attributes did not match", "", component.getSimpleDataAttributes());
114     }
115 
116     @Test
117     /**
118      * test that get all attributes js works ok even when the data attributes are null
119      */
120     public void testGetAllDataAttributesJsWhenNull() throws Exception {
121         component.setDataAttributes(null);
122         assertEquals("simple attributes did not match", "", component.getAllDataAttributesJs());
123     }
124 
125     /**
126      * test that controls that need to override getComplexAttributes work as expected
127      */
128     @Test
129     public void testGetComplexAttributesOverridingControls() {
130         Component[] overridingControls = {new TextControl(), new TextAreaControl(), new FileControl(), new UserControl()};
131         for (int i=0; i<overridingControls.length; i++) {
132             overridingControls[i].setDataAttributes(dataAttributes);
133             assertTrue(overridingControls[i].getClass() + " does not override getComplexAttributes",
134                     overridingControls[i].getAllDataAttributesJs().equalsIgnoreCase(
135                             overridingControls[i].getComplexDataAttributesJs()));
136         }
137         // other controls should have a different value for
138         Component[] nonOverridingControls = {new Image(), new Action(), new LinkField(), new Message()};
139         for (Component component: nonOverridingControls) {
140             component.setDataAttributes(dataAttributes);
141             assertFalse(component.getClass() + " should not override getComplexAttributes",
142                     component.getAllDataAttributesJs().equalsIgnoreCase(
143                             component.getComplexDataAttributesJs()));
144         }
145     }
146 }