ExtendTwineXmlTransformer { public void transform(final InputStream input, final OutputStream output) { Objects.requireNonNull(input); Objects.requireNonNull(output); try { DocumentBuilderFactory documentBuilderFactory = SafeXmlFactory.InternalOnlyDocumentBuilderFactory.newInstance(); documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); Document document = builder.parse(input); TransformerFactory transformerFactory = SafeXmlFactory.InternalOnlyTransformerFactory.newInstance(); transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); StreamSource stylesheetSource = new StreamSource(getClass().getResourceAsStream("/extend.xsl")); Transformer transformer = transformerFactory.newTransformer(stylesheetSource); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(output); transformer.transform(source, result); } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) { throw new ExtendTwineXmlTransformFailedException(e); } }  void transform(final InputStream input, final OutputStream output);  }
@Test public void transform_MetadataPassage_ReturnsValidXTwineML() throws IOException, SAXException { byte[] result; try(InputStream in = getClass().getResourceAsStream("/xml/metadata.xml"); ByteArrayOutputStream out = new ByteArrayOutputStream()) { transformer.transform(in, out); result = out.toByteArray(); } try(InputStream in = new ByteArrayInputStream(result)) { Source inputSource = new StreamSource(in); validator.validate(inputSource); in.reset(); Document document = documentBuilder.parse(in); NodeList xtwMetadatas = document.getElementsByTagName("xtw-metadata"); assertEquals(1, xtwMetadatas.getLength()); assertEquals("Just a Title!", xtwMetadatas.item(0).getChildNodes().item(1).getTextContent()); } }
@Test public void transform_NoMetadataPassage_ReturnsValidXTwineML() throws IOException, SAXException { byte[] result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/metadata_none.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { transformer.transform(inputStream, outputStream); result = outputStream.toByteArray(); } try(InputStream in = new ByteArrayInputStream(result)) { Source inputSource = new StreamSource(in); validator.validate(inputSource); in.reset(); Document document = documentBuilder.parse(in); NodeList xtwMetadatas = document.getElementsByTagName("xtw-metadata"); assertEquals(0, xtwMetadatas.getLength()); } }
@Test public void transform_UnexpectedElement_IgnoresUnexpectedElements() throws IOException, SAXException { byte[] result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/metadata_unexpected_element.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { transformer.transform(inputStream, outputStream); result = outputStream.toByteArray(); } try(InputStream in = new ByteArrayInputStream(result)) { Document document = documentBuilder.parse(in); NodeList xtwMetadatas = document.getElementsByTagName("xtw-metadata"); assertEquals(1, xtwMetadatas.getLength()); assertEquals("Title is still set!", xtwMetadatas.item(0).getChildNodes().item(1).getTextContent()); } }
@Test @Ignore("#43 throw exception, or try to repair the document for this scenario?") public void transform_MetadataInvalidClosingTag_ThrowsException() { try(InputStream inputStream = getClass().getResourceAsStream("/xml/metadata_not_well_formed.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { transformer.transform(inputStream, outputStream); } catch(Throwable e) { Assert.assertTrue(String.format("Expected .transform to throw a %s exception, but instead got an %s", ExtendTwineXmlTransformFailedException.class, e), e instanceof ExtendTwineXmlTransformFailedException); return; } Assert.fail("Transform did now throw an exception"); }
@Test public void transform_MetadataPassageEmpty_ThrowsException() throws IOException, SAXException { byte[] result; try(InputStream inputStream = getClass().getResourceAsStream("/xml/metadata_empty.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { transformer.transform(inputStream, outputStream); result = outputStream.toByteArray(); } try(InputStream in = new ByteArrayInputStream(result)) { Document document = documentBuilder.parse(in); } }
@Test(expected = ExtendTwineXmlTransformFailedException.class) public void transform_XmlNotWellFormed_ThrowsException() throws IOException { try(InputStream inputStream = getClass().getResourceAsStream("/xml/not_well_formed.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { transformer.transform(inputStream, outputStream); } }
@Test(expected = NullPointerException.class) public void transform_NullInput_ThrowsNullPointerException() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); transformer.transform(null, outputStream); }
@Test(expected = NullPointerException.class) public void transform_NullOutput_ThrowsNullPointerException() { InputStream inputStream = getClass().getResourceAsStream("/xml/metadata.xml"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); transformer.transform(inputStream, null); }