1 /*
   2  * Copyright (c) 2011, 2013, 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  @summary <rdar://problem/3755801>
  27  @summary Empty String causes Table Header height to be miscalculated
  28  @summary com.apple.junit.javax.swing.JTable.JTableHeader
  29  @run junit EmptyTableHeader
  30  */
  31 import junit.framework.*;
  32 
  33 import javax.swing.*;
  34 import javax.swing.table.AbstractTableModel;
  35 import javax.swing.table.JTableHeader;
  36 import java.awt.*;
  37 import sun.awt.SunToolkit;
  38 
  39 public class EmptyTableHeader extends TestCase {
  40 
  41     protected void setUp() {
  42         // called by JUnit before the actual testing
  43     }
  44     JTable fTable;
  45     JFrame fFrame;
  46 
  47     public void testEmptyHeader() throws Exception {
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49             @Override
  50             public void run() {
  51                 fFrame = new JFrame("Test Window");
  52 
  53 
  54                 // Create a panel to hold all other components
  55                 JPanel topPanel = new JPanel();
  56                 topPanel.setLayout(new BorderLayout());
  57 
  58                 // Create a new table instance
  59                 MyTableModel myModel = new MyTableModel();
  60                 fTable = new JTable(myModel);
  61 
  62                 // Add the table to a scrolling pane
  63                 JScrollPane scrollPane = new JScrollPane(fTable);
  64                 topPanel.add(scrollPane, BorderLayout.CENTER);
  65 
  66 
  67                 fFrame.getContentPane().setLayout(new BorderLayout());
  68                 fFrame.getContentPane().add(BorderLayout.CENTER, topPanel);
  69 
  70 
  71                 fFrame.setSize(400, 450);
  72                 fFrame.setLocation(20, 20);
  73                 fFrame.setVisible(true);
  74             }
  75         });
  76         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
  77         JTableHeader header = fTable.getTableHeader();
  78         assertTrue("JTableHeader greater than 5 pixels tall with empty string first element.",
  79                 header.getSize().height > 5);
  80         SwingUtilities.invokeAndWait(new Runnable() {
  81             @Override
  82             public void run() {
  83                 fFrame.setVisible(false);
  84                 fFrame.dispose();
  85             }
  86         });
  87     }
  88 
  89     protected void tearDown() throws Exception {
  90         SwingUtilities.invokeAndWait(new Runnable() {
  91             @Override
  92             public void run() {
  93                 fFrame.setVisible(false);
  94                 fFrame.dispose();
  95             }
  96         });
  97     }
  98 
  99     class MyTableModel extends AbstractTableModel {
 100 
 101         final String[] columnNames = {"",
 102             "Last Name",
 103             "Sport"};
 104         final Object[][] data = {
 105             {"Scott", "Adler",
 106                 "Snowboarding"}
 107         };
 108 
 109         @Override
 110         public int getColumnCount() {
 111             return columnNames.length;
 112         }
 113 
 114         @Override
 115         public int getRowCount() {
 116             return data.length;
 117         }
 118 
 119         @Override
 120         public String getColumnName(int col) {
 121             return columnNames[col];
 122         }
 123 
 124         @Override
 125         public Object getValueAt(int row, int col) {
 126             return data[row][col];
 127         }
 128 
 129         /*
 130          * JTable uses this method to determine the default renderer/
 131          * editor for each cell.  If we didn't implement this method,
 132          * then the last column would contain text ("true"/"false"),
 133          * rather than a check box.
 134          */
 135         public Class<?> getColumnClass(int c) {
 136             return getValueAt(0, c).getClass();
 137         }
 138     }
 139 
 140     //-----------------------------------------------------------------------------
 141     //                  BOILERPLATE CODE FROM HERE ON DOWN
 142     //-----------------------------------------------------------------------------
 143     public static Test suite() {
 144         // This assembles a test suite.
 145         // It will trigger all testXXX() calls in the specified class.
 146         return new TestSuite(EmptyTableHeader.class);
 147     }
 148 
 149     public static void main(String[] args) throws RuntimeException {
 150         TestResult tr = junit.textui.TestRunner.run(suite());
 151         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 152             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
 153         }
 154     }
 155 }