Skip to content

Commit 47909f7

Browse files
committed
fix: replace Java 9+ List.of/Set.of/Map.of with JDK 8 compatible APIs
- Replace List.of(x) with Arrays.asList(x) and Collections.emptyList() where appropriate - Replace Map.of(k,v) single-entry with Collections.singletonMap(k,v) - Replace Map.of() with Collections.emptyMap() - Replace multi-entry Map.of with LinkedHashMap initializer (preserves insertion order) - Replace List.of(...) constants with Collections.unmodifiableList(Arrays.asList(...)) - All 122 tests pass with mvn test
1 parent 7a98d8d commit 47909f7

5 files changed

Lines changed: 68 additions & 32 deletions

File tree

‎src/test/java/io/github/easy4j/openclaw/OpenClawHttpApiCoverageTest.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.junit.jupiter.api.Test;
2828

2929
import java.io.IOException;
30+
import java.util.Arrays;
31+
import java.util.Collections;
3032
import java.util.List;
3133
import java.util.Map;
3234
import java.util.concurrent.CopyOnWriteArrayList;
@@ -79,12 +81,12 @@ void tearDown() {
7981
void shouldCoverChatModelsStreamingAndValidation() throws Exception {
8082
try (OpenClawChatClient chat = new OpenClawChatClient(config, new ObjectMapper(), client)) {
8183
ChatRequest agentRequest = ChatRequest.builder().agent("openclaw/default")
82-
.messages(List.of(ChatMessage.ofUser("hello"))).build();
84+
.messages(Arrays.asList(ChatMessage.ofUser("hello"))).build();
8385
ChatRequest modelRequest = ChatRequest.builder().model("gpt-5.4")
84-
.messages(List.of(ChatMessage.ofUser("hello"))).build();
86+
.messages(Arrays.asList(ChatMessage.ofUser("hello"))).build();
8587

8688
assertEquals("chat-id", chat.chatCompletion(agentRequest).getId());
87-
assertEquals("chat-id", chat.chatCompletion(modelRequest, Map.of("X-Custom", "value")).getId());
89+
assertEquals("chat-id", chat.chatCompletion(modelRequest, Collections.singletonMap("X-Custom", "value")).getId());
8890
AtomicBoolean cancellationRegistered = new AtomicBoolean();
8991
assertThrows(OpenClawHttpException.class, () -> chat.chatCompletion(agentRequest, null, callback -> {
9092
cancellationRegistered.set(true);
@@ -100,17 +102,17 @@ void shouldCoverChatModelsStreamingAndValidation() throws Exception {
100102

101103
assertThrows(NullPointerException.class, () -> chat.chatCompletion(null));
102104
assertThrows(IllegalArgumentException.class, () -> chat.chatCompletion(ChatRequest.builder()
103-
.messages(List.of(ChatMessage.ofUser("hello"))).build()));
105+
.messages(Arrays.asList(ChatMessage.ofUser("hello"))).build()));
104106
assertThrows(IllegalArgumentException.class, () -> chat.chatCompletion(ChatRequest.builder()
105-
.agent("openclaw/default").messages(List.of()).build()));
107+
.agent("openclaw/default").messages(Collections.emptyList()).build()));
106108

107109
StreamingChatResponse stream = chat.chatCompletionStream(agentRequest);
108110
assertEquals("hello", stream.get(3, TimeUnit.SECONDS).getChoices().get(0).getDelta().getContent());
109111
StreamingChatResponse callbackStream = chat.chatCompletionStream(agentRequest,
110112
StreamingChatResponse.builder().onDelta(ignored -> { }).onChunk(ignored -> { })
111113
.onToolCall(ignored -> { }).onComplete(ignored -> { }).onError(ignored -> { }));
112114
assertEquals("hello", callbackStream.get(3, TimeUnit.SECONDS).getChoices().get(0).getDelta().getContent());
113-
try (Response raw = chat.chatCompletionStreamRaw(agentRequest, Map.of("X-Raw", "yes"))) {
115+
try (Response raw = chat.chatCompletionStreamRaw(agentRequest, Collections.singletonMap("X-Raw", "yes"))) {
114116
assertEquals(200, raw.code());
115117
}
116118

@@ -126,19 +128,19 @@ void shouldCoverResponsesEmbeddingsToolsAndBaseHttpErrors() {
126128
OpenClawEmbeddingsClient embeddings = new OpenClawEmbeddingsClient(config, null, client);
127129
OpenClawToolInvokeClient tools = new OpenClawToolInvokeClient(config, null, client)) {
128130
assertNotNull(responses.createResponse(ResponseRequest.builder().agent("openclaw/default").input("hello").build()));
129-
assertNotNull(responses.createResponse(ResponseRequest.builder().model("gpt-5.4").input(List.of("hello")).build()));
131+
assertNotNull(responses.createResponse(ResponseRequest.builder().model("gpt-5.4").input(Arrays.asList("hello")).build()));
130132
assertThrows(IllegalArgumentException.class, () -> responses.createResponse(ResponseRequest.builder().input("hello").build()));
131133
assertThrows(IllegalArgumentException.class, () -> responses.createResponse(ResponseRequest.builder().agent("a").build()));
132134

133135
assertNotNull(embeddings.createEmbeddings(EmbeddingsRequest.builder().agent("openclaw/default").input("hello").build()));
134-
assertNotNull(embeddings.createEmbeddings(EmbeddingsRequest.builder().model("embed-model").input(List.of("hello")).build()));
136+
assertNotNull(embeddings.createEmbeddings(EmbeddingsRequest.builder().model("embed-model").input(Arrays.asList("hello")).build()));
135137
assertThrows(IllegalArgumentException.class, () -> embeddings.createEmbeddings(EmbeddingsRequest.builder().input("hello").build()));
136138
assertThrows(IllegalArgumentException.class, () -> embeddings.createEmbeddings(EmbeddingsRequest.builder().agent("a").build()));
137139

138140
ToolInvokeRequest request = new ToolInvokeRequest();
139141
request.setTool("browser");
140142
request.setAction("open");
141-
request.setArgs(Map.of("url", "http://localhost"));
143+
request.setArgs(Collections.singletonMap("url", "http://localhost"));
142144
assertTrue(tools.invoke(request).getOk());
143145
assertThrows(NullPointerException.class, () -> tools.invoke(null));
144146
assertThrows(IllegalArgumentException.class, () -> tools.invoke(new ToolInvokeRequest()));
@@ -181,7 +183,7 @@ void shouldCoverWebhookBodiesAuthenticationParsingAndErrors() {
181183
assertTrue(webhooks.postHooksWake("wake", "later").contains("runId"));
182184
assertTrue(webhooks.postMappedHook("/hooks/custom", null).contains("runId"));
183185
assertThrows(IllegalArgumentException.class, () -> webhooks.postHooksWake(" ", "now"));
184-
assertThrows(IllegalArgumentException.class, () -> webhooks.postMappedHook("bad/name", Map.of()));
186+
assertThrows(IllegalArgumentException.class, () -> webhooks.postMappedHook("bad/name", Collections.emptyMap()));
185187
assertThrows(IllegalArgumentException.class, () -> OpenClawWebhookClient.buildHooksAgentBody(new HookRequest()));
186188
assertEquals("custom", OpenClawWebhookClient.normalizeHookName(" /hooks/custom "));
187189
assertTrue(OpenClawWebhookClient.parseOk("{\"ok\":true}"));
@@ -192,9 +194,9 @@ void shouldCoverWebhookBodiesAuthenticationParsingAndErrors() {
192194
assertNull(OpenClawWebhookClient.parseRunId("invalid"));
193195

194196
status.set(500);
195-
assertThrows(OpenClawHttpException.class, () -> webhooks.postMappedHook("custom", Map.of()));
197+
assertThrows(OpenClawHttpException.class, () -> webhooks.postMappedHook("custom", Collections.emptyMap()));
196198
transportFailure.set(true);
197-
assertThrows(OpenClawHttpException.class, () -> webhooks.postMappedHook("custom", Map.of()));
199+
assertThrows(OpenClawHttpException.class, () -> webhooks.postMappedHook("custom", Collections.emptyMap()));
198200
}
199201
}
200202

@@ -219,7 +221,7 @@ void shouldBuildHeadersSessionKeysAndMessageFactories() {
219221
assertEquals("system", ChatMessage.ofSystem("s").getRole());
220222
assertEquals("user", ChatMessage.ofUser("u").getRole());
221223
assertEquals("assistant", ChatMessage.ofAssistant("a").getRole());
222-
assertEquals(toolCall, ChatMessage.ofAssistant(null, List.of(toolCall)).getToolCalls().get(0));
224+
assertEquals(toolCall, ChatMessage.ofAssistant(null, Arrays.asList(toolCall)).getToolCalls().get(0));
223225
assertEquals("tool", ChatMessage.ofTool("id", "result").getRole());
224226
}
225227

‎src/test/java/io/github/easy4j/openclaw/OpenClawOkHttpClientTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.List;
2021
import java.util.concurrent.CountDownLatch;
2122
import java.util.concurrent.atomic.AtomicInteger;
@@ -108,7 +109,7 @@ void shouldReturnStreamingHandleBeforeResponseHeadersArrive() throws Exception {
108109
config.setStartupCheckEnabled(false);
109110
ChatRequest request = ChatRequest.builder()
110111
.agent("openclaw/default")
111-
.messages(List.of(ChatMessage.ofUser("ping")))
112+
.messages(Arrays.asList(ChatMessage.ofUser("ping")))
112113
.build();
113114

114115
try (OpenClawClient client = new OpenClawClient(config, new ObjectMapper(), external)) {
@@ -165,7 +166,7 @@ void shouldKeepFiftyConcurrentStreamsIndependent() throws Exception {
165166
config.setStartupCheckEnabled(false);
166167
ChatRequest request = ChatRequest.builder()
167168
.agent("openclaw/default")
168-
.messages(List.of(ChatMessage.ofUser("ping")))
169+
.messages(Arrays.asList(ChatMessage.ofUser("ping")))
169170
.build();
170171

171172
try (OpenClawClient client = new OpenClawClient(config, new ObjectMapper(), external)) {

‎src/test/java/io/github/easy4j/openclaw/OpenClawSseToolsCoverageTest.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.io.ByteArrayInputStream;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.Collections;
1719
import java.util.List;
1820
import java.util.Map;
1921
import java.util.concurrent.atomic.AtomicInteger;
@@ -125,14 +127,14 @@ void shouldBuildToolsParseArgumentsAndCreateResponseInputs() {
125127
assertThrows(IllegalArgumentException.class, () -> Tools.parseArgsAsMap(call));
126128
assertThrows(NullPointerException.class, () -> Tools.parseArgs(null, Map.class));
127129

128-
ChatMessage message = ChatMessage.ofAssistant(null, List.of(call));
130+
ChatMessage message = ChatMessage.ofAssistant(null, Arrays.asList(call));
129131
assertTrue(Tools.hasToolCalls(message));
130132
assertFalse(Tools.hasToolCalls(null));
131133
assertTrue(Tools.isToolCallFinish("tool_calls"));
132134
assertEquals(1, Tools.extractToolCalls(message).size());
133135
assertTrue(Tools.extractToolCalls(ChatMessage.ofUser("hello")).isEmpty());
134136
assertEquals("plain", Tools.toolResult("id", "plain").getContent());
135-
assertTrue(Tools.toolResult("id", Map.of("ok", true)).getContent().contains("true"));
137+
assertTrue(Tools.toolResult("id", Collections.singletonMap("ok", true)).getContent().contains("true"));
136138

137139
assertEquals("message", ResponseRequest.InputItem.message().role("user").content("hello").build().getType());
138140
assertEquals("function_call_output", ResponseRequest.InputItem.functionCallOutput()
@@ -152,15 +154,15 @@ private ChatChunk chunk(String id, String model, String role, String content,
152154
ChatChunk.DeltaMessage delta = new ChatChunk.DeltaMessage();
153155
delta.setRole(role);
154156
delta.setContent(content);
155-
delta.setToolCalls(toolCall == null ? null : List.of(toolCall));
157+
delta.setToolCalls(toolCall == null ? null : Arrays.asList(toolCall));
156158
ChatChunk.DeltaChoice choice = new ChatChunk.DeltaChoice();
157159
choice.setIndex(0);
158160
choice.setDelta(delta);
159161
choice.setFinishReason(finishReason);
160162
ChatChunk chunk = new ChatChunk();
161163
chunk.setId(id);
162164
chunk.setModel(model);
163-
chunk.setChoices(List.of(choice));
165+
chunk.setChoices(Arrays.asList(choice));
164166
return chunk;
165167
}
166168

‎src/test/java/io/github/easy4j/openclaw/cli/opts/CliOptionsComprehensiveCoverageTest.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.lang.reflect.InvocationTargetException;
88
import java.lang.reflect.Method;
99
import java.lang.reflect.Modifier;
10+
import java.util.Arrays;
11+
import java.util.Collections;
1012
import java.util.List;
1113
import java.util.Map;
1214

@@ -22,7 +24,7 @@
2224
*/
2325
class CliOptionsComprehensiveCoverageTest {
2426

25-
private static final List<Class<?>> OPTION_TYPES = List.of(
27+
private static final List<Class<?>> OPTION_TYPES = Collections.unmodifiableList(Arrays.asList(
2628
PluginsOptions.class,
2729
ChannelsOptions.class,
2830
AgentsOptions.class,
@@ -42,7 +44,7 @@ class CliOptionsComprehensiveCoverageTest {
4244
DoctorOptions.class,
4345
WorktreesOptions.class,
4446
HealthCommandOptions.class
45-
);
47+
));
4648

4749
@Test
4850
void everyPublicBuilderPathMustGenerateValidImmutableArguments() throws Exception {
@@ -122,10 +124,10 @@ private Object sampleValue(Class<?> type) throws Exception {
122124
return values;
123125
}
124126
if (List.class.isAssignableFrom(type)) {
125-
return List.of("value");
127+
return Arrays.asList("value");
126128
}
127129
if (Map.class.isAssignableFrom(type)) {
128-
return Map.of("key", "value");
130+
return Collections.singletonMap("key", "value");
129131
}
130132
try {
131133
Object nestedBuilder = type.getMethod("builder").invoke(null);

‎src/test/java/io/github/easy4j/openclaw/ws/OpenClawGatewayWsClientIntegrationTest.java‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
import java.net.InetSocketAddress;
2424
import java.net.URI;
25+
import java.util.Arrays;
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.LinkedHashMap;
29+
import java.util.List;
2530
import java.util.Map;
2631
import java.util.concurrent.CountDownLatch;
2732
import java.util.concurrent.TimeUnit;
@@ -176,32 +181,56 @@ private TestGateway() {
176181
String method = request.path("method").asText();
177182
if (method.equals("connect")) {
178183
connectRequest.set(request);
179-
conn.send(response(id, true, Map.of(
180-
"type", "hello-ok", "protocol", 1,
181-
"server", Map.of("version", "test", "connId", "connection"),
182-
"features", Map.of("methods", java.util.List.of("chat.send"), "events", java.util.List.of("chat")),
183-
"auth", Map.of("role", "operator", "scopes", java.util.List.of("operator.read")),
184-
"policy", Map.of("maxPayload", 1024, "maxBufferedBytes", 1024, "tickIntervalMs", 1000))));
184+
conn.send(response(id, true, buildHelloOkMap()));
185185
return;
186186
}
187187
if (method.equals("chat.send")) {
188-
conn.send(response(id, true, Map.of("runId", id)));
188+
conn.send(response(id, true, Collections.singletonMap("runId", id)));
189189
conn.send("{\"type\":\"event\",\"event\":\"chat\",\"payload\":{\"runId\":\"" + id + "\",\"delta\":\"hello\"}}");
190190
conn.send("{\"type\":\"event\",\"event\":\"chat\",\"payload\":{\"runId\":\"" + id + "\",\"done\":true}}");
191191
return;
192192
}
193193
if (errorNext.compareAndSet(true, false)) {
194194
conn.send("{\"type\":\"res\",\"id\":\"" + id + "\",\"ok\":false,\"error\":{\"code\":\"FAILED\",\"message\":\"failed\"}}");
195195
} else {
196-
conn.send(response(id, true, Map.of()));
196+
conn.send(response(id, true, Collections.emptyMap()));
197197
}
198198
} catch (Exception e) {
199199
throw new AssertionError(e);
200200
}
201201
}
202202

203203
private String response(String id, boolean ok, Object payload) throws Exception {
204-
return mapper.writeValueAsString(Map.of("type", "res", "id", id, "ok", ok, "payload", payload));
204+
Map<String, Object> responseMap = new LinkedHashMap<String, Object>();
205+
responseMap.put("type", "res");
206+
responseMap.put("id", id);
207+
responseMap.put("ok", ok);
208+
responseMap.put("payload", payload);
209+
return mapper.writeValueAsString(responseMap);
210+
}
211+
212+
private Map<String, Object> buildHelloOkMap() {
213+
Map<String, Object> server = new LinkedHashMap<String, Object>();
214+
server.put("version", "test");
215+
server.put("connId", "connection");
216+
Map<String, Object> features = new LinkedHashMap<String, Object>();
217+
features.put("methods", Arrays.asList("chat.send"));
218+
features.put("events", Arrays.asList("chat"));
219+
Map<String, Object> auth = new LinkedHashMap<String, Object>();
220+
auth.put("role", "operator");
221+
auth.put("scopes", Arrays.asList("operator.read"));
222+
Map<String, Object> policy = new LinkedHashMap<String, Object>();
223+
policy.put("maxPayload", 1024);
224+
policy.put("maxBufferedBytes", 1024);
225+
policy.put("tickIntervalMs", 1000);
226+
Map<String, Object> helloOk = new LinkedHashMap<String, Object>();
227+
helloOk.put("type", "hello-ok");
228+
helloOk.put("protocol", 1);
229+
helloOk.put("server", server);
230+
helloOk.put("features", features);
231+
helloOk.put("auth", auth);
232+
helloOk.put("policy", policy);
233+
return helloOk;
205234
}
206235

207236
private void sendRaw(String message) {

0 commit comments

Comments
 (0)