Skip to content

Commit 81b2533

Browse files
HeikoKlareakurtakov
authored andcommitted
Simplify and improve several assertions in org.eclipse.ui.tests
- Replace fail statements with proper assertions - Remove unused code after fail statements - Remove obsolete error messages
1 parent 30af5e8 commit 81b2533

17 files changed

+45
-130
lines changed

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/internal/ide/DirectoryProposalContentAssistTestCase.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import static org.junit.Assert.assertEquals;
1717
import static org.junit.Assert.assertTrue;
18-
import static org.junit.Assert.fail;
1918

2019
import java.util.Arrays;
2120
import java.util.Objects;
@@ -87,9 +86,7 @@ public void assertProposalSize(int size) {
8786
Shell[] shells = getFieldAssistWindow().getDisplay().getShells();
8887
Optional<Table> tableOptional = Arrays.stream(shells).map(this::retrieveTable)
8988
.filter(Objects::nonNull).findFirst();
90-
if (!tableOptional.isPresent()) {
91-
fail("Couldn't assert pop-up proposal size - pop-up seems closed.");
92-
}
89+
assertTrue("Couldn't assert pop-up proposal size - pop-up seems closed.", tableOptional.isPresent());
9390
TableItem[] proposals = tableOptional.get().getItems();
9491

9592
assertEquals("Proposal size must be " + size, size, proposals.length);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/activities/PersistanceTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
package org.eclipse.ui.tests.activities;
1616

1717
import static org.junit.Assert.assertFalse;
18+
import static org.junit.Assert.assertNotEquals;
1819
import static org.junit.Assert.assertNotNull;
1920
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
2121

2222
import org.eclipse.ui.PlatformUI;
2323
import org.eclipse.ui.activities.IActivity;
@@ -44,9 +44,7 @@ public void testCategoryPermutations() throws NotDefinedException {
4444
assertNotNull(category.getDescription());
4545

4646
for (String string : manager.getDefinedCategoryIds()) {
47-
if (manager.getCategory(string).getName().equals("org.eclipse.ui.PT.C3")) {
48-
fail("Found category that should not be.");
49-
}
47+
assertNotEquals("org.eclipse.ui.PT.C3", manager.getCategory(string).getName());
5048
}
5149
}
5250

@@ -82,9 +80,7 @@ public void testActivityPermutations() throws NotDefinedException {
8280
assertNotNull(activity.getDescription());
8381

8482
for (String string : manager.getDefinedActivityIds()) {
85-
if (manager.getActivity(string).getName().equals("org.eclipse.ui.PT.A3")) {
86-
fail("Found activity that should not be.");
87-
}
83+
assertNotEquals("org.eclipse.ui.PT.A3", manager.getActivity(string).getName());
8884
}
8985
}
9086
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IAggregateWorkingSetTest.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,8 @@ public void XXXtestWorkingSetSaveRestoreAggregates() throws Throwable {
307307
assertNotNull("Unable to save/restore correctly", restoredC);
308308
assertNotNull("Unable to save/restore correctly", restoredB);
309309

310-
IWorkingSet[] componenents1 = wSetB.getComponents();
311-
IWorkingSet[] componenents2 = restoredB.getComponents();
312-
313-
if (componenents1.length != componenents2.length) {
314-
assertArrayEquals(nameB + " has lost data in the process of save/restore: " + restoredB,
315-
wSetB.getComponents(), restoredB.getComponents());
316-
} else {
317-
for (int i = 0; i < componenents1.length; i++) {
318-
if (!componenents1[i].equals(componenents2[i])) {
319-
assertEquals(nameB + " has lost data in the process of save/restore: " + restoredB,
320-
componenents1[i].toString(), componenents2[i].toString());
321-
fail("equals() and toString() do not match for: " + componenents1[i] + " and "
322-
+ componenents2[i]);
323-
}
324-
}
325-
}
326-
310+
assertArrayEquals(nameB + " has lost data in the process of save/restore: " + restoredB,
311+
wSetB.getComponents(), restoredB.getComponents());
327312
} finally {
328313
// restore
329314
IWorkingSet set = manager.getWorkingSet(nameA);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IEditorPartTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ public void testGetShellAfterClose() throws Throwable {
137137
}
138138

139139
List<IStatus> list = errors.get("org.eclipse.ui.workbench");
140-
if (list == null || list.isEmpty()) {
141-
fail("No error reported on accessing shell after part disposal");
142-
}
140+
assertFalse("No error reported on accessing shell after part disposal", list == null || list.isEmpty());
143141
assertEquals(1, list.size());
144142
Throwable ex = list.get(0).getException();
145143
assertTrue("Unexpected exception: " + ex, ex instanceof IllegalStateException);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/ModalContextCrashTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*******************************************************************************/
1515
package org.eclipse.ui.tests.concurrency;
1616

17+
import static org.junit.Assert.assertFalse;
1718
import static org.junit.Assert.assertThrows;
1819
import static org.junit.Assert.fail;
1920

@@ -39,6 +40,7 @@ public void testCrash() throws Exception {
3940
if (Thread.interrupted()) {
4041
fail("Thread was interrupted at end of test");
4142
}
43+
assertFalse(Thread.interrupted());
4244
}
4345

4446
private static final class CrashingRunnable implements IRunnableWithProgress, IThreadListener {

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/NestedSyncExecDeadlockTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
**********************************************************************/
1515
package org.eclipse.ui.tests.concurrency;
1616

17-
import static org.junit.Assert.fail;
17+
import static org.junit.Assert.assertFalse;
1818

1919
import org.eclipse.core.resources.IProject;
2020
import org.eclipse.core.resources.IResource;
@@ -111,16 +111,12 @@ public void tearDown() throws Exception {
111111
@Test
112112
public void testDeadlock() throws Exception {
113113
doTest(1000 * 30); // 30 secs almost always locks
114-
if (Thread.interrupted()) {
115-
fail("Thread was interrupted at end of test");
116-
}
114+
assertFalse(Thread.interrupted());
117115
}
118116

119117
@Test
120118
public void testOK() throws Exception {
121119
doTest(0); // 0 rarely locks
122-
if (Thread.interrupted()) {
123-
fail("Thread was interrupted at end of test");
124-
}
120+
assertFalse(Thread.interrupted());
125121
}
126122
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/SyncExecWhileUIThreadWaitsForLock.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.ui.tests.concurrency;
1616

1717
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
1819
import static org.junit.Assert.assertTrue;
1920
import static org.junit.Assert.fail;
2021

@@ -86,9 +87,7 @@ public void tearDown() throws Exception {
8687

8788
@Test
8889
public void testDeadlock() throws Exception {
89-
if (Thread.interrupted()) {
90-
fail("Thread was interrupted at start of test");
91-
}
90+
assertFalse(Thread.interrupted());
9291
final ILock lock = Job.getJobManager().newLock();
9392
final boolean[] blocked = new boolean[] {false};
9493
final boolean[] lockAcquired= new boolean[] {false};
@@ -163,9 +162,6 @@ public void run() {
163162
assertTrue("Unexpected: " + label, label.startsWith("UI thread"));
164163
label = ((LogEntry) children[1]).getMessage();
165164
assertTrue("Unexpected: " + label, label.startsWith("SyncExec"));
166-
if (Thread.interrupted()) {
167-
// TODO: re-enable this check after bug 505920 is fixed
168-
// fail("Thread was interrupted at end of test");
169-
}
165+
assertFalse(Thread.interrupted());
170166
}
171167
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/TestBug105491.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.concurrency;
1515

16+
import static org.junit.Assert.assertFalse;
1617
import static org.junit.Assert.fail;
1718

1819
import java.lang.reflect.InvocationTargetException;
@@ -79,9 +80,7 @@ public void threadChange(Thread thread) {
7980
*/
8081
@Test
8182
public void testBug() throws CoreException {
82-
if (Thread.interrupted()) {
83-
fail("Thread was interrupted at start of test");
84-
}
83+
assertFalse(Thread.interrupted());
8584

8685
if (Util.isWindows()) {
8786
// unstable on Windows with 2 cores, see bug 543693
@@ -98,8 +97,6 @@ public void testBug() throws CoreException {
9897
// ignore
9998
}
10099
}, workspace.getRoot(), IResource.NONE, null);
101-
if (Thread.interrupted()) {
102-
fail("Thread was interrupted at end of test");
103-
}
100+
assertFalse(Thread.interrupted());
104101
}
105102
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/TransferRuleTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.concurrency;
1515

16-
import static org.junit.Assert.fail;
17-
18-
import java.lang.reflect.InvocationTargetException;
19-
2016
import org.eclipse.core.runtime.IProgressMonitor;
2117
import org.eclipse.core.runtime.jobs.ISchedulingRule;
2218
import org.eclipse.core.runtime.jobs.Job;
@@ -87,7 +83,7 @@ public void threadChange(Thread thread) {
8783
}
8884

8985
@Test
90-
public void testModalContextTransfer() throws InvocationTargetException, InterruptedException {
86+
public void testModalContextTransfer() throws Throwable {
9187
ISchedulingRule rule = new TestRule();
9288
TestRunnable runnable = new TestRunnable(rule);
9389
try {
@@ -101,8 +97,7 @@ public void testModalContextTransfer() throws InvocationTargetException, Interru
10197
}
10298
//check the runnable for errors
10399
if (runnable.error != null) {
104-
runnable.error.printStackTrace();
105-
fail("1.2");
100+
throw runnable.error;
106101
}
107102
}
108103
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/ExportArchiveFileOperationTest.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.util.ArrayList;
22+
import java.util.Arrays;
2223
import java.util.Enumeration;
2324
import java.util.HashSet;
2425
import java.util.List;
@@ -327,9 +328,7 @@ protected void doSetUp() throws Exception {
327328
new File(FileSystemHelper.getRandomLocation(FileSystemHelper.getTempDir())
328329
.toOSString());
329330
localDirectory = destination.getAbsolutePath();
330-
if (!destination.mkdirs()) {
331-
fail("Could not set up destination directory for " + getName());
332-
}
331+
assertTrue(destination.mkdirs());
333332
setUpData();
334333
flattenPaths = false;
335334
excludeProjectPath = false;
@@ -344,9 +343,7 @@ protected void doTearDown() throws Exception {
344343
File[] files = root.listFiles();
345344
if (files != null){
346345
for (File file : files) {
347-
if (!file.delete()) {
348-
fail("Could not delete " + file.getAbsolutePath());
349-
}
346+
assertTrue("Could not delete " + file.getAbsolutePath(), file.delete());
350347
}
351348
}
352349
root.delete();
@@ -471,16 +468,13 @@ private void verifyFiles(List<String> files) {
471468
}
472469

473470
private void verifyFile(String entryName){
474-
for (String fileName : fileNames) {
475-
boolean dotProjectFileShouldBePresent = ".project".equals(entryName) && !flattenPaths && !excludeProjectPath;
476-
if (fileName.equals(entryName) || dotProjectFileShouldBePresent) {
477-
return;
478-
}
479-
}
480471
if (entryName.equals("org.eclipse.core.resources.prefs")) {
481472
return;
482473
}
483-
fail("Could not find file named: " + entryName);
474+
assertTrue("Could not find file named: " + entryName, Arrays.stream(fileNames).anyMatch(fileName -> {
475+
boolean dotProjectFileShouldBePresent = ".project".equals(entryName) && !flattenPaths && !excludeProjectPath;
476+
return fileName.equals(entryName) || dotProjectFileShouldBePresent;
477+
}));
484478
}
485479

486480
private void verifyFolders(Set<String> folderNames) {
@@ -489,11 +483,8 @@ private void verifyFolders(Set<String> folderNames) {
489483
continue;
490484
}
491485
if (!isDirectory(folderName)){
492-
if (flattenPaths) {
493-
fail(folderName + " is not an expected folder");
494-
} else if (!project.getName().equals(folderName)) {
495-
fail(folderName + " is not an expected folder");
496-
}
486+
assertFalse(folderName + " is not an expected folder",
487+
flattenPaths || !project.getName().equals(folderName));
497488
}
498489
}
499490
}

0 commit comments

Comments
 (0)