INT-2768 Polishing - Docs, White Space

Add what's new; fix white space in test case.
This commit is contained in:
Gary Russell
2013-09-25 17:43:33 -04:00
parent 2f8053f585
commit 5617d2e2d8
3 changed files with 154 additions and 138 deletions

View File

@@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -49,176 +49,176 @@ import org.springframework.xml.transform.StringSource;
*/
public class XsltPayloadTransformerTests {
private XsltPayloadTransformer transformer;
private XsltPayloadTransformer transformer;
private String docAsString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
private final String docAsString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
private String outputAsString = "<bob>test</bob>";
private final String outputAsString = "<bob>test</bob>";
@Before
public void setUp() throws Exception {
transformer = new XsltPayloadTransformer(getXslResource());
@Before
public void setUp() throws Exception {
transformer = new XsltPayloadTransformer(getXslResource());
transformer.afterPropertiesSet();
}
}
@Test
public void testDocumentAsPayload() throws Exception {
Object transformed = transformer.doTransform(buildMessage(XmlTestUtil
.getDocumentForString(docAsString)));
assertTrue("Wrong return type for document payload", Document.class
.isAssignableFrom(transformed.getClass()));
Document transformedDocument = (Document) transformed;
assertXMLEqual(outputAsString, XmlTestUtil
.docToString(transformedDocument));
}
@Test
public void testDocumentAsPayload() throws Exception {
Object transformed = transformer.doTransform(buildMessage(XmlTestUtil
.getDocumentForString(docAsString)));
assertTrue("Wrong return type for document payload", Document.class
.isAssignableFrom(transformed.getClass()));
Document transformedDocument = (Document) transformed;
assertXMLEqual(outputAsString, XmlTestUtil
.docToString(transformedDocument));
}
@Test
public void testSourceAsPayload() throws Exception {
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong return type for source payload", DOMResult.class,
transformed.getClass());
DOMResult result = (DOMResult) transformed;
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
.getDocumentForString(outputAsString), (Document) result
.getNode());
}
@Test
public void testSourceAsPayload() throws Exception {
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong return type for source payload", DOMResult.class,
transformed.getClass());
DOMResult result = (DOMResult) transformed;
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
.getDocumentForString(outputAsString), (Document) result
.getNode());
}
@Test
public void testStringAsPayload() throws Exception {
Object transformed = transformer.doTransform(buildMessage(docAsString));
assertEquals("Wrong return type for string payload", String.class,
transformed.getClass());
String transformedString = (String) transformed;
assertXMLEqual("String incorrect after transform", outputAsString,
transformedString);
}
@Test
public void testStringAsPayload() throws Exception {
Object transformed = transformer.doTransform(buildMessage(docAsString));
assertEquals("Wrong return type for string payload", String.class,
transformed.getClass());
String transformedString = (String) transformed;
assertXMLEqual("String incorrect after transform", outputAsString,
transformedString);
}
@Test
public void testStringAsPayloadUseResultFactoryTrue() throws Exception {
transformer.setAlwaysUseResultFactory(true);
Object transformed = transformer.doTransform(buildMessage(docAsString));
assertEquals("Wrong return type for useFactories true",
DOMResult.class, transformed.getClass());
DOMResult result = (DOMResult) transformed;
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
.getDocumentForString(outputAsString), (Document) result
.getNode());
}
@Test
public void testStringAsPayloadUseResultFactoryTrue() throws Exception {
transformer.setAlwaysUseResultFactory(true);
Object transformed = transformer.doTransform(buildMessage(docAsString));
assertEquals("Wrong return type for useFactories true",
DOMResult.class, transformed.getClass());
DOMResult result = (DOMResult) transformed;
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
.getDocumentForString(outputAsString), (Document) result
.getNode());
}
@Test
public void testSourceWithResultTransformer() throws Exception {
Integer returnValue = new Integer(13);
transformer = new XsltPayloadTransformer(getXslResource(),
new StubResultTransformer(returnValue));
@Test
public void testSourceWithResultTransformer() throws Exception {
Integer returnValue = new Integer(13);
transformer = new XsltPayloadTransformer(getXslResource(),
new StubResultTransformer(returnValue));
transformer.afterPropertiesSet();
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong value from result conversion", returnValue,
transformed);
}
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong value from result conversion", returnValue,
transformed);
}
@Test
public void testXsltPayloadWithTransformerFactoryClassname() throws Exception {
Integer returnValue = new Integer(13);
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
@Test
public void testXsltPayloadWithTransformerFactoryClassname() throws Exception {
Integer returnValue = new Integer(13);
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
transformer.afterPropertiesSet();
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong value from result conversion", returnValue,
transformed);
}
Object transformed = transformer
.doTransform(buildMessage(new StringSource(docAsString)));
assertEquals("Wrong value from result conversion", returnValue,
transformed);
}
@Test(expected = TransformerFactoryConfigurationError.class)
public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception {
transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
@Test(expected = TransformerFactoryConfigurationError.class)
public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception {
transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
transformer.afterPropertiesSet();
transformer.doTransform(buildMessage(new StringSource(docAsString)));
}
transformer.doTransform(buildMessage(new StringSource(docAsString)));
}
@Test(expected = TransformerException.class)
public void testNonXmlString() throws Exception {
transformer.doTransform(buildMessage("test"));
}
@Test(expected = TransformerException.class)
public void testNonXmlString() throws Exception {
transformer.doTransform(buildMessage("test"));
}
@Test(expected = MessagingException.class)
public void testUnsupportedPayloadType() throws Exception {
transformer.doTransform(buildMessage(new Long(12)));
}
@Test(expected = MessagingException.class)
public void testUnsupportedPayloadType() throws Exception {
transformer.doTransform(buildMessage(new Long(12)));
}
@Test
public void testXsltWithImports() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
@Test
public void testXsltWithImports() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
transformer.afterPropertiesSet();
assertEquals(transformer.doTransform(buildMessage(docAsString)),
assertEquals(transformer.doTransform(buildMessage(docAsString)),
outputAsString);
}
}
@Test
public void documentInStringResultOut() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
@Test
public void documentInStringResultOut() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
transformer.afterPropertiesSet();
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
}
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
}
@Test
public void stringInDomResultOut() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
@Test
public void stringInDomResultOut() throws Exception {
Resource resource = new ClassPathResource("transform-with-import.xsl",
this.getClass());
transformer = new XsltPayloadTransformer(resource);
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
transformer.afterPropertiesSet();
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
}
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
}
@Test
public void docInStringOut() throws Exception {
transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText());
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
@Test
public void docInStringOut() throws Exception {
transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText());
transformer.setResultFactory(new StringResultFactory());
transformer.setAlwaysUseResultFactory(true);
transformer.afterPropertiesSet();
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
assertEquals("Wrong content in string", "hello world", returned.toString());
}
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
assertEquals("Wrong content in string", "hello world", returned.toString());
}
protected Message<?> buildMessage(Object payload) {
return MessageBuilder.withPayload(payload).build();
}
protected Message<?> buildMessage(Object payload) {
return MessageBuilder.withPayload(payload).build();
}
private Resource getXslResource() throws Exception {
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"order\"><bob>test</bob></xsl:template></xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes("UTF-8"));
}
private Resource getXslResource() throws Exception {
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"order\"><bob>test</bob></xsl:template></xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes("UTF-8"));
}
private Resource getXslResourceThatOutputsText() throws Exception {
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"text\" encoding=\"UTF-8\" /><xsl:template match=\"order\">hello world</xsl:template></xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes("UTF-8"));
}
private Resource getXslResourceThatOutputsText() throws Exception {
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"text\" encoding=\"UTF-8\" /><xsl:template match=\"order\">hello world</xsl:template></xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes("UTF-8"));
}
public static class StubResultTransformer implements ResultTransformer {
public static class StubResultTransformer implements ResultTransformer {
private Object objectToReturn;
private final Object objectToReturn;
public StubResultTransformer(Object objectToReturn) {
this.objectToReturn = objectToReturn;
}
public StubResultTransformer(Object objectToReturn) {
this.objectToReturn = objectToReturn;
}
public Object transformResult(Result result) {
return objectToReturn;
}
}
public Object transformResult(Result result) {
return objectToReturn;
}
}
}