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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml.out.xpath;
19 import cz.frantovo.alt2xml.out.ActionContext;
20 import java.io.ByteArrayOutputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Properties;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.transform.dom.DOMResult;
28 import static org.testng.Assert.*;
29 import org.testng.annotations.BeforeMethod;
30 import org.testng.annotations.Test;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class XPathActionNGTest {
38 private final DocumentBuilderFactory documentBuilderFactory;
39 private final DocumentBuilder documentBuilder;
40 private Properties actionProperties;
41 private List<String> actionData;
43 public XPathActionNGTest() throws ParserConfigurationException {
44 documentBuilderFactory = DocumentBuilderFactory.newInstance();
45 documentBuilder = documentBuilderFactory.newDocumentBuilder();
48 private DOMResult makeEmptyResult() {
49 return new DOMResult(documentBuilder.newDocument());
52 private static byte[] makeStringResult(String value) {
53 return (value + "\n").getBytes();
57 public void setupMethod() {
58 actionProperties = new Properties();
59 actionData = new ArrayList<>();
60 System.out.println("\n--- Output: --------");
64 public void testRun_simple() throws Exception {
65 actionData.add("1+1");
67 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("2"));
70 private void setTyped(Boolean typed) {
71 actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString());
75 public void testRun_simpleParameters() throws Exception {
78 actionData.add("1+$a");
83 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3"));
87 public void testRun_simpleMoreParameters() throws Exception {
90 actionData.add("$a+$b");
98 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6"));
102 public void testRun_simpleTypedParameters() throws Exception {
105 actionData.add("$a+$b");
108 actionData.add("InteGER");
112 actionData.add("integeR");
115 doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8"));
118 private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
120 ByteArrayOutputStream baos = new ByteArrayOutputStream();
121 ActionContext actionContext = new ActionContext(baos);
123 actionContext.setActionProperties(actionProperties);
124 actionContext.setActionData(actionData);
126 XPathAction instance = new XPathAction(actionContext);
127 instance.run(domResult);
128 System.out.write(baos.toByteArray());
130 assertEquals(baos.toByteArray(), expectedResult);