1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 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.core.internal.analysis;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17 
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 
22 import org.jacoco.core.analysis.IClassCoverage;
23 import org.jacoco.core.analysis.ICoverageNode;
24 import org.jacoco.core.analysis.IPackageCoverage;
25 import org.jacoco.core.analysis.ISourceFileCoverage;
26 import org.junit.Test;
27 
28 /**
29  * Unit tests for {@link BundleCoverageImpl}.
30  */
31 public class BundleCoverageImplTest {
32 
33 	@Test
testProperties()34 	public void testProperties() {
35 		Collection<IClassCoverage> classes = Collections.emptySet();
36 		Collection<ISourceFileCoverage> sourcefiles = Collections.emptySet();
37 		Collection<IPackageCoverage> packages = Collections
38 				.singleton((IPackageCoverage) new PackageCoverageImpl("p1",
39 						classes, sourcefiles));
40 		BundleCoverageImpl bundle = new BundleCoverageImpl("testbundle",
41 				packages);
42 		assertEquals(ICoverageNode.ElementType.BUNDLE, bundle.getElementType());
43 		assertEquals("testbundle", bundle.getName());
44 		assertEquals(packages, bundle.getPackages());
45 	}
46 
47 	@Test
testCounters()48 	public void testCounters() {
49 		Collection<IClassCoverage> classes = Collections.emptySet();
50 		Collection<ISourceFileCoverage> sourcefiles = Collections.emptySet();
51 		final IPackageCoverage p1 = new PackageCoverageImpl("p1", classes,
52 				sourcefiles) {
53 			{
54 				classCounter = CounterImpl.getInstance(1, 0);
55 				methodCounter = CounterImpl.getInstance(2, 0);
56 				branchCounter = CounterImpl.getInstance(3, 0);
57 				instructionCounter = CounterImpl.getInstance(4, 0);
58 				lineCounter = CounterImpl.getInstance(5, 0);
59 			}
60 		};
61 		final IPackageCoverage p2 = new PackageCoverageImpl("p1", classes,
62 				sourcefiles) {
63 			{
64 				classCounter = CounterImpl.getInstance(1, 0);
65 				methodCounter = CounterImpl.getInstance(2, 0);
66 				branchCounter = CounterImpl.getInstance(3, 0);
67 				instructionCounter = CounterImpl.getInstance(4, 0);
68 				lineCounter = CounterImpl.getInstance(5, 0);
69 			}
70 		};
71 		BundleCoverageImpl bundle = new BundleCoverageImpl("testbundle",
72 				Arrays.asList(p1, p2));
73 		assertEquals(CounterImpl.getInstance(2, 0), bundle.getClassCounter());
74 		assertEquals(CounterImpl.getInstance(4, 0), bundle.getMethodCounter());
75 		assertEquals(CounterImpl.getInstance(6, 0), bundle.getBranchCounter());
76 		assertEquals(CounterImpl.getInstance(8, 0),
77 				bundle.getInstructionCounter());
78 		assertEquals(CounterImpl.getInstance(10, 0), bundle.getLineCounter());
79 	}
80 
81 	@Test
testGroupByPackage()82 	public void testGroupByPackage() {
83 		ClassCoverageImpl ca = new ClassCoverageImpl("p1/A", 1, false);
84 		ca.setSourceFileName("A.java");
85 		ClassCoverageImpl cb = new ClassCoverageImpl("p2/B", 2, false);
86 		cb.setSourceFileName("B.java");
87 		ISourceFileCoverage sb = new SourceFileCoverageImpl("B.java", "p2");
88 		ISourceFileCoverage sc = new SourceFileCoverageImpl("C.java", "p3");
89 		BundleCoverageImpl bundle = new BundleCoverageImpl("bundle",
90 				Arrays.asList((IClassCoverage) ca, (IClassCoverage) cb),
91 				Arrays.asList(sb, sc));
92 
93 		Collection<IPackageCoverage> packages = bundle.getPackages();
94 		assertEquals(3, packages.size(), 0.0);
95 
96 		IPackageCoverage p1 = findPackage("p1", packages);
97 		assertNotNull(p1);
98 		assertEquals(Collections.singletonList(ca), p1.getClasses());
99 		assertTrue(p1.getSourceFiles().isEmpty());
100 
101 		IPackageCoverage p2 = findPackage("p2", packages);
102 		assertNotNull(p2);
103 		assertEquals(Collections.singletonList(cb), p2.getClasses());
104 		assertEquals(Collections.singletonList(sb), p2.getSourceFiles());
105 
106 		IPackageCoverage p3 = findPackage("p3", packages);
107 		assertNotNull(p3);
108 		assertTrue(p3.getClasses().isEmpty());
109 		assertEquals(Collections.singletonList(sc), p3.getSourceFiles());
110 	}
111 
findPackage(String name, Collection<IPackageCoverage> packages)112 	private IPackageCoverage findPackage(String name,
113 			Collection<IPackageCoverage> packages) {
114 		for (IPackageCoverage p : packages) {
115 			if (name.equals(p.getName())) {
116 				return p;
117 			}
118 		}
119 		return null;
120 	}
121 
122 }
123