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
     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 makeEmptyResult() {
    50 		return new DOMResult(documentBuilder.newDocument());
    51 	}
    52 
    53 	private static byte[] makeStringResult(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(makeEmptyResult(), actionData, actionProperties, makeStringResult("2"));
    69 	}
    70 
    71 	private void setTyped(Boolean typed) {
    72 		actionProperties.put(XPathAction.PARAMETER_TYPED_PARAMETERS, typed.toString());
    73 	}
    74 
    75 	@Test
    76 	public void testRun_simpleParameters() throws Exception {
    77 		setTyped(false);
    78 
    79 		actionData.add("1+$a");
    80 
    81 		actionData.add("a");
    82 		actionData.add("2");
    83 
    84 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("3"));
    85 	}
    86 
    87 	@Test
    88 	public void testRun_simpleMoreParameters() throws Exception {
    89 		setTyped(false);
    90 
    91 		actionData.add("$a+$b");
    92 
    93 		actionData.add("a");
    94 		actionData.add("2");
    95 
    96 		actionData.add("b");
    97 		actionData.add("4");
    98 
    99 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("6"));
   100 	}
   101 
   102 	@Test
   103 	public void testRun_simpleTypedParameters() throws Exception {
   104 		setTyped(true);
   105 
   106 		actionData.add("$a+$b");
   107 
   108 		actionData.add("a");
   109 		actionData.add("InteGER");
   110 		actionData.add("4");
   111 
   112 		actionData.add("b");
   113 		actionData.add("integeR");
   114 		actionData.add("4");
   115 
   116 		doTest(makeEmptyResult(), actionData, actionProperties, makeStringResult("8"));
   117 	}
   118 
   119 	private void doTest(DOMResult domResult, List<String> actionData, Properties actionProperties, byte[] expectedResult) throws Exception {
   120 
   121 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
   122 		ActionContext actionContext = new ActionContext(baos);
   123 
   124 		actionContext.setActionProperties(actionProperties);
   125 		actionContext.setActionData(actionData);
   126 
   127 		XPathAction instance = new XPathAction(actionContext);
   128 		instance.run(domResult);
   129 		System.out.write(baos.toByteArray());
   130 
   131 		assertEquals(baos.toByteArray(), expectedResult);
   132 	}
   133 }