-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #586 from focus-shift/improve-xmlutil-performane
XML unmarshal: performance improvements - static XMLUtil with static converters
- Loading branch information
Showing
6 changed files
with
44 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 12 additions & 42 deletions
54
jollyday-jaxb/src/test/java/de/focus_shift/jollyday/jaxb/test/XMLUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,30 @@ | ||
package de.focus_shift.jollyday.jaxb.test; | ||
|
||
import de.focus_shift.jollyday.core.spi.Configuration; | ||
import de.focus_shift.jollyday.jaxb.XMLUtil; | ||
import jakarta.xml.bind.JAXBContext; | ||
import jakarta.xml.bind.JAXBElement; | ||
import jakarta.xml.bind.JAXBException; | ||
import jakarta.xml.bind.Unmarshaller; | ||
import de.focus_shift.jollyday.jaxb.mapping.Configuration; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class XMLUtilTest { | ||
|
||
@Mock | ||
private XMLUtil.JAXBContextCreator contextCreator; | ||
@Mock | ||
private InputStream inputStream; | ||
|
||
@InjectMocks | ||
private final XMLUtil xmlUtil = new XMLUtil(); | ||
private final XMLUtil sut = new XMLUtil(); | ||
|
||
@Test | ||
void testUnmarshallConfigurationNullCheck() { | ||
assertThatThrownBy(() -> xmlUtil.unmarshallConfiguration(null)).isInstanceOf(IllegalArgumentException.class); | ||
assertThatThrownBy(() -> sut.unmarshallConfiguration(null)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void testUnmarshallConfigurationException() throws IOException, JAXBException { | ||
when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenThrow(new JAXBException("")).thenThrow(new JAXBException("")); | ||
assertThatThrownBy(() -> xmlUtil.unmarshallConfiguration(inputStream)).isInstanceOf(IllegalStateException.class); | ||
verify(inputStream, never()).close(); | ||
} | ||
|
||
@Test | ||
void testUnmarshallConfiguration() throws JAXBException { | ||
final JAXBContext ctx = mock(JAXBContext.class); | ||
final Unmarshaller unmarshaller = mock(Unmarshaller.class); | ||
@SuppressWarnings("unchecked") final JAXBElement<Configuration> element = mock(JAXBElement.class); | ||
when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenReturn(null).thenReturn(ctx); | ||
when(ctx.createUnmarshaller()).thenReturn(unmarshaller); | ||
when(unmarshaller.unmarshal(inputStream)).thenReturn(element); | ||
xmlUtil.unmarshallConfiguration(inputStream); | ||
verify(element).getValue(); | ||
@ParameterizedTest | ||
@ValueSource(strings = {"Holidays_at.xml", "Holidays_de.xml", "Holidays_gb.xml", "Holidays_ua.xml", "Holidays_tr.xml", "Holidays_za.xml"}) | ||
void unmarshalRealResource(String holidayFileName) { | ||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("holidays/" + holidayFileName); | ||
final Configuration configuration = sut.unmarshallConfiguration(inputStream); | ||
assertThat(configuration.getHolidays()).isNotNull(); | ||
} | ||
} |