WW-4731 Adds additional test case to confirm that everything is ok

This commit is contained in:
Lukasz Lenart
2017-01-10 10:28:54 +01:00
parent fc6ffba9cf
commit f48c9620fc
4 changed files with 70 additions and 6 deletions
@@ -171,8 +171,9 @@ public class XSLTResult implements Result {
if (location != null) {
templates = getTemplates(location);
transformer = templates.newTransformer();
} else
} else {
transformer = TransformerFactory.newInstance().newTransformer();
}
transformer.setURIResolver(getURIResolver());
transformer.setErrorListener(buildErrorListener());
@@ -247,8 +248,7 @@ public class XSLTResult implements Result {
* function. The default is an instance of ServletURIResolver, which operates relative to the servlet context.
*/
protected URIResolver getURIResolver() {
return new ServletURIResolver(
ServletActionContext.getServletContext());
return new ServletURIResolver(ServletActionContext.getServletContext());
}
protected Templates getTemplates(final String path) throws TransformerException, IOException {
@@ -35,6 +35,7 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -82,6 +83,22 @@ public class XSLTResultTest extends StrutsInternalTestCase {
assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
}
public void testSimpleTransform5() throws Exception {
result.setParse(false);
result.setStylesheetLocation("XSLTResultTest6.xsl");
result.execute(mai);
String out = response.getContentAsString();
assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
assertTrue(out.contains("<title>WebWork in Action</title>"));
assertTrue(out.contains("<author>Patrick and Jason</author>"));
assertTrue(out.contains("<editions><edition value=\"I\">I</edition><edition value=\"IV\">IV</edition></editions>"));
assertTrue(out.contains("<book><title/><author/><editions/></book>"));
assertTrue(out.contains("<title>XWork not in Action</title>"));
assertTrue(out.contains("<author>Superman</author>"));
assertTrue(out.contains("<editions><edition value=\"1234\">1234</edition><edition value=\"345\">345</edition><edition value=\"6667\">6667</edition></editions>"));
}
public void testSimpleTransformParse() throws Exception {
result.setParse(true);
result.setStylesheetLocation("${top.myLocation}");
@@ -277,9 +294,9 @@ public class XSLTResultTest extends StrutsInternalTestCase {
public List getBooks() {
List list = new ArrayList();
list.add(new Book("WebWork in Action", "Patrick and Jason"));
list.add(new Book("WebWork in Action", "Patrick and Jason", Arrays.asList("I", "IV")));
list.add(null);
list.add(new Book("XWork not in Action", "Superman"));
list.add(new Book("XWork not in Action", "Superman", Arrays.asList("1234", "345", "6667")));
return list;
}
@@ -289,10 +306,12 @@ public class XSLTResultTest extends StrutsInternalTestCase {
private String title;
private String author;
private List<String> editions;
public Book(String title, String author) {
public Book(String title, String author, List<String> editions) {
this.title = title;
this.author = author;
this.editions = editions;
}
public String getTitle() {
@@ -302,5 +321,9 @@ public class XSLTResultTest extends StrutsInternalTestCase {
public String getAuthor() {
return author;
}
public List<String> getEditions() {
return editions;
}
}
}
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); 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
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="result">
<books>
<xsl:for-each select="books/item">
<book>
<title><xsl:value-of select="title"/></title>
<author><xsl:value-of select="author"/></author>
<editions>
<xsl:for-each select="editions/item">
<edition><xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></edition>
</xsl:for-each>
</editions>
</book>
</xsl:for-each>
</books>
</xsl:template>
</xsl:stylesheet>
+1
View File
@@ -9,5 +9,6 @@
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
<Logger name="org.apache.struts2.views.xslt" level="debug"/>
</Loggers>
</Configuration>