Repository: incubator-freemarker Updated Branches: refs/heads/3 4e60f9e50 -> 59412b293
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/java/org/apache/freemarker/dom/DOMTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/dom/DOMTest.java b/src/test/java/org/apache/freemarker/dom/DOMTest.java new file mode 100644 index 0000000..06f3779 --- /dev/null +++ b/src/test/java/org/apache/freemarker/dom/DOMTest.java @@ -0,0 +1,161 @@ +/* + * 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. + */ +package org.apache.freemarker.dom; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.freemarker.core.TemplateException; +import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.util.XMLLoader; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class DOMTest extends TemplateTest { + + @Test + public void xpathDetectionBugfix() throws Exception { + addDocToDataModel("<root><a>A</a><b>B</b><c>C</c></root>"); + assertOutput("${doc.root.b['following-sibling::c']}", "C"); + assertOutput("${doc.root.b['following-sibling::*']}", "C"); + } + + @Test + public void xmlnsPrefixes() throws Exception { + addDocToDataModel("<root xmlns='http://example.com/ns1' xmlns:ns2='http://example.com/ns2'>" + + "<a>A</a><ns2:b>B</ns2:b><c a1='1' ns2:a2='2'/></root>"); + + String ftlHeader = "<#ftl ns_prefixes={'D':'http://example.com/ns1', 'n2':'http://example.com/ns2'}>"; + + // @@markup: + assertOutput("${doc.@@markup}", + "<a:root xmlns:a=\"http://example.com/ns1\" xmlns:b=\"http://example.com/ns2\">" + + "<a:a>A</a:a><b:b>B</b:b><a:c a1=\"1\" b:a2=\"2\" />" + + "</a:root>"); + assertOutput(ftlHeader + + "${doc.@@markup}", + "<root xmlns=\"http://example.com/ns1\" xmlns:n2=\"http://example.com/ns2\">" + + "<a>A</a><n2:b>B</n2:b><c a1=\"1\" n2:a2=\"2\" /></root>"); + assertOutput("<#ftl ns_prefixes={'D':'http://example.com/ns1'}>" + + "${doc.@@markup}", + "<root xmlns=\"http://example.com/ns1\" xmlns:a=\"http://example.com/ns2\">" + + "<a>A</a><a:b>B</a:b><c a1=\"1\" a:a2=\"2\" /></root>"); + + // When there's no matching prefix declared via the #ftl header, return null for qname: + assertOutput("${doc?children[0].@@qname!'null'}", "null"); + assertOutput("${doc?children[0]?children[1].@@qname!'null'}", "null"); + assertOutput("${doc?children[0]?children[2]['@*'][1].@@qname!'null'}", "null"); + + // When we have prefix declared in the #ftl header: + assertOutput(ftlHeader + "${doc?children[0].@@qname}", "root"); + assertOutput(ftlHeader + "${doc?children[0]?children[1].@@qname}", "n2:b"); + assertOutput(ftlHeader + "${doc?children[0]?children[2].@@qname}", "c"); + assertOutput(ftlHeader + "${doc?children[0]?children[2]['@*'][0].@@qname}", "a1"); + assertOutput(ftlHeader + "${doc?children[0]?children[2]['@*'][1].@@qname}", "n2:a2"); + // Unfortunately these include the xmlns attributes, but that would be non-BC to fix now: + assertThat(getOutput(ftlHeader + "${doc?children[0].@@start_tag}"), startsWith("<root")); + assertThat(getOutput(ftlHeader + "${doc?children[0]?children[1].@@start_tag}"), startsWith("<n2:b")); + } + + @Test + public void namespaceUnaware() throws Exception { + addNSUnawareDocToDataModel("<root><x:a>A</x:a><:>B</:><xyz::c>C</xyz::c></root>"); + assertOutput("${doc.root['x:a']}", "A"); + assertOutput("${doc.root[':']}", "B"); + try { + assertOutput("${doc.root['xyz::c']}", "C"); + fail(); + } catch (TemplateException e) { + assertThat(e.getMessage(), containsString("xyz")); + } + } + + private void addDocToDataModel(String xml) throws SAXException, IOException, ParserConfigurationException { + addToDataModel("doc", XMLLoader.toModel(new InputSource(new StringReader(xml)))); + } + + private void addDocToDataModelNoSimplification(String xml) throws SAXException, IOException, ParserConfigurationException { + addToDataModel("doc", XMLLoader.toModel(new InputSource(new StringReader(xml)), false)); + } + + private void addNSUnawareDocToDataModel(String xml) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance(); + newFactory.setNamespaceAware(false); + DocumentBuilder builder = newFactory.newDocumentBuilder(); + Document doc = builder.parse(new InputSource(new StringReader(xml))); + addToDataModel("doc", doc); + } + + @Test + public void testInvalidAtAtKeyErrors() throws Exception { + addDocToDataModel("<r><multipleMatches /><multipleMatches /></r>"); + assertErrorContains("${doc.r.@@invalid_key}", "Unsupported @@ key", "@invalid_key"); + assertErrorContains("${doc.@@start_tag}", "@@start_tag", "not supported", "document"); + assertErrorContains("${doc.@@}", "\"@@\"", "not supported", "document"); + assertErrorContains("${doc.r.noMatch.@@invalid_key}", "Unsupported @@ key", "@invalid_key"); + assertErrorContains("${doc.r.multipleMatches.@@invalid_key}", "Unsupported @@ key", "@invalid_key"); + assertErrorContains("${doc.r.noMatch.@@attributes_markup}", "single XML node", "@@attributes_markup"); + assertErrorContains("${doc.r.multipleMatches.@@attributes_markup}", "single XML node", "@@attributes_markup"); + } + + @Test + public void testAtAtSiblingElement() throws Exception { + addDocToDataModel("<r><a/><b/></r>"); + assertOutput("${doc.r.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.@@next_sibling_element?size}", "0"); + assertOutput("${doc.r.a.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.a.@@next_sibling_element.@@qname}", "b"); + assertOutput("${doc.r.b.@@previous_sibling_element.@@qname}", "a"); + assertOutput("${doc.r.b.@@next_sibling_element?size}", "0"); + + addDocToDataModel("<r>\r\n\t <a/>\r\n\t <b/>\r\n\t </r>"); + assertOutput("${doc.r.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.@@next_sibling_element?size}", "0"); + assertOutput("${doc.r.a.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.a.@@next_sibling_element.@@qname}", "b"); + assertOutput("${doc.r.b.@@previous_sibling_element.@@qname}", "a"); + assertOutput("${doc.r.b.@@next_sibling_element?size}", "0"); + + addDocToDataModel("<r>t<a/>t<b/>t</r>"); + assertOutput("${doc.r.a.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.a.@@next_sibling_element?size}", "0"); + assertOutput("${doc.r.b.@@previous_sibling_element?size}", "0"); + assertOutput("${doc.r.b.@@next_sibling_element?size}", "0"); + + addDocToDataModelNoSimplification("<r><a/> <!-- --><?pi?> <b/></r>"); + assertOutput("${doc.r.a.@@next_sibling_element.@@qname}", "b"); + assertOutput("${doc.r.b.@@previous_sibling_element.@@qname}", "a"); + + addDocToDataModelNoSimplification("<r><a/> <!-- -->t<!-- --> <b/></r>"); + assertOutput("${doc.r.a.@@next_sibling_element?size}", "0"); + assertOutput("${doc.r.b.@@previous_sibling_element?size}", "0"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java index 14acde6..2d136f5 100644 --- a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java +++ b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java @@ -65,7 +65,7 @@ import org.apache.freemarker.core.model.impl.beans.BeansWrapperBuilder; import org.apache.freemarker.core.model.impl.beans.BooleanModel; import org.apache.freemarker.core.model.impl.beans.Java7MembersOnlyBeansWrapper; import org.apache.freemarker.core.model.impl.beans.ResourceBundleModel; -import org.apache.freemarker.core.model.impl.dom.NodeModel; +import org.apache.freemarker.dom.NodeModel; import org.apache.freemarker.core.templateresolver.impl.FileTemplateLoader; import org.apache.freemarker.core.util._NullArgumentException; import org.apache.freemarker.core.util._NullWriter; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestSuite.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestSuite.java b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestSuite.java index 5133b0a..b768e2e 100644 --- a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestSuite.java +++ b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestSuite.java @@ -34,7 +34,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.apache.freemarker.core.Configuration; import org.apache.freemarker.core.Version; -import org.apache.freemarker.core.model.impl.dom.NodeModel; +import org.apache.freemarker.dom.NodeModel; import org.apache.freemarker.core.util._StringUtil; import org.w3c.dom.Attr; import org.w3c.dom.Document; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/java/org/apache/freemarker/test/util/XMLLoader.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/util/XMLLoader.java b/src/test/java/org/apache/freemarker/test/util/XMLLoader.java index 7226f97..781db93 100644 --- a/src/test/java/org/apache/freemarker/test/util/XMLLoader.java +++ b/src/test/java/org/apache/freemarker/test/util/XMLLoader.java @@ -28,7 +28,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.apache.freemarker.core.model.impl.dom.NodeModel; +import org.apache.freemarker.dom.NodeModel; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/resources/org/apache/freemarker/core/model/impl/dom/DOMSiblingTest.xml ---------------------------------------------------------------------- diff --git a/src/test/resources/org/apache/freemarker/core/model/impl/dom/DOMSiblingTest.xml b/src/test/resources/org/apache/freemarker/core/model/impl/dom/DOMSiblingTest.xml deleted file mode 100644 index bdb3e45..0000000 --- a/src/test/resources/org/apache/freemarker/core/model/impl/dom/DOMSiblingTest.xml +++ /dev/null @@ -1,31 +0,0 @@ -<?xml version="1.0"?> -<!-- - 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. ---> -<person> - <gender>male</gender> - <name>pradeep</name> - <dob>12th August</dob><address>Chennai, India</address> - <!--This is a comment Node --> - <?xml-stylesheet type="text/css" href="style.css"?> - <profession>Software Engineer</profession> - <![CDATA[ ]]> - <hobby>gardening</hobby> - <![CDATA[this is a valid cdata]]> - <phone>12345678</phone> -</person> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/59412b29/src/test/resources/org/apache/freemarker/dom/DOMSiblingTest.xml ---------------------------------------------------------------------- diff --git a/src/test/resources/org/apache/freemarker/dom/DOMSiblingTest.xml b/src/test/resources/org/apache/freemarker/dom/DOMSiblingTest.xml new file mode 100644 index 0000000..bdb3e45 --- /dev/null +++ b/src/test/resources/org/apache/freemarker/dom/DOMSiblingTest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<person> + <gender>male</gender> + <name>pradeep</name> + <dob>12th August</dob><address>Chennai, India</address> + <!--This is a comment Node --> + <?xml-stylesheet type="text/css" href="style.css"?> + <profession>Software Engineer</profession> + <![CDATA[ ]]> + <hobby>gardening</hobby> + <![CDATA[this is a valid cdata]]> + <phone>12345678</phone> +</person>