java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java
changeset 70 ead6a0d4e022
child 73 bb0aa5d23f2e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java	Thu Sep 04 17:56:58 2014 +0200
     1.3 @@ -0,0 +1,96 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.out.xpath;
    1.22 +
    1.23 +import cz.frantovo.alt2xml.out.ActionContext;
    1.24 +import java.io.ByteArrayOutputStream;
    1.25 +import java.util.ArrayList;
    1.26 +import java.util.List;
    1.27 +import java.util.Properties;
    1.28 +import javax.xml.parsers.DocumentBuilder;
    1.29 +import javax.xml.parsers.DocumentBuilderFactory;
    1.30 +import javax.xml.parsers.ParserConfigurationException;
    1.31 +import javax.xml.transform.dom.DOMResult;
    1.32 +import static org.testng.Assert.*;
    1.33 +import org.testng.annotations.BeforeMethod;
    1.34 +import org.testng.annotations.Test;
    1.35 +
    1.36 +/**
    1.37 + *
    1.38 + * @author Ing. František Kučera (frantovo.cz)
    1.39 + */
    1.40 +public class XPathActionNGTest {
    1.41 +
    1.42 +	private final DocumentBuilderFactory documentBuilderFactory;
    1.43 +	private final DocumentBuilder documentBuilder;
    1.44 +	private Properties actionProperties;
    1.45 +	private List<String> actionData;
    1.46 +
    1.47 +	public XPathActionNGTest() throws ParserConfigurationException {
    1.48 +		documentBuilderFactory = DocumentBuilderFactory.newInstance();
    1.49 +		documentBuilder = documentBuilderFactory.newDocumentBuilder();
    1.50 +	}
    1.51 +
    1.52 +	private DOMResult generateEmptyResult() {
    1.53 +		return new DOMResult(documentBuilder.newDocument());
    1.54 +	}
    1.55 +
    1.56 +	private static byte[] generateStringResult(String value) {
    1.57 +		return (value + "\n").getBytes();
    1.58 +	}
    1.59 +
    1.60 +	@BeforeMethod
    1.61 +	public void setupMethod() {
    1.62 +		actionProperties = new Properties();
    1.63 +		actionData = new ArrayList<>();
    1.64 +		System.out.println("\n--- Output: --------");
    1.65 +	}
    1.66 +
    1.67 +	@Test
    1.68 +	public void testRun_simple() throws Exception {
    1.69 +		actionData.add("1+1");
    1.70 +
    1.71 +		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2"));
    1.72 +	}
    1.73 +
    1.74 +	@Test
    1.75 +	public void testRun_simpleParameters() throws Exception {
    1.76 +		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE);
    1.77 +
    1.78 +		actionData.add("1+$a");
    1.79 +		actionData.add("a");
    1.80 +		actionData.add("2");
    1.81 +
    1.82 +		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3"));
    1.83 +	}
    1.84 +
    1.85 +	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
    1.86 +
    1.87 +		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    1.88 +		ActionContext actionContext = new ActionContext(baos);
    1.89 +
    1.90 +		actionContext.setActionProperties(actionProperties);
    1.91 +		actionContext.setActionData(actionData);
    1.92 +
    1.93 +		XPathAction instance = new XPathAction(actionContext);
    1.94 +		instance.run(domResult);
    1.95 +		System.out.write(baos.toByteArray());
    1.96 +
    1.97 +		assertEquals(baos.toByteArray(), expectedResult);
    1.98 +	}
    1.99 +}