aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java
blob: 992b9fe983eca7fe9487b42ba84daa3f94cf7095 (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 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.core.analysis;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.test.TargetLoader;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link Analyzer}.
 */
public class AnalyzerTest {

	private Analyzer analyzer;

	private final Set<String> classes = new HashSet<String>();

	private class EmptyStructureVisitor implements ICoverageVisitor {

		public void visitCoverage(IClassCoverage coverage) {
			final String name = coverage.getName();
			assertTrue("Class already processed: " + name, classes.add(name));
		}
	}

	@Before
	public void setup() {
		analyzer = new Analyzer(new ExecutionDataStore(),
				new EmptyStructureVisitor());
	}

	@Test
	public void testAnalyzeClass1() throws IOException {
		analyzer.analyzeClass(TargetLoader.getClassData(AnalyzerTest.class));
		assertEquals(
				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
				classes);
	}

	@Test
	public void testAnalyzeClass2() throws IOException {
		analyzer.analyzeClass(TargetLoader
				.getClassDataAsBytes(AnalyzerTest.class));
		assertEquals(
				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
				classes);
	}

	@Test
	public void testAnalyzeArchive() throws IOException {
		final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		final ZipOutputStream zip = new ZipOutputStream(buffer);
		zip.putNextEntry(new ZipEntry(
				"org/jacoco/core/analysis/AnalyzerTest.class"));
		zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
		zip.finish();
		final int count = analyzer.analyzeArchive(new ByteArrayInputStream(
				buffer.toByteArray()));
		assertEquals(1, count);
		assertEquals(
				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
				classes);
	}

	@Test
	public void testAnalyzeAll1() throws IOException {
		final int count = analyzer.analyzeAll(TargetLoader
				.getClassData(AnalyzerTest.class));
		assertEquals(1, count);
		assertEquals(
				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
				classes);
	}

	@Test
	public void testAnalyzeAll2() throws IOException {
		final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		final ZipOutputStream zip = new ZipOutputStream(buffer);
		zip.putNextEntry(new ZipEntry(
				"org/jacoco/core/analysis/AnalyzerTest.class"));
		zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
		zip.finish();
		final int count = analyzer.analyzeAll(new ByteArrayInputStream(buffer
				.toByteArray()));
		assertEquals(1, count);
		assertEquals(
				Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
				classes);
	}

	@Test
	public void testAnalyzeAll3() throws IOException {
		final int count = analyzer.analyzeAll(new ByteArrayInputStream(
				new byte[0]));
		assertEquals(0, count);
		assertEquals(Collections.emptySet(), classes);
	}

}