aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.cli.test/src/org/jacoco/cli/internal/XmlDocumentationTest.java
blob: 83f79bc975ee56e2426e8e7d272bfa2cd4728e66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
 * Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.cli.internal;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * Unit tests for {@link XmlDocumentation}.
 */
public class XmlDocumentationTest {

	@Rule
	public TemporaryFolder tmp = new TemporaryFolder();

	private DocumentBuilder builder;
	private XPath xpath;

	@Before
	public void before() throws Exception {
		final DocumentBuilderFactory builderFactory = DocumentBuilderFactory
				.newInstance();
		builder = builderFactory.newDocumentBuilder();
		builder.setErrorHandler(new ErrorHandler() {
			public void error(SAXParseException exception) throws SAXException {
				fail(exception.getMessage());
			}

			public void fatalError(SAXParseException exception)
					throws SAXException {
				fail(exception.getMessage());
			}

			public void warning(SAXParseException exception)
					throws SAXException {
				fail(exception.getMessage());
			}
		});

		xpath = XPathFactory.newInstance().newXPath();
	}

	@Test
	public void should_create_documentation() throws Exception {
		File file = new File(tmp.getRoot(), "doc.xml");

		XmlDocumentation.main(file.getAbsolutePath());

		Document doc = parse(file);

		assertContains("java -jar jacococli.jar report",
				"/documentation/command[@name='report']/usage/text()", doc);

		assertContains("Generate reports",
				"/documentation/command[@name='report']/description/text()",
				doc);

		assertContains("<execfiles>",
				"/documentation/command[@name='report']/option[1]/usage/text()",
				doc);

		assertContains("false",
				"/documentation/command[@name='report']/option[1]/@required",
				doc);

		assertContains("true",
				"/documentation/command[@name='report']/option[1]/@multiple",
				doc);

		assertContains("-classfiles <path>",
				"/documentation/command[@name='report']/option[2]/usage/text()",
				doc);

		assertContains("true",
				"/documentation/command[@name='report']/option[2]/@multiple",
				doc);

	}

	private Document parse(File file) throws Exception {
		InputStream in = new FileInputStream(file);
		try {
			return builder.parse(new InputSource(in));
		} finally {
			in.close();
		}
	}

	private void assertContains(String expected, String query, Document doc)
			throws XPathExpressionException {
		final String actual = eval(query, doc);
		assertTrue(actual, actual.contains(expected));
	}

	private String eval(String query, Document doc)
			throws XPathExpressionException {
		return (String) xpath.evaluate(query, doc, XPathConstants.STRING);
	}

}