Skip to content

Commit af2c663

Browse files
dkasimovskiyclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 0fbbc49 commit af2c663

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
surface into a single parameterized suite.
1414
- 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.
1515
- Upgrade TQE to v3.5.0.
16+
- Extract `ObjectMapper` to a static field in the test `tdg.Utils` helper to avoid recreating it on every `sendUsers`/`getUsers` call.
1617

1718
### Documentation
1819

testcontainers/src/test/java/org/testcontainers/containers/tdg/Utils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525

2626
public abstract class Utils {
2727

28+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
29+
2830
private Utils() {}
2931

3032
public static List<User> sendUsers(List<User> users, TDGContainer<?> container)
3133
throws IOException {
3234
final List<User> result = new ArrayList<>();
33-
final ObjectMapper objectMapper = new ObjectMapper();
3435
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
3536
for (final User user : users) {
36-
final String jsonUser = objectMapper.writeValueAsString(user);
37+
final String jsonUser = OBJECT_MAPPER.writeValueAsString(user);
3738
final String address = HttpHost.unsecure(container.httpMappedAddress()) + "/data/User";
3839
final HttpPost post = new HttpPost(address);
3940
final HttpEntity entity = new StringEntity(jsonUser, ContentType.APPLICATION_JSON);
@@ -50,7 +51,7 @@ public static List<User> sendUsers(List<User> users, TDGContainer<?> container)
5051
+ ", "
5152
+ response.getReasonPhrase());
5253
}
53-
return objectMapper.readValue(
54+
return OBJECT_MAPPER.readValue(
5455
EntityUtils.toString(response.getEntity()), User.class);
5556
}));
5657
}
@@ -59,7 +60,6 @@ public static List<User> sendUsers(List<User> users, TDGContainer<?> container)
5960
}
6061

6162
public static List<User> getUsers(int count, TDGContainer<?> node) throws IOException {
62-
final ObjectMapper objectMapper = new ObjectMapper();
6363
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
6464
final String address =
6565
HttpHost.unsecure(node.httpMappedAddress()) + "/data/User?first=" + count;
@@ -73,7 +73,7 @@ public static List<User> getUsers(int count, TDGContainer<?> node) throws IOExce
7373
+ ", "
7474
+ response.getReasonPhrase());
7575
}
76-
return objectMapper.readValue(
76+
return OBJECT_MAPPER.readValue(
7777
EntityUtils.toString(response.getEntity()), new TypeReference<List<User>>() {});
7878
});
7979
}

0 commit comments

Comments
 (0)