Home | History | Annotate | Download | only in internal
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Marc R. Hoffmann - initial API and implementation
     10  *
     11  *******************************************************************************/
     12 package org.jacoco.cli.internal;
     13 
     14 import static org.junit.Assert.assertTrue;
     15 import static org.junit.Assert.fail;
     16 
     17 import java.io.File;
     18 import java.io.FileInputStream;
     19 import java.io.InputStream;
     20 
     21 import javax.xml.parsers.DocumentBuilder;
     22 import javax.xml.parsers.DocumentBuilderFactory;
     23 import javax.xml.xpath.XPath;
     24 import javax.xml.xpath.XPathConstants;
     25 import javax.xml.xpath.XPathExpressionException;
     26 import javax.xml.xpath.XPathFactory;
     27 
     28 import org.junit.Before;
     29 import org.junit.Rule;
     30 import org.junit.Test;
     31 import org.junit.rules.TemporaryFolder;
     32 import org.w3c.dom.Document;
     33 import org.xml.sax.ErrorHandler;
     34 import org.xml.sax.InputSource;
     35 import org.xml.sax.SAXException;
     36 import org.xml.sax.SAXParseException;
     37 
     38 /**
     39  * Unit tests for {@link XmlDocumentation}.
     40  */
     41 public class XmlDocumentationTest {
     42 
     43 	@Rule
     44 	public TemporaryFolder tmp = new TemporaryFolder();
     45 
     46 	private DocumentBuilder builder;
     47 	private XPath xpath;
     48 
     49 	@Before
     50 	public void before() throws Exception {
     51 		final DocumentBuilderFactory builderFactory = DocumentBuilderFactory
     52 				.newInstance();
     53 		builder = builderFactory.newDocumentBuilder();
     54 		builder.setErrorHandler(new ErrorHandler() {
     55 			public void error(SAXParseException exception) throws SAXException {
     56 				fail(exception.getMessage());
     57 			}
     58 
     59 			public void fatalError(SAXParseException exception)
     60 					throws SAXException {
     61 				fail(exception.getMessage());
     62 			}
     63 
     64 			public void warning(SAXParseException exception)
     65 					throws SAXException {
     66 				fail(exception.getMessage());
     67 			}
     68 		});
     69 
     70 		xpath = XPathFactory.newInstance().newXPath();
     71 	}
     72 
     73 	@Test
     74 	public void should_create_documentation() throws Exception {
     75 		File file = new File(tmp.getRoot(), "doc.xml");
     76 
     77 		XmlDocumentation.main(file.getAbsolutePath());
     78 
     79 		Document doc = parse(file);
     80 
     81 		assertContains("java -jar jacococli.jar report",
     82 				"/documentation/command[@name='report']/usage/text()", doc);
     83 
     84 		assertContains("Generate reports",
     85 				"/documentation/command[@name='report']/description/text()",
     86 				doc);
     87 
     88 		assertContains("<execfiles>",
     89 				"/documentation/command[@name='report']/option[1]/usage/text()",
     90 				doc);
     91 
     92 		assertContains("false",
     93 				"/documentation/command[@name='report']/option[1]/@required",
     94 				doc);
     95 
     96 		assertContains("true",
     97 				"/documentation/command[@name='report']/option[1]/@multiple",
     98 				doc);
     99 
    100 		assertContains("-classfiles <path>",
    101 				"/documentation/command[@name='report']/option[2]/usage/text()",
    102 				doc);
    103 
    104 		assertContains("true",
    105 				"/documentation/command[@name='report']/option[2]/@multiple",
    106 				doc);
    107 
    108 	}
    109 
    110 	private Document parse(File file) throws Exception {
    111 		InputStream in = new FileInputStream(file);
    112 		try {
    113 			return builder.parse(new InputSource(in));
    114 		} finally {
    115 			in.close();
    116 		}
    117 	}
    118 
    119 	private void assertContains(String expected, String query, Document doc)
    120 			throws XPathExpressionException {
    121 		final String actual = eval(query, doc);
    122 		assertTrue(actual, actual.contains(expected));
    123 	}
    124 
    125 	private String eval(String query, Document doc)
    126 			throws XPathExpressionException {
    127 		return (String) xpath.evaluate(query, doc, XPathConstants.STRING);
    128 	}
    129 
    130 }
    131