jdk/test/javax/swing/JTable/6894632/bug6894632.java
author vromero
Thu, 05 Jan 2017 11:16:39 -0800
changeset 43033 c37b643739a7
parent 40128 e635645d2a8a
permissions -rw-r--r--
8148100: Convert lambda most specific positive tests to check runtime behavior Reviewed-by: mcimadamore Contributed-by: bsrbnd@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29893
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     1
/*
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     4
 *
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     8
 *
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    13
 * accompanied this code).
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    14
 *
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    18
 *
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    21
 * questions.
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    22
 */
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    23
40128
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    24
/*
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    25
 * @test
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    26
 * @key headful
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    27
 * @bug 6894632
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    28
 * @summary Removing rows from a DefaultTableModel with a RowSorter deselectes
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    29
 * last row
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    30
 * @author Semyon Sadetsky
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 29893
diff changeset
    31
 */
29893
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    32
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    33
import javax.swing.*;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    34
import javax.swing.table.DefaultTableModel;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    35
import javax.swing.table.TableModel;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    36
import javax.swing.table.TableRowSorter;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    37
import java.util.ArrayList;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    38
import java.util.List;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    39
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    40
public class bug6894632 {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    41
    private static JTable table;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    42
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    44
        SwingUtilities.invokeAndWait(new Runnable() {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    45
            public void run() {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    46
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    47
                //init table with empty sort order
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    48
                test(new ArrayList<RowSorter.SortKey>());
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    49
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    50
                //init table as unsorted
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    51
                List<RowSorter.SortKey> sortKeys = new ArrayList<>();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    52
                sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    53
                test(sortKeys);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    54
            }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    55
        });
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    56
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    57
        System.out.println("ok");
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    58
    }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    59
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    60
    static void test(final List<RowSorter.SortKey> sortKeys) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    61
        final JFrame frame = new JFrame();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    62
        try {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    63
            frame.setUndecorated(true);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    64
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    65
            table = new JTable();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    66
            DefaultTableModel tableModel =
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    67
                    new DefaultTableModel(10, 1) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    68
                        public Object getValueAt(int row, int column) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    69
                            return row == getRowCount() - 1 ? row + "==last" :
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    70
                                    row;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    71
                        }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    72
                    };
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    73
            table.setModel(tableModel);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    74
            TableRowSorter<TableModel> sorter =
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    75
                    new TableRowSorter<TableModel>(tableModel);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    76
            sorter.setSortKeys(sortKeys);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    77
            table.setRowSorter(sorter);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    78
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    79
            frame.setContentPane(table);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    80
            frame.pack();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    81
            frame.setLocationRelativeTo(null);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    82
            frame.setVisible(true);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    83
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    84
            int lastRow = table.getRowCount() - 1;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    85
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    86
            //select last row
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    87
            table.setRowSelectionInterval(lastRow, lastRow);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    88
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    89
            //remove row above the last
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    90
            tableModel.removeRow(lastRow - 1);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    91
            lastRow = table.getRowCount() - 1;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    92
            if (lastRow != table.getSelectedRow()) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    93
                throw new RuntimeException("last row must be still selected");
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    94
            }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    95
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    96
            //sort table
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    97
            sortKeys.clear();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    98
            sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.ASCENDING));
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
    99
            sorter.setSortKeys(sortKeys);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   100
            //remove row above the last
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   101
            lastRow = table.getRowCount() - 1;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   102
            tableModel.removeRow(lastRow - 1);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   103
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   104
            if (!table.getValueAt(table.getSelectedRow(), 0).toString()
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   105
                    .endsWith("==last")) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   106
                throw new RuntimeException(
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   107
                        "row ends with \"==last\" row must be still selected");
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   108
            }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   109
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   110
            //make table unsorted again
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   111
            sortKeys.clear();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   112
            sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   113
            sorter.setSortKeys(sortKeys);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   114
            //remove row above the last
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   115
            lastRow = table.getRowCount() - 1;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   116
            tableModel.removeRow(lastRow - 1);
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   117
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   118
            lastRow = table.getRowCount() - 1;
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   119
            if (lastRow != table.getSelectedRow()) {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   120
                throw new RuntimeException(
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   121
                        "last row must be still selected");
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   122
            }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   123
        } finally {
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   124
            frame.dispose();
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   125
        }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   126
    }
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   127
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   128
fb1610d0d75a 6894632: Removing rows from a DefaultTableModel with a RowSorter deselectes last row
ssadetsky
parents:
diff changeset
   129
}