View Javadoc
1   /**
2    * Copyright 2005-2014 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 static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  import java.util.TreeMap;
30  
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.kuali.rice.krad.uif.element.Action;
34  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
35  import org.kuali.rice.krad.uif.service.ViewHelperService;
36  import org.kuali.rice.krad.uif.util.CopyUtils;
37  import org.kuali.rice.krad.uif.view.View;
38  
39  /**
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class ComponentBaseTest {
43      private Component component;
44      private TreeMap<String, String> dataAttributes;
45  
46      @Before
47      public void setUp() throws Exception {
48          // use an action field, since ComponentBase is abstract
49          component = new Action();
50          component.setId("action1");
51          // used a TreeMap since it makes specific guarantees as to the order of entries
52          dataAttributes = new TreeMap<String, String>();
53          // set data attributes - for testing purposes only - they do not have any functional significance
54          dataAttributes.put("iconTemplateName", "cool-icon-%s.png");
55          dataAttributes.put("transitions", "3");
56          component.setDataAttributes(dataAttributes);
57      }
58  
59      @Test
60      /**
61       * test that simple data attributes are converted into inline attributes ok
62       */
63      public void testGetSimpleDataAttributes() throws Exception {
64          assertNotNull(component.getSimpleDataAttributes());
65          String expected = " data-iconTemplateName=\"cool-icon-%s.png\" data-transitions=\"3\"";
66          assertEquals("simple attributes did not match", expected, component.getSimpleDataAttributes());
67      }
68  
69      @Test
70      /**
71       * test that simple data attributes are converted into inline attributes ok  when data attributes are null
72       */
73      public void testGetSimpleDataAttributesWhenNull() throws Exception {
74          View view = mock(View.class);
75          ViewHelperService helper = mock(ViewHelperService.class);
76          when(view.getViewHelperService()).thenReturn(helper);
77          ViewLifecycle.encapsulateLifecycle(view, null, null, new Runnable(){
78              @Override
79              public void run() {
80                  Component copy = CopyUtils.copy(component);
81                  copy.setDataAttributes(null);
82                  assertEquals("simple attributes did not match", "", copy.getSimpleDataAttributes());
83              }});
84      }
85  }