Skip to content

Commit 9b3c91d

Browse files
committed
Cleanup fail() calls in several ResourceTest implementations #903
* Removes unnecessary try-catch blocks or replaces them with assertThrows statements * Throw exceptions instead of calling fail() * Remove unnecessary cleanup functionality Contributes to #903
1 parent fd794b4 commit 9b3c91d

18 files changed

+445
-1074
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/properties/PropertyManagerTest.java

Lines changed: 39 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static org.eclipse.core.tests.resources.ResourceTestPluginConstants.PI_RESOURCES_TESTS;
1818
import static org.junit.Assert.assertNotEquals;
19+
import static org.junit.Assert.assertThrows;
1920

2021
import java.nio.charset.StandardCharsets;
2122
import java.util.ArrayList;
@@ -57,18 +58,14 @@ public String getStringValue() {
5758
}
5859
}
5960

60-
private void createProperties(IFile target, QualifiedName[] names, String[] values) {
61+
private void createProperties(IFile target, QualifiedName[] names, String[] values) throws CoreException {
6162
for (int i = 0; i < names.length; i++) {
6263
names[i] = new QualifiedName("org.eclipse.core.tests", "prop" + i);
6364
values[i] = "property value" + i;
6465
}
6566
// create properties
6667
for (int i = 0; i < names.length; i++) {
67-
try {
68-
target.setPersistentProperty(names[i], values[i]);
69-
} catch (CoreException e) {
70-
fail("1." + i, e);
71-
}
68+
target.setPersistentProperty(names[i], values[i]);
7269
}
7370
}
7471

@@ -112,29 +109,14 @@ protected void doGetSetProperties(IFile target, String threadID, QualifiedName[]
112109
}
113110
}
114111

