From 7d46a3bf23f024fc50f9752f621216ff225a058b Mon Sep 17 00:00:00 2001 From: Dmitry Kasimovskiy Date: Wed, 3 Jun 2026 16:36:17 +0300 Subject: [PATCH] refactor(testcontainers): extract ObjectMapper to static field in tdg.Utils ObjectMapper instances are thread-safe after configuration and can be safely reused. Hoisting the instance to a static final field removes the duplicated `new ObjectMapper()` allocations in sendUsers() and getUsers() and avoids creating a fresh mapper (and re-reading its configuration) on every call. Co-Authored-By: Claude Opus 4.7 --- CHANGELOG.md | 1 + .../java/org/testcontainers/containers/tdg/Utils.java | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9e58e82..c259d114 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ surface into a single parameterized suite. - Add constructor/builder parameters to supply the initial Lua script as a string or as a file path, and optional additional script paths copied into the container data directory (`Tarantool2Container`, `CartridgeClusterContainer`, `VshardClusterContainer`); simplify bundled `server.lua` accordingly. - Upgrade TQE to v3.5.0. +- Extract `ObjectMapper` to a static field in the test `tdg.Utils` helper to avoid recreating it on every `sendUsers`/`getUsers` call. ### Documentation diff --git a/testcontainers/src/test/java/org/testcontainers/containers/tdg/Utils.java b/testcontainers/src/test/java/org/testcontainers/containers/tdg/Utils.java index 2e206366..3add2e50 100644 --- a/testcontainers/src/test/java/org/testcontainers/containers/tdg/Utils.java +++ b/testcontainers/src/test/java/org/testcontainers/containers/tdg/Utils.java @@ -25,15 +25,16 @@ public abstract class Utils { + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private Utils() {} public static List sendUsers(List users, TDGContainer container) throws IOException { final List result = new ArrayList<>(); - final ObjectMapper objectMapper = new ObjectMapper(); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { for (final User user : users) { - final String jsonUser = objectMapper.writeValueAsString(user); + final String jsonUser = OBJECT_MAPPER.writeValueAsString(user); final String address = HttpHost.unsecure(container.httpMappedAddress()) + "/data/User"; final HttpPost post = new HttpPost(address); final HttpEntity entity = new StringEntity(jsonUser, ContentType.APPLICATION_JSON); @@ -50,7 +51,7 @@ public static List sendUsers(List users, TDGContainer container) + ", " + response.getReasonPhrase()); } - return objectMapper.readValue( + return OBJECT_MAPPER.readValue( EntityUtils.toString(response.getEntity()), User.class); })); } @@ -59,7 +60,6 @@ public static List sendUsers(List users, TDGContainer container) } public static List getUsers(int count, TDGContainer node) throws IOException { - final ObjectMapper objectMapper = new ObjectMapper(); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { final String address = HttpHost.unsecure(node.httpMappedAddress()) + "/data/User?first=" + count; @@ -73,7 +73,7 @@ public static List getUsers(int count, TDGContainer node) throws IOExce + ", " + response.getReasonPhrase()); } - return objectMapper.readValue( + return OBJECT_MAPPER.readValue( EntityUtils.toString(response.getEntity()), new TypeReference>() {}); }); }