samples/deadlock/test/org/apidesign/deadlock/DeadlockTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 105 5c7a65ed657a
child 133 50bf1b976c0d
permissions -rw-r--r--
Truncating all examples to 80 characters per line
     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.JPanel;
     9 import javax.swing.SwingUtilities;
    10 import org.junit.Test;
    11 import org.netbeans.junit.Log;
    12 import org.netbeans.junit.NbTestCase;
    13 import static org.junit.Assert.*;
    14 
    15 // BEGIN: deadlock.test
    16 public class DeadlockTest extends NbTestCase {
    17     static final Logger LOG = Logger.getLogger(
    18         DeadlockTest.class.getName()
    19     );
    20     
    21     public DeadlockTest(String n) {
    22         super(n);
    23     }
    24 
    25     @Override
    26     protected int timeOut() {
    27         return 10000;
    28     }
    29     
    30     
    31     public static class StrangePanel extends LabelProvider {
    32         @Override
    33         public Dimension getPreferredSize () {
    34             try {
    35                 Thread.sleep(1000);
    36                 JLabel sampleLabel = createLabel();
    37                 return sampleLabel.getPreferredSize();
    38             } catch (InterruptedException ex) {
    39                 Logger.getLogger(
    40                     DeadlockTest.class.getName()).log(Level.SEVERE, null, ex
    41                 );
    42                 return super.getPreferredSize();
    43             }
    44         }
    45     }
    46     
    47     
    48     
    49 
    50     public void testCreateLabel() throws Exception {
    51         final LabelProvider instance = new StrangePanel();
    52         
    53         class R implements Runnable {
    54             public void run() {
    55                 JFrame f = new JFrame();
    56                 f.add(instance);
    57                 f.setVisible(true);
    58                 f.pack();
    59             }
    60         }
    61 
    62         R showFrame = new R();
    63         SwingUtilities.invokeLater(showFrame);
    64         
    65         Thread.sleep(500);
    66         JLabel result = instance.createLabel();
    67         assertNotNull("Creates the result", result);
    68     }
    69 
    70 }
    71 // END: deadlock.test
    72