-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathRequestScopeTest.java
More file actions
105 lines (85 loc) · 3.27 KB
/
RequestScopeTest.java
File metadata and controls
105 lines (85 loc) · 3.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
/*
* 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 java.lang.reflect.Constructor;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class RequestScopeTest {
@AfterEach
void tearDown() {
// Ensure the ThreadLocal is cleared after each test to prevent state bleeding
RequestScope.threadLocal().remove();
}
@Test
@DisplayName("Test private constructor for full line coverage")
void testPrivateConstructor() throws Exception {
Constructor<RequestScope> constructor = RequestScope.class.getDeclaredConstructor();
constructor.setAccessible(true);
RequestScope instance = constructor.newInstance();
assertNotNull(instance);
}
@Test
@DisplayName("Test empty state behavior (null context map)")
void testEmptyState() {
// Map is null at this point
assertFalse(RequestScope.hasBind("key"));
assertNull(RequestScope.get("key"));
assertNull(RequestScope.unbind("key"));
}
@Test
@DisplayName("Test bind, get, and hasBind logic")
void testBindAndGet() {
// 1. Map is null, createMap = true
String previous = RequestScope.bind("myKey", "myValue");
assertNull(previous);
// 2. hasBind branches
assertTrue(RequestScope.hasBind("myKey"));
assertFalse(RequestScope.hasBind("missingKey"));
// 3. get branches
assertEquals("myValue", RequestScope.get("myKey"));
assertNull(RequestScope.get("missingKey"));
// 4. Bind on existing key (returns old value)
String old = RequestScope.bind("myKey", "newValue");
assertEquals("myValue", old);
assertEquals("newValue", RequestScope.get("myKey"));
}
@Test
@DisplayName("Test unbind and internal cleanup branches")
void testUnbindAndCleanup() {
// Unbind when contextMap is completely null
assertNull(RequestScope.unbind("someKey"));
// Setup: Bind two distinct keys
RequestScope.bind("k1", "v1");
RequestScope.bind("k2", "v2");
ThreadLocal<Map<Object, Object>> tl = RequestScope.threadLocal();
assertNotNull(tl.get());
assertEquals(2, tl.get().size());
// 1. Unbind one key -> map not empty -> ctx.isEmpty() == false
String removed1 = RequestScope.unbind("k1");
assertEquals("v1", removed1);
assertNotNull(tl.get(), "ThreadLocal map should not be removed yet");
assertEquals(1, tl.get().size());
// 2. Unbind missing key -> map not empty
assertNull(RequestScope.unbind("missingKey"));
// 3. Unbind last key -> map becomes empty -> ctx.isEmpty() == true -> CONTEXT_TL.remove()
String removed2 = RequestScope.unbind("k2");
assertEquals("v2", removed2);
assertNull(tl.get(), "ThreadLocal map should be completely removed now");
// 4. Unbind again when contextMap has been removed
assertNull(RequestScope.unbind("k2"));
}
@Test
@DisplayName("Test threadLocal instance exposure")
void testThreadLocal() {
ThreadLocal<Map<Object, Object>> tl1 = RequestScope.threadLocal();
ThreadLocal<Map<Object, Object>> tl2 = RequestScope.threadLocal();
assertNotNull(tl1);
assertSame(tl1, tl2, "Should return the exact same ThreadLocal instance");
}
}