aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.core.test/src/org/jacoco/core/tools/ExecFileLoaderTest.java
blob: d825910f7882f05dd59eff53b22a8d09aad57d3b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * Copyright (c) 2009, 2018 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.tools;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.ExecutionDataReader;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.ExecutionDataWriter;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.core.data.SessionInfoStore;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
 * Unit tests for {@link ExecFileLoader}.
 */
public class ExecFileLoaderTest {

	@Rule
	public final TemporaryFolder sourceFolder = new TemporaryFolder();

	private ExecFileLoader loader;

	@Before
	public void setup() {
		loader = new ExecFileLoader();
	}

	@Test
	public void testLoadFile() throws IOException {
		loader.load(createFile("a"));
		loader.load(createFile("bb"));

		assertLoaderContents("a", "bb");
	}

	@Test
	public void testLoadInputStream() throws IOException {
		final FileInputStream in1 = new FileInputStream(createFile("a"));
		loader.load(in1);
		in1.close();
		final FileInputStream in2 = new FileInputStream(createFile("bb"));
		loader.load(in2);
		in2.close();

		assertLoaderContents("a", "bb");
	}

	@Test(expected = IOException.class)
	public void testLoadBrokenContent() throws IOException {
		final File file = new File(sourceFolder.getRoot(), "broken.exec");
		final FileWriter writer = new FileWriter(file);
		writer.write("Invalid Content");
		writer.close();

		loader.load(file);
	}

	@Test
	public void testSaveFile() throws IOException {
		final File file = new File(sourceFolder.getRoot(), "target.exec");

		// Write invalid data to ensure the file is actually overwritten:
		final OutputStream out = new FileOutputStream(file);
		out.write("invalid".getBytes());
		out.close();

		loader.load(createFile("a"));
		loader.save(file, false);

		assertFileContents(file, "a");
	}

	@Test
	public void testSaveFileAppend() throws IOException {
		final File file = createFile("a");

		loader.load(createFile("bb"));
		loader.save(file, true);

		assertFileContents(file, "a", "bb");
	}

	@Test
	public void testCreateSubfolders() throws IOException {
		final File file = new File(sourceFolder.getRoot(), "a/b/c/target.exec");

		loader.load(createFile("a"));
		loader.save(file, true);

		assertFileContents(file, "a");
	}

	private File createFile(String id) throws IOException {
		final File file = new File(sourceFolder.getRoot(), id + ".exec");
		final FileOutputStream out = new FileOutputStream(file);
		final ExecutionDataWriter writer = new ExecutionDataWriter(out);
		final int value = id.length();
		writer.visitClassExecution(new ExecutionData(value, id,
				new boolean[] { true }));
		writer.visitSessionInfo(new SessionInfo(id, value, value));
		out.close();
		return file;
	}

	private void assertLoaderContents(String... expected) {
		assertContents(loader.getExecutionDataStore(),
				loader.getSessionInfoStore(), expected);
	}

	private void assertFileContents(File file, String... expected)
			throws IOException {
		final InputStream in = new FileInputStream(file);
		final ExecutionDataStore execStore = new ExecutionDataStore();
		final SessionInfoStore sessionStore = new SessionInfoStore();
		final ExecutionDataReader reader = new ExecutionDataReader(in);
		reader.setExecutionDataVisitor(execStore);
		reader.setSessionInfoVisitor(sessionStore);
		reader.read();
		assertContents(execStore, sessionStore, expected);
	}

	private void assertContents(ExecutionDataStore execStore,
			SessionInfoStore sessionStore, String... expected) {
		final List<SessionInfo> infos = sessionStore.getInfos();

		assertEquals(expected.length, execStore.getContents().size());
		assertEquals(expected.length, infos.size());
		int idx = 0;
		for (String id : expected) {
			assertEquals(id, execStore.get(id.length()).getName());
			assertEquals(id, infos.get(idx++).getId());
		}
	}

}