|
16 | 16 |
|
17 | 17 | package org.springframework.util.xml;
|
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
19 | 20 | import java.io.InputStream;
|
20 | 21 | import javax.xml.stream.XMLInputFactory;
|
21 | 22 | import javax.xml.stream.XMLStreamException;
|
| 23 | +import javax.xml.transform.Transformer; |
| 24 | +import javax.xml.transform.TransformerFactory; |
| 25 | +import javax.xml.transform.dom.DOMResult; |
| 26 | +import javax.xml.transform.sax.SAXSource; |
22 | 27 |
|
| 28 | +import static org.junit.Assert.assertEquals; |
23 | 29 | import org.junit.Before;
|
24 | 30 | import org.junit.Test;
|
25 | 31 | import static org.mockito.BDDMockito.*;
|
26 | 32 | import org.mockito.invocation.InvocationOnMock;
|
27 | 33 | import org.mockito.stubbing.Answer;
|
| 34 | +import org.w3c.dom.Node; |
28 | 35 | import org.xml.sax.Attributes;
|
29 | 36 | import org.xml.sax.ContentHandler;
|
30 | 37 | import org.xml.sax.InputSource;
|
@@ -103,6 +110,25 @@ public void contentHandlerNoNamespacesPrefixes() throws Exception {
|
103 | 110 | verifyIdenticalInvocations(standardContentHandler, contentHandler);
|
104 | 111 | }
|
105 | 112 |
|
| 113 | + @Test |
| 114 | + public void whitespace() throws Exception { |
| 115 | + String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>"; |
| 116 | + |
| 117 | + Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
| 118 | + |
| 119 | + AbstractStaxXMLReader staxXmlReader = createStaxXmlReader( |
| 120 | + new ByteArrayInputStream(xml.getBytes("UTF-8"))); |
| 121 | + |
| 122 | + SAXSource source = new SAXSource(staxXmlReader, new InputSource()); |
| 123 | + DOMResult result = new DOMResult(); |
| 124 | + |
| 125 | + transformer.transform(source, result); |
| 126 | + |
| 127 | + Node node1 = result.getNode().getFirstChild().getFirstChild(); |
| 128 | + assertEquals(" ", node1.getTextContent()); |
| 129 | + assertEquals(" Some text ", node1.getNextSibling().getTextContent()); |
| 130 | + } |
| 131 | + |
106 | 132 | @Test
|
107 | 133 | public void lexicalHandler() throws Exception {
|
108 | 134 | Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
|
@@ -130,7 +156,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
|
130 | 156 | verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler);
|
131 | 157 | }
|
132 | 158 |
|
133 |
| - private final LexicalHandler mockLexicalHandler() throws Exception { |
| 159 | + private LexicalHandler mockLexicalHandler() throws Exception { |
134 | 160 | LexicalHandler lexicalHandler = mock(LexicalHandler.class);
|
135 | 161 | willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt());
|
136 | 162 | return lexicalHandler;
|
|
0 commit comments