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 17:56:58 +0200
changeset 70 ead6a0d4e022
child 73 bb0aa5d23f2e
permissions -rw-r--r--
out-xpath: unit test
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@70
    49
	private DOMResult generateEmptyResult() {
franta-hg@70
    50
		return new DOMResult(documentBuilder.newDocument());
franta-hg@70
    51
	}
franta-hg@70
    52
franta-hg@70
    53
	private static byte[] generateStringResult(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@70
    68
		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2"));
franta-hg@70
    69
	}
franta-hg@70
    70
franta-hg@70
    71
	@Test
franta-hg@70
    72
	public void testRun_simpleParameters() throws Exception {
franta-hg@70
    73
		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE);
franta-hg@70
    74
franta-hg@70
    75
		actionData.add("1+$a");
franta-hg@70
    76
		actionData.add("a");
franta-hg@70
    77
		actionData.add("2");
franta-hg@70
    78
franta-hg@70
    79
		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3"));
franta-hg@70
    80
	}
franta-hg@70
    81
franta-hg@70
    82
	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
franta-hg@70
    83
franta-hg@70
    84
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
franta-hg@70
    85
		ActionContext actionContext = new ActionContext(baos);
franta-hg@70
    86
franta-hg@70
    87
		actionContext.setActionProperties(actionProperties);
franta-hg@70
    88
		actionContext.setActionData(actionData);
franta-hg@70
    89
franta-hg@70
    90
		XPathAction instance = new XPathAction(actionContext);
franta-hg@70
    91
		instance.run(domResult);
franta-hg@70
    92
		System.out.write(baos.toByteArray());
franta-hg@70
    93
franta-hg@70
    94
		assertEquals(baos.toByteArray(), expectedResult);
franta-hg@70
    95
	}
franta-hg@70
    96
}