franta-hg@70: /** franta-hg@70: * Alt2XML franta-hg@70: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@70: * franta-hg@70: * This program is free software: you can redistribute it and/or modify franta-hg@70: * it under the terms of the GNU General Public License as published by franta-hg@111: * the Free Software Foundation, version 3 of the License. franta-hg@70: * franta-hg@70: * This program is distributed in the hope that it will be useful, franta-hg@70: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@70: * GNU General Public License for more details. franta-hg@70: * franta-hg@70: * You should have received a copy of the GNU General Public License franta-hg@70: * along with this program. If not, see . franta-hg@70: */ franta-hg@70: package cz.frantovo.alt2xml.out.xpath; franta-hg@70: franta-hg@70: import cz.frantovo.alt2xml.out.ActionContext; franta-hg@70: import java.io.ByteArrayOutputStream; franta-hg@70: import java.util.ArrayList; franta-hg@70: import java.util.List; franta-hg@70: import java.util.Properties; franta-hg@70: import javax.xml.parsers.DocumentBuilder; franta-hg@70: import javax.xml.parsers.DocumentBuilderFactory; franta-hg@70: import javax.xml.parsers.ParserConfigurationException; franta-hg@70: import javax.xml.transform.dom.DOMResult; franta-hg@70: import static org.testng.Assert.*; franta-hg@70: import org.testng.annotations.BeforeMethod; franta-hg@70: import org.testng.annotations.Test; franta-hg@70: franta-hg@70: /** franta-hg@70: * franta-hg@70: * @author Ing. František Kučera (frantovo.cz) franta-hg@70: */ franta-hg@70: public class XPathActionNGTest { franta-hg@70: franta-hg@70: private final DocumentBuilderFactory documentBuilderFactory; franta-hg@70: private final DocumentBuilder documentBuilder; franta-hg@70: private Properties actionProperties; franta-hg@70: private List actionData; franta-hg@70: franta-hg@70: public XPathActionNGTest() throws ParserConfigurationException { franta-hg@70: documentBuilderFactory = DocumentBuilderFactory.newInstance(); franta-hg@70: documentBuilder = documentBuilderFactory.newDocumentBuilder(); franta-hg@70: } franta-hg@70: franta-hg@73: private DOMResult makeEmptyResult() { franta-hg@70: return new DOMResult(documentBuilder.newDocument()); franta-hg@70: } franta-hg@70: franta-hg@73: private static byte[] makeStringResult(String value) { franta-hg@70: return (value + "\n").getBytes(); franta-hg@70: } franta-hg@70: franta-hg@70: @BeforeMethod franta-hg@70: public void setupMethod() { franta-hg@70: actionProperties = new Properties(); franta-hg@70: actionData = new ArrayList<>(); franta-hg@70: System.out.println("\n--- Output: --------"); franta-hg@70: } franta-hg@70: franta-hg@70: @Test franta-hg@70: public void testRun_simple() throws Exception { franta-hg@70: actionData.add("1+1"); franta-hg@70: franta-hg@73: doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("2")); franta-hg@73: } franta-hg@73: franta-hg@73: private void setTyped(Boolean typed) { franta-hg@73: actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString()); franta-hg@70: } franta-hg@70: franta-hg@70: @Test franta-hg@70: public void testRun_simpleParameters() throws Exception { franta-hg@73: setTyped(false); franta-hg@70: franta-hg@70: actionData.add("1+$a"); franta-hg@73: franta-hg@70: actionData.add("a"); franta-hg@70: actionData.add("2"); franta-hg@70: franta-hg@73: doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3")); franta-hg@73: } franta-hg@73: franta-hg@73: @Test franta-hg@73: public void testRun_simpleMoreParameters() throws Exception { franta-hg@73: setTyped(false); franta-hg@73: franta-hg@73: actionData.add("$a+$b"); franta-hg@73: franta-hg@73: actionData.add("a"); franta-hg@73: actionData.add("2"); franta-hg@73: franta-hg@73: actionData.add("b"); franta-hg@73: actionData.add("4"); franta-hg@73: franta-hg@73: doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6")); franta-hg@73: } franta-hg@73: franta-hg@73: @Test franta-hg@73: public void testRun_simpleTypedParameters() throws Exception { franta-hg@73: setTyped(true); franta-hg@73: franta-hg@73: actionData.add("$a+$b"); franta-hg@73: franta-hg@73: actionData.add("a"); franta-hg@73: actionData.add("InteGER"); franta-hg@73: actionData.add("4"); franta-hg@73: franta-hg@73: actionData.add("b"); franta-hg@73: actionData.add("integeR"); franta-hg@73: actionData.add("4"); franta-hg@73: franta-hg@73: doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8")); franta-hg@70: } franta-hg@70: franta-hg@70: private void doTest(DOMResult domResult, List actionData, Properties actionProperties, byte[] expectedResult) throws Exception { franta-hg@70: franta-hg@70: ByteArrayOutputStream baos = new ByteArrayOutputStream(); franta-hg@70: ActionContext actionContext = new ActionContext(baos); franta-hg@70: franta-hg@70: actionContext.setActionProperties(actionProperties); franta-hg@70: actionContext.setActionData(actionData); franta-hg@70: franta-hg@70: XPathAction instance = new XPathAction(actionContext); franta-hg@70: instance.run(domResult); franta-hg@70: System.out.write(baos.toByteArray()); franta-hg@70: franta-hg@70: assertEquals(baos.toByteArray(), expectedResult); franta-hg@70: } franta-hg@70: }