Skip to content

Commit 7ec52db

Browse files
HeikoKlareakurtakov
authored andcommitted
Migrate try-catch-fail patterns to assertThrows
Replace try-catch-fail exception testing patterns with JUnit assertThrows, extracting any in-catch assertions or state resets to after the assertThrows call.
1 parent 2c05fab commit 7ec52db

3 files changed

Lines changed: 90 additions & 209 deletions

File tree

  • tests
    • org.eclipse.jface.tests.databinding.conformance/src/org/eclipse/jface/databinding/conformance/util
    • org.eclipse.jface.tests/src/org/eclipse/jface/tests/fieldassist
    • org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions

tests/org.eclipse.jface.tests.databinding.conformance/src/org/eclipse/jface/databinding/conformance/util/RealmTester.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.eclipse.jface.databinding.conformance.util;
1717

18+
import static org.junit.Assert.assertThrows;
19+
1820
import org.eclipse.core.databinding.observable.Realm;
1921
import org.eclipse.core.runtime.AssertionFailedException;
2022
import org.junit.Assert;
@@ -64,11 +66,8 @@ public static void exerciseCurrent(Runnable runnable) {
6466
previousRealm.setCurrent(false);
6567
}
6668

67-
try {
68-
runnable.run();
69-
Assert.fail("Incorrect realm, exception should have been thrown");
70-
} catch (AssertionFailedException e) {
71-
}
69+
assertThrows("Incorrect realm, exception should have been thrown", AssertionFailedException.class,
70+
runnable::run);
7271
} finally {
7372
setDefault(previousRealm);
7473
}
@@ -90,10 +89,7 @@ public static void exerciseCurrent(Runnable runnable, CurrentRealm realm) {
9089

9190
realm.setCurrent(false);
9291

93-
try {
94-
runnable.run();
95-
Assert.fail("Incorrect realm, exception should have been thrown");
96-
} catch (AssertionFailedException e) {
97-
}
92+
assertThrows("Incorrect realm, exception should have been thrown", AssertionFailedException.class,
93+
runnable::run);
9894
}
9995
}

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/fieldassist/FieldAssistAPITests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
******************************************************************************/
1515
package org.eclipse.jface.tests.fieldassist;
1616

17-
import static org.junit.jupiter.api.Assertions.assertEquals;
1817
import static org.junit.jupiter.api.Assertions.assertNull;
19-
import static org.junit.jupiter.api.Assertions.fail;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

2121
import org.eclipse.jface.fieldassist.ContentProposal;
2222
import org.eclipse.jface.fieldassist.IContentProposal;
@@ -67,12 +67,9 @@ public void testContentProposalWithDescription() {
6767

6868
@Test
6969
public void testInitializationWithInvalidCursor() {
70-
try {
71-
proposal = new ContentProposal(content, label, description, 100);
72-
fail("4.0");
73-
} catch (IllegalArgumentException e) {
74-
assertNull(proposal, "It is expected to be null");
75-
}
70+
assertThrows(IllegalArgumentException.class,
71+
() -> proposal = new ContentProposal(content, label, description, 100), "4.0");
72+
assertNull(proposal, "It is expected to be null");
7673
}
7774

7875
@Override

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions/RangeTest.java

Lines changed: 79 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.workbench.texteditor.tests.revisions;
1515

16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1617
import static org.junit.jupiter.api.Assertions.assertEquals;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
18-
import static org.junit.jupiter.api.Assertions.fail;
1919

2020
import org.junit.jupiter.api.Test;
2121

@@ -128,227 +128,115 @@ public void testSplit() throws Exception {
128128
}
129129

130130
@Test
131-
public void testIllegalOperations() throws Exception {
131+
public void testIllegalOperations() {
132132

133-
try {
134-
Range.copy(null);
135-
fail();
136-
} catch (NullPointerException e) {
137-
}
133+
assertThrows(NullPointerException.class, () -> Range.copy(null));
138134

139-
try {
140-
Range.createRelative(0, 0);
141-
fail();
142-
} catch (LineIndexOutOfBoundsException e) {
143-
}
135+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createRelative(0, 0));
144136

145-
try {
146-
Range.createRelative(0, -1);
147-
fail();
148-
} catch (LineIndexOutOfBoundsException e) {
149-
}
137+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createRelative(0, -1));
150138

151-
try {
152-
Range.createRelative(-1, 0);
153-
fail();
154-
} catch (LineIndexOutOfBoundsException e) {
155-
}
139+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createRelative(-1, 0));
156140

