3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml.out.xpath;
20 import cz.frantovo.alt2xml.out.ActionContext;
21 import java.io.ByteArrayOutputStream;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Properties;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.transform.dom.DOMResult;
29 import static org.testng.Assert.*;
30 import org.testng.annotations.BeforeMethod;
31 import org.testng.annotations.Test;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class XPathActionNGTest {
39 private final DocumentBuilderFactory documentBuilderFactory;
40 private final DocumentBuilder documentBuilder;
41 private Properties actionProperties;
42 private List<String> actionData;
44 public XPathActionNGTest() throws ParserConfigurationException {
45 documentBuilderFactory = DocumentBuilderFactory.newInstance();
46 documentBuilder = documentBuilderFactory.newDocumentBuilder();
49 private DOMResult generateEmptyResult() {
50 return new DOMResult(documentBuilder.newDocument());
53 private static byte[] generateStringResult(String value) {
54 return (value + "\n").getBytes();
58 public void setupMethod() {
59 actionProperties = new Properties();
60 actionData = new ArrayList<>();
61 System.out.println("\n--- Output: --------");
65 public void testRun_simple() throws Exception {
66 actionData.add("1+1");
68 doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2"));
72 public void testRun_simpleParameters() throws Exception {
73 actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE);
75 actionData.add("1+$a");
79 doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3"));
82 private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 ActionContext actionContext = new ActionContext(baos);
87 actionContext.setActionProperties(actionProperties);
88 actionContext.setActionData(actionData);
90 XPathAction instance = new XPathAction(actionContext);
91 instance.run(domResult);
92 System.out.write(baos.toByteArray());
94 assertEquals(baos.toByteArray(), expectedResult);