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
     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, version 3 of the License.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package cz.frantovo.alt2xml.out.xpath;
    18 
    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;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class XPathActionNGTest {
    37 
    38 	private final DocumentBuilderFactory documentBuilderFactory;
    39 	private final DocumentBuilder documentBuilder;
    40 	private Properties actionProperties;
    41 	private List<String> actionData;
    42 
    43 	public XPathActionNGTest() throws ParserConfigurationException {
    44 		documentBuilderFactory = DocumentBuilderFactory.newInstance();
    45 		documentBuilder = documentBuilderFactory.newDocumentBuilder();
    46 	}
    47 
    48 	private DOMResult makeEmptyResult() {
    49 		return new DOMResult(documentBuilder.newDocument());
    50 	}
    51 
    52 	private static byte[] makeStringResult(String value) {
    53 		return (value + "\n").getBytes();
    54 	}
    55 
    56 	@BeforeMethod
    57 	public void setupMethod() {
    58 		actionProperties = new Properties();
    59 		actionData = new ArrayList<>();
    60 		System.out.println("\n--- Output: --------");
    61 	}
    62 
    63 	@Test
    64 	public void testRun_simple() throws Exception {
    65 		actionData.add("1+1");
    66 
    67 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("2"));
    68 	}
    69 
    70 	private void setTyped(Boolean typed) {
    71 		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString());
    72 	}
    73 
    74 	@Test
    75 	public void testRun_simpleParameters() throws Exception {
    76 		setTyped(false);
    77 
    78 		actionData.add("1+$a");
    79 
    80 		actionData.add("a");
    81 		actionData.add("2");
    82 
    83 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3"));
    84 	}
    85 
    86 	@Test
    87 	public void testRun_simpleMoreParameters() throws Exception {
    88 		setTyped(false);
    89 
    90 		actionData.add("$a+$b");
    91 
    92 		actionData.add("a");
    93 		actionData.add("2");
    94 
    95 		actionData.add("b");
    96 		actionData.add("4");
    97 
    98 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6"));
    99 	}
   100 
   101 	@Test
   102 	public void testRun_simpleTypedParameters() throws Exception {
   103 		setTyped(true);
   104 
   105 		actionData.add("$a+$b");
   106 
   107 		actionData.add("a");
   108 		actionData.add("InteGER");
   109 		actionData.add("4");
   110 
   111 		actionData.add("b");
   112 		actionData.add("integeR");
   113 		actionData.add("4");
   114 
   115 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8"));
   116 	}
   117 
   118 	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
   119 
   120 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
   121 		ActionContext actionContext = new ActionContext(baos);
   122 
   123 		actionContext.setActionProperties(actionProperties);
   124 		actionContext.setActionData(actionData);
   125 
   126 		XPathAction instance = new XPathAction(actionContext);
   127 		instance.run(domResult);
   128 		System.out.write(baos.toByteArray());
   129 
   130 		assertEquals(baos.toByteArray(), expectedResult);
   131 	}
   132 }