|
16 | 16 | package uk.co.real_logic.sbe.xml; |
17 | 17 |
|
18 | 18 | import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
19 | 21 | import org.w3c.dom.Document; |
20 | 22 | import org.w3c.dom.NodeList; |
21 | 23 | import org.xml.sax.SAXException; |
|
31 | 33 | import javax.xml.xpath.XPathFactory; |
32 | 34 | import java.io.ByteArrayInputStream; |
33 | 35 | import java.io.IOException; |
| 36 | +import java.io.InputStream; |
34 | 37 | import java.util.HashMap; |
35 | 38 | import java.util.List; |
36 | 39 | import java.util.Map; |
37 | 40 |
|
38 | 41 | import static org.hamcrest.MatcherAssert.assertThat; |
39 | 42 | import static org.hamcrest.core.Is.is; |
40 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
41 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 43 | +import static org.junit.jupiter.api.Assertions.*; |
42 | 44 | import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
43 | 45 |
|
44 | 46 | public class EnumTypeTest |
@@ -223,6 +225,61 @@ public void shouldThrowExceptionWhenDuplicateNameSpecified() |
223 | 225 | parseTestXmlWithMap("/types/enum", testXmlString)); |
224 | 226 | } |
225 | 227 |
|
| 228 | + @Test |
| 229 | + public void shouldThrowExceptionWhenImplicitNullValueIsUsed() |
| 230 | + { |
| 231 | + final String testXmlString = |
| 232 | + "<types>" + |
| 233 | + "<enum name=\"test\" encodingType=\"uint8\">" + |
| 234 | + " <validValue name=\"one\">1</validValue>" + |
| 235 | + " <validValue name=\"invalidNullValue\">255</validValue>" + |
| 236 | + "</enum>" + |
| 237 | + "</types>"; |
| 238 | + |
| 239 | + assertThrows(IllegalArgumentException.class, () -> |
| 240 | + parseTestXmlWithMap("/types/enum", testXmlString)); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + public void shouldThrowExceptionWhenExplicitNullValueIsUsed() |
| 245 | + { |
| 246 | + final String testXmlString = |
| 247 | + "<types>" + |
| 248 | + "<enum name=\"test\" encodingType=\"uint8\" presence=\"optional\" nullValue=\"5\">" + |
| 249 | + " <validValue name=\"one\">1</validValue>" + |
| 250 | + " <validValue name=\"invalidNullValue\">5</validValue>" + |
| 251 | + "</enum>" + |
| 252 | + "</types>"; |
| 253 | + |
| 254 | + assertThrows(IllegalArgumentException.class, () -> |
| 255 | + parseTestXmlWithMap("/types/enum", testXmlString)); |
| 256 | + } |
| 257 | + |
| 258 | + @ParameterizedTest |
| 259 | + @ValueSource(longs = { (long)Integer.MIN_VALUE - 1, (long)Integer.MAX_VALUE + 1 }) |
| 260 | + public void shouldThrowExceptionWhenIntValueIsOutOfRange(final long value) |
| 261 | + { |
| 262 | + final String testXmlString = |
| 263 | + "<types>" + |
| 264 | + "<enum name=\"test\" encodingType=\"int32\">" + |
| 265 | + " <validValue name=\"X\">" + value + "</validValue>" + |
| 266 | + "</enum>" + |
| 267 | + "</types>"; |
| 268 | + |
| 269 | + final NumberFormatException exception = assertThrows(NumberFormatException.class, () -> |
| 270 | + parseTestXmlWithMap("/types/enum", testXmlString)); |
| 271 | + assertEquals("For input string: \"" + value + "\"", exception.getMessage()); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void shouldThrowExceptionIfEnumValueIsOutOfCustomValueRange() throws IOException |
| 276 | + { |
| 277 | + final InputStream file = Tests.getLocalResource("error-handler-enum-violates-min-max-value-range.xml"); |
| 278 | + final IllegalStateException exception = assertThrows(IllegalStateException.class, |
| 279 | + () -> parse(file, ParserOptions.builder().suppressOutput(true).build())); |
| 280 | + assertEquals("had 4 errors", exception.getMessage()); |
| 281 | + } |
| 282 | + |
226 | 283 | @Test |
227 | 284 | public void shouldHandleEncodingTypesWithNamedTypes() throws Exception |
228 | 285 | { |
|
0 commit comments