Skip to content

Commit bc73bbd

Browse files
committed
[Java] Add testing callback that will put the test method name onto the thread name.
1 parent b808784 commit bc73bbd

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.extension.AfterTestExecutionCallback;
19+
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
20+
import org.junit.jupiter.api.extension.ExtensionContext;
21+
22+
import java.lang.reflect.Method;
23+
24+
public class ThreadNamingTestCallback implements BeforeTestExecutionCallback, AfterTestExecutionCallback
25+
{
26+
private final ThreadLocal<String> oldThreadName = new ThreadLocal<>();
27+
{
28+
oldThreadName.set("UNKNOWN");
29+
}
30+
31+
public void beforeTestExecution(final ExtensionContext context) throws Exception
32+
{
33+
final String className = context.getTestClass().map(Class::getSimpleName).orElse("UNKNOWN");
34+
final String methodName = context.getTestMethod().map(Method::getName).orElse("UNKNOWN");
35+
final Thread thread = Thread.currentThread();
36+
final String existingThreadName = thread.getName();
37+
final String testThreadName = "TEST::" + className + "." + methodName;
38+
oldThreadName.set(existingThreadName);
39+
thread.setName(testThreadName);
40+
}
41+
42+
public void afterTestExecution(final ExtensionContext context) throws Exception
43+
{
44+
Thread.currentThread().setName(oldThreadName.get());
45+
oldThreadName.remove();
46+
}
47+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)