Unit tests for Sibling and file renamed as per convention
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/0ad01886 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/0ad01886 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/0ad01886 Branch: refs/heads/2.3-gae Commit: 0ad018867388ce67e9d2d7e3cac6494ed89bcc43 Parents: 9e73d00 Author: Pradeep <[email protected]> Authored: Sun Dec 6 20:51:24 2015 +0530 Committer: Pradeep <[email protected]> Committed: Mon Jan 11 19:20:32 2016 +0530 ---------------------------------------------------------------------- src/main/java/freemarker/core/BuiltIn.java | 4 +- .../java/freemarker/core/BuiltInExtForNode.java | 36 -------------- .../java/freemarker/core/BuiltInForNodeEx.java | 36 ++++++++++++++ .../freemarker/core/BuiltInsExtForNode.java | 41 ---------------- .../java/freemarker/core/BuiltInsForNodes.java | 22 ++++++--- .../java/freemarker/ext/dom/ElementModel.java | 9 +++- src/test/java/freemarker/core/SiblingTest.java | 50 +++++++++++++++----- .../freemarker/core/siblingDataModel.xml | 2 +- 8 files changed, 100 insertions(+), 100 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/core/BuiltIn.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltIn.java b/src/main/java/freemarker/core/BuiltIn.java index ee02625..c9d93d5 100644 --- a/src/main/java/freemarker/core/BuiltIn.java +++ b/src/main/java/freemarker/core/BuiltIn.java @@ -62,8 +62,8 @@ import freemarker.core.BuiltInsForSequences.seq_index_ofBI; import freemarker.core.BuiltInsForSequences.sortBI; import freemarker.core.BuiltInsForSequences.sort_byBI; import freemarker.core.BuiltInsForStringsMisc.evalBI; -import freemarker.core.BuiltInsExtForNode.previousSiblingBI; -import freemarker.core.BuiltInsExtForNode.nextSiblingBI; +import freemarker.core.BuiltInsForNodes.previousSiblingBI; +import freemarker.core.BuiltInsForNodes.nextSiblingBI; import freemarker.template.Configuration; import freemarker.template.TemplateDateModel; import freemarker.template.TemplateModel; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/core/BuiltInExtForNode.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltInExtForNode.java b/src/main/java/freemarker/core/BuiltInExtForNode.java deleted file mode 100644 index 00e55fa..0000000 --- a/src/main/java/freemarker/core/BuiltInExtForNode.java +++ /dev/null @@ -1,36 +0,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. - */ -package freemarker.core; - -import freemarker.template.*; - -public abstract class BuiltInExtForNode extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateNodeModelEx) { - return calculateResult((TemplateNodeModelEx) model, env); - } else { - throw new NonNodeException(target, model, env); - } - } - abstract TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) - throws TemplateModelException; -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/core/BuiltInForNodeEx.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltInForNodeEx.java b/src/main/java/freemarker/core/BuiltInForNodeEx.java new file mode 100644 index 0000000..8bfe6b6 --- /dev/null +++ b/src/main/java/freemarker/core/BuiltInForNodeEx.java @@ -0,0 +1,36 @@ +/* + * 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 freemarker.core; + +import freemarker.template.*; + +public abstract class BuiltInForNodeEx extends BuiltIn { + @Override + TemplateModel _eval(Environment env) + throws TemplateException { + TemplateModel model = target.eval(env); + if (model instanceof TemplateNodeModelEx) { + return calculateResult((TemplateNodeModelEx) model, env); + } else { + throw new NonNodeException(target, model, env); + } + } + abstract TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) + throws TemplateModelException; +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/core/BuiltInsExtForNode.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltInsExtForNode.java b/src/main/java/freemarker/core/BuiltInsExtForNode.java deleted file mode 100644 index b675b9a..0000000 --- a/src/main/java/freemarker/core/BuiltInsExtForNode.java +++ /dev/null @@ -1,41 +0,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. - */ -package freemarker.core; - -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateNodeModelEx; - -public class BuiltInsExtForNode { - - static class previousSiblingBI extends BuiltInExtForNode { - @Override - TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) throws TemplateModelException { - return nodeModel.getPreviousSibling(); - } - } - - static class nextSiblingBI extends BuiltInExtForNode { - @Override - TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) throws TemplateModelException { - return nodeModel.getNextSibling(); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/core/BuiltInsForNodes.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltInsForNodes.java b/src/main/java/freemarker/core/BuiltInsForNodes.java index 2fc8c54..fa2c6ec 100644 --- a/src/main/java/freemarker/core/BuiltInsForNodes.java +++ b/src/main/java/freemarker/core/BuiltInsForNodes.java @@ -22,12 +22,7 @@ package freemarker.core; import java.util.List; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import freemarker.template.SimpleScalar; -import freemarker.template.SimpleSequence; -import freemarker.template.TemplateMethodModel; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateNodeModel; +import freemarker.template.*; import freemarker.template.utility.StringUtil; /** @@ -97,7 +92,20 @@ class BuiltInsForNodes { return result; } } - + + static class previousSiblingBI extends BuiltInForNodeEx { + @Override + TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) throws TemplateModelException { + return nodeModel.getPreviousSibling(); + } + } + + static class nextSiblingBI extends BuiltInForNodeEx { + @Override + TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) throws TemplateModelException { + return nodeModel.getNextSibling(); + } + } // Can't be instantiated private BuiltInsForNodes() { } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/main/java/freemarker/ext/dom/ElementModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/dom/ElementModel.java b/src/main/java/freemarker/ext/dom/ElementModel.java index 99d8e23..41844d7 100644 --- a/src/main/java/freemarker/ext/dom/ElementModel.java +++ b/src/main/java/freemarker/ext/dom/ElementModel.java @@ -91,14 +91,14 @@ class ElementModel extends NodeModel implements TemplateScalarModel { } if (key.equals("@@previous")) { Node previousSibling = node.getPreviousSibling(); - while(previousSibling.getNodeType() != Node.ELEMENT_NODE) { + while(!this.isSignificantNode(previousSibling)) { previousSibling = previousSibling.getPreviousSibling(); } return wrap(previousSibling); } if (key.equals("@@next")) { Node nextSibling = node.getNextSibling(); - while(nextSibling.getNodeType() != Node.ELEMENT_NODE) { + while(!this.isSignificantNode(nextSibling)) { nextSibling = nextSibling.getNextSibling(); } return wrap(nextSibling); @@ -121,6 +121,11 @@ class ElementModel extends NodeModel implements TemplateScalarModel { return super.get(key); } + public boolean isSignificantNode(Node node) throws TemplateModelException { + boolean isEmpty = node.getTextContent().trim().isEmpty(); + boolean significantNode = !isEmpty; + return significantNode; + } public String getAsString() throws TemplateModelException { NodeList nl = node.getChildNodes(); String result = ""; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/test/java/freemarker/core/SiblingTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/core/SiblingTest.java b/src/test/java/freemarker/core/SiblingTest.java index b38cec0..0db4df8 100644 --- a/src/test/java/freemarker/core/SiblingTest.java +++ b/src/test/java/freemarker/core/SiblingTest.java @@ -3,7 +3,10 @@ package freemarker.core; import freemarker.ext.dom.NodeModel; import freemarker.template.TemplateException; import freemarker.test.TemplateTest; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; @@ -33,25 +36,50 @@ import java.util.Map; */ public class SiblingTest extends TemplateTest { - @Override - protected Object getDataModel() { - Map dataModel = new HashMap(); - String dataModelFileUrl = this.getClass().getResource(".").toString() + "/siblingDataModel.xml"; + @Before + public void setUp() { try { - dataModel.put( - "doc", NodeModel.parse(new File("build/test-classes/freemarker/core/siblingDataModel.xml"))); + InputSource is = new InputSource(getClass().getResourceAsStream("siblingDataModel.xml")); + addToDataModel("doc", NodeModel.parse(is) ); } catch (Exception e) { System.out.println("Exception while parsing the dataModel xml"); e.printStackTrace(); } - return dataModel; } + + @Test + public void testEmptyPreviousSibling() throws IOException, TemplateException { + String ftl = "${doc.person.name?previousSibling}"; + assertOutput(ftl, "\n "); + } + + @Test + public void testNonEmptyPreviousSibling() throws IOException, TemplateException { + String ftl = "${doc.person.address?previousSibling}"; + assertOutput(ftl, "12th August"); + } + @Test - public void testPreviousSibling() throws IOException, TemplateException { - String ftl = "<#assign sibling>${doc.person.name?previousSibling}</#assign>" + - "${sibling?trim}" ; - assertOutput(ftl, ""); + public void testEmptyNextSibling() throws IOException, TemplateException { + String ftl = "${doc.person.name?nextSibling}"; + assertOutput(ftl, "\n "); } + @Test + public void testNonEmptyNextSibling() throws IOException, TemplateException { + String ftl = "${doc.person.dob?nextSibling}"; + assertOutput(ftl, "Chennai, India"); + } + @Test + public void testSignificantNextSibling() throws IOException, TemplateException { + String ftl = "${doc.person.name.@@next}"; + assertOutput(ftl, "12th August"); + } + + @Test + public void testSignificantPreviousSibling() throws IOException, TemplateException { + String ftl = "${doc.person.name.@@previous}"; + assertOutput(ftl, "male"); + } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/0ad01886/src/test/resources/freemarker/core/siblingDataModel.xml ---------------------------------------------------------------------- diff --git a/src/test/resources/freemarker/core/siblingDataModel.xml b/src/test/resources/freemarker/core/siblingDataModel.xml index f743e2e..13f928a 100644 --- a/src/test/resources/freemarker/core/siblingDataModel.xml +++ b/src/test/resources/freemarker/core/siblingDataModel.xml @@ -2,5 +2,5 @@ <person> <gender>male</gender> <name>pradeep</name> - <dob>12th August</dob> + <dob>12th August</dob><address>Chennai, India</address> </person>
