AnalyzerTest.java revision fc340a20c201bec9c0ee31ec16c85766477a1edf
1/*******************************************************************************
2 * Copyright (c) 2009, 2013 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.core.analysis;
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertTrue;
16
17import java.io.ByteArrayInputStream;
18import java.io.ByteArrayOutputStream;
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.OutputStream;
23import java.util.Arrays;
24import java.util.Collections;
25import java.util.HashSet;
26import java.util.Set;
27import java.util.jar.JarInputStream;
28import java.util.jar.Pack200;
29import java.util.zip.GZIPOutputStream;
30import java.util.zip.ZipEntry;
31import java.util.zip.ZipOutputStream;
32
33import org.jacoco.core.data.ExecutionDataStore;
34import org.jacoco.core.test.TargetLoader;
35import org.junit.Before;
36import org.junit.Rule;
37import org.junit.Test;
38import org.junit.rules.TemporaryFolder;
39
40/**
41 * Unit tests for {@link Analyzer}.
42 */
43public class AnalyzerTest {
44
45	@Rule
46	public TemporaryFolder folder = new TemporaryFolder();
47
48	private Analyzer analyzer;
49
50	private final Set<String> classes = new HashSet<String>();
51
52	private class EmptyStructureVisitor implements ICoverageVisitor {
53
54		public void visitCoverage(IClassCoverage coverage) {
55			final String name = coverage.getName();
56			assertTrue("Class already processed: " + name, classes.add(name));
57		}
58	}
59
60	@Before
61	public void setup() {
62		analyzer = new Analyzer(new ExecutionDataStore(),
63				new EmptyStructureVisitor());
64	}
65
66	@Test
67	public void testAnalyzeClass1() throws IOException {
68		analyzer.analyzeClass(TargetLoader.getClassData(AnalyzerTest.class));
69		assertEquals(
70				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
71				classes);
72	}
73
74	@Test
75	public void testAnalyzeClass2() throws IOException {
76		analyzer.analyzeClass(TargetLoader
77				.getClassDataAsBytes(AnalyzerTest.class));
78		assertEquals(
79				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
80				classes);
81	}
82
83	@Test
84	public void testAnalyzeAll_Class() throws IOException {
85		final int count = analyzer.analyzeAll(TargetLoader
86				.getClassData(AnalyzerTest.class));
87		assertEquals(1, count);
88		assertEquals(
89				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
90				classes);
91	}
92
93	@Test
94	public void testAnalyzeAll_Zip() throws IOException {
95		final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
96		final ZipOutputStream zip = new ZipOutputStream(buffer);
97		zip.putNextEntry(new ZipEntry(
98				"org/jacoco/core/analysis/AnalyzerTest.class"));
99		zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
100		zip.finish();
101		final int count = analyzer.analyzeAll(new ByteArrayInputStream(buffer
102				.toByteArray()));
103		assertEquals(1, count);
104		assertEquals(
105				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
106				classes);
107	}
108
109	@Test
110	public void testAnalyzeAll_Pack200() throws IOException {
111		final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
112		final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
113		zip.putNextEntry(new ZipEntry(
114				"org/jacoco/core/analysis/AnalyzerTest.class"));
115		zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
116		zip.finish();
117
118		final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
119		GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
120		Pack200.newPacker().pack(
121				new JarInputStream(new ByteArrayInputStream(
122						zipbuffer.toByteArray())), gzipOutput);
123		gzipOutput.finish();
124
125		final int count = analyzer.analyzeAll(new ByteArrayInputStream(
126				pack200buffer.toByteArray()));
127		assertEquals(1, count);
128		assertEquals(
129				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
130				classes);
131	}
132
133	@Test
134	public void testAnalyzeAll_Empty() throws IOException {
135		final int count = analyzer.analyzeAll(new ByteArrayInputStream(
136				new byte[0]));
137		assertEquals(0, count);
138		assertEquals(Collections.emptySet(), classes);
139	}
140
141	@Test
142	public void testAnalyzeAll_Folder() throws IOException {
143		createClassfile("bin1", AnalyzerTest.class);
144		final int count = analyzer.analyzeAll(folder.getRoot());
145		assertEquals(1, count);
146		assertEquals(
147				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
148				classes);
149	}
150
151	@Test
152	public void testAnalyzeAll_Path() throws IOException {
153		createClassfile("bin1", Analyzer.class);
154		createClassfile("bin2", AnalyzerTest.class);
155		String path = "bin1" + File.pathSeparator + "bin2";
156		final int count = analyzer.analyzeAll(path, folder.getRoot());
157		assertEquals(2, count);
158		assertEquals(
159				new HashSet<String>(Arrays.asList(
160						"org/jacoco/core/analysis/Analyzer",
161						"org/jacoco/core/analysis/AnalyzerTest")), classes);
162	}
163
164	@Test(expected = IOException.class)
165	public void testAnalyzeAll_BrokenZip() throws IOException {
166		File file = new File(folder.getRoot(), "broken.zip");
167		OutputStream out = new FileOutputStream(file);
168		ZipOutputStream zip = new ZipOutputStream(out);
169		zip.putNextEntry(new ZipEntry("brokenentry.txt"));
170		out.write(0x23); // Unexpected data here
171		zip.close();
172		analyzer.analyzeAll(file);
173	}
174
175	private void createClassfile(final String dir, final Class<?> source)
176			throws IOException {
177		File file = new File(folder.getRoot(), dir);
178		file.mkdirs();
179		file = new File(file, "some.class");
180		OutputStream out = new FileOutputStream(file);
181		out.write(TargetLoader.getClassDataAsBytes(source));
182		out.close();
183	}
184
185}
186