|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 | package org.lfenergy.compas.scl.validator.util;
|
5 | 5 |
|
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import javax.xml.stream.XMLInputFactory; |
| 9 | +import javax.xml.stream.XMLStreamException; |
| 10 | +import javax.xml.stream.events.StartElement; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | +import static org.lfenergy.compas.scl.validator.util.StaxUtil.getAttributeValue; |
| 16 | +import static org.lfenergy.compas.scl.validator.util.StaxUtil.isElement; |
| 17 | + |
6 | 18 | public class StaxUtilTest {
|
| 19 | + @Test |
| 20 | + void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden() { |
| 21 | + assertThrows(UnsupportedOperationException.class, StaxUtil::new); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void isElement_WhenCalledWithExpectedElementName_ThenFirstElementHasCorrectElementName() throws XMLStreamException, IOException { |
| 26 | + var element = getFirstElement(); |
| 27 | + if (element != null) { |
| 28 | + var result = isElement(element, "SCL"); |
| 29 | + assertTrue(result, "No SCL Element Found"); |
| 30 | + } else { |
| 31 | + fail("XML File couldn't be read."); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void isElement_WhenCalledWithUnknownElementName_ThenFirstElementHasIncorrectElementName() throws XMLStreamException, IOException { |
| 37 | + var element = getFirstElement(); |
| 38 | + if (element != null) { |
| 39 | + var result = isElement(element, "Unknown"); |
| 40 | + assertFalse(result, "Unknown Element shouldn't be Found"); |
| 41 | + } else { |
| 42 | + fail("XML File couldn't be read."); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void getAttributeValue_WhenCalledWithKnownAttribute_ThenAttributeValueReturned() throws XMLStreamException, IOException { |
| 48 | + var element = getFirstElement(); |
| 49 | + if (element != null) { |
| 50 | + var result = getAttributeValue(element, "revision"); |
| 51 | + assertEquals("B", result); |
| 52 | + } else { |
| 53 | + fail("XML File couldn't be read."); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void getAttributeValue_WhenCalledWithUnknownAttribute_ThenNullReturned() throws XMLStreamException, IOException { |
| 59 | + var element = getFirstElement(); |
| 60 | + if (element != null) { |
| 61 | + var result = getAttributeValue(element, "unknown"); |
| 62 | + assertNull(result); |
| 63 | + } else { |
| 64 | + fail("XML File couldn't be read."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private StartElement getFirstElement() throws XMLStreamException, IOException { |
| 69 | + try (var inputStream = getClass().getResourceAsStream("/scl/example.scd")) { |
| 70 | + var xmlInputFactory = XMLInputFactory.newInstance(); |
| 71 | + var reader = xmlInputFactory.createXMLEventReader(inputStream); |
| 72 | + |
| 73 | + while (reader.hasNext()) { |
| 74 | + var nextEvent = reader.nextEvent(); |
| 75 | + if (nextEvent.isStartElement()) { |
| 76 | + return nextEvent.asStartElement(); |
| 77 | + } |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
7 | 82 | }
|
0 commit comments