# HG changeset patch # User František Kučera # Date 1409846218 -7200 # Node ID ead6a0d4e02245e2ec852ebf90d6a8a46f3affbc # Parent f9c73ee492877d9e0e778262af4bf4b6684a35fa out-xpath: unit test diff -r f9c73ee49287 -r ead6a0d4e022 java/alt2xml-out-xpath/nbproject/project.properties --- a/java/alt2xml-out-xpath/nbproject/project.properties Thu Sep 04 17:56:43 2014 +0200 +++ b/java/alt2xml-out-xpath/nbproject/project.properties Thu Sep 04 17:56:58 2014 +0200 @@ -42,7 +42,8 @@ javac.target=1.7 javac.test.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${build.classes.dir}:\ + ${libs.testng.classpath} javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= diff -r f9c73ee49287 -r ead6a0d4e022 java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java Thu Sep 04 17:56:58 2014 +0200 @@ -0,0 +1,96 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml.out.xpath; + +import cz.frantovo.alt2xml.out.ActionContext; +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.dom.DOMResult; +import static org.testng.Assert.*; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class XPathActionNGTest { + + private final DocumentBuilderFactory documentBuilderFactory; + private final DocumentBuilder documentBuilder; + private Properties actionProperties; + private List actionData; + + public XPathActionNGTest() throws ParserConfigurationException { + documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilder = documentBuilderFactory.newDocumentBuilder(); + } + + private DOMResult generateEmptyResult() { + return new DOMResult(documentBuilder.newDocument()); + } + + private static byte[] generateStringResult(String value) { + return (value + "\n").getBytes(); + } + + @BeforeMethod + public void setupMethod() { + actionProperties = new Properties(); + actionData = new ArrayList<>(); + System.out.println("\n--- Output: --------"); + } + + @Test + public void testRun_simple() throws Exception { + actionData.add("1+1"); + + doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2")); + } + + @Test + public void testRun_simpleParameters() throws Exception { + actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE); + + actionData.add("1+$a"); + actionData.add("a"); + actionData.add("2"); + + doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3")); + } + + private void doTest(DOMResult domResult, List actionData, Properties actionProperties, byte[] expectedResult) throws Exception { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ActionContext actionContext = new ActionContext(baos); + + actionContext.setActionProperties(actionProperties); + actionContext.setActionData(actionData); + + XPathAction instance = new XPathAction(actionContext); + instance.run(domResult); + System.out.write(baos.toByteArray()); + + assertEquals(baos.toByteArray(), expectedResult); + } +}