Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
#15 extends tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agabrys committed Jun 17, 2018
1 parent d3a1aae commit 0d34bf9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
Expand Up @@ -39,7 +39,7 @@ public class XsltTransformer {
* @since 1.0
*/
public String transform(final String xml, final File xslt) throws TransformException {
final Transformer transformer = createTransformer(TransformerFactory.newInstance(), xslt);
final Transformer transformer = createTransformer(createTransformerFactory(), xslt);
final StringWriter writer = new StringWriter();
try {
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(writer));
Expand All @@ -52,10 +52,19 @@ public String transform(final String xml, final File xslt) throws TransformExcep

Transformer createTransformer(final TransformerFactory factory, final File xslt) throws TransformException {
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory.newTransformer(new StreamSource(xslt));
} catch (final TransformerConfigurationException e) {
throw new TransformException(e);
}
}

TransformerFactory createTransformerFactory() throws TransformException {
final TransformerFactory factory = TransformerFactory.newInstance();
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (final TransformerConfigurationException e) {
throw new TransformException(e);
}
return factory;
}
}
Expand Up @@ -2,8 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -31,46 +30,16 @@

public class XsltTransformerTest {

@Test(expected = TransformException.class)
public void createTransformer_configurationIsUnsupported_throwsException()
throws TransformerConfigurationException, TransformException {

final TransformerFactory factory = mock(TransformerFactory.class);
doThrow(TransformerConfigurationException.class).when(factory).setFeature(anyString(), anyBoolean());
final File xslt = mock(File.class);

new XsltTransformer().createTransformer(factory, xslt);
}

@Test
public void createTransformer_configurationIsSupported_returnsTransformer()
throws TransformerConfigurationException, TransformException {
final TransformerFactory factory = mock(TransformerFactory.class);
final Transformer transformer = mock(Transformer.class);
when(factory.newTransformer(any(StreamSource.class))).thenReturn(transformer);

final File xslt = mock(File.class);
when(xslt.toURI()).thenReturn(URI.create("file.xslt"));

final Transformer result = new XsltTransformer().createTransformer(factory, xslt);

assertThat(result).isSameAs(transformer);
verify(factory).setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
final ArgumentCaptor<StreamSource> captor = ArgumentCaptor.forClass(StreamSource.class);
verify(factory).newTransformer(captor.capture());
final StreamSource source = captor.getValue();
assertThat(source).isNotNull();
assertThat(source.getSystemId()).isEqualTo("file.xslt");
}

@Test(expected = TransformException.class)
public void transform_invalidData_throwsException() throws TransformerException, TransformException {
final XsltTransformer xsltTransformer = spy(new XsltTransformer());

final TransformerFactory factory = mock(TransformerFactory.class);
doReturn(factory).when(xsltTransformer).createTransformerFactory();
final Transformer transformer = mock(Transformer.class);
doReturn(transformer).when(xsltTransformer).createTransformer(any(TransformerFactory.class), any(File.class));
doThrow(TransformerException.class).when(transformer).transform(any(StreamSource.class), any(StreamResult.class));
doReturn(transformer).when(xsltTransformer).createTransformer(eq(factory), any(File.class));

doThrow(TransformerException.class).when(transformer).transform(any(StreamSource.class), any(StreamResult.class));
final String xml = "xml";
final File xslt = mock(File.class);

Expand All @@ -81,8 +50,10 @@ public void transform_invalidData_throwsException() throws TransformerException,
public void transform_correctData_returnsTransformedDocument() throws TransformerException, TransformException {
final XsltTransformer xsltTransformer = spy(new XsltTransformer());

final TransformerFactory factory = mock(TransformerFactory.class);
doReturn(factory).when(xsltTransformer).createTransformerFactory();
final Transformer transformer = mock(Transformer.class);
doReturn(transformer).when(xsltTransformer).createTransformer(any(TransformerFactory.class), any(File.class));
doReturn(transformer).when(xsltTransformer).createTransformer(eq(factory), any(File.class));

doAnswer(new Answer<Void>() {

Expand All @@ -101,4 +72,43 @@ public Void answer(final InvocationOnMock invocation) throws IOException {

assertThat(result).isEqualTo("document");
}

@Test(expected = TransformException.class)
public void createTransformer_sourceIsInvalid_throwsException() throws TransformerConfigurationException, TransformException {
final TransformerFactory factory = mock(TransformerFactory.class);
when(factory.newTransformer(any(StreamSource.class))).thenThrow(TransformerConfigurationException.class);

final File xslt = mock(File.class);
when(xslt.toURI()).thenReturn(URI.create("uri"));

new XsltTransformer().createTransformer(factory, xslt);
}

@Test
public void createTransformer_sourceIsValid_returnsTransformer() throws TransformerConfigurationException, TransformException {
final TransformerFactory factory = mock(TransformerFactory.class);
final Transformer transformer = mock(Transformer.class);
when(factory.newTransformer(any(StreamSource.class))).thenReturn(transformer);

final File xslt = mock(File.class);
final String uri = "file.xslt";
when(xslt.toURI()).thenReturn(URI.create(uri));

final Transformer result = new XsltTransformer().createTransformer(factory, xslt);

assertThat(result).isSameAs(transformer);
final ArgumentCaptor<StreamSource> captor = ArgumentCaptor.forClass(StreamSource.class);
verify(factory).newTransformer(captor.capture());
final StreamSource source = captor.getValue();
assertThat(source).isNotNull();
assertThat(source.getSystemId()).isEqualTo(uri);
}

@Test
public void createTransformerFactory() throws TransformException {
final TransformerFactory result = new XsltTransformer().createTransformerFactory();

assertThat(result).isNotNull();
assertThat(result.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)).isTrue();
}
}

0 comments on commit 0d34bf9

Please sign in to comment.