Skip to content

Commit 6db14c8

Browse files
Merge pull request #424 from com-pas/fix/add_synchronized_when_copySclElement
fix : synchronized copySclElement
2 parents fcbc3d3 + ee094ff commit 6db14c8

File tree

2 files changed

+30
-6
lines changed
  • sct-commons/src

2 files changed

+30
-6
lines changed

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/Utils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public final class Utils {
3131
private static final Pattern MAC_ADDRESS_PATTERN = Pattern.compile("[0-9A-F]{2}([-:][0-9A-F]{2}){5}", Pattern.CASE_INSENSITIVE);
3232

3333
private static JAXBContext jaxbContext = null;
34-
private static Unmarshaller unmarshaller = null;
3534

3635
/**
3736
* Private Constructor, should not be instanced
@@ -149,7 +148,7 @@ public static String xpathAttributeFilter(String name, Collection<String> value)
149148
* @param s1 first string
150149
* @param s2 seconde string
151150
* @return true if strings are equals or both blank, false otherwise
152-
* @see org.apache.commons.lang3.StringUtils#isBlank(CharSequence)
151+
* @see StringUtils#isBlank(CharSequence)
153152
*/
154153
public static boolean equalsOrBothBlank(String s1, String s2) {
155154
return Objects.equals(s1, s2)
@@ -167,9 +166,9 @@ public static boolean equalsOrBothBlank(String s1, String s2) {
167166
* @param s2 second String to compare
168167
* @return when s1 and s2 are not blank, same result as {@link String#compare(CharSequence, CharSequence)},
169168
* 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.
170-
* @see java.util.Comparator#compare(Object, Object)
171-
* @see org.apache.commons.lang3.StringUtils#isBlank(CharSequence)
172-
* @see java.util.Comparator#nullsFirst(Comparator)
169+
* @see Comparator#compare(Object, Object)
170+
* @see StringUtils#isBlank(CharSequence)
171+
* @see Comparator#nullsFirst(Comparator)
173172
*/
174173
public static int blanksFirstComparator(String s1, String s2) {
175174
if (StringUtils.isBlank(s1)){
@@ -314,11 +313,12 @@ public static String toHex(long number, int length) {
314313
* @return copy of the object
315314
*/
316315
public static <T> T copySclElement(T object, Class<T> clazz) {
316+
Unmarshaller unmarshaller;
317317
try {
318318
if (jaxbContext == null) {
319319
jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model");
320-
unmarshaller = jaxbContext.createUnmarshaller();
321320
}
321+
unmarshaller = jaxbContext.createUnmarshaller();
322322
JAXBElement<T> contentObject = new JAXBElement<>(new QName(clazz.getSimpleName()), clazz, object);
323323
JAXBSource source = new JAXBSource(jaxbContext, contentObject);
324324
return unmarshaller.unmarshal(source, clazz).getValue();

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/UtilsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.lfenergy.compas.sct.commons.exception.ScdException;
1919

2020
import java.util.*;
21+
import java.util.concurrent.*;
2122
import java.util.stream.Stream;
2223

2324
import static org.assertj.core.api.Assertions.*;
@@ -482,6 +483,29 @@ void copySclElement_should_throwException() {
482483
.hasMessage("org.lfenergy.compas.sct.commons.dto.FCDAInfo is not known to this context");
483484
}
484485

486+
@Test
487+
void copySclElement_should_succeed_when_syncRead() throws ExecutionException, InterruptedException {
488+
// Given
489+
TLN tln = new TLN();
490+
tln.setLnType("T1");
491+
tln.getLnClass().add(TLLN0Enum.LLN_0.value());
492+
ExecutorService service = Executors.newFixedThreadPool(2);
493+
Callable<TLN> copySclElementTlnCallable = () -> copySclElement(tln, TLN.class);
494+
// When
495+
List<Future<TLN>> result = service.invokeAll(List.of(copySclElementTlnCallable, copySclElementTlnCallable));
496+
service.shutdown();
497+
TLN[] tlns = new TLN[]{result.getFirst().get(), result.getLast().get()};
498+
// Then
499+
assertThat(tlns).hasSize(2);
500+
assertThat(tlns[0])
501+
.isNotSameAs(tlns[1])
502+
.isNotSameAs(tln);
503+
assertThat(tlns[0])
504+
.usingRecursiveComparison()
505+
.isEqualTo(tlns[1])
506+
.isEqualTo(tln);
507+
}
508+
485509
@Test
486510
void extractFromP_should_return_value_for_given_type(){
487511
// Given

0 commit comments

Comments
 (0)