From 5ce1d501244e67eb30cf23ea3d00236a9aaabc0c Mon Sep 17 00:00:00 2001 From: Samir Romdhani Date: Fri, 20 Sep 2024 17:49:13 +0200 Subject: [PATCH] fix : add synchronized --- .../compas/sct/commons/util/Utils.java | 2 +- .../compas/sct/commons/util/UtilsTest.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/Utils.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/Utils.java index 430e830ec..0c42e329a 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/Utils.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/Utils.java @@ -313,7 +313,7 @@ public static String toHex(long number, int length) { * @param type of the object * @return copy of the object */ - public static T copySclElement(T object, Class clazz) { + public static synchronized T copySclElement(T object, Class clazz) { try { if (jaxbContext == null) { jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model"); diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/UtilsTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/UtilsTest.java index 71512ec86..67daa809f 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/UtilsTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/UtilsTest.java @@ -18,6 +18,7 @@ import org.lfenergy.compas.sct.commons.exception.ScdException; import java.util.*; +import java.util.concurrent.*; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.*; @@ -482,6 +483,30 @@ void copySclElement_should_throwException() { .hasMessage("org.lfenergy.compas.sct.commons.dto.FCDAInfo is not known to this context"); } + @Test + void copySclElement_should_should_succeed_when_syncRead() throws ExecutionException, InterruptedException { + // Given + TLN tln = new TLN(); + tln.setLnType("T1"); + tln.getLnClass().add(TLLN0Enum.LLN_0.value()); + ExecutorService service = Executors.newFixedThreadPool(2); + Callable copySclElementTlnCallable = new Callable() { + @Override + public TLN call() { + return copySclElement(tln, TLN.class); + } + }; + // When + List> result = service.invokeAll(Arrays.asList(copySclElementTlnCallable, copySclElementTlnCallable)); + service.shutdown(); + TLN[] tlns = new TLN[]{result.getFirst().get(), result.getLast().get()}; + // Then + assertThat(tlns).hasSize(2); + assertThat(tlns[0]).isNotSameAs(tlns[1]).isNotSameAs(tln); + assertThat(tlns[0]).usingRecursiveComparison().isEqualTo(tln); + assertThat(tlns[1]).usingRecursiveComparison().isEqualTo(tln); + } + @Test void extractFromP_should_return_value_for_given_type(){ // Given