1/*******************************************************************************
2 * Copyright (c) 2009, 2017 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 *******************************************************************************/
12package org.jacoco.cli.internal;
13
14import static org.junit.Assert.assertTrue;
15import static org.junit.Assert.fail;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.InputStream;
20
21import javax.xml.parsers.DocumentBuilder;
22import javax.xml.parsers.DocumentBuilderFactory;
23import javax.xml.xpath.XPath;
24import javax.xml.xpath.XPathConstants;
25import javax.xml.xpath.XPathExpressionException;
26import javax.xml.xpath.XPathFactory;
27
28import org.junit.Before;
29import org.junit.Rule;
30import org.junit.Test;
31import org.junit.rules.TemporaryFolder;
32import org.w3c.dom.Document;
33import org.xml.sax.ErrorHandler;
34import org.xml.sax.InputSource;
35import org.xml.sax.SAXException;
36import org.xml.sax.SAXParseException;
37
38/**
39 * Unit tests for {@link XmlDocumentation}.
40 */
41public 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