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