Skip to content

Commit ff75d40

Browse files
HeikoKlarefedejeanne
authored andcommitted
Ensure proper cleanup after WorkspaceSessionTests
WorkspaceSessionTests overwrite the tearDown() method with an empty implementation to avoid that cleanup is performed after each test method (i.e., session). However, the cleanup functionality is not called after the last of the sessions of a test class has finished, thus potentially leaving the system in an unclean state and not ensuring test isolation. This change adds a test method to the WorkspaceSessionTest that is supposed to be executed after all actual test methods and executes the tearDown() functionality to clean up. Those WorkspaceSessionTests implementations that had overwritten the tearDown() method are adapted to properly clean up after each of their test methods if required.
1 parent 11d7170 commit ff75d40

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

resources/tests/org.eclipse.core.tests.resources.saveparticipant/src/org/eclipse/core/tests/resources/saveparticipant/SaveManager2Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.eclipse.core.tests.resources.saveparticipant1.SaveParticipant1Plugin;
2727
import org.eclipse.core.tests.resources.saveparticipant3.SaveParticipant3Plugin;
2828
import org.osgi.framework.Bundle;
29-
import org.osgi.framework.BundleException;
3029

3130
import junit.framework.Test;
3231
import junit.framework.TestSuite;

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/WorkspacePreferencesTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ protected void setUp() throws Exception {
4343
workspace.setDescription(Workspace.defaultWorkspaceDescription());
4444
}
4545

