out-xpath: unit test
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 17:56:58 +0200
changeset 70ead6a0d4e022
parent 69 f9c73ee49287
child 71 75556166817a
out-xpath: unit test
java/alt2xml-out-xpath/nbproject/project.properties
java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java
     1.1 --- a/java/alt2xml-out-xpath/nbproject/project.properties	Thu Sep 04 17:56:43 2014 +0200
     1.2 +++ b/java/alt2xml-out-xpath/nbproject/project.properties	Thu Sep 04 17:56:58 2014 +0200
     1.3 @@ -42,7 +42,8 @@
     1.4  javac.target=1.7
     1.5  javac.test.classpath=\
     1.6      ${javac.classpath}:\
     1.7 -    ${build.classes.dir}
     1.8 +    ${build.classes.dir}:\
     1.9 +    ${libs.testng.classpath}
    1.10  javac.test.processorpath=\
    1.11      ${javac.test.classpath}
    1.12  javadoc.additionalparam=
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java	Thu Sep 04 17:56:58 2014 +0200
     2.3 @@ -0,0 +1,96 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml.out.xpath;
    2.22 +
    2.23 +import cz.frantovo.alt2xml.out.ActionContext;
    2.24 +import java.io.ByteArrayOutputStream;
    2.25 +import java.util.ArrayList;
    2.26 +import java.util.List;
    2.27 +import java.util.Properties;
    2.28 +import javax.xml.parsers.DocumentBuilder;
    2.29 +import javax.xml.parsers.DocumentBuilderFactory;
    2.30 +import javax.xml.parsers.ParserConfigurationException;
    2.31 +import javax.xml.transform.dom.DOMResult;
    2.32 +import static org.testng.Assert.*;
    2.33 +import org.testng.annotations.BeforeMethod;
    2.34 +import org.testng.annotations.Test;
    2.35 +
    2.36 +/**
    2.37 + *
    2.38 + * @author Ing. František Kučera (frantovo.cz)
    2.39 + */
    2.40 +public class XPathActionNGTest {
    2.41 +
    2.42 +	private final DocumentBuilderFactory documentBuilderFactory;
    2.43 +	private final DocumentBuilder documentBuilder;
    2.44 +	private Properties actionProperties;
    2.45 +	private List<String> actionData;
    2.46 +
    2.47 +	public XPathActionNGTest() throws ParserConfigurationException {
    2.48 +		documentBuilderFactory = DocumentBuilderFactory.newInstance();
    2.49 +		documentBuilder = documentBuilderFactory.newDocumentBuilder();
    2.50 +	}
    2.51 +
    2.52 +	private DOMResult generateEmptyResult() {
    2.53 +		return new DOMResult(documentBuilder.newDocument());
    2.54 +	}
    2.55 +
    2.56 +	private static byte[] generateStringResult(String value) {
    2.57 +		return (value + "\n").getBytes();
    2.58 +	}
    2.59 +
    2.60 +	@BeforeMethod
    2.61 +	public void setupMethod() {
    2.62 +		actionProperties = new Properties();
    2.63 +		actionData = new ArrayList<>();
    2.64 +		System.out.println("\n--- Output: --------");
    2.65 +	}
    2.66 +
    2.67 +	@Test
    2.68 +	public void testRun_simple() throws Exception {
    2.69 +		actionData.add("1+1");
    2.70 +
    2.71 +		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2"));
    2.72 +	}
    2.73 +
    2.74 +	@Test
    2.75 +	public void testRun_simpleParameters() throws Exception {
    2.76 +		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE);
    2.77 +
    2.78 +		actionData.add("1+$a");
    2.79 +		actionData.add("a");
    2.80 +		actionData.add("2");
    2.81 +
    2.82 +		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3"));
    2.83 +	}
    2.84 +
    2.85 +	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
    2.86 +
    2.87 +		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    2.88 +		ActionContext actionContext = new ActionContext(baos);
    2.89 +
    2.90 +		actionContext.setActionProperties(actionProperties);
    2.91 +		actionContext.setActionData(actionData);
    2.92 +
    2.93 +		XPathAction instance = new XPathAction(actionContext);
    2.94 +		instance.run(domResult);
    2.95 +		System.out.write(baos.toByteArray());
    2.96 +
    2.97 +		assertEquals(baos.toByteArray(), expectedResult);
    2.98 +	}
    2.99 +}