samples/deadlock/test/org/apidesign/deadlock/DeadlockSyncOnTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 28 Jun 2010 09:29:30 +0200
changeset 356 23511f8a9718
parent 105 5c7a65ed657a
permissions -rw-r--r--
Dealing with random failures on overloaded machines: preload JLabel and its classes; increase the timeOut
     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 public class DeadlockSyncOnTest extends NbTestCase {
    13     static final Logger LOG = Logger.getLogger(DeadlockSyncOnTest.class.getName());
    14     private static final JLabel preLoadClasses = new JLabel();
    15     
    16     public DeadlockSyncOnTest(String n) {
    17         super(n);
    18     }
    19 
    20     @Override
    21     protected int timeOut() {
    22         return 30000;
    23     }
    24     
    25     
    26     public static class StrangePanel extends LabelProviderSyncOnTreeLock {
    27         @Override
    28         public Dimension getPreferredSize () {
    29             try {
    30                 Thread.sleep(1000);
    31                 JLabel sampleLabel = createLabel();
    32                 return sampleLabel.getPreferredSize();
    33             } catch (InterruptedException ex) {
    34                 Logger.getLogger(DeadlockSyncOnTest.class.getName()).log(Level.SEVERE, null, ex);
    35                 return super.getPreferredSize();
    36             }
    37         }
    38     }
    39     
    40     
    41     
    42 
    43     public void testCreateLabel() throws Exception {
    44         final LabelProviderSyncOnTreeLock instance = new StrangePanel();
    45         
    46         class R implements Runnable {
    47             public void run() {
    48                 JFrame f = new JFrame();
    49                 f.add(instance);
    50                 f.setVisible(true);
    51                 f.pack();
    52             }
    53         }
    54 
    55         R showFrame = new R();
    56         SwingUtilities.invokeLater(showFrame);
    57         
    58         Thread.sleep(500);
    59         JLabel result = instance.createLabel();
    60         assertNotNull("Creates the result", result);
    61     }
    62 
    63 }
    64