115-
private void join(Thread[] threads) {
116-
//wait for all threads to finish
117-
for (Thread thread : threads) {
118-
try {
119-
thread.join();
120-
} catch (InterruptedException e) {
121-
fail("#join", e);
122-
}
123-
}
124-
}
125-
126112
/**
127113
* Tests concurrent acces to the property store.
128114
*/
129-
public void testConcurrentAccess() {
115+
public void testConcurrentAccess() throws Exception {
130116

131117
// create common objects
132118
final IFile target = projects[0].getFile("target");
133-
try {
134-
target.create(getRandomContents(), true, getMonitor());
135-
} catch (CoreException e) {
136-
fail("0.0", e);
137-
}
119+
target.create(getRandomContents(), true, getMonitor());
138120

139121
// prepare keys and values
140122
final int N = 50;
@@ -144,24 +126,19 @@ public void testConcurrentAccess() {
144126

145127
final CoreException[] errorPointer = new CoreException[1];
146128
Thread[] threads = createThreads(target, names, values, errorPointer);
147-
join(threads);
148-
if (errorPointer[0] != null) {
149-
fail("2.0", errorPointer[0]);
129+
for (Thread thread : threads) {
130+
thread.join();
150131
}
151-
152-
// remove trash
153-
try {
154-
target.delete(true, getMonitor());
155-
} catch (CoreException e) {
156-
fail("20.0", e);
132+
if (errorPointer[0] != null) {
133+
throw errorPointer[0];
157134
}
158135
}
159136

160137
/**
161138
* Tests concurrent access to the property store while the project is being
162139
* deleted.
163140
*/
164-
public void testConcurrentDelete() throws CoreException {
141+
public void testConcurrentDelete() throws Exception {
165142
Thread[] threads;
166143
final IFile target = projects[0].getFile("target");
167144
final int REPEAT = 8;
@@ -178,29 +155,17 @@ public void testConcurrentDelete() throws CoreException {
178155

179156
final CoreException[] errorPointer = new CoreException[1];
180157
threads = createThreads(target, names, values, errorPointer);
181-
try {
182-
//give the threads a chance to start
183-
Thread.sleep(10);
184-
} catch (InterruptedException e) {
185-
fail("1.98", e);
186-
}
187-
try {
188-
//delete the project while the threads are still running
189-
target.getProject().delete(IResource.NONE, getMonitor());
190-
} catch (CoreException e) {
191-
fail("1.99." + i, e);
158+
// give the threads a chance to start
159+
Thread.sleep(10);
160+
// delete the project while the threads are still running
161+
target.getProject().delete(IResource.NONE, getMonitor());
162+
for (Thread thread : threads) {
163+
thread.join();
192164
}
193-
join(threads);
194165
if (errorPointer[0] != null) {
195-
fail("2.0." + i, errorPointer[0]);
166+
throw errorPointer[0];
196167
}
197168
}
198-
// remove trash
199-
try {
200-
target.delete(true, getMonitor());
201-
} catch (CoreException e) {
202-
fail("20.0", e);
203-
}
204169
}
205170

206171
public void testCache() throws Throwable {
@@ -398,7 +363,6 @@ public void testDeleteProperties() throws Throwable {
398363
assertNull("3.1", manager.getProperty(source, propName));
399364
assertNull("3.2", manager.getProperty(sourceFolder, propName));
400365
assertNull("3.3", manager.getProperty(sourceFile, propName));
401-
402366
}
403367

404368
/**
@@ -411,33 +375,16 @@ public void testFileRename() throws CoreException {
411375
IFile file1a = folder.getFile("file1");
412376
ensureExistsInWorkspace(file1a, true);
413377
QualifiedName key = new QualifiedName(PI_RESOURCES_TESTS, "key");
414-
try {
415-
file1a.setPersistentProperty(key, "value");
416-
} catch (CoreException e) {
417-
fail("0.5", e);
418-
}
419-
try {
420-
file1a.move(IPath.fromOSString("file2"), true, getMonitor());
421-
} catch (CoreException e) {
422-
fail("0.6", e);
423-
}
378+
file1a.setPersistentProperty(key, "value");
379+
file1a.move(IPath.fromOSString("file2"), true, getMonitor());
424380
IFile file1b = folder.getFile("file1");
425381
ensureExistsInWorkspace(file1b, true);
426382
String value = null;
427-
try {
428-
value = file1b.getPersistentProperty(key);
429-
} catch (CoreException e) {
430-
fail("0.8", e);
431-
}
383+
value = file1b.getPersistentProperty(key);
432384
assertNull("1.0", value);
433385
file1a = folder.getFile("file2");
434-
try {
435-
value = file1a.getPersistentProperty(key);
436-
} catch (CoreException e) {
437-
fail("1.9", e);
438-
}
386+
value = file1a.getPersistentProperty(key);
439387
assertEquals("2.0", "value", value);
440-
441388
}
442389

443390
/**
@@ -449,46 +396,25 @@ public void testFolderRename() throws CoreException {
449396
IFolder folder1a = project.getFolder("folder1");
450397
ensureExistsInWorkspace(folder1a, true);
451398
QualifiedName key = new QualifiedName(PI_RESOURCES_TESTS, "key");
452-
try {
453-
folder1a.setPersistentProperty(key, "value");
454-
} catch (CoreException e) {
455-
fail("0.5", e);
456-
}
457-
try {
458-
folder1a.move(IPath.fromOSString("folder2"), true, getMonitor());
459-
} catch (CoreException e) {
460-
fail("0.6", e);
461-
}
399+
folder1a.setPersistentProperty(key, "value");
400+
folder1a.move(IPath.fromOSString("folder2"), true, getMonitor());
462401
IFolder folder1b = project.getFolder("folder1");
463402
ensureExistsInWorkspace(folder1b, true);
464403
String value = null;
465-
try {
466-
value = folder1b.getPersistentProperty(key);
467-
} catch (CoreException e) {
468-
fail("0.8", e);
469-
}
404+
value = folder1b.getPersistentProperty(key);
470405
assertNull("1.0", value);
471406
folder1a = project.getFolder("folder2");
472-
try {
473-
value = folder1a.getPersistentProperty(key);
474-
} catch (CoreException e) {
475-
fail("1.9", e);
476-
}
407+
value = folder1a.getPersistentProperty(key);
477408
assertEquals("2.0", "value", value);
478-
479409
}
480410

481411
/**
482412
* Do a stress test by adding a very large property to the store.
483413
*/
484-
public void testLargeProperty() {
414+
public void testLargeProperty() throws CoreException {
485415
// create common objects
486416
IFile target = projects[0].getFile("target");
487-
try {
488-
target.create(getRandomContents(), true, getMonitor());
489-
} catch (CoreException e) {
490-
fail("0.0", e);
491-
}
417+
target.create(getRandomContents(), true, getMonitor());
492418

493419
QualifiedName name = new QualifiedName("stressTest", "prop");
494420
final int SIZE = 10000;
@@ -497,20 +423,7 @@ public void testLargeProperty() {
497423
valueBuf.append("a");
498424
}
499425
String value = valueBuf.toString();
500-
try {
501-
target.setPersistentProperty(name, value);
502-
//should fail
503-
fail("1.0");
504-
} catch (CoreException e) {
505-
// expected
506-
}
507-
508-
// remove trash
509-
try {
510-
target.delete(true, getMonitor());
511-
} catch (CoreException e) {
512-
fail("20.0", e);
513-
}
426+
assertThrows(CoreException.class, () -> target.setPersistentProperty(name, value));
514427
}
515428

516429
/**
@@ -521,32 +434,15 @@ public void testProjectRename() throws CoreException {
521434
IProject project1a = root.getProject("proj1");
522435
ensureExistsInWorkspace(project1a, true);
523436
QualifiedName key = new QualifiedName(PI_RESOURCES_TESTS, "key");
524-
try {
525-
project1a.setPersistentProperty(key, "value");
526-
} catch (CoreException e) {
527-
fail("0.5", e);
528-
}
529-
try {
530-
project1a.move(IPath.fromOSString("proj2"), true, getMonitor());
531-
} catch (CoreException e) {
532-
fail("0.6", e);
533-
}
437+
project1a.setPersistentProperty(key, "value");
438+
project1a.move(IPath.fromOSString("proj2"), true, getMonitor());
534439
IProject project1b = root.getProject("proj1");
535440
ensureExistsInWorkspace(project1b, true);
536-
String value = null;
537-
try {
538-
value = project1b.getPersistentProperty(key);
539-
} catch (CoreException e) {
540-
fail("0.8", e);
541-
}
441+
String value = project1b.getPersistentProperty(key);
542442
assertNull("1.0", value);
543443

544444
project1a = root.getProject("proj2");
545-
try {
546-
value = project1a.getPersistentProperty(key);
547-
} catch (CoreException e) {
548-
fail("1.9", e);
549-
}
445+
value = project1a.getPersistentProperty(key);
550446
assertEquals("2.0", "value", value);
551447
}
552448

@@ -582,20 +478,13 @@ public void testProperties() throws Throwable {
582478
}
583479
assertEquals("3.0", 0, manager.getProperties(target).size());
584480
manager.deleteProperties(target, IResource.DEPTH_INFINITE);
585-
586-
// remove trash
587-
target.delete(false, monitor);
588481
}
589482

590-
public void testSimpleUpdate() {
483+
public void testSimpleUpdate() throws CoreException {
591484

592485
// create common objects
593486
IFile target = projects[0].getFile("target");
594-
try {
595-
target.create(getRandomContents(), true, getMonitor());
596-
} catch (CoreException e) {
597-
fail("0.0", e);
598-
}
487+
target.create(getRandomContents(), true, getMonitor());
599488

600489
// prepare keys and values
601490
int N = 3;
@@ -608,51 +497,27 @@ public void testSimpleUpdate() {
608497

609498
// create properties
610499
for (int i = 0; i < N; i++) {
611-
try {
612-
target.setPersistentProperty(names[i], values[i]);
613-
} catch (CoreException e) {
614-
fail("1." + i, e);
615-
}
500+
target.setPersistentProperty(names[i], values[i]);
616501
}
617502

618503
// verify
619504
for (int i = 0; i < N; i++) {
620-
try {
621-
assertTrue("2.0", target.getPersistentProperty(names[i]).equals(values[i]));
622-
} catch (CoreException e) {
623-
fail("3." + i, e);
624-
}
505+
assertTrue("2.0", target.getPersistentProperty(names[i]).equals(values[i]));
625506
}
626507

627508
for (int j = 0; j < 20; j++) {
628-
629509
// change properties
630510
for (int i = 0; i < N; i++) {
631-
try {
632-
values[i] = values[i] + " - changed";
633-
target.setPersistentProperty(names[i], values[i]);
634-
} catch (CoreException e) {
635-
fail("4." + i, e);
636-
}
511+
values[i] = values[i] + " - changed";
512+
target.setPersistentProperty(names[i], values[i]);
637513
}
638514

639515
// verify
640516
for (int i = 0; i < N; i++) {
641-
try {
642-
assertTrue("5.0", target.getPersistentProperty(names[i]).equals(values[i]));
643-
} catch (CoreException e) {
644-
fail("6." + i, e);
645-
}
517+
assertTrue("5.0", target.getPersistentProperty(names[i]).equals(values[i]));
646518
}
647519

648520
}
649-
650-
// remove trash
651-
try {
652-
target.delete(true, getMonitor());
653-
} catch (CoreException e) {
654-
fail("20.0", e);
655-
}
656521
}
657522

658523
}

0 commit comments

Comments
 (0)