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.widget;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.krad.datadictionary.validation.Employee;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.container.CollectionGroup;
24  import org.kuali.rice.krad.uif.field.DataField;
25  import org.kuali.rice.krad.uif.layout.TableLayoutManager;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.web.form.UifFormBase;
28  
29  import java.util.ArrayList;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Set;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * test the RichTable widget
38   */
39  
40  public class RichTableTest {
41  
42      public static final String S_TYPE = "{\"sType\" : \"numeric\", \"aTargets\": [0]}";
43      public static final String S_SORT_DATA_TARGETS_1 = "{\"sSortDataType\" : \"dom-text\" , \"sType\" : \"string\", \"aTargets\": [1]}";
44      public static final String S_SORT_DATA_TARGETS_2 = S_SORT_DATA_TARGETS_1.replace("1", "2");
45      public static final String S_SORT_DATA_TARGETS_3 = S_SORT_DATA_TARGETS_1.replace("1", "3");
46  
47      public static final String EXPECTED = S_TYPE + ", " +
48              S_SORT_DATA_TARGETS_1 + " , " +
49              S_SORT_DATA_TARGETS_2 + " , " +
50              S_SORT_DATA_TARGETS_3;
51  
52      public static final String B_VISIBLE_FALSE_TARGETS_1 = "{bVisible: false, \"aTargets\": [1]}";
53      public static final String B_SORTABLE_FALSE_TARGETS_3 = "{'bSortable': false, \"aTargets\": [3]}";
54      
55      private RichTable richTable;
56      private CollectionGroup group;
57  
58      //private
59      @Before
60      public void setup() {
61  
62          richTable = new RichTable();
63  
64          group = new CollectionGroup();
65          group.setCollectionObjectClass(Employee.class);
66          TableLayoutManager layoutManager = new TableLayoutManager();
67          layoutManager.setRenderSequenceField(true);
68          group.setLayoutManager(layoutManager);
69          group.setIncludeLineSelectionField(false);
70          group.setRenderLineActions(false);
71  
72          List<Component> items = new ArrayList<Component>(1);
73          DataField name = new DataField();
74          name.setPropertyName("employeeId");
75          items.add(name);
76          DataField number = new DataField();
77          number.setPropertyName("positionTitle");
78          items.add(number);
79          DataField contactEmail = new DataField();
80          contactEmail.setPropertyName("contactEmail");
81          items.add(contactEmail);
82  
83          group.setItems(items);
84      }
85  
86      @Test
87      /**
88       * test that without aoColumns being set explicitly, the default behaviour continues
89       */
90      public void testComponentOptionsDefault() throws Exception {
91          assertRichTableComponentOptions(null, "[" + EXPECTED + " ]", UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
92      }
93  
94      @Test
95      /**
96       * test that when aoColumns is explicitly set, it is integrated into the rich table rendering logic
97       */
98      public void testComponentOptionsAoColumnsJSOptions() throws Exception {
99          String innerColValues = "{bVisible: false}, null, null";
100         assertRichTableComponentOptions("[" + innerColValues + "]", "[" + EXPECTED + " ," + innerColValues + "]", UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
101     }
102 
103     @Test
104     /**
105      * test whether a hidden column, when marked as sortable is still hidden
106      */
107     public void testComponentOptionsHideColumnOnRichTable() {
108         Set<String> hiddenColumns = new HashSet<String>();
109         hiddenColumns.add("employeeId");
110         Set<String> sortableColumns = new HashSet<String>();
111         sortableColumns.add("positionTitle");
112         richTable.setSortableColumns(sortableColumns);
113         richTable.setHiddenColumns(hiddenColumns);
114         String expected = "[" + S_TYPE + ", " +
115                 B_VISIBLE_FALSE_TARGETS_1 + ", " +
116                 S_SORT_DATA_TARGETS_2 +", " +
117                 B_SORTABLE_FALSE_TARGETS_3 + "]";
118         assertRichTableComponentOptions(null, expected, UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
119     }
120 
121     @Test
122     /**
123      * test that sortableColumns and hiddenColumns, when set on layoutManager, will not override those properties on the richTable
124      */
125     public void testComponentOptionsHideColumnOnLayoutManager() {
126         // set rich table properties
127         Set<String> richTableHiddenColumns = new HashSet<String>();
128         richTableHiddenColumns.add("employeeId");
129         Set<String> sortableColumns = new HashSet<String>();
130         sortableColumns.add("positionTitle");
131         richTable.setSortableColumns(sortableColumns);
132         richTable.setHiddenColumns(richTableHiddenColumns);
133         // set layout manager properties
134         Set<String> lmHiddenColumns = new HashSet<String>();
135         lmHiddenColumns.add("contactEmail");
136         Set<String> lmSortableColumns = new HashSet<String>();
137         lmSortableColumns.add("employeeId");
138         ((TableLayoutManager) group.getLayoutManager()).setSortableColumns(lmSortableColumns);
139         ((TableLayoutManager) group.getLayoutManager()).setHiddenColumns(lmHiddenColumns);
140         // Watch out for spaces
141         String expected = "[" + EXPECTED.replace(S_SORT_DATA_TARGETS_1 + " ,", B_VISIBLE_FALSE_TARGETS_1 + ",") + "]";
142         expected = expected.replace(S_SORT_DATA_TARGETS_2 + " ,", S_SORT_DATA_TARGETS_2 + ",");
143         expected = expected.replace(S_SORT_DATA_TARGETS_3, B_SORTABLE_FALSE_TARGETS_3);
144         assertRichTableComponentOptions(null, expected, UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
145     }
146 
147     /**
148      * a common method to test rich table options
149      *
150      * @param optionsOnGroup - a string in JSON format of the options set on the collection group
151      * @param optionsOnRichTable - a string in JSON format of the options set on the rich table
152      * @param optionKey - a string with the rich table option key being tested
153      */
154     private void assertRichTableComponentOptions(String optionsOnGroup, String optionsOnRichTable, String optionKey) {
155         richTable.getTemplateOptions().put(optionKey, optionsOnGroup);
156         richTable.performFinalize(new View(), new UifFormBase(), group);
157         assertEquals(optionsOnRichTable, richTable.getTemplateOptions().get(optionKey));
158     }
159 }