TwineArchiveParser { public TwStoriesdata parse(final InputStream inputStream) { try { JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(getClass().getResource("/format.xsd")); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(event -> IGNORED_ERRORS.stream().anyMatch(ignoredError -> event.getMessage().startsWith(ignoredError))); JAXBElement<TwStoriesdata> unmarshalledObject = (JAXBElement<TwStoriesdata>) unmarshaller.unmarshal(inputStream); return unmarshalledObject.getValue(); } catch (SAXException | JAXBException e) { throw new TwineParseFailedException(e); } }  TwStoriesdata parse(final InputStream inputStream);  }
@Test public void parse_ValidXmlFile_ReturnsTwStoriesData() throws Exception { TwStoriesdata result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/valid.xml")) { result = parser.parse(inputStream); } assertNotNull(result); assertThat(result.getTwStorydata().size(), is(1)); assertThat(result.getTwStorydata().get(0).getTwPassagedata().size(), is(2)); }
@Test public void parse_NewLineInPassage_NewLinesAreRetained() throws Exception { TwStoriesdata result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/valid.xml")) { result = parser.parse(inputStream); } assertNotNull(result); assertThat(result.getTwStorydata().size(), is(1)); assertTrue(result.getTwStorydata().get(0).getTwPassagedata().get(0).getValue().contains("\n")); }
@Test(expected = TwineParseFailedException.class) public void parse_InvalidXmlFile_ThrowsTwineParseFailedException() throws Exception { try(InputStream inputStream = getClass().getResourceAsStream("/xml/invalid.xml")) { parser.parse(inputStream); } }
@Test public void parse_ExtraAttributeInXmlFile_IgnoresAttribute() throws Exception { TwStoriesdata result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/extra_attribute.xml")) { result = parser.parse(inputStream); } assertNotNull(result); }
@Test(expected = IOException.class) public void parse_InputDoesNotExist_ThrowsIOException() throws Exception { File doesNotExist = new File("/xml/doesNotExist.xml"); try(InputStream inputStream = new FileInputStream(doesNotExist)) { parser.parse(inputStream); } }
@Test(expected = IllegalArgumentException.class) public void parse_NullParameter_ThrowsIllegalArgumentException() { parser.parse(null); }