Onward! Towards deadlock tests.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/LabelProvider.java Sat Jun 14 09:54:28 2008 +0200
1.3 @@ -0,0 +1,24 @@
1.4 +package org.apidesign.deadlock;
1.5 +
1.6 +import java.util.HashSet;
1.7 +import java.util.logging.Level;
1.8 +import java.util.logging.Logger;
1.9 +import javax.swing.JLabel;
1.10 +import javax.swing.JPanel;
1.11 +
1.12 +public class LabelProvider extends JPanel {
1.13 + private static Logger LOG = Logger.getLogger(LabelProvider.class.getName());
1.14 +
1.15 + // BEGIN: deadlock.provider
1.16 + private HashSet<JLabel> allCreated = new HashSet<JLabel>();
1.17 +
1.18 + public synchronized JLabel createLabel () {
1.19 + LOG.log(Level.INFO, "Will create JLabel");
1.20 + JLabel l = new JLabel ();
1.21 + LOG.log(Level.INFO, "Label created {0}", l);
1.22 + allCreated.add (l);
1.23 + return l;
1.24 + }
1.25 + // END: deadlock.provider
1.26 +
1.27 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/ExecutorServiceTest.java Sat Jun 14 09:54:28 2008 +0200
2.3 @@ -0,0 +1,34 @@
2.4 +package org.apidesign.deadlock;
2.5 +
2.6 +import java.util.concurrent.ExecutorService;
2.7 +import java.util.concurrent.Executors;
2.8 +import java.util.concurrent.Future;
2.9 +import java.util.concurrent.TimeUnit;
2.10 +import org.junit.Assert;
2.11 +import org.junit.Test;
2.12 +
2.13 +public class ExecutorServiceTest implements Runnable {
2.14 + private int cnt;
2.15 +
2.16 + @Test
2.17 + public void showUsageOfExecutionService() throws Exception {
2.18 + Runnable someRunnable = this;
2.19 +
2.20 + // BEGIN: deadlock.service
2.21 + ExecutorService service = Executors.newSingleThreadExecutor();
2.22 +
2.23 + // scheduling is quite different
2.24 + Future<?> task = service.submit(someRunnable);
2.25 +
2.26 + // compared to waiting
2.27 + task.get(1000, TimeUnit.MILLISECONDS);
2.28 + // END: deadlock.service
2.29 +
2.30 + Assert.assertEquals("Cnt executed", 1, cnt);
2.31 + }
2.32 +
2.33 + public void run() {
2.34 + cnt++;
2.35 + }
2.36 +
2.37 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/LabelProviderTest.java Sat Jun 14 09:54:28 2008 +0200
3.3 @@ -0,0 +1,71 @@
3.4 +package org.apidesign.deadlock;
3.5 +
3.6 +import java.awt.Dimension;
3.7 +import java.util.logging.Logger;
3.8 +import javax.swing.JFrame;
3.9 +import javax.swing.JLabel;
3.10 +import javax.swing.JPanel;
3.11 +import javax.swing.SwingUtilities;
3.12 +import org.junit.Test;
3.13 +import org.netbeans.junit.Log;
3.14 +import org.netbeans.junit.NbTestCase;
3.15 +import static org.junit.Assert.*;
3.16 +
3.17 +public class LabelProviderTest extends NbTestCase {
3.18 + static final Logger LOG = Logger.getLogger(LabelProviderTest.class.getName());
3.19 +
3.20 + public LabelProviderTest(String n) {
3.21 + super(n);
3.22 + }
3.23 +
3.24 + @Override
3.25 + protected int timeOut() {
3.26 + return 10000;
3.27 + }
3.28 +
3.29 +
3.30 + public static class StrangePanel extends LabelProvider {
3.31 + @Override
3.32 + // BEGIN: deadlock.pref.size
3.33 + public Dimension getPreferredSize () {
3.34 + JLabel sampleLabel = createLabel();
3.35 + return sampleLabel.getPreferredSize ();
3.36 + }
3.37 + // END: deadlock.pref.size
3.38 + }
3.39 +
3.40 +
3.41 +
3.42 +
3.43 + public void testCreateLabel() {
3.44 + final LabelProvider instance = new StrangePanel();
3.45 +
3.46 + class R implements Runnable {
3.47 + public void run() {
3.48 + LOG.info("In AWT thread");
3.49 + JFrame f = new JFrame();
3.50 + f.add(instance);
3.51 + f.setVisible(true);
3.52 + f.pack();
3.53 + }
3.54 + }
3.55 +
3.56 + Log.controlFlow(
3.57 + Logger.getLogger("org.apidesign"),
3.58 + Logger.getLogger("global"),
3.59 + "THREAD: main MSG: Begin" +
3.60 + "THREAD: .*AWT.* MSG: In.*thread" +
3.61 + "THREAD: main MSG: Will create JLabel" +
3.62 + "THREAD: .*AWT.* MSG: Will create JLabel",
3.63 + 300
3.64 + );
3.65 +
3.66 + R showFrame = new R();
3.67 + SwingUtilities.invokeLater(showFrame);
3.68 +
3.69 + LOG.info("Begin");
3.70 + JLabel result = instance.createLabel();
3.71 + assertNotNull("Creates the result", result);
3.72 + }
3.73 +
3.74 +}
3.75 \ No newline at end of file