|
10 | 10 | *******************************************************************************/
|
11 | 11 | package org.eclipse.rcptt.internal.core.model;
|
12 | 12 |
|
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | + |
13 | 15 | import java.io.IOException;
|
14 | 16 | import java.io.InputStream;
|
| 17 | +import java.util.function.Consumer; |
15 | 18 |
|
16 | 19 | import org.eclipse.core.resources.IFile;
|
17 | 20 | import org.eclipse.core.resources.IProject;
|
18 | 21 | import org.eclipse.core.resources.IProjectDescription;
|
| 22 | +import org.eclipse.core.resources.IResource; |
19 | 23 | import org.eclipse.core.resources.IWorkspace;
|
20 | 24 | import org.eclipse.core.resources.ResourcesPlugin;
|
21 | 25 | import org.eclipse.core.runtime.CoreException;
|
@@ -54,17 +58,40 @@ public void before() throws CoreException {
|
54 | 58 | }
|
55 | 59 |
|
56 | 60 | @Test
|
57 |
| - public void noResourceleaks() throws CoreException, IOException { |
58 |
| - try (InputStream is = getClass().getResourceAsStream("testcase.test")) { |
59 |
| - TESTCASE_FILE.create(is, IFile.REPLACE|IFile.FORCE, null); |
60 |
| - } |
61 |
| - IFile previousFile = TESTCASE_FILE; |
62 |
| - for (int i = 0; i < 10000; i++) { |
63 |
| - IFile currentFile = PROJECT.getFile("t"+i+".test"); |
64 |
| - previousFile.move(currentFile.getFullPath(), true, false, null); |
65 |
| - previousFile = currentFile; |
66 |
| - ITestCase testcase = (ITestCase) RcpttCore.create(currentFile); |
67 |
| - Assert.assertEquals("_-dqP0BOHEeOQfY3L4mNcSA", testcase.getID()); |
| 61 | + public void noResourceleaksID() { |
| 62 | + noResourceleaks(testcase -> Assert.assertEquals("_-dqP0BOHEeOQfY3L4mNcSA", testcase.getID())); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void noResourceleaksExists() { |
| 67 | + noResourceleaks(testcase -> assertTrue(testcase.exists())); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private interface ThrowingConsumer<T> { |
| 72 | + void accept(T data) throws Exception; |
| 73 | + } |
| 74 | + |
| 75 | + public void noResourceleaks(ThrowingConsumer<ITestCase> action) { |
| 76 | + try { |
| 77 | + try (InputStream is = getClass().getResourceAsStream("testcase.test")) { |
| 78 | + TESTCASE_FILE.create(is, IFile.REPLACE|IFile.FORCE, null); |
| 79 | + } |
| 80 | + IFile previousFile = TESTCASE_FILE; |
| 81 | + for (int i = 0; i < 1000; i++) { |
| 82 | + IFile currentFile = PROJECT.getFile("t"+i+".test"); |
| 83 | + previousFile.move(currentFile.getFullPath(), true, false, null); |
| 84 | + previousFile = currentFile; |
| 85 | + ITestCase testcase = (ITestCase) RcpttCore.create(currentFile); |
| 86 | + action.accept(testcase); |
| 87 | + if ( i % 2 == 0) { |
| 88 | + action.accept(testcase); // sometimes single access does not cause leak |
| 89 | + } |
| 90 | + System.gc(); // Detect leaks with org.eclipse.rcptt.core.persistence.LeakDetector |
| 91 | + NO_ERRORS.assertNoErrors(); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + throw new AssertionError(e); |
68 | 95 | }
|
69 | 96 | }
|
70 | 97 |
|
|
0 commit comments