From ee094ff90548cd1dc2f13952715d6680c132103f Mon Sep 17 00:00:00 2001 From: Samir Romdhani Date: Fri, 20 Sep 2024 17:49:13 +0200 Subject: [PATCH] fix : add synchronized Signed-off-by: Samir Romdhani --- .../compas/sct/commons/util/Utils.java | 12 +++++----- .../compas/sct/commons/util/UtilsTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) 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..6515ac110 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 @@ -31,7 +31,6 @@ public final class Utils { private static final Pattern MAC_ADDRESS_PATTERN = Pattern.compile("[0-9A-F]{2}([-:][0-9A-F]{2}){5}", Pattern.CASE_INSENSITIVE); private static JAXBContext jaxbContext = null; - private static Unmarshaller unmarshaller = null; /** * Private Constructor, should not be instanced @@ -149,7 +148,7 @@ public static String xpathAttributeFilter(String name, Collection value) * @param s1 first string * @param s2 seconde string * @return true if strings are equals or both blank, false otherwise - * @see org.apache.commons.lang3.StringUtils#isBlank(CharSequence) + * @see StringUtils#isBlank(CharSequence) */ public static boolean equalsOrBothBlank(String s1, String s2) { return Objects.equals(s1, s2) @@ -167,9 +166,9 @@ public static boolean equalsOrBothBlank(String s1, String s2) { * @param s2 second String to compare * @return when s1 and s2 are not blank, same result as {@link String#compare(CharSequence, CharSequence)}, * zero when s1 and s2 are both blanks, negative integer when s1 is blank and s2 is not, positive integer when s1 is not blank but s2 is. - * @see java.util.Comparator#compare(Object, Object) - * @see org.apache.commons.lang3.StringUtils#isBlank(CharSequence) - * @see java.util.Comparator#nullsFirst(Comparator) + * @see Comparator#compare(Object, Object) + * @see StringUtils#isBlank(CharSequence) + * @see Comparator#nullsFirst(Comparator) */ public static int blanksFirstComparator(String s1, String s2) { if (StringUtils.isBlank(s1)){ @@ -314,11 +313,12 @@ public static String toHex(long number, int length) { * @return copy of the object */ public static T copySclElement(T object, Class clazz) { + Unmarshaller unmarshaller; try { if (jaxbContext == null) { jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model"); - unmarshaller = jaxbContext.createUnmarshaller(); } + unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement contentObject = new JAXBElement<>(new QName(clazz.getSimpleName()), clazz, object); JAXBSource source = new JAXBSource(jaxbContext, contentObject); return unmarshaller.unmarshal(source, clazz).getValue(); 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..055f11978 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,29 @@ void copySclElement_should_throwException() { .hasMessage("org.lfenergy.compas.sct.commons.dto.FCDAInfo is not known to this context"); } + @Test + void copySclElement_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 = () -> copySclElement(tln, TLN.class); + // When + List> result = service.invokeAll(List.of(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(tlns[1]) + .isEqualTo(tln); + } + @Test void extractFromP_should_return_value_for_given_type(){ // Given