| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.testutils.annotations;
|
|---|
| 3 |
|
|---|
| 4 | import java.lang.annotation.ElementType;
|
|---|
| 5 | import java.lang.annotation.Retention;
|
|---|
| 6 | import java.lang.annotation.RetentionPolicy;
|
|---|
| 7 | import java.lang.annotation.Target;
|
|---|
| 8 |
|
|---|
| 9 | import org.junit.jupiter.api.extension.BeforeEachCallback;
|
|---|
| 10 | import org.junit.jupiter.api.extension.ExtendWith;
|
|---|
| 11 | import org.junit.jupiter.api.extension.ExtensionContext;
|
|---|
| 12 | import org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Ensure that assertions in the edt are caught.
|
|---|
| 16 | * The default mocker is {@link EDTAssertionMocker}. If you want to use a different one,
|
|---|
| 17 | * you must use {@link org.junit.jupiter.api.extension.RegisterExtension} with
|
|---|
| 18 | * {@link AssertionsExtension#setMocker(Runnable)}.
|
|---|
| 19 | */
|
|---|
| 20 | @Target({ElementType.TYPE, ElementType.METHOD})
|
|---|
| 21 | @Retention(RetentionPolicy.RUNTIME)
|
|---|
| 22 | @ExtendWith(AssertionsInEDT.AssertionsExtension.class)
|
|---|
| 23 | public @interface AssertionsInEDT {
|
|---|
| 24 | /**
|
|---|
| 25 | * Check for assertions in the EDT
|
|---|
| 26 | */
|
|---|
| 27 | class AssertionsExtension implements BeforeEachCallback {
|
|---|
| 28 | private Runnable edtAssertionMockingRunnable = EDTAssertionMocker::new;
|
|---|
| 29 | @Override
|
|---|
| 30 | public void beforeEach(ExtensionContext context) throws Exception {
|
|---|
| 31 | this.edtAssertionMockingRunnable.run();
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Re-raise AssertionErrors thrown in the EDT where they would have normally been swallowed.
|
|---|
| 36 | * @param edtAssertionMockingRunnable Runnable for initializing this functionality
|
|---|
| 37 | *
|
|---|
| 38 | * @return this instance, for easy chaining
|
|---|
| 39 | */
|
|---|
| 40 | public AssertionsExtension setMocker(Runnable edtAssertionMockingRunnable) {
|
|---|
| 41 | this.edtAssertionMockingRunnable = edtAssertionMockingRunnable;
|
|---|
| 42 | return this;
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|