157-
try {
158-
Range.createRelative(-1, -1);
159-
fail();
160-
} catch (LineIndexOutOfBoundsException e) {
161-
}
141+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createRelative(-1, -1));
162142

163-
try {
164-
Range.createAbsolute(0, 0);
165-
fail();
166-
} catch (LineIndexOutOfBoundsException e) {
167-
}
143+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(0, 0));
168144

169-
try {
170-
Range.createAbsolute(0, -1);
171-
fail();
172-
} catch (LineIndexOutOfBoundsException e) {
173-
}
145+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(0, -1));
174146

175-
try {
176-
Range.createAbsolute(-1, 0);
177-
fail();
178-
} catch (LineIndexOutOfBoundsException e) {
179-
}
147+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(-1, 0));
180148

181-
try {
182-
Range.createAbsolute(-1, 12);
183-
fail();
184-
} catch (LineIndexOutOfBoundsException e) {
185-
}
149+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(-1, 12));
186150

187-
try {
188-
Range.createAbsolute(10, 10);
189-
fail();
190-
} catch (LineIndexOutOfBoundsException e) {
191-
}
151+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(10, 10));
192152

193-
try {
194-
Range.createAbsolute(12, 10);
195-
fail();
196-
} catch (LineIndexOutOfBoundsException e) {
197-
}
153+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(12, 10));
198154

199-
try {
200-
Range.createAbsolute(12, -3);
201-
fail();
202-
} catch (LineIndexOutOfBoundsException e) {
203-
}
155+
assertThrows(LineIndexOutOfBoundsException.class, () -> Range.createAbsolute(12, -3));
204156

205157
Range r= Range.createRelative(5, 10);
206158

207-
try {
208-
r.moveBy(-6);
209-
fail();
210-
} catch (LineIndexOutOfBoundsException e) {
211-
assertEquals(5, r.start());
212-
assertEquals(10, r.length());
213-
assertConsistency(r);
214-
}
159+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.moveBy(-6));
160+
assertEquals(5, r.start());
161+
assertEquals(10, r.length());
162+
assertConsistency(r);
215163

216164
r.moveBy(-4);
217-
try {
218-
r.moveBy(-2);
219-
fail();
220-
} catch (LineIndexOutOfBoundsException e) {
221-
r.moveBy(4);
222-
assertEquals(5, r.start());
223-
assertEquals(10, r.length());
224-
assertConsistency(r);
225-
}
165+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.moveBy(-2));
166+
r.moveBy(4);
167+
assertEquals(5, r.start());
168+
assertEquals(10, r.length());
169+
assertConsistency(r);
226170

227-
try {
228-
r.resizeBy(-11);
229-
fail();
230-
} catch (LineIndexOutOfBoundsException e) {
231-
assertEquals(5, r.start());
232-
assertEquals(10, r.length());
233-
assertConsistency(r);
234-
}
171+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.resizeBy(-11));
172+
assertEquals(5, r.start());
173+
assertEquals(10, r.length());
174+
assertConsistency(r);
235175

236-
try {
237-
r.resizeAndMoveBy(-6);
238-
fail();
239-
} catch (LineIndexOutOfBoundsException e) {
240-
assertEquals(5, r.start());
241-
assertEquals(10, r.length());
242-
assertConsistency(r);
243-
}
176+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.resizeAndMoveBy(-6));
177+
assertEquals(5, r.start());
178+
assertEquals(10, r.length());
179+
assertConsistency(r);
244180

245-
try {
246-
r.resizeAndMoveBy(10);
247-
fail();
248-
} catch (LineIndexOutOfBoundsException e) {
249-
assertEquals(5, r.start());
250-
assertEquals(10, r.length());
251-
assertConsistency(r);
252-
}
181+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.resizeAndMoveBy(10));
182+
assertEquals(5, r.start());
183+
assertEquals(10, r.length());
184+
assertConsistency(r);
253185

254-
try {
255-
r.resizeAndMoveBy(11);
256-
fail();
257-
} catch (LineIndexOutOfBoundsException e) {
258-
assertEquals(5, r.start());
259-
assertEquals(10, r.length());
260-
assertConsistency(r);
261-
}
186+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.resizeAndMoveBy(11));
187+
assertEquals(5, r.start());
188+
assertEquals(10, r.length());
189+
assertConsistency(r);
262190

