001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.uif.widget; 017 018 import org.junit.Before; 019 import org.junit.Test; 020 import org.kuali.rice.krad.datadictionary.validation.Account; 021 import org.kuali.rice.krad.datadictionary.validation.Employee; 022 import org.kuali.rice.krad.uif.UifConstants; 023 import org.kuali.rice.krad.uif.component.Component; 024 import org.kuali.rice.krad.uif.container.CollectionGroup; 025 import org.kuali.rice.krad.uif.field.DataField; 026 import org.kuali.rice.krad.uif.layout.TableLayoutManager; 027 import org.kuali.rice.krad.uif.view.View; 028 import org.kuali.rice.krad.web.form.UifFormBase; 029 030 import java.util.ArrayList; 031 import java.util.HashSet; 032 import java.util.List; 033 import java.util.Set; 034 035 import static org.junit.Assert.assertEquals; 036 import static org.junit.Assert.assertNotNull; 037 038 /** 039 * test the RichTable widget 040 */ 041 042 public class RichTableTest { 043 044 private RichTable richTable; 045 private CollectionGroup group; 046 047 //private 048 @Before 049 public void setup(){ 050 051 richTable = new RichTable(); 052 053 group = new CollectionGroup(); 054 group.setCollectionObjectClass(Employee.class); 055 TableLayoutManager layoutManager = new TableLayoutManager(); 056 layoutManager.setRenderSequenceField(true); 057 group.setLayoutManager(layoutManager); 058 group.setRenderSelectField(false); 059 group.setRenderLineActions(false); 060 061 List<Component> items = new ArrayList<Component>(1); 062 DataField name = new DataField(); 063 name.setPropertyName("employeeId"); 064 items.add(name); 065 DataField number = new DataField(); 066 number.setPropertyName("positionTitle"); 067 items.add(number); 068 DataField contactEmail = new DataField(); 069 contactEmail.setPropertyName("contactEmail"); 070 items.add(contactEmail); 071 072 group.setItems(items); 073 } 074 075 @Test 076 /** 077 * test that without aoColumns being set explicitly, the default behaviour continues 078 */ 079 public void testComponentOptionsDefault() throws Exception { 080 String expected = "[ null ,{\"sSortDataType\" : \"dom-text\" , \"sType\" : \"string\"} , " 081 + "{\"sSortDataType\" : \"dom-text\" , \"sType\" : \"string\"} , " 082 + "{\"sSortDataType\" : \"dom-text\" , \"sType\" : \"string\"} ]"; 083 assertRichTableComponentOptions(null, expected, UifConstants.TableToolsKeys.AO_COLUMNS); 084 } 085 086 087 @Test 088 /** 089 * test that when aoColumns is explicitly set, it is integrated into the rich table rendering logic 090 */ 091 public void testComponentOptionsAoColumnsJSOptions() throws Exception { 092 String innerColValues = "{bVisible: false}, null, null"; 093 String options = "[" + innerColValues + "]"; 094 String expected = "[ null ," + innerColValues + " ]"; 095 assertRichTableComponentOptions(options, expected, UifConstants.TableToolsKeys.AO_COLUMNS); 096 } 097 098 @Test 099 /** 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 }