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