Path: blob/master/test/jdk/javax/swing/JTable/DefaultRowSorterCacheTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 700796727* @summary Verifies that DefaultRowSorter updates its cache properly when the28* all the row are deleted and then new rows are added to the table29* @run main DefaultRowSorterCacheTest30*/313233import javax.swing.table.DefaultTableModel;34import javax.swing.table.TableRowSorter;35import javax.swing.SwingUtilities;3637class CustomTableModel extends DefaultTableModel38{39public CustomTableModel(Object[][] data, Object[] columnNames) {40super(data, columnNames);41}4243@Override44public Class<?> getColumnClass(int columnIndex) {45if (getRowCount() > 0) {46return getValueAt(0, columnIndex).getClass();47}48return super.getColumnClass(columnIndex);49}50}5152public class DefaultRowSorterCacheTest {5354public void testSort() {55Object[] values = new Object[]{1, 2, 10};56Object[][] data = new Object[][]{{values[0]}, {values[1]}, {values[2]}};5758//Create custom table model59DefaultTableModel model = new CustomTableModel(data, new Object[]{"A"});6061TableRowSorter<DefaultTableModel> rowSorter =62new TableRowSorter<DefaultTableModel>(model);63rowSorter.toggleSortOrder(0);6465for (int row = 0; row < model.getRowCount(); row++) {66// values are in sorted ascending. so the row index and67// view index from sorter should be same68if (row != rowSorter.convertRowIndexToView(row)) {69throw new RuntimeException("Wrong sorting before making any " +70"changes in test case");71}72}7374// clear model and notify sorter75model.setRowCount(0);76rowSorter.rowsDeleted(0, values.length - 1);7778// refill the model and notify sorter79for (int i = 0; i < values.length; i++) {80model.addRow(new Object[]{values[i]});81rowSorter.rowsInserted(i, i);82}8384for (int row = 0; row < model.getRowCount(); row++) {85// values are in sorted ascending. so the row index and86// view index from sorter should be same87if (row != rowSorter.convertRowIndexToView(row)) {88throw new RuntimeException("Wrong sorting at end of test case");89}90}91}9293public static void main(String[] args) throws Exception{94SwingUtilities.invokeAndWait(new Runnable() {95@Override96public void run() {97new DefaultRowSorterCacheTest().testSort();98}99});100}101}102103104