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       8048887 8164937
  27   @summary   Tests SortingFTP for an exception caused by the tim-sort algo.
  28   @author    anton.tarasov: area=awt.focus
  29   @run       main JDK8048887
  30 */
  31 
  32 import javax.swing.JFrame;
  33 import javax.swing.JPanel;
  34 import javax.swing.SwingUtilities;
  35 import java.awt.Dimension;
  36 import java.awt.Color;
  37 import java.awt.GridBagLayout;
  38 import java.awt.GridBagConstraints;
  39 import java.awt.event.WindowAdapter;
  40 import java.awt.event.WindowEvent;
  41 import java.util.concurrent.CountDownLatch;
  42 import java.util.concurrent.TimeUnit;
  43 
  44 public class JDK8048887 {
  45 
  46     static volatile boolean passed = true;
  47 
  48     public static void main(String[] args) {
  49         JDK8048887 app = new JDK8048887();
  50         app.start();
  51     }
  52 
  53     public void start() {
  54         final CountDownLatch latch = new CountDownLatch(1);
  55 
  56         SwingUtilities.invokeLater(() -> {
  57                 // Catch the original exception which sounds like:
  58                 // java.lang.IllegalArgumentException: Comparison method violates its general contract!
  59                 Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  60                         public void uncaughtException(Thread t, Throwable e) {
  61                             e.printStackTrace();
  62                             if (e instanceof IllegalArgumentException) {
  63                                 passed = false;
  64                                 latch.countDown();
  65                             }
  66                         }
  67                     });
  68 
  69                 TestDialog d = new TestDialog();
  70                 // It's expected that the dialog is focused on start.
  71                 // The listener is called after the FTP completes processing and the bug is reproduced or not.
  72                 d.addWindowFocusListener(new WindowAdapter() {
  73                         public void windowGainedFocus(WindowEvent e) {
  74                             latch.countDown();
  75                         }
  76                 });
  77                 d.setVisible(true);
  78         });
  79 
  80         try {
  81             latch.await(5, TimeUnit.SECONDS);
  82         } catch (InterruptedException e) {
  83             e.printStackTrace();
  84         }
  85 
  86         if (passed)
  87             System.out.println("Test passed.");
  88         else
  89             throw new RuntimeException("Test failed!");
  90     }
  91 }
  92 
  93 class TestDialog extends JFrame {
  94 
  95     // The layout of the components reproduces the transitivity issue
  96     // with SortingFocusTraversalPolicy relying on the tim-sort algo.
  97 
  98     private static int[] Xs = new int[] {71, 23, 62, 4, 79, 39, 34, 9, 84, 58, 30, 34, 38, 15, 69, 10, 44, 95, 70, 54,
  99     44, 62, 77, 64, 70, 83, 31, 48, 96, 54, 40, 3, 60, 58, 3, 20, 94, 54, 26, 19, 48, 47, 12, 70, 86, 43, 71, 97, 19,
 100     69, 90, 22, 43, 76, 10, 60, 29, 49, 9, 9, 15, 73, 85, 80, 81, 35, 87, 43, 17, 57, 38, 44, 29, 86, 96, 15, 57, 26,
 101     27, 78, 26, 87, 43, 6, 4, 16, 57, 99, 32, 86, 96, 5, 50, 69, 12, 4, 36, 84, 71, 60, 22, 46, 11, 44, 87, 3, 23, 14,
 102     43, 25, 32, 44, 11, 18, 77, 2, 51, 87, 88, 53, 69, 37, 14, 10, 25, 73, 39, 33, 91, 51, 96, 9, 74, 66, 70, 42, 72,
 103     7, 82, 40, 91, 33, 83, 54, 33, 50, 83, 1, 81, 32, 66, 11, 75, 56, 53, 45, 1, 69, 46, 31, 79, 58, 12, 20, 92, 49,
 104     50, 90, 33, 8, 43, 93, 72, 78, 9, 56, 84, 60, 30, 39, 33, 88, 84, 56, 49, 47, 4, 90, 57, 6, 23, 96, 37, 88, 22, 79,
 105     35, 80, 45, 55};
 106 
 107     public TestDialog() {
 108         JPanel panel = new JPanel(new GridBagLayout());
 109         GridBagConstraints gbc = new GridBagConstraints();
 110         for (int i=0; i < Xs.length; i++) {
 111             gbc.gridx = Xs[i];
 112             gbc.gridy = 100 - gbc.gridx;
 113             panel.add(new MyComponent(), gbc);
 114         }
 115         getRootPane().getContentPane().add(panel);
 116         pack();
 117     }
 118 
 119     public static class MyComponent extends JPanel {
 120         private final static Dimension SIZE = new Dimension(1,1);
 121 
 122         public MyComponent() {
 123             setBackground(Color.BLACK);
 124             setOpaque(true);
 125         }
 126 
 127         @Override
 128         public Dimension getPreferredSize() {
 129             return SIZE;
 130         }
 131     }
 132 }