|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 Real Logic Limited. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.aeron.test; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Disabled; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | + |
| 24 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | +import static org.hamcrest.Matchers.containsString; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.fail; |
| 28 | + |
| 29 | +/** |
| 30 | + * This has some disabled tests as they are evaluating behaviour of the callback when tests fail, so enabling them |
| 31 | + * will cause the full build to fail. The tests are there, so that the code can be easily tested manually. |
| 32 | + */ |
| 33 | +@ExtendWith(ThreadNamingTestCallback.class) |
| 34 | +public class ThreadNamingCallbackTest |
| 35 | +{ |
| 36 | + private String threadName = "unset"; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() |
| 40 | + { |
| 41 | + threadName = Thread.currentThread().getName(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testThatSucceeds() |
| 46 | + { |
| 47 | + assertThat(Thread.currentThread().getName(), containsString("testThatSucceeds")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @Disabled |
| 52 | + void testThatFails() |
| 53 | + { |
| 54 | + assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); |
| 55 | + assertThat(Thread.currentThread().getName(), containsString("testThatFails")); |
| 56 | + fail("forced failure"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @Disabled |
| 61 | + void testThatThrowsCheckedException() throws Exception |
| 62 | + { |
| 63 | + assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); |
| 64 | + assertThat(Thread.currentThread().getName(), containsString("testThatThrowsCheckedException")); |
| 65 | + throw new Exception("forced failure"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @Disabled |
| 70 | + void testThatThrowsUncheckedException() |
| 71 | + { |
| 72 | + assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); |
| 73 | + assertThat(Thread.currentThread().getName(), containsString("testThatThrowsUncheckedException")); |
| 74 | + throw new RuntimeException("forced failure"); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterEach |
| 78 | + void tearDown() |
| 79 | + { |
| 80 | + assertEquals(threadName, Thread.currentThread().getName()); |
| 81 | + } |
| 82 | +} |
0 commit comments