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
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.alt2xml.out.xpath;
    19 
    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;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class XPathActionNGTest {
    38 
    39 	private final DocumentBuilderFactory documentBuilderFactory;
    40 	private final DocumentBuilder documentBuilder;
    41 	private Properties actionProperties;
    42 	private List<String> actionData;
    43 
    44 	public XPathActionNGTest() throws ParserConfigurationException {
    45 		documentBuilderFactory = DocumentBuilderFactory.newInstance();
    46 		documentBuilder = documentBuilderFactory.newDocumentBuilder();
    47 	}
    48 
    49 	private DOMResult generateEmptyResult() {
    50 		return new DOMResult(documentBuilder.newDocument());
    51 	}
    52 
    53 	private static byte[] generateStringResult(String value) {
    54 		return (value + "\n").getBytes();
    55 	}
    56 
    57 	@BeforeMethod
    58 	public void setupMethod() {
    59 		actionProperties = new Properties();
    60 		actionData = new ArrayList<>();
    61 		System.out.println("\n--- Output: --------");
    62 	}
    63 
    64 	@Test
    65 	public void testRun_simple() throws Exception {
    66 		actionData.add("1+1");
    67 
    68 		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("2"));
    69 	}
    70 
    71 	@Test
    72 	public void testRun_simpleParameters() throws Exception {
    73 		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, Boolean.FALSE);
    74 
    75 		actionData.add("1+$a");
    76 		actionData.add("a");
    77 		actionData.add("2");
    78 
    79 		doTest(generateEmptyResult(), actionData, actionProperties, generateStringResult("3"));
    80 	}
    81 
    82 	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
    83 
    84 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    85 		ActionContext actionContext = new ActionContext(baos);
    86 
    87 		actionContext.setActionProperties(actionProperties);
    88 		actionContext.setActionData(actionData);
    89 
    90 		XPathAction instance = new XPathAction(actionContext);
    91 		instance.run(domResult);
    92 		System.out.write(baos.toByteArray());
    93 
    94 		assertEquals(baos.toByteArray(), expectedResult);
    95 	}
    96 }