|
14 | 14 | *******************************************************************************/
|
15 | 15 | package org.eclipse.swt.tests.junit;
|
16 | 16 |
|
| 17 | +import org.eclipse.swt.graphics.Resource; |
| 18 | +import org.junit.AfterClass; |
| 19 | +import org.junit.BeforeClass; |
17 | 20 | import org.junit.runner.JUnitCore;
|
18 | 21 | import org.junit.runner.RunWith;
|
19 | 22 | import org.junit.runners.Suite;
|
20 | 23 |
|
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
21 | 27 | /**
|
22 | 28 | * Suite for running most SWT test cases (all except for browser tests).
|
23 | 29 | */
|
|
41 | 47 | Test_org_eclipse_swt_accessibility_AccessibleEvent.class,
|
42 | 48 | Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class })
|
43 | 49 | public class AllNonBrowserTests {
|
| 50 | + private static List<Error> leakedResources; |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public static void beforeClass() { |
| 54 | + // Set up ResourceTracked to detect any leaks |
| 55 | + leakedResources = new ArrayList<> (); |
| 56 | + Resource.setNonDisposeHandler (error -> { |
| 57 | + // Can't 'Assert.fail()' here, because Handler is called on |
| 58 | + // some other thread and Exception will simply be ignored. |
| 59 | + // Workaround: collect errors and report later. |
| 60 | + leakedResources.add (error); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + * It would be easier to understand if errors here were reported |
| 66 | + * through a test and not through @AfterClass, but this is a |
| 67 | + * suite class and not a test class, so it can't have tests. |
| 68 | + */ |
| 69 | + @AfterClass |
| 70 | + public static void afterClass() { |
| 71 | + // Run GC in order do detect any outstanding leaks |
| 72 | + System.gc (); |
| 73 | + // And wait a bit to let Resource.ResourceTracker, that is |
| 74 | + // queued to another thread, do its job. It is unfortunate that |
| 75 | + // there is no simple better way to wait except waiting some |
| 76 | + // fixed timeout. |
| 77 | + try { |
| 78 | + Thread.sleep (100); |
| 79 | + } catch (InterruptedException e) { |
| 80 | + // Just ignore |
| 81 | + } |
| 82 | + |
| 83 | + for (Error leak : leakedResources) { |
| 84 | + // For some reason, printing to System.err in JUnit test has no effect |
| 85 | + leak.printStackTrace (System.out); |
| 86 | + } |
| 87 | + |
| 88 | + if (0 != leakedResources.size ()) { |
| 89 | + // In order to help tools to filter fail reason out of entire log, throw the first leak. |
| 90 | + String hint = leakedResources.size () + " leaks found, the first is:"; |
| 91 | + throw new AssertionError(hint, leakedResources.get (0)); |
| 92 | + } |
| 93 | + } |
44 | 94 |
|
45 | 95 | public static void main(String[] args) {
|
46 | 96 | JUnitCore.main(AllNonBrowserTests.class.getName());
|
|
0 commit comments