Home | History | Annotate | Download | only in data
      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.data;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.junit.Assert.assertFalse;
     16 import static org.junit.Assert.assertNotNull;
     17 import static org.junit.Assert.assertNull;
     18 import static org.junit.Assert.assertSame;
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import java.util.Arrays;
     22 import java.util.Collections;
     23 import java.util.HashMap;
     24 import java.util.HashSet;
     25 import java.util.Map;
     26 import java.util.Set;
     27 
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 
     31 /**
     32  * Unit tests for {@link ExecutionDataStore}.
     33  */
     34 public class ExecutionDataStoreTest implements IExecutionDataVisitor {
     35 
     36 	private ExecutionDataStore store;
     37 
     38 	private Map<Long, ExecutionData> dataOutput;
     39 
     40 	@Before
     41 	public void setup() {
     42 		store = new ExecutionDataStore();
     43 		dataOutput = new HashMap<Long, ExecutionData>();
     44 	}
     45 
     46 	@Test
     47 	public void testEmpty() {
     48 		assertNull(store.get(123));
     49 		assertFalse(store.contains("org/jacoco/example/Foo"));
     50 		store.accept(this);
     51 		assertEquals(Collections.emptyMap(), dataOutput);
     52 	}
     53 
     54 	@Test
     55 	public void testPut() {
     56 		final boolean[] probes = new boolean[] { false, false, true };
     57 		store.put(new ExecutionData(1000, "Sample", probes));
     58 		final ExecutionData data = store.get(1000);
     59 		assertSame(probes, data.getProbes());
     60 		assertTrue(store.contains("Sample"));
     61 		store.accept(this);
     62 		assertEquals(Collections.singletonMap(Long.valueOf(1000), data),
     63 				dataOutput);
     64 	}
     65 
     66 	@Test
     67 	public void testReentrantAccept() {
     68 		final boolean[] probes = new boolean[] { false, false, true };
     69 		store.put(new ExecutionData(1000, "Sample0", probes));
     70 		store.put(new ExecutionData(1001, "Sample1", probes));
     71 		store.accept(new IExecutionDataVisitor() {
     72 			public void visitClassExecution(ExecutionData data) {
     73 				store.put(new ExecutionData(1002, "Sample2", probes));
     74 				ExecutionDataStoreTest.this.visitClassExecution(data);
     75 			}
     76 		});
     77 		assertEquals(2, dataOutput.size());
     78 	}
     79 
     80 	@Test
     81 	public void testGetContents() {
     82 		final boolean[] probes = new boolean[] {};
     83 		final ExecutionData a = new ExecutionData(1000, "A", probes);
     84 		store.put(a);
     85 		final ExecutionData aa = new ExecutionData(1000, "A", probes);
     86 		store.put(aa);
     87 		final ExecutionData b = new ExecutionData(1001, "B", probes);
     88 		store.put(b);
     89 		final Set<ExecutionData> actual = new HashSet<ExecutionData>(
     90 				store.getContents());
     91 		final Set<ExecutionData> expected = new HashSet<ExecutionData>(
     92 				Arrays.asList(a, b));
     93 		assertEquals(expected, actual);
     94 	}
     95 
     96 	@Test
     97 	public void testGetWithoutCreate() {
     98 		final ExecutionData data = new ExecutionData(1000, "Sample",
     99 				new boolean[] {});
    100 		store.put(data);
    101 		assertSame(data, store.get(1000));
    102 	}
    103 
    104 	@Test
    105 	public void testGetWithCreate() {
    106 		final Long id = Long.valueOf(1000);
    107 		final ExecutionData data = store.get(id, "Sample", 3);
    108 		assertEquals(1000, data.getId());
    109 		assertEquals("Sample", data.getName());
    110 		assertEquals(3, data.getProbes().length);
    111 		assertFalse(data.getProbes()[0]);
    112 		assertFalse(data.getProbes()[1]);
    113 		assertFalse(data.getProbes()[2]);
    114 		assertSame(data, store.get(id, "Sample", 3));
    115 		assertTrue(store.contains("Sample"));
    116 	}
    117 
    118 	@Test(expected = IllegalStateException.class)
    119 	public void testGetNegative1() {
    120 		final boolean[] data = new boolean[] { false, false, true };
    121 		store.put(new ExecutionData(1000, "Sample", data));
    122 		store.get(Long.valueOf(1000), "Other", 3);
    123 	}
    124 
    125 	@Test(expected = IllegalStateException.class)
    126 	public void testGetNegative2() {
    127 		final boolean[] data = new boolean[] { false, false, true };
    128 		store.put(new ExecutionData(1000, "Sample", data));
    129 		store.get(Long.valueOf(1000), "Sample", 4);
    130 	}
    131 
    132 	@Test(expected = IllegalStateException.class)
    133 	public void testPutNegative() {
    134 		final boolean[] data = new boolean[0];
    135 		store.put(new ExecutionData(1000, "Sample1", data));
    136 		store.put(new ExecutionData(1000, "Sample2", data));
    137 	}
    138 
    139 	@Test
    140 	public void testMerge() {
    141 		final boolean[] data1 = new boolean[] { false, true, false, true };
    142 		store.visitClassExecution(new ExecutionData(1000, "Sample", data1));
    143 		final boolean[] data2 = new boolean[] { false, true, true, false };
    144 		store.visitClassExecution(new ExecutionData(1000, "Sample", data2));
    145 
    146 		final boolean[] result = store.get(1000).getProbes();
    147 		assertFalse(result[0]);
    148 		assertTrue(result[1]);
    149 		assertTrue(result[2]);
    150 		assertTrue(result[3]);
    151 	}
    152 
    153 	@Test(expected = IllegalStateException.class)
    154 	public void testMergeNegative() {
    155 		final boolean[] data1 = new boolean[] { false, false };
    156 		store.visitClassExecution(new ExecutionData(1000, "Sample", data1));
    157 		final boolean[] data2 = new boolean[] { false, false, false };
    158 		store.visitClassExecution(new ExecutionData(1000, "Sample", data2));
    159 	}
    160 
    161 	@Test
    162 	public void testSubtract() {
    163 		final boolean[] data1 = new boolean[] { false, true, false, true };
    164 		store.put(new ExecutionData(1000, "Sample", data1));
    165 		final boolean[] data2 = new boolean[] { false, false, true, true };
    166 		store.subtract(new ExecutionData(1000, "Sample", data2));
    167 
    168 		final boolean[] result = store.get(1000).getProbes();
    169 		assertFalse(result[0]);
    170 		assertTrue(result[1]);
    171 		assertFalse(result[2]);
    172 		assertFalse(result[3]);
    173 	}
    174 
    175 	@Test
    176 	public void testSubtractOtherId() {
    177 		final boolean[] data1 = new boolean[] { false, true };
    178 		store.put(new ExecutionData(1000, "Sample1", data1));
    179 		final boolean[] data2 = new boolean[] { true, true };
    180 		store.subtract(new ExecutionData(2000, "Sample2", data2));
    181 
    182 		final boolean[] result = store.get(1000).getProbes();
    183 		assertFalse(result[0]);
    184 		assertTrue(result[1]);
    185 
    186 		assertNull(store.get(2000));
    187 	}
    188 
    189 	@Test
    190 	public void testSubtractStore() {
    191 		final boolean[] data1 = new boolean[] { false, true, false, true };
    192 		store.put(new ExecutionData(1000, "Sample", data1));
    193 
    194 		final ExecutionDataStore store2 = new ExecutionDataStore();
    195 		final boolean[] data2 = new boolean[] { false, false, true, true };
    196 		store2.put(new ExecutionData(1000, "Sample", data2));
    197 
    198 		store.subtract(store2);
    199 
    200 		final boolean[] result = store.get(1000).getProbes();
    201 		assertFalse(result[0]);
    202 		assertTrue(result[1]);
    203 		assertFalse(result[2]);
    204 		assertFalse(result[3]);
    205 	}
    206 
    207 	@Test
    208 	public void testReset() throws InstantiationException,
    209 			IllegalAccessException {
    210 		final boolean[] data1 = new boolean[] { true, true, false };
    211 		store.put(new ExecutionData(1000, "Sample", data1));
    212 		store.reset();
    213 		final boolean[] data2 = store.get(1000).getProbes();
    214 		assertNotNull(data2);
    215 		assertFalse(data2[0]);
    216 		assertFalse(data2[1]);
    217 		assertFalse(data2[2]);
    218 	}
    219 
    220 	// === IExecutionDataOutput ===
    221 
    222 	public void visitClassExecution(final ExecutionData data) {
    223 		dataOutput.put(Long.valueOf(data.getId()), data);
    224 	}
    225 
    226 }
    227