Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : synchronized copySclElement #424

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -149,7 +148,7 @@ public static String xpathAttributeFilter(String name, Collection<String> 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)
Expand All @@ -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)){
Expand Down Expand Up @@ -314,11 +313,12 @@ public static String toHex(long number, int length) {
* @return copy of the object
*/
public static <T> T copySclElement(T object, Class<T> clazz) {
Unmarshaller unmarshaller;
try {
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model");
unmarshaller = jaxbContext.createUnmarshaller();
}
unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<T> contentObject = new JAXBElement<>(new QName(clazz.getSimpleName()), clazz, object);
JAXBSource source = new JAXBSource(jaxbContext, contentObject);
return unmarshaller.unmarshal(source, clazz).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<TLN> copySclElementTlnCallable = () -> copySclElement(tln, TLN.class);
// When
List<Future<TLN>> 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
Expand Down
Loading