1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 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.examples;
13 
14 import static org.jacoco.examples.ConsoleOutput.containsLine;
15 
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 
21 import org.junit.Rule;
22 import org.junit.Test;
23 
24 /**
25  * Tests for {@link ClassInfo}.
26  */
27 public class ClassInfoTest {
28 
29 	@Rule
30 	public ConsoleOutput console = new ConsoleOutput();
31 
32 	@Test
testRunExample()33 	public void testRunExample() throws Exception {
34 
35 		final String[] args = new String[] { createClassFile() };
36 		new ClassInfo(console.stream).execute(args);
37 
38 		console.expect(containsLine("class name:   org/jacoco/examples/ClassInfoTest"));
39 		console.expect(containsLine("methods:      3"));
40 		console.expect(containsLine("branches:     2"));
41 		console.expect(containsLine("complexity:   4"));
42 	}
43 
createClassFile()44 	private String createClassFile() throws IOException {
45 		InputStream in = getClass().getResource(
46 				getClass().getSimpleName() + ".class").openStream();
47 		File f = File.createTempFile("Example", ".class");
48 		FileOutputStream out = new FileOutputStream(f);
49 		int b;
50 		while ((b = in.read()) != -1) {
51 			out.write(b);
52 		}
53 		in.close();
54 		out.close();
55 		return f.getPath();
56 	}
57 }
58