This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
/
XsltTransformerTest.java
114 lines (89 loc) · 4.74 KB
/
XsltTransformerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package biz.gabrys.maven.plugins.directory.content.transform;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import javax.xml.XMLConstants;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class XsltTransformerTest {
@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(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);
xsltTransformer.transform(xml, xslt);
}
@Test
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(eq(factory), any(File.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(final InvocationOnMock invocation) throws IOException {
final StreamResult result = (StreamResult) invocation.getArgument(1);
result.getWriter().append("document");
return null;
}
}).when(transformer).transform(any(StreamSource.class), any(StreamResult.class));
final String xml = "xml";
final File xslt = mock(File.class);
final String result = xsltTransformer.transform(xml, xslt);
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();
}
}