|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 | +import java.awt.Component; |
| 25 | +import java.awt.EventQueue; |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.ObjectInputStream; |
| 30 | +import java.io.ObjectOutputStream; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 33 | + |
| 34 | +import javax.swing.JLabel; |
| 35 | +import javax.swing.JTable; |
| 36 | +import javax.swing.UIManager; |
| 37 | +import javax.swing.UnsupportedLookAndFeelException; |
| 38 | + |
| 39 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 40 | + |
| 41 | +/** |
| 42 | + * @test |
| 43 | + * @bug 6441373 |
| 44 | + * @summary Checks that editing/non-editing JTable is serializable |
| 45 | + * @run main/timeout=260/othervm -Xmx32m JTableSerialization |
| 46 | + */ |
| 47 | +public final class JTableSerialization { |
| 48 | + |
| 49 | + private static JTable table; |
| 50 | + private static final int ROW = 1; |
| 51 | + private static final int COLUMN = 1; |
| 52 | + private static final String SOME_TEST_LABEL = "Some TEST label"; |
| 53 | + private static final String TEST_EDIT_VALUE = "Test EDIT value"; |
| 54 | + |
| 55 | + public static void main(String[] argv) throws Exception { |
| 56 | + for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) { |
| 57 | + AtomicBoolean go = new AtomicBoolean(false); |
| 58 | + EventQueue.invokeAndWait(() -> go.set(tryLookAndFeel(laf))); |
| 59 | + if (!go.get()) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + for (boolean editing : new boolean[]{true, false}) { |
| 63 | + EventQueue.invokeAndWait(JTableSerialization::init); |
| 64 | + long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(20); |
| 65 | + while (System.nanoTime() < endtime) { |
| 66 | + // need to jump to/from EDT to flush all pending events |
| 67 | + EventQueue.invokeAndWait(() -> test(editing)); |
| 68 | + } |
| 69 | + EventQueue.invokeAndWait(JTableSerialization::validate); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static void init() { |
| 75 | + JLabel label = new JLabel(SOME_TEST_LABEL); |
| 76 | + table = new JTable(2, 2); |
| 77 | + table.add(label); |
| 78 | + table.setValueAt(TEST_EDIT_VALUE, ROW, COLUMN); |
| 79 | + checkNonEditingState(table); |
| 80 | + } |
| 81 | + |
| 82 | + private static void test(boolean editing) { |
| 83 | + if (editing) { |
| 84 | + table.editCellAt(ROW, COLUMN); |
| 85 | + checkEditingState(table); |
| 86 | + } |
| 87 | + table = copyTable(table); |
| 88 | + checkNonEditingState(table); |
| 89 | + } |
| 90 | + |
| 91 | + private static void validate() { |
| 92 | + Object value = table.getValueAt(ROW, COLUMN); |
| 93 | + if (!value.equals(TEST_EDIT_VALUE)) { |
| 94 | + throw new RuntimeException("Wrong value: " + value); |
| 95 | + } |
| 96 | + for (Component component : table.getComponents()) { |
| 97 | + if (component instanceof JLabel) { |
| 98 | + if (((JLabel) component).getText().equals(SOME_TEST_LABEL)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + throw new RuntimeException("JLabel is not found"); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + private static void checkNonEditingState(JTable jt) { |
| 108 | + if (jt.isEditing()) { |
| 109 | + throw new RuntimeException("Should not be editing"); |
| 110 | + } |
| 111 | + if (jt.getEditorComponent() != null) { |
| 112 | + throw new RuntimeException("Editor should be null"); |
| 113 | + } |
| 114 | + int row = jt.getEditingRow(); |
| 115 | + if (row != -1) { |
| 116 | + throw new RuntimeException("Expected row -1 but was: " + row); |
| 117 | + } |
| 118 | + int column = jt.getEditingColumn(); |
| 119 | + if (column != -1) { |
| 120 | + throw new RuntimeException("Expected column -1 but was: " + column); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static void checkEditingState(JTable jt) { |
| 125 | + if (!jt.isEditing()) { |
| 126 | + throw new RuntimeException("Should be editing"); |
| 127 | + } |
| 128 | + if (jt.getEditorComponent() == null) { |
| 129 | + throw new RuntimeException("Editor should not be null"); |
| 130 | + } |
| 131 | + if (jt.getEditingRow() != ROW) { |
| 132 | + throw new RuntimeException("Row should be: " + ROW); |
| 133 | + } |
| 134 | + if (jt.getEditingColumn() != COLUMN) { |
| 135 | + throw new RuntimeException("Column should be: " + COLUMN); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static JTable copyTable(JTable jt) { |
| 140 | + try { |
| 141 | + byte[] bdata; |
| 142 | + try (var baos = new ByteArrayOutputStream(); |
| 143 | + var oos = new ObjectOutputStream(baos)) |
| 144 | + { |
| 145 | + oos.writeObject(jt); |
| 146 | + bdata = baos.toByteArray(); |
| 147 | + } |
| 148 | + try (var bais = new ByteArrayInputStream(bdata); |
| 149 | + var ois = new ObjectInputStream(bais)) |
| 150 | + { |
| 151 | + return (JTable) ois.readObject(); |
| 152 | + } |
| 153 | + } catch (IOException | ClassNotFoundException e) { |
| 154 | + throw new RuntimeException(e); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static boolean tryLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 159 | + try { |
| 160 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 161 | + System.out.println("LookAndFeel: " + laf.getClassName()); |
| 162 | + return true; |
| 163 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 164 | + return false; |
| 165 | + } catch (Exception e) { |
| 166 | + throw new RuntimeException(e); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments