1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8160087
27 * @summary Change IOOBE to warning in the scenarios when it had not being
28 *          thrown before the JDK-8078514
29 * @run main/othervm DefaultRowSorterIOOBEtest
30 */
31
32import javax.swing.*;
33import javax.swing.table.AbstractTableModel;
34import javax.swing.table.TableModel;
35import javax.swing.table.TableRowSorter;
36import java.io.ByteArrayOutputStream;
37import java.io.PrintStream;
38import java.util.ArrayList;
39import java.util.List;
40
41public class DefaultRowSorterIOOBEtest extends TableRowSorter<TableModel> {
42    static List<String> rows = new ArrayList<>();
43
44    static TableModel tableModel = new AbstractTableModel() {
45
46        @Override
47        public int getRowCount() {
48            return rows.size();
49        }
50
51        @Override
52        public int getColumnCount() {
53            return 1;
54        }
55
56        @Override
57        public Object getValueAt(int rowIndex, int columnIndex) {
58            return rows.get(rowIndex);
59        }
60    };
61
62    public static void main(String[] args) {
63        DefaultRowSorter<TableModel, Integer> sorter =
64            new DefaultRowSorter<>() {
65            {
66                setModelWrapper(new SorterModelWrapper());
67            }
68        };
69
70        PrintStream err = System.err;
71        ByteArrayOutputStream bos = new ByteArrayOutputStream(10000) {
72            @Override
73            public synchronized void write(byte[] b, int off, int len) {
74                super.write(b, off, len);
75                err.print(new String(b, off, len));
76            }
77        };
78        System.setErr(new PrintStream(bos));
79
80        rows.add("New");
81
82        sorter.convertRowIndexToView(0);
83        sorter.convertRowIndexToModel(0);
84
85        String out = new String(bos.toByteArray());
86        if(out.indexOf("WARNING:") < 0) {
87            throw new RuntimeException("No warnings found");
88        }
89    }
90
91    static class SorterModelWrapper extends
92                            DefaultRowSorter.ModelWrapper<TableModel, Integer> {
93
94        @Override
95        public TableModel getModel() {
96            return tableModel;
97        }
98
99        @Override
100        public int getColumnCount() {
101            return tableModel.getColumnCount();
102        }
103
104        @Override
105        public int getRowCount() {
106            return tableModel.getRowCount();
107        }
108
109        @Override
110        public Object getValueAt(int row, int column) {
111            return tableModel.getValueAt(row, column);
112        }
113
114        @Override
115        public Integer getIdentifier(int row) {
116            return row;
117        }
118    }
119}
120