Moved tests from testsuite to web.servlet
This commit is contained in:
@@ -0,0 +1,392 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.xslt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.ErrorListener;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.core.JdkVersion;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link AbstractXsltView} class.
|
||||
*
|
||||
* @author Darren Davison
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
* @since 11.03.2005
|
||||
*/
|
||||
public class TestXsltViewTests extends TestCase {
|
||||
|
||||
private TestXsltView view;
|
||||
|
||||
private int warnings = 0;
|
||||
|
||||
private int errors = 0;
|
||||
|
||||
private int fatal = 0;
|
||||
|
||||
|
||||
public void setUp() {
|
||||
this.view = new TestXsltView();
|
||||
}
|
||||
|
||||
|
||||
public void testNoSuchStylesheet() {
|
||||
view.setStylesheetLocation(new FileSystemResource("/does/not/exist.xsl"));
|
||||
try {
|
||||
view.initApplicationContext();
|
||||
fail("Must have thrown ApplicationContextException");
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testCustomErrorListener() {
|
||||
view.setErrorListener(new ErrorListener() {
|
||||
public void warning(TransformerException ex) {
|
||||
incWarnings();
|
||||
}
|
||||
public void error(TransformerException ex) {
|
||||
incErrors();
|
||||
}
|
||||
public void fatalError(TransformerException ex) {
|
||||
incFatals();
|
||||
}
|
||||
});
|
||||
|
||||
// loaded stylesheet is not well formed
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/errors.xsl"));
|
||||
try {
|
||||
view.initApplicationContext();
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
// shouldn't really happen, but can be let through by XSLT engine
|
||||
assertTrue(ex.getCause() instanceof TransformerException);
|
||||
}
|
||||
assertEquals(1, fatal);
|
||||
assertEquals(1, errors);
|
||||
assertEquals(0, warnings);
|
||||
}
|
||||
|
||||
public void testRender() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Source createXsltSource(Map model, String root, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
Hero hero = (Hero) model.get("hero");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement(root);
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
return new DOMSource(node);
|
||||
}
|
||||
};
|
||||
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/sunnyDay.xsl"));
|
||||
view.setIndent(true);
|
||||
view.initApplicationContext();
|
||||
|
||||
view.render(new ModelAndView().addObject("hero", new Hero("Jet", 24, "BOOM")).getModel(), request, response);
|
||||
assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
|
||||
String text = response.getContentAsString();
|
||||
assertEquals("<hero name=\"Jet\" age=\"24\" catchphrase=\"BOOM\" sex=\"Female\"/>", text.trim());
|
||||
}
|
||||
|
||||
public void testRenderWithCustomContentType() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Source createXsltSource(Map model, String root, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
Hero hero = (Hero) model.get("hero");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement(root);
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
return new DOMSource(node);
|
||||
}
|
||||
};
|
||||
|
||||
view.setContentType("text/plain");
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/sunnyDay.xsl"));
|
||||
view.setIndent(true);
|
||||
view.initApplicationContext();
|
||||
|
||||
view.render(new ModelAndView().addObject("hero", new Hero("Jet", 24, "BOOM")).getModel(), request, response);
|
||||
assertEquals("text/plain", response.getContentType());
|
||||
String text = response.getContentAsString();
|
||||
assertEquals("<hero name=\"Jet\" age=\"24\" catchphrase=\"BOOM\" sex=\"Female\"/>", text.trim());
|
||||
}
|
||||
|
||||
public void testRenderWithSingleSourceInModel() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Map getParameters(HttpServletRequest request) {
|
||||
Map parameters = new HashMap();
|
||||
parameters.put("sex", "Male");
|
||||
return parameters;
|
||||
}
|
||||
};
|
||||
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/sunnyDay.xsl"));
|
||||
Properties outputProperties = new Properties();
|
||||
outputProperties.setProperty("indent", "false");
|
||||
view.setOutputProperties(outputProperties);
|
||||
view.initApplicationContext();
|
||||
|
||||
Hero hero = new Hero("Jet", 24, "BOOM");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement("hero");
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
|
||||
view.render(new ModelAndView().addObject("hero", new DOMSource(node)).getModel(), request, response);
|
||||
assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
|
||||
String text = response.getContentAsString();
|
||||
assertEquals("<hero name=\"Jet\" age=\"24\" catchphrase=\"BOOM\" sex=\"Male\"/>", text.trim());
|
||||
}
|
||||
|
||||
public void testRenderWithSingleNodeInModel() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setWriterAccessAllowed(false);
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Map getParameters(HttpServletRequest request) {
|
||||
Map parameters = new HashMap();
|
||||
parameters.put("sex", "Male");
|
||||
return parameters;
|
||||
}
|
||||
};
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/sunnyDay.xsl"));
|
||||
view.initApplicationContext();
|
||||
|
||||
Hero hero = new Hero("Jet", 24, "BOOM");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement("hero");
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
|
||||
view.render(new ModelAndView().addObject("hero", node).getModel(), request, response);
|
||||
String text = response.getContentAsString();
|
||||
assertEquals("<hero name=\"Jet\" age=\"24\" catchphrase=\"BOOM\" sex=\"Male\"/>", text.trim());
|
||||
}
|
||||
|
||||
public void testRenderWithNoStylesheetSpecified() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Map getParameters(HttpServletRequest request) {
|
||||
Map parameters = new HashMap();
|
||||
parameters.put("sex", "Male");
|
||||
return parameters;
|
||||
}
|
||||
};
|
||||
|
||||
Properties outputProperties = new Properties();
|
||||
outputProperties.setProperty("indent", "false");
|
||||
view.setOutputProperties(outputProperties);
|
||||
view.initApplicationContext();
|
||||
|
||||
Hero hero = new Hero("Jet", 24, "BOOM");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement("hero");
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
|
||||
view.render(new ModelAndView().addObject("hero", new DOMSource(node)).getModel(), request, response);
|
||||
assertEquals("text/xml;charset=ISO-8859-1", response.getContentType());
|
||||
String text = response.getContentAsString().trim();
|
||||
assertTrue(text.startsWith("<?xml"));
|
||||
assertTrue(text.indexOf("<hero") != -1);
|
||||
assertTrue(text.indexOf("age=\"24\"") != -1);
|
||||
assertTrue(text.indexOf("catchphrase=\"BOOM\"") != -1);
|
||||
assertTrue(text.indexOf("name=\"Jet\"") != -1);
|
||||
}
|
||||
|
||||
public void testRenderSingleNodeInModelWithExplicitDocRootName() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setOutputStreamAccessAllowed(false);
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Source createXsltSource(Map model, String root, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
Hero hero = (Hero) model.get("hero");
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element node = document.createElement(root);
|
||||
node.setAttribute("name", hero.getName());
|
||||
node.setAttribute("age", hero.getAge() + "");
|
||||
node.setAttribute("catchphrase", hero.getCatchphrase());
|
||||
return new DOMSource(node);
|
||||
}
|
||||
protected Map getParameters(HttpServletRequest request) {
|
||||
Map parameters = new HashMap();
|
||||
parameters.put("sex", "Male");
|
||||
return parameters;
|
||||
}
|
||||
protected boolean useWriter() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/sunnyDayExplicitRoot.xsl"));
|
||||
view.setUseSingleModelNameAsRoot(false);
|
||||
view.setRoot("baddie");
|
||||
view.initApplicationContext();
|
||||
view.render(new ModelAndView().addObject("hero", new Hero("Jet", 24, "BOOM")).getModel(), request, response);
|
||||
String text = response.getContentAsString();
|
||||
assertTrue(text.trim().startsWith("<baddie "));
|
||||
}
|
||||
|
||||
/**
|
||||
* Not a test per-se, but rather only here to validate the example
|
||||
* given in the reference documentation.
|
||||
*/
|
||||
public void testMyFirstWordsExampleFromTheReferenceDocumentation() throws Exception {
|
||||
// TODO: Why does this test not even work on JDK 1.4?
|
||||
// Maybe because of the Xalan version there?
|
||||
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
|
||||
return;
|
||||
}
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
AbstractXsltView view = new AbstractXsltView() {
|
||||
protected Source createXsltSource(
|
||||
Map model, String rootName, HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element root = document.createElement(rootName);
|
||||
List words = (List) model.get("wordList");
|
||||
for (Iterator it = words.iterator(); it.hasNext();) {
|
||||
String nextWord = (String) it.next();
|
||||
Element wordNode = document.createElement("word");
|
||||
Text textNode = document.createTextNode(nextWord);
|
||||
wordNode.appendChild(textNode);
|
||||
root.appendChild(wordNode);
|
||||
}
|
||||
return new DOMSource(root);
|
||||
}
|
||||
};
|
||||
|
||||
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/firstWords.xsl"));
|
||||
view.setIndent(true);
|
||||
view.initApplicationContext();
|
||||
|
||||
Map map = new HashMap();
|
||||
List wordList = new ArrayList();
|
||||
wordList.add("hello");
|
||||
wordList.add("world");
|
||||
map.put("wordList", wordList);
|
||||
|
||||
view.render(new ModelAndView("home", map).getModel(), request, response);
|
||||
String text = response.getContentAsString();
|
||||
assertTrue(text.trim().startsWith("<html"));
|
||||
}
|
||||
|
||||
|
||||
private void incWarnings() {
|
||||
warnings++;
|
||||
}
|
||||
|
||||
private void incErrors() {
|
||||
errors++;
|
||||
}
|
||||
|
||||
private void incFatals() {
|
||||
fatal++;
|
||||
}
|
||||
|
||||
|
||||
private static final class TestXsltView extends AbstractXsltView {
|
||||
}
|
||||
|
||||
|
||||
private static final class Hero {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private String catchphrase;
|
||||
|
||||
public Hero() {
|
||||
}
|
||||
|
||||
public Hero(String name, int age, String catchphrase) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.catchphrase = catchphrase;
|
||||
}
|
||||
|
||||
public String getCatchphrase() {
|
||||
return catchphrase;
|
||||
}
|
||||
|
||||
public void setCatchphrase(String catchphrase) {
|
||||
this.catchphrase = catchphrase;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.xslt;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XsltViewResolverTests extends TestCase {
|
||||
|
||||
public void testResolveView() throws Exception {
|
||||
StaticApplicationContext ctx = new StaticApplicationContext();
|
||||
|
||||
String prefix = ClassUtils.classPackageAsResourcePath(getClass());
|
||||
String suffix = ".xsl";
|
||||
String viewName = "products";
|
||||
|
||||
XsltViewResolver viewResolver = new XsltViewResolver();
|
||||
viewResolver.setPrefix(prefix);
|
||||
viewResolver.setSuffix(suffix);
|
||||
viewResolver.setApplicationContext(ctx);
|
||||
|
||||
XsltView view = (XsltView) viewResolver.resolveViewName(viewName, Locale.ENGLISH);
|
||||
assertNotNull("View should not be null", view);
|
||||
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.xslt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.JdkVersion;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class XsltViewTests {
|
||||
|
||||
private static final String HTML_OUTPUT = "/org/springframework/web/servlet/view/xslt/products.xsl";
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.request = new MockHttpServletRequest();
|
||||
this.response = new MockHttpServletResponse();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void withNoSource() throws Exception {
|
||||
final XsltView view = getXsltView(HTML_OUTPUT);
|
||||
view.render(new HashMap(), request, response);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void withoutUrl() throws Exception {
|
||||
final XsltView view = new XsltView();
|
||||
view.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleTransformWithSource() throws Exception {
|
||||
Source source = new StreamSource(getProductDataResource().getInputStream());
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", source);
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTransformWithDocument() throws Exception {
|
||||
org.w3c.dom.Document document = getDomDocument();
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", document);
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTransformWithNode() throws Exception {
|
||||
org.w3c.dom.Document document = getDomDocument();
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", document.getDocumentElement());
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTransformWithInputStream() throws Exception {
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", getProductDataResource().getInputStream());
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTransformWithReader() throws Exception {
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", new InputStreamReader(getProductDataResource().getInputStream()));
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTransformWithResource() throws Exception {
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", getProductDataResource());
|
||||
doTestWithModel(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSourceKey() throws Exception {
|
||||
XsltView view = getXsltView(HTML_OUTPUT);
|
||||
view.setSourceKey("actualData");
|
||||
|
||||
Map model = new HashMap();
|
||||
model.put("actualData", getProductDataResource());
|
||||
model.put("otherData", new ClassPathResource("dummyData.xsl", getClass()));
|
||||
|
||||
view.render(model, this.request, this.response);
|
||||
assertHtmlOutput(this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentTypeCarriedFromTemplate() throws Exception {
|
||||
XsltView view = getXsltView(HTML_OUTPUT);
|
||||
|
||||
Source source = new StreamSource(getProductDataResource().getInputStream());
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", source);
|
||||
|
||||
view.render(model, this.request, this.response);
|
||||
assertTrue(this.response.getContentType().startsWith("text/html"));
|
||||
assertEquals("UTF-8", this.response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModelParametersCarriedAcross() throws Exception {
|
||||
Map model = new HashMap();
|
||||
model.put("someKey", getProductDataResource());
|
||||
model.put("title", "Product List");
|
||||
doTestWithModel(model);
|
||||
assertTrue(this.response.getContentAsString().indexOf("Product List") > -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticAttributesCarriedAcross() throws Exception {
|
||||
XsltView view = getXsltView(HTML_OUTPUT);
|
||||
view.setSourceKey("actualData");
|
||||
view.addStaticAttribute("title", "Product List");
|
||||
|
||||
Map model = new HashMap();
|
||||
model.put("actualData", getProductDataResource());
|
||||
model.put("otherData", new ClassPathResource("dummyData.xsl", getClass()));
|
||||
|
||||
view.render(model, this.request, this.response);
|
||||
assertHtmlOutput(this.response.getContentAsString());
|
||||
assertTrue(this.response.getContentAsString().indexOf("Product List") > -1);
|
||||
|
||||
}
|
||||
|
||||
private org.w3c.dom.Document getDomDocument() throws ParserConfigurationException, SAXException, IOException {
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
org.w3c.dom.Document document = builder.parse(getProductDataResource().getInputStream());
|
||||
return document;
|
||||
}
|
||||
|
||||
private void doTestWithModel(Map model) throws Exception {
|
||||
XsltView view = getXsltView(HTML_OUTPUT);
|
||||
view.render(model, this.request, this.response);
|
||||
assertHtmlOutput(this.response.getContentAsString());
|
||||
}
|
||||
|
||||
private void assertHtmlOutput(String output) throws Exception {
|
||||
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
|
||||
// TODO: find out why the SAXReader.read call fails on JDK 1.4 and 1.3
|
||||
return;
|
||||
}
|
||||
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(new StringReader(output));
|
||||
List nodes = document.getRootElement().selectNodes("/html/body/table/tr");
|
||||
|
||||
Element tr1 = (Element) nodes.get(0);
|
||||
assertRowElement(tr1, "1", "Whatsit", "12.99");
|
||||
Element tr2 = (Element) nodes.get(1);
|
||||
assertRowElement(tr2, "2", "Thingy", "13.99");
|
||||
Element tr3 = (Element) nodes.get(2);
|
||||
assertRowElement(tr3, "3", "Gizmo", "14.99");
|
||||
Element tr4 = (Element) nodes.get(3);
|
||||
assertRowElement(tr4, "4", "Cranktoggle", "11.99");
|
||||
}
|
||||
|
||||
private void assertRowElement(Element elem, String id, String name, String price) {
|
||||
Element idElem = (Element) elem.elements().get(0);
|
||||
Element nameElem = (Element) elem.elements().get(1);
|
||||
Element priceElem = (Element) elem.elements().get(2);
|
||||
|
||||
assertEquals("ID incorrect.", id, idElem.getText());
|
||||
assertEquals("Name incorrect.", name, nameElem.getText());
|
||||
assertEquals("Price incorrect.", price, priceElem.getText());
|
||||
}
|
||||
|
||||
private XsltView getXsltView(String templatePath) {
|
||||
XsltView view = new XsltView();
|
||||
view.setUrl(templatePath);
|
||||
view.setApplicationContext(new StaticApplicationContext());
|
||||
view.initApplicationContext();
|
||||
return view;
|
||||
}
|
||||
|
||||
private Resource getProductDataResource() {
|
||||
return new ClassPathResource("productData.xml", getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<products/>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<!-- invalid template - should not be loaded by TransformerFactory -->
|
||||
<xsl:template match="/">
|
||||
<xsl:sort order="ascending">
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:output method="html" omit-xml-declaration="yes"/>
|
||||
|
||||
<xsl:template match="/">
|
||||
<html>
|
||||
<head><title>Hello!</title></head>
|
||||
<body>
|
||||
<h1>My First Words</h1>
|
||||
<xsl:apply-templates/>
|
||||
</body>
|
||||
</html>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template match="word">
|
||||
<xsl:value-of select="."/><br/>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,6 @@
|
||||
<products>
|
||||
<product id="1" name="Whatsit" price="12.99"/>
|
||||
<product id="2" name="Thingy" price="13.99"/>
|
||||
<product id="3" name="Gizmo" price="14.99"/>
|
||||
<product id="4" name="Cranktoggle" price="11.99"/>
|
||||
</products>
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:import href="productsImport.xsl"/>
|
||||
|
||||
<xsl:output method="xml" indent="yes" media-type="text/html"/>
|
||||
<xsl:param name="title"/>
|
||||
|
||||
<xsl:template match="/products/product">
|
||||
<tr>
|
||||
<td>
|
||||
<xsl:value-of select="@id"/>
|
||||
</td>
|
||||
<td>
|
||||
<xsl:value-of select="@name"/>
|
||||
</td>
|
||||
<td>
|
||||
<xsl:value-of select="@price"/>
|
||||
</td>
|
||||
</tr>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:output method="xml" indent="yes" media-type="text/html"/>
|
||||
<xsl:param name="title"/>
|
||||
|
||||
<xsl:template match="/">
|
||||
<html>
|
||||
<head>
|
||||
<title><xsl:value-of select="$title"/></title>
|
||||
</head>
|
||||
<body>
|
||||
<table>
|
||||
<xsl:apply-templates select="/products/product"/>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:output method="xml" omit-xml-declaration="yes"/>
|
||||
|
||||
<xsl:param name="sex">Female</xsl:param>
|
||||
|
||||
<xsl:template match="*">
|
||||
<xsl:element name="hero">
|
||||
<xsl:attribute name="name">
|
||||
<xsl:value-of select="@name"/>
|
||||
</xsl:attribute>
|
||||
<xsl:attribute name="age">
|
||||
<xsl:value-of select="@age"/>
|
||||
</xsl:attribute>
|
||||
<xsl:attribute name="catchphrase">
|
||||
<xsl:value-of select="@catchphrase"/>
|
||||
</xsl:attribute>
|
||||
<xsl:attribute name="sex">
|
||||
<xsl:value-of select="$sex"/>
|
||||
</xsl:attribute>
|
||||
</xsl:element>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:output method="xml" omit-xml-declaration="yes"/>
|
||||
|
||||
<xsl:param name="sex">Female</xsl:param>
|
||||
|
||||
<xsl:template match="*">
|
||||
<xsl:copy-of select="."/>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:template match="/">
|
||||
This is a valid template!
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
Reference in New Issue
Block a user