46-
@Override
47-
protected void tearDown() throws Exception {
48-
super.tearDown();
46+
private void setDefaultWorkspaceDescription() throws CoreException {
4947
workspace.setDescription(Workspace.defaultWorkspaceDescription());
5048
}
5149

5250
/**
5351
* Tests properties state in a brand new workspace (must match defaults).
5452
*/
55-
public void testDefaults() {
53+
public void testDefaults() throws CoreException {
5654
IWorkspaceDescription description = Workspace.defaultWorkspaceDescription();
5755

5856
assertEquals("1.0", description, preferences);
@@ -66,13 +64,15 @@ public void testDefaults() {
6664
for (String property : descriptionProperties) {
6765
assertTrue("2.0 - Description property is not default: " + property, defaultPropertiesList.contains(property));
6866
}
67+
68+
setDefaultWorkspaceDescription();
6969
}
7070

7171
/**
7272
* Makes changes in the preferences and ensure they are reflected in the
7373
* workspace description.
7474
*/
75-
public void testSetPreferences() {
75+
public void testSetPreferences() throws CoreException {
7676
preferences.setValue(ResourcesPlugin.PREF_AUTO_BUILDING, true);
7777
assertTrue("1.0", workspace.getDescription().isAutoBuilding());
7878

@@ -110,6 +110,8 @@ public void testSetPreferences() {
110110

111111
preferences.setValue(ResourcesPlugin.PREF_KEEP_DERIVED_STATE, true);
112112
assertTrue("4.1", workspace.getDescription().isKeepDerivedState());
113+
114+
setDefaultWorkspaceDescription();
113115
}
114116

115117
/**
@@ -152,6 +154,8 @@ public void testEvents() throws CoreException {
152154
} finally {
153155
preferences.removePropertyChangeListener(listener);
154156
}
157+
158+
setDefaultWorkspaceDescription();
155159
}
156160

157161
/**
@@ -204,6 +208,8 @@ public void testImportExport() throws CoreException {
204208
ensureDoesNotExistInFileSystem(originalPreferencesFile.removeLastSegments(1).toFile());
205209
ensureDoesNotExistInFileSystem(modifiedPreferencesFile.removeLastSegments(1).toFile());
206210
}
211+
212+
setDefaultWorkspaceDescription();
207213
}
208214

209215
/**
@@ -234,6 +240,8 @@ public void testSetDescription() throws CoreException {
234240
// the original value should remain set
235241
assertEquals("3.1", 90000, workspace.getDescription().getFileStateLongevity());
236242
assertEquals("3.2", 90000, preferences.getLong(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY));
243+
244+
setDefaultWorkspaceDescription();
237245
}
238246

239247
/**

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceSessionTest.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
* @see org.eclipse.core.tests.session.WorkspaceSessionTestSuite
3636
*/
3737
public class WorkspaceSessionTest extends ResourceTest {
38-
protected String testName;
39-
4038
/**
4139
* Constructor for WorkspaceSessionTest.
4240
*/
@@ -54,9 +52,27 @@ public WorkspaceSessionTest(String name) {
5452
super(name);
5553
}
5654

55+
/**
56+
* Executes the cleanup functionality in {@link ResourceTest#tearDown()} after
57+
* the last of the session of this test class (i.e., the last <code>test*</code>
58+
* method) has finished.
59+
* <p>
60+
* The test methods are executed in lexicographic order of their names, e.g.,
61+
* <code>test1</code> before <code>test2</code>. The name of this method is
62+
* chosen so that it comes lexicographically rather safely after all other
63+
* actual test methods.
64+
*/
65+
public void test___cleanup() throws Exception {
66+
super.tearDown();
67+
}
68+
69+
/**
70+
* Cleanup is done after all sessions of this test class in
71+
* {@link #test___cleanup()}. This method is overwritten to do nothing in order
72+
* to not cleanup between the sessions.
73+
*/
5774
@Override
58-
protected void tearDown() throws Exception {
59-
// We should not run super.tearDown() on session tests.
60-
// If needed, we should call it explicitly.
75+
protected final void tearDown() throws Exception {
6176
}
77+
6278
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/FindDeletedMembersTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.eclipse.core.resources.IProject;
2323
import org.eclipse.core.resources.IResource;
2424
import org.eclipse.core.resources.IWorkspaceRoot;
25+
import org.eclipse.core.runtime.CoreException;
2526
import org.eclipse.core.tests.resources.AutomatedResourceTests;
2627
import org.eclipse.core.tests.resources.WorkspaceSessionTest;
2728
import org.eclipse.core.tests.session.WorkspaceSessionTestSuite;
@@ -58,8 +59,7 @@ protected void setUp() throws Exception {
5859

5960
}
6061

61-
@Override
62-
protected void tearDown() throws Exception {
62+
private void saveWorkspace() throws CoreException {
6363
getWorkspace().save(true, getMonitor());
6464
}
6565

@@ -74,6 +74,8 @@ public void test1() throws Exception {
7474
// create and delete a file
7575
pfile.create(getRandomContents(), true, getMonitor());
7676
pfile.delete(true, true, getMonitor());
77+
78+
saveWorkspace();
7779
}
7880

7981
public void test2() throws Exception {
@@ -102,6 +104,8 @@ public void test2() throws Exception {
102104

103105
// recreate the file
104106
pfile.create(getRandomContents(), true, getMonitor());
107+
108+
saveWorkspace();
105109
}
106110

107111
public void test3() throws Exception {
@@ -138,6 +142,8 @@ public void test3() throws Exception {
138142
folder.create(true, true, getMonitor());
139143
file.create(getRandomContents(), true, getMonitor());
140144
file.delete(true, true, getMonitor());
145+
146+
saveWorkspace();
141147
}
142148

143149
public void test4() throws Exception {
@@ -167,6 +173,8 @@ public void test4() throws Exception {
167173

168174
// deleting the folder should bring it back into history
169175
folder.delete(true, true, getMonitor());
176+
177+
saveWorkspace();
170178
}
171179

172180
public void test5() throws Exception {
@@ -219,6 +227,8 @@ public void test5() throws Exception {
219227
file2.create(getRandomContents(), true, getMonitor());
220228
file3.create(getRandomContents(), true, getMonitor());
221229
folder.delete(true, true, getMonitor());
230+
231+
saveWorkspace();
222232
}
223233

224234
public void test6() throws Exception {
@@ -271,6 +281,8 @@ public void test6() throws Exception {
271281
assertEquals("3.12", 1, df.length);
272282

273283
project.delete(true, getMonitor());
284+
285+
saveWorkspace();
274286
}
275287

276288
public void test7() throws Exception {
@@ -314,6 +326,8 @@ public void test7() throws Exception {
314326

315327
df = folder2.findDeletedMembersWithHistory(IResource.DEPTH_INFINITE, getMonitor());
316328
assertEquals("4.12", 0, df.length);
329+
330+
saveWorkspace();
317331
}
318332

319333
public static Test suite() {

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/ProjectPreferenceSessionTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public static Test suite() {
3737
// return new ProjectPreferenceSessionTest("testDeleteFileBeforeLoad2");
3838
}
3939

40-
@Override
41-
protected void tearDown() throws Exception {
40+
private void saveWorkspace() throws Exception {
4241
getWorkspace().save(true, getMonitor());
4342
}
4443

@@ -62,6 +61,7 @@ public void testDeleteFileBeforeLoad1() throws Exception {
6261
IFile file = project.getFile(IPath.fromOSString(DIR_NAME).append(qualifier).addFileExtension(FILE_EXTENSION));
6362
assertTrue("2.0", file.exists());
6463
assertTrue("2.1", file.getLocation().toFile().exists());
64+
saveWorkspace();
6565
}
6666

6767
public void testDeleteFileBeforeLoad2() throws Exception {
@@ -85,6 +85,7 @@ public void testDeleteFileBeforeLoad2() throws Exception {
8585
} finally {
8686
Platform.removeLogListener(listener);
8787
}
88+
saveWorkspace();
8889
}
8990

9091
/*
@@ -98,13 +99,15 @@ public void testSaveLoad1() throws Exception {
9899
Preferences node = context.getNode("test.save.load");
99100
node.put("key", "value");
100101
node.flush();
102+
saveWorkspace();
101103
}
102104

103-
public void testSaveLoad2() {
105+
public void testSaveLoad2() throws Exception {
104106
IProject project = getProject("testSaveLoad");
105107
IScopeContext context = new ProjectScope(project);
106108
Preferences node = context.getNode("test.save.load");
107109
assertEquals("1.0", "value", node.get("key", null));
110+
saveWorkspace();
108111
}
109112

110113
private static IProject getProject(String name) {

0 commit comments

Comments
 (0)