263-
try {
264-
r.resizeAndMoveBy(20);
265-
fail();
266-
} catch (LineIndexOutOfBoundsException e) {
267-
assertEquals(5, r.start());
268-
assertEquals(10, r.length());
269-
assertConsistency(r);
270-
}
191+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.resizeAndMoveBy(20));
192+
assertEquals(5, r.start());
193+
assertEquals(10, r.length());
194+
assertConsistency(r);
271195

272-
try {
273-
r.setLength(0);
274-
fail();
275-
} catch (LineIndexOutOfBoundsException e) {
276-
assertEquals(5, r.start());
277-
assertEquals(10, r.length());
278-
assertConsistency(r);
279-
}
196+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setLength(0));
197+
assertEquals(5, r.start());
198+
assertEquals(10, r.length());
199+
assertConsistency(r);
280200

281-
try {
282-
r.setLength(-1);
283-
fail();
284-
} catch (LineIndexOutOfBoundsException e) {
285-
assertEquals(5, r.start());
286-
assertEquals(10, r.length());
287-
assertConsistency(r);
288-
}
201+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setLength(-1));
202+
assertEquals(5, r.start());
203+
assertEquals(10, r.length());
204+
assertConsistency(r);
289205

290-
try {
291-
r.moveTo(-1);
292-
fail();
293-
} catch (LineIndexOutOfBoundsException e) {
294-
assertEquals(5, r.start());
295-
assertEquals(10, r.length());
296-
assertConsistency(r);
297-
}
206+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.moveTo(-1));
207+
assertEquals(5, r.start());
208+
assertEquals(10, r.length());
209+
assertConsistency(r);
298210

299-
try {
300-
r.setEnd(5);
301-
fail();
302-
} catch (LineIndexOutOfBoundsException e) {
303-
assertEquals(5, r.start());
304-
assertEquals(10, r.length());
305-
assertConsistency(r);
306-
}
211+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setEnd(5));
212+
assertEquals(5, r.start());
213+
assertEquals(10, r.length());
214+
assertConsistency(r);
307215

308-
try {
309-
r.setEnd(3);
310-
fail();
311-
} catch (LineIndexOutOfBoundsException e) {
312-
assertEquals(5, r.start());
313-
assertEquals(10, r.length());
314-
assertConsistency(r);
315-
}
216+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setEnd(3));
217+
assertEquals(5, r.start());
218+
assertEquals(10, r.length());
219+
assertConsistency(r);
316220

317-
try {
318-
r.setEnd(-5);
319-
fail();
320-
} catch (LineIndexOutOfBoundsException e) {
321-
assertEquals(5, r.start());
322-
assertEquals(10, r.length());
323-
assertConsistency(r);
324-
}
221+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setEnd(-5));
222+
assertEquals(5, r.start());
223+
assertEquals(10, r.length());
224+
assertConsistency(r);
325225

326-
try {
327-
r.setStart(18);
328-
fail();
329-
} catch (LineIndexOutOfBoundsException e) {
330-
assertEquals(5, r.start());
331-
assertEquals(10, r.length());
332-
assertConsistency(r);
333-
}
226+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setStart(18));
227+
assertEquals(5, r.start());
228+
assertEquals(10, r.length());
229+
assertConsistency(r);
334230

335-
try {
336-
r.moveEndTo(9);
337-
fail();
338-
} catch (LineIndexOutOfBoundsException e) {
339-
assertEquals(5, r.start());
340-
assertEquals(10, r.length());
341-
assertConsistency(r);
342-
}
231+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.moveEndTo(9));
232+
assertEquals(5, r.start());
233+
assertEquals(10, r.length());
234+
assertConsistency(r);
343235

344-
try {
345-
r.setLengthAndMove(16);
346-
fail();
347-
} catch (LineIndexOutOfBoundsException e) {
348-
assertEquals(5, r.start());
349-
assertEquals(10, r.length());
350-
assertConsistency(r);
351-
}
236+
assertThrows(LineIndexOutOfBoundsException.class, () -> r.setLengthAndMove(16));
237+
assertEquals(5, r.start());
238+
assertEquals(10, r.length());
239+
assertConsistency(r);
352240
}
353241

354242
@Test

0 commit comments

Comments
 (0)