java/alt2xml-out-xpath/test/cz/frantovo/alt2xml/out/xpath/XPathActionNGTest.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 73 bb0aa5d23f2e
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@70
     1
/**
franta-hg@70
     2
 * Alt2XML
franta-hg@70
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@70
     4
 *
franta-hg@70
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@70
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@70
     8
 *
franta-hg@70
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@70
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@70
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@70
    12
 * GNU General Public License for more details.
franta-hg@70
    13
 *
franta-hg@70
    14
 * You should have received a copy of the GNU General Public License
franta-hg@70
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@70
    16
 */
franta-hg@70
    17
package cz.frantovo.alt2xml.out.xpath;
franta-hg@70
    18
franta-hg@70
    19
import cz.frantovo.alt2xml.out.ActionContext;
franta-hg@70
    20
import java.io.ByteArrayOutputStream;
franta-hg@70
    21
import java.util.ArrayList;
franta-hg@70
    22
import java.util.List;
franta-hg@70
    23
import java.util.Properties;
franta-hg@70
    24
import javax.xml.parsers.DocumentBuilder;
franta-hg@70
    25
import javax.xml.parsers.DocumentBuilderFactory;
franta-hg@70
    26
import javax.xml.parsers.ParserConfigurationException;
franta-hg@70
    27
import javax.xml.transform.dom.DOMResult;
franta-hg@70
    28
import static org.testng.Assert.*;
franta-hg@70
    29
import org.testng.annotations.BeforeMethod;
franta-hg@70
    30
import org.testng.annotations.Test;
franta-hg@70
    31
franta-hg@70
    32
/**
franta-hg@70
    33
 *
franta-hg@70
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@70
    35
 */
franta-hg@70
    36
public class XPathActionNGTest {
franta-hg@70
    37
franta-hg@70
    38
	private final DocumentBuilderFactory documentBuilderFactory;
franta-hg@70
    39
	private final DocumentBuilder documentBuilder;
franta-hg@70
    40
	private Properties actionProperties;
franta-hg@70
    41
	private List<String> actionData;
franta-hg@70
    42
franta-hg@70
    43
	public XPathActionNGTest() throws ParserConfigurationException {
franta-hg@70
    44
		documentBuilderFactory = DocumentBuilderFactory.newInstance();
franta-hg@70
    45
		documentBuilder = documentBuilderFactory.newDocumentBuilder();
franta-hg@70
    46
	}
franta-hg@70
    47
franta-hg@73
    48
	private DOMResult makeEmptyResult() {
franta-hg@70
    49
		return new DOMResult(documentBuilder.newDocument());
franta-hg@70
    50
	}
franta-hg@70
    51
franta-hg@73
    52
	private static byte[] makeStringResult(String value) {
franta-hg@70
    53
		return (value + "\n").getBytes();
franta-hg@70
    54
	}
franta-hg@70
    55
franta-hg@70
    56
	@BeforeMethod
franta-hg@70
    57
	public void setupMethod() {
franta-hg@70
    58
		actionProperties = new Properties();
franta-hg@70
    59
		actionData = new ArrayList<>();
franta-hg@70
    60
		System.out.println("\n--- Output: --------");
franta-hg@70
    61
	}
franta-hg@70
    62
franta-hg@70
    63
	@Test
franta-hg@70
    64
	public void testRun_simple() throws Exception {
franta-hg@70
    65
		actionData.add("1+1");
franta-hg@70
    66
franta-hg@73
    67
		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("2"));
franta-hg@73
    68
	}
franta-hg@73
    69
franta-hg@73
    70
	private void setTyped(Boolean typed) {
franta-hg@73
    71
		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString());
franta-hg@70
    72
	}
franta-hg@70
    73
franta-hg@70
    74
	@Test
franta-hg@70
    75
	public void testRun_simpleParameters() throws Exception {
franta-hg@73
    76
		setTyped(false);
franta-hg@70
    77
franta-hg@70
    78
		actionData.add("1+$a");
franta-hg@73
    79
franta-hg@70
    80
		actionData.add("a");
franta-hg@70
    81
		actionData.add("2");
franta-hg@70
    82
franta-hg@73
    83
		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3"));
franta-hg@73
    84
	}
franta-hg@73
    85
franta-hg@73
    86
	@Test
franta-hg@73
    87
	public void testRun_simpleMoreParameters() throws Exception {
franta-hg@73
    88
		setTyped(false);
franta-hg@73
    89
franta-hg@73
    90
		actionData.add("$a+$b");
franta-hg@73
    91
franta-hg@73
    92
		actionData.add("a");
franta-hg@73
    93
		actionData.add("2");
franta-hg@73
    94
franta-hg@73
    95
		actionData.add("b");
franta-hg@73
    96
		actionData.add("4");
franta-hg@73
    97
franta-hg@73
    98
		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6"));
franta-hg@73
    99
	}
franta-hg@73
   100
franta-hg@73
   101
	@Test
franta-hg@73
   102
	public void testRun_simpleTypedParameters() throws Exception {
franta-hg@73
   103
		setTyped(true);
franta-hg@73
   104
franta-hg@73
   105
		actionData.add("$a+$b");
franta-hg@73
   106
franta-hg@73
   107
		actionData.add("a");
franta-hg@73
   108
		actionData.add("InteGER");
franta-hg@73
   109
		actionData.add("4");
franta-hg@73
   110
franta-hg@73
   111
		actionData.add("b");
franta-hg@73
   112
		actionData.add("integeR");
franta-hg@73
   113
		actionData.add("4");
franta-hg@73
   114
franta-hg@73
   115
		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8"));
franta-hg@70
   116
	}
franta-hg@70
   117
franta-hg@70
   118
	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
franta-hg@70
   119
franta-hg@70
   120
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
franta-hg@70
   121
		ActionContext actionContext = new ActionContext(baos);
franta-hg@70
   122
franta-hg@70
   123
		actionContext.setActionProperties(actionProperties);
franta-hg@70
   124
		actionContext.setActionData(actionData);
franta-hg@70
   125
franta-hg@70
   126
		XPathAction instance = new XPathAction(actionContext);
franta-hg@70
   127
		instance.run(domResult);
franta-hg@70
   128
		System.out.write(baos.toByteArray());
franta-hg@70
   129
franta-hg@70
   130
		assertEquals(baos.toByteArray(), expectedResult);
franta-hg@70
   131
	}
franta-hg@70
   132
}