Skip to content

Commit 0c50d4e

Browse files
committed
test: 同步 JDK 8 兼容测试写法(Collections 系列/显式类型),保持三分支同源
1 parent 8340353 commit 0c50d4e

4 files changed

Lines changed: 55 additions & 46 deletions

File tree

src/test/java/io/github/easy4j/opencode/OpenCodeClientTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import com.fasterxml.jackson.databind.json.JsonMapper;
55
import io.github.easy4j.opencode.api.OpenCodeHttpClient;
66
import io.github.easy4j.opencode.api.OpenCodeSseClient;
7+
import io.github.easy4j.opencode.api.model.Agent;
8+
import io.github.easy4j.opencode.api.model.HealthStatus;
9+
import io.github.easy4j.opencode.api.model.OpenCodeConfig;
10+
import io.github.easy4j.opencode.api.model.Session;
711
import io.github.easy4j.opencode.cli.OpenCodeCli;
812
import io.github.easy4j.opencode.cli.OpenCodeCliExecutor;
913
import io.github.easy4j.opencode.cli.OpenCodeCliResult;
14+
import java.util.List;
15+
1016
import okhttp3.OkHttpClient;
1117
import okhttp3.mockwebserver.MockResponse;
1218
import okhttp3.mockwebserver.MockWebServer;
@@ -132,7 +138,7 @@ void shouldDelegateHealthToHttpClient() {
132138
OpenCodeHttpClient httpClient = new OpenCodeHttpClient(httpConfig, new JsonMapper(), null);
133139
OpenCodeClient client = new OpenCodeClient(config, httpClient, null, null);
134140

135-
var health = client.health();
141+
HealthStatus health = client.health();
136142
assertNotNull(health);
137143
assertTrue(health.getHealthy());
138144
client.close();
@@ -150,7 +156,7 @@ void shouldDelegateListSessions() {
150156
OpenCodeHttpClient httpClient = new OpenCodeHttpClient(httpConfig, new JsonMapper(), null);
151157
OpenCodeClient client = new OpenCodeClient(config, httpClient, null, null);
152158

153-
var sessions = client.listSessions();
159+
List<Session> sessions = client.listSessions();
154160
assertNotNull(sessions);
155161
assertEquals(1, sessions.size());
156162
client.close();
@@ -168,7 +174,7 @@ void shouldDelegateListAgents() {
168174
OpenCodeHttpClient httpClient = new OpenCodeHttpClient(httpConfig, new JsonMapper(), null);
169175
OpenCodeClient client = new OpenCodeClient(config, httpClient, null, null);
170176

171-
var agents = client.listAgents();
177+
List<Agent> agents = client.listAgents();
172178
assertNotNull(agents);
173179
assertEquals(1, agents.size());
174180
client.close();
@@ -186,7 +192,7 @@ void shouldDelegateGetConfig() {
186192
OpenCodeHttpClient httpClient = new OpenCodeHttpClient(httpConfig, new JsonMapper(), null);
187193
OpenCodeClient client = new OpenCodeClient(config, httpClient, null, null);
188194

189-
var codeConfig = client.getOpenCodeConfig();
195+
OpenCodeConfig codeConfig = client.getOpenCodeConfig();
190196
assertNotNull(codeConfig);
191197
assertEquals("dark", codeConfig.getTheme());
192198
client.close();

src/test/java/io/github/easy4j/opencode/api/OpenCodeSseClientTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void shouldSubscribeWithContext() throws InterruptedException {
7575
Thread.sleep(500);
7676
assertNotNull(received.get());
7777

78-
var request = server.takeRequest(2, TimeUnit.SECONDS);
78+
okhttp3.mockwebserver.RecordedRequest request = server.takeRequest(2, TimeUnit.SECONDS);
7979
assertNotNull(request);
8080
assertEquals("/data/project", request.getHeader("X-OpenCode-Directory"));
8181
subscription.cancel();
@@ -121,7 +121,7 @@ void shouldFilterByEventTypes() throws InterruptedException {
121121

122122
AtomicReference<SseEvent> received = new AtomicReference<>();
123123
SseSubscription subscription = sseClient.subscribeEventTypes(
124-
Set.of("wanted"), received::set);
124+
Collections.singleton("wanted"), received::set);
125125

126126
Thread.sleep(500);
127127
assertNotNull(received.get());
@@ -197,7 +197,7 @@ public void onEvent(SseEvent event) {
197197
Thread.sleep(500);
198198
assertNotNull(received.get());
199199

200-
var request = server.takeRequest(2, TimeUnit.SECONDS);
200+
okhttp3.mockwebserver.RecordedRequest request = server.takeRequest(2, TimeUnit.SECONDS);
201201
assertNotNull(request);
202202
assertEquals("/data/proj", request.getHeader("X-OpenCode-Directory"));
203203
subscription.cancel();

src/test/java/io/github/easy4j/opencode/api/event/EventHandlerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.easy4j.opencode.api.event;
22

3+
import java.util.Collections;
4+
35
import io.github.easy4j.opencode.api.sse.SseEvent;
46
import org.junit.jupiter.api.Test;
57

@@ -17,14 +19,14 @@ void shouldNotThrowOnDefaultMethods() {
1719
EventHandler handler = new EventHandler() {};
1820
SseEvent event = new SseEvent();
1921
event.setType("test");
20-
event.setProperties(Map.of());
22+
event.setProperties(Collections.emptyMap());
2123

2224
// All default methods should be no-op and not throw
2325
assertDoesNotThrow(() -> handler.onEvent(event));
2426
assertDoesNotThrow(() -> handler.onSessionIdle("sess-1", event));
2527
assertDoesNotThrow(() -> handler.onSessionError("sess-1", "error", event));
2628
assertDoesNotThrow(() -> handler.onTextDelta("delta", event));
27-
assertDoesNotThrow(() -> handler.onToolCall("bash", Map.of(), event));
29+
assertDoesNotThrow(() -> handler.onToolCall("bash", Collections.emptyMap(), event));
2830
assertDoesNotThrow(() -> handler.onToolResult("use-1", "output", event));
2931
assertDoesNotThrow(() -> handler.onMessage("msg-1", "assistant", event));
3032
assertDoesNotThrow(() -> handler.onSessionStatus("sess-1", "idle", event));

src/test/java/io/github/easy4j/opencode/api/model/ModelClassesTest.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.junit.jupiter.api.Test;
88

9+
import java.util.Arrays;
910
import java.util.Collections;
1011
import java.util.List;
1112
import java.util.Map;
@@ -83,7 +84,7 @@ void shouldCreateChatRequestWithAllFields() {
8384
req.setModel("anthropic/claude-sonnet-4-5");
8485
req.setMessages(Collections.singletonList(ChatMessage.user("hi")));
8586
req.setStream(true);
86-
req.setStreamOptions(Map.of("include_usage", true));
87+
req.setStreamOptions(Collections.singletonMap("include_usage", true));
8788
req.setAgent("coder");
8889
req.setSystem("system prompt");
8990
req.setMaxTokens(1024);
@@ -223,7 +224,7 @@ void shouldCreateCommand() {
223224
cmd.setName("/help");
224225
cmd.setDescription("Show help");
225226
cmd.setTemplate("help {{arg}}");
226-
cmd.setArgs(List.of("arg"));
227+
cmd.setArgs(Collections.singletonList("arg"));
227228
cmd.setAgent("coder");
228229
assertEquals("/help", cmd.getName());
229230
assertEquals("Show help", cmd.getDescription());
@@ -240,7 +241,7 @@ void shouldCreateCommand() {
240241
void shouldCreateEvent() {
241242
SseEvent event = new SseEvent();
242243
event.setType("session.idle");
243-
event.setProperties(Map.of("sessionID", "sess-1"));
244+
event.setProperties(Collections.singletonMap("sessionID", "sess-1"));
244245
assertEquals("session.idle", event.getType());
245246
assertEquals("sess-1", event.getProperties().get("sessionID"));
246247
}
@@ -309,10 +310,10 @@ void shouldCreateFileNode() {
309310
void shouldCreateFileSearchResult() {
310311
FileSearchResult result = new FileSearchResult();
311312
result.setPath("src/Main.java");
312-
result.setLines(List.of("public class Main {}"));
313+
result.setLines(Collections.singletonList("public class Main {}"));
313314
result.setLineNumber(1);
314315
result.setAbsoluteOffset(0);
315-
result.setSubmatches(List.of());
316+
result.setSubmatches(Collections.emptyList());
316317
assertEquals("src/Main.java", result.getPath());
317318
assertEquals(1, result.getLineNumber());
318319
}
@@ -355,7 +356,7 @@ void shouldCreateLspStatus() {
355356
lsp.setName("Eclipse JDT");
356357
lsp.setRoot("/project");
357358
lsp.setStatus("running");
358-
lsp.setDiagnostics(List.of());
359+
lsp.setDiagnostics(Collections.emptyList());
359360
assertEquals("jdtls", lsp.getId());
360361
assertEquals("running", lsp.getStatus());
361362
}
@@ -369,8 +370,8 @@ void shouldCreateMcpStatus() {
369370
McpStatus mcp = new McpStatus();
370371
mcp.setName("github");
371372
mcp.setStatus("connected");
372-
mcp.setConfig(Map.of("url", "http://localhost"));
373-
mcp.setTools(List.of("tool1"));
373+
mcp.setConfig(Collections.singletonMap("url", "http://localhost"));
374+
mcp.setTools(Collections.singletonList("tool1"));
374375
assertEquals("github", mcp.getName());
375376
assertEquals("connected", mcp.getStatus());
376377
assertEquals(1, mcp.getTools().size());
@@ -403,7 +404,7 @@ void shouldCreateMessageInfo() {
403404
Message msg = new Message();
404405
msg.setId("msg-1");
405406
info.setInfo(msg);
406-
info.setParts(List.of());
407+
info.setParts(Collections.emptyList());
407408
assertEquals("msg-1", info.getInfo().getId());
408409
assertTrue(info.getParts().isEmpty());
409410
}
@@ -424,15 +425,15 @@ void shouldCreateOpenCodeConfig() {
424425
cfg.setUsername("user");
425426
cfg.setShare("public");
426427
cfg.setAutoshare(true);
427-
cfg.setMode(Map.of("k", "v"));
428-
cfg.setProvider_(Map.of("k", "v"));
429-
cfg.setProviders(Map.of("k", "v"));
430-
cfg.setAgent_(Map.of("k", "v"));
431-
cfg.setAgents(Map.of("k", "v"));
432-
cfg.setPermission(Map.of("k", "v"));
433-
cfg.setTools(Map.of("k", "v"));
434-
cfg.setExperimental(Map.of("k", "v"));
435-
cfg.setExtra(Map.of("k", "v"));
428+
cfg.setMode(Collections.singletonMap("k", "v"));
429+
cfg.setProvider_(Collections.singletonMap("k", "v"));
430+
cfg.setProviders(Collections.singletonMap("k", "v"));
431+
cfg.setAgent_(Collections.singletonMap("k", "v"));
432+
cfg.setAgents(Collections.singletonMap("k", "v"));
433+
cfg.setPermission(Collections.singletonMap("k", "v"));
434+
cfg.setTools(Collections.singletonMap("k", "v"));
435+
cfg.setExperimental(Collections.singletonMap("k", "v"));
436+
cfg.setExtra(Collections.singletonMap("k", "v"));
436437
assertEquals("dark", cfg.getTheme());
437438
assertEquals("anthropic/claude-sonnet-4-5", cfg.getModel());
438439
assertEquals("coder", cfg.getAgent());
@@ -498,8 +499,8 @@ void shouldCreatePermissionRequest() {
498499
pr.setSessionID("sess-1");
499500
pr.setPermission("bash");
500501
pr.setDescription("run ls");
501-
pr.setMetadata(Map.of("command", "ls"));
502-
pr.setPatterns(List.of("ls *"));
502+
pr.setMetadata(Collections.singletonMap("command", "ls"));
503+
pr.setPatterns(Collections.singletonList("ls *"));
503504
assertEquals("perm-1", pr.getId());
504505
assertEquals("bash", pr.getPermission());
505506
assertEquals(1, pr.getPatterns().size());
@@ -518,7 +519,7 @@ void shouldCreateProject() {
518519
project.setWorktree("/project");
519520
project.setVcsDir("/project/.git");
520521
project.setVcs("git");
521-
project.setSandboxes(List.of("sandbox1"));
522+
project.setSandboxes(Collections.singletonList("sandbox1"));
522523
project.setCreatedAt("2025-01-01");
523524
assertEquals("proj-1", project.getId());
524525
assertEquals("my-project", project.getName());
@@ -550,7 +551,7 @@ void shouldCreatePromptRequestOfTextWithModel() {
550551
@Test
551552
void shouldSetAllPromptRequestFields() {
552553
PromptRequest req = new PromptRequest();
553-
req.setParts(List.of());
554+
req.setParts(Collections.emptyList());
554555
req.setModel(new PromptRequest.ModelRef("anthropic", "claude-sonnet-4-5"));
555556
req.setAgent("coder");
556557
req.setNoReply(true);
@@ -577,7 +578,7 @@ void shouldExtractTextContent() {
577578
toolPart.setText("ignored");
578579

579580
PromptResult result = new PromptResult();
580-
result.setParts(List.of(textPart, textPart2, toolPart));
581+
result.setParts(Arrays.asList(textPart, textPart2, toolPart));
581582
assertEquals("hello world", result.getTextContent());
582583
}
583584

@@ -593,7 +594,7 @@ void shouldReturnEmptyStringWhenNoTextParts() {
593594
toolPart.setType("tool_use");
594595
toolPart.setText("data");
595596
PromptResult result = new PromptResult();
596-
result.setParts(List.of(toolPart));
597+
result.setParts(Collections.singletonList(toolPart));
597598
assertEquals("", result.getTextContent());
598599
}
599600

@@ -608,9 +609,9 @@ void shouldCreateProvider() {
608609
p.setName("Anthropic");
609610
p.setDescription("AI provider");
610611
p.setSource("builtin");
611-
p.setAuthMethods(List.of());
612-
p.setModels(Map.of());
613-
p.setOptions(Map.of());
612+
p.setAuthMethods(Collections.emptyList());
613+
p.setModels(Collections.emptyMap());
614+
p.setOptions(Collections.emptyMap());
614615
assertEquals("anthropic", p.getId());
615616
assertEquals("Anthropic", p.getName());
616617
}
@@ -642,9 +643,9 @@ void shouldCreateProviderAuthMethod() {
642643
ProviderAuthMethod method = new ProviderAuthMethod();
643644
method.setLabel("API Key");
644645
method.setType("api-key");
645-
method.setSchema(Map.of("type", "string"));
646-
method.setPrefill(Map.of());
647-
method.setPromptOptions(List.of());
646+
method.setSchema(Collections.singletonMap("type", "string"));
647+
method.setPrefill(Collections.emptyMap());
648+
method.setPromptOptions(Collections.emptyList());
648649
assertEquals("API Key", method.getLabel());
649650
assertEquals("api-key", method.getType());
650651
}
@@ -656,10 +657,10 @@ void shouldCreateProviderAuthMethod() {
656657
@Test
657658
void shouldCreateProviderList() {
658659
ProviderList list = new ProviderList();
659-
list.setAll(List.of());
660-
list.setDefaults(Map.of("default", "anthropic/claude-sonnet-4-5"));
661-
list.setDefault_(Map.of("default", "anthropic/claude-sonnet-4-5"));
662-
list.setConnected(List.of("anthropic"));
660+
list.setAll(Collections.emptyList());
661+
list.setDefaults(Collections.singletonMap("default", "anthropic/claude-sonnet-4-5"));
662+
list.setDefault_(Collections.singletonMap("default", "anthropic/claude-sonnet-4-5"));
663+
list.setConnected(Collections.singletonList("anthropic"));
663664
assertTrue(list.getAll().isEmpty());
664665
assertEquals(1, list.getConnected().size());
665666
}
@@ -679,7 +680,7 @@ void shouldCreateQuestionRequest() {
679680
opt.setLabel("Option A");
680681
opt.setDescription("First option");
681682
opt.setPreview("preview");
682-
qr.setOptions(List.of(opt));
683+
qr.setOptions(Collections.singletonList(opt));
683684
assertEquals("q-1", qr.getId());
684685
assertEquals(1, qr.getOptions().size());
685686
assertEquals("Option A", qr.getOptions().get(0).getLabel());
@@ -697,7 +698,7 @@ void shouldCreateSession() {
697698
s.setParentId(null);
698699
s.setCreatedAt("2025-01-01");
699700
s.setUpdatedAt("2025-01-02");
700-
s.setMetadata(Map.of("key", "val"));
701+
s.setMetadata(Collections.singletonMap("key", "val"));
701702
assertEquals("sess-1", s.getId());
702703
assertEquals("my-session", s.getTitle());
703704
}
@@ -759,7 +760,7 @@ void shouldCreateSymbol() {
759760
sym.setContainerName("io.github.easy4j.opencode");
760761
sym.setLocation("OpenCodeClient.java:50");
761762
sym.setUri("file:///project/OpenCodeClient.java");
762-
sym.setRange(Map.of());
763+
sym.setRange(Collections.emptyMap());
763764
assertEquals("OpenCodeClient", sym.getName());
764765
assertEquals("class", sym.getKind());
765766
}

0 commit comments

Comments
 (0)