-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathJoobyRunHookTest.java
More file actions
136 lines (104 loc) · 4.27 KB
/
JoobyRunHookTest.java
File metadata and controls
136 lines (104 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.function.Consumer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import io.jooby.internal.MutedServer;
class JoobyRunHookTest {
private static final String PROPERTY_NAME = "___jooby_run_hook__";
private MockedStatic<MutedServer> mutedServerMock;
private Server mockServer;
private Server mockMutedServer;
@BeforeEach
void setUp() {
mockServer = mock(Server.class);
mockMutedServer = mock(Server.class);
// Mock the static MutedServer.mute() call
mutedServerMock = mockStatic(MutedServer.class);
mutedServerMock.when(() -> MutedServer.mute(mockServer)).thenReturn(mockMutedServer);
// Ensure property is clean before each test
System.clearProperty(PROPERTY_NAME);
// Reset our test hook state
TestHook.capturedServer = null;
}
@AfterEach
void tearDown() {
mutedServerMock.close();
System.clearProperty(PROPERTY_NAME);
}
// --- Helper class to act as the valid hook ---
public static class TestHook implements Consumer<Server> {
public static Server capturedServer;
public TestHook() {} // Must have a public no-arg constructor
@Override
public void accept(Server server) {
capturedServer = server;
}
}
@Test
@DisplayName("Branch 1: ClassLoader is NOT ModuleClassLoader")
void testNotModuleClassLoader() {
ClassLoader standardLoader = new ClassLoader() {};
System.setProperty(PROPERTY_NAME, "SomeClass");
Jooby.joobyRunHook(standardLoader, mockServer);
// Verification: The if-block is skipped, so the property is NEVER cleared
assertEquals("SomeClass", System.getProperty(PROPERTY_NAME));
}
@Test
@DisplayName("Branch 2: ModuleClassLoader, but property is null")
void testModuleClassLoader_NullProperty() {
ClassLoader jbossLoader = new org.jboss.modules.ModuleClassLoader();
// Property is already null via setUp()
Jooby.joobyRunHook(jbossLoader, mockServer);
// Verification: The property gets actively set to empty string
assertEquals("", System.getProperty(PROPERTY_NAME));
assertNull(TestHook.capturedServer); // Hook logic skipped
}
@Test
@DisplayName("Branch 3: ModuleClassLoader, but property is empty")
void testModuleClassLoader_EmptyProperty() {
ClassLoader jbossLoader = new org.jboss.modules.ModuleClassLoader();
System.setProperty(PROPERTY_NAME, "");
Jooby.joobyRunHook(jbossLoader, mockServer);
// Verification: Property remains empty, hook logic skipped
assertEquals("", System.getProperty(PROPERTY_NAME));
assertNull(TestHook.capturedServer);
}
@Test
@DisplayName("Branch 4: ModuleClassLoader and Valid Hook (Happy Path)")
void testModuleClassLoader_ValidHook() {
ClassLoader jbossLoader = new org.jboss.modules.ModuleClassLoader();
System.setProperty(PROPERTY_NAME, TestHook.class.getName());
Jooby.joobyRunHook(jbossLoader, mockServer);
// Verification: Property cleared
assertEquals("", System.getProperty(PROPERTY_NAME));
// Verification: MutedServer.mute was called
mutedServerMock.verify(() -> MutedServer.mute(mockServer));
// Verification: The consumer was instantiated and accept() was called with the muted server
assertEquals(mockMutedServer, TestHook.capturedServer);
}
@Test
@DisplayName("Branch 5: ModuleClassLoader and Invalid Hook (Exception Path)")
void testModuleClassLoader_InvalidHook() {
ClassLoader jbossLoader = new org.jboss.modules.ModuleClassLoader();
System.setProperty(PROPERTY_NAME, "com.example.DoesNotExist");
// The try/catch will catch the ClassNotFoundException and wrap it in SneakyThrows.propagate
// SneakyThrows.propagate usually throws a RuntimeException, so we expect an exception here.
assertThrows(
Exception.class,
() -> {
Jooby.joobyRunHook(jbossLoader, mockServer);
});
// Verification: Property was still cleared before the crash
assertEquals("", System.getProperty(PROPERTY_NAME));
}
}