samples/deadlock/test/org/apidesign/deadlock/DeadlockTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 154 0fd5e9c500b9
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
     1 package org.apidesign.deadlock;
     2 
     3 import java.awt.Dimension;
     4 import java.util.logging.Level;
     5 import java.util.logging.Logger;
     6 import javax.swing.JFrame;
     7 import javax.swing.JLabel;
     8 import javax.swing.SwingUtilities;
     9 import org.netbeans.junit.NbTestCase;
    10 import static org.junit.Assert.*;
    11 
    12 // BEGIN: deadlock.test
    13 public class DeadlockTest extends NbTestCase {
    14     static final Logger LOG = Logger.getLogger(
    15         DeadlockTest.class.getName()
    16     );
    17     
    18     public DeadlockTest(String n) {
    19         super(n);
    20     }
    21 
    22     @Override
    23     protected int timeOut() {
    24         return 10000;
    25     }
    26     
    27     
    28     public static class StrangePanel extends LabelProvider {
    29         @Override
    30         public Dimension getPreferredSize () {
    31             try {
    32                 Thread.sleep(1000);
    33                 JLabel sampleLabel = createLabel();
    34                 return sampleLabel.getPreferredSize();
    35             } catch (InterruptedException ex) {
    36                 Logger l = Logger.getLogger(
    37                     DeadlockTest.class.getName()
    38                 );
    39                 l.log(Level.SEVERE, null, ex);
    40                 return super.getPreferredSize();
    41             }
    42         }
    43     }
    44     
    45     
    46     
    47 
    48     public void testCreateLabel() throws Exception {
    49         if (Boolean.getBoolean("no.failures")) return;
    50         final LabelProvider instance = new StrangePanel();
    51         
    52         class R implements Runnable {
    53             public void run() {
    54                 JFrame f = new JFrame();
    55                 f.add(instance);
    56                 f.setVisible(true);
    57                 f.pack();
    58             }
    59         }
    60 
    61         R showFrame = new R();
    62         SwingUtilities.invokeLater(showFrame);
    63         
    64         Thread.sleep(500);
    65         JLabel result = instance.createLabel();
    66         assertNotNull("Creates the result", result);
    67     }
    68 
    69 }
    70 // END: deadlock.test
    71