author | pbansal |
Thu, 01 Feb 2018 16:38:25 +0530 | |
changeset 48733 | 8850bd195bbd |
permissions | -rw-r--r-- |
48733
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
1 |
/* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
4 |
* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
8 |
* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
13 |
* accompanied this code). |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
14 |
* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
18 |
* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
21 |
* questions. |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
22 |
*/ |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
23 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
24 |
/* |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
25 |
* @test |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
26 |
* @key headful |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
27 |
* @bug 7007967 |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
28 |
* @summary Verifies that DefaultRowSorter updates its cache properly when the |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
29 |
* all the row are deleted and then new rows are added to the table |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
30 |
* @run main DefaultRowSorterCacheTest |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
31 |
*/ |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
32 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
33 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
34 |
import javax.swing.table.DefaultTableModel; |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
35 |
import javax.swing.table.TableRowSorter; |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
36 |
import javax.swing.SwingUtilities; |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
37 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
38 |
class CustomTableModel extends DefaultTableModel |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
39 |
{ |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
40 |
public CustomTableModel(Object[][] data, Object[] columnNames) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
41 |
super(data, columnNames); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
42 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
43 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
44 |
@Override |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
45 |
public Class<?> getColumnClass(int columnIndex) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
46 |
if (getRowCount() > 0) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
47 |
return getValueAt(0, columnIndex).getClass(); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
48 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
49 |
return super.getColumnClass(columnIndex); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
50 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
51 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
52 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
53 |
public class DefaultRowSorterCacheTest { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
54 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
55 |
public void testSort() { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
56 |
Object[] values = new Object[]{1, 2, 10}; |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
57 |
Object[][] data = new Object[][]{{values[0]}, {values[1]}, {values[2]}}; |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
58 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
59 |
//Create custom table model |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
60 |
DefaultTableModel model = new CustomTableModel(data, new Object[]{"A"}); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
61 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
62 |
TableRowSorter<DefaultTableModel> rowSorter = |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
63 |
new TableRowSorter<DefaultTableModel>(model); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
64 |
rowSorter.toggleSortOrder(0); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
65 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
66 |
for (int row = 0; row < model.getRowCount(); row++) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
67 |
// values are in sorted ascending. so the row index and |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
68 |
// view index from sorter should be same |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
69 |
if (row != rowSorter.convertRowIndexToView(row)) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
70 |
throw new RuntimeException("Wrong sorting before making any " + |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
71 |
"changes in test case"); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
72 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
73 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
74 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
75 |
// clear model and notify sorter |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
76 |
model.setRowCount(0); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
77 |
rowSorter.rowsDeleted(0, values.length - 1); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
78 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
79 |
// refill the model and notify sorter |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
80 |
for (int i = 0; i < values.length; i++) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
81 |
model.addRow(new Object[]{values[i]}); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
82 |
rowSorter.rowsInserted(i, i); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
83 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
84 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
85 |
for (int row = 0; row < model.getRowCount(); row++) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
86 |
// values are in sorted ascending. so the row index and |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
87 |
// view index from sorter should be same |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
88 |
if (row != rowSorter.convertRowIndexToView(row)) { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
89 |
throw new RuntimeException("Wrong sorting at end of test case"); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
90 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
91 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
92 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
93 |
|
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
94 |
public static void main(String[] args) throws Exception{ |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
95 |
SwingUtilities.invokeAndWait(new Runnable() { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
96 |
@Override |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
97 |
public void run() { |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
98 |
new DefaultRowSorterCacheTest().testSort(); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
99 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
100 |
}); |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
101 |
} |
8850bd195bbd
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents:
diff
changeset
|
102 |
} |