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 makeEmptyResult() {
50 return new DOMResult(documentBuilder.newDocument());
53 private static byte[] makeStringResult(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(makeEmptyResult(), actionData, actionProperties, makeStringResult("2"));
71 private void setTyped(Boolean typed) {
72 actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString());
76 public void testRun_simpleParameters() throws Exception {
79 actionData.add("1+$a");
84 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3"));
88 public void testRun_simpleMoreParameters() throws Exception {
91 actionData.add("$a+$b");
99 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6"));
103 public void testRun_simpleTypedParameters() throws Exception {
106 actionData.add("$a+$b");
109 actionData.add("InteGER");
113 actionData.add("integeR");
116 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8"));
119 private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
121 ByteArrayOutputStream baos = new ByteArrayOutputStream();
122 ActionContext actionContext = new ActionContext(baos);
124 actionContext.setActionProperties(actionProperties);
125 actionContext.setActionData(actionData);
127 XPathAction instance = new XPathAction(actionContext);
128 instance.run(domResult);
129 System.out.write(baos.toByteArray());
131 assertEquals(baos.toByteArray(), expectedResult);