aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java
blob: 8616763d3dadf331671f02d58811e020d5d6d1ad (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
 * Copyright (c) 2009, 2013 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:
 *    Evgeny Mandrikov - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.agent.rt.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.jacoco.agent.rt.internal.controller.IAgentController;
import org.jacoco.agent.rt.internal.controller.LocalController;
import org.jacoco.agent.rt.internal.controller.TcpClientController;
import org.jacoco.agent.rt.internal.controller.TcpServerController;
import org.jacoco.core.JaCoCo;
import org.jacoco.core.data.ExecutionDataReader;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.core.runtime.AgentOptions;
import org.jacoco.core.runtime.AgentOptions.OutputMode;
import org.jacoco.core.runtime.RuntimeData;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
 * Unit tests for {@link Agent}.
 */
public class AgentTest implements IExceptionLogger {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	private AgentOptions options;
	private File execfile;

	private Exception exception;

	@Before
	public void setup() {
		options = new AgentOptions();
		execfile = new File(folder.getRoot(), "jacoco.exec");
		options.setOutput(OutputMode.file);
		options.setDestfile(execfile.getAbsolutePath());
	}

	@Test
	public void testCreateController() {
		Agent agent = new Agent(options, this);

		options.setOutput(OutputMode.file);
		assertEquals(LocalController.class, agent.createAgentController()
				.getClass());

		options.setOutput(OutputMode.tcpserver);
		assertEquals(TcpServerController.class, agent.createAgentController()
				.getClass());

		options.setOutput(OutputMode.tcpclient);
		assertEquals(TcpClientController.class, agent.createAgentController()
				.getClass());
	}

	@Test
	public void testStartupShutdown() throws Exception {
		options.setSessionId("testsession");
		Agent agent = new Agent(options, this);
		agent.startup();

		assertEquals("testsession", agent.getData().getSessionId());

		agent.shutdown();

		assertTrue(execfile.isFile());
		assertTrue(execfile.length() > 0);
		assertNull(exception);
	}

	@Test
	public void testShutdownWithException() throws Exception {
		final Exception expected = new Exception();
		Agent agent = new Agent(options, this) {
			@Override
			IAgentController createAgentController() {
				return new IAgentController() {
					public void startup(AgentOptions options, RuntimeData data) {
					}

					public void shutdown() throws Exception {
						throw expected;
					}

					public void writeExecutionData(boolean reset) {
					}
				};
			}
		};
		agent.startup();

		agent.shutdown();

		assertSame(expected, exception);
	}

	@Test
	public void testNoSessionId() throws Exception {
		Agent agent = new Agent(options, this);

		final String defaultId = agent.getData().getSessionId();

		agent.startup();

		assertFalse(defaultId.equals(agent.getData().getSessionId()));
		assertNull(exception);
	}

	@Test
	public void testNoDumpOnExit() throws Exception {
		options.setDumpOnExit(false);
		Agent agent = new Agent(options, this);

		agent.startup();
		agent.shutdown();

		assertEquals(0, execfile.length());
		assertNull(exception);
	}

	@Test
	public void testInvalidExecFile() throws Exception {
		options.setDestfile(folder.getRoot().getAbsolutePath());
		Agent agent = new Agent(options, this);

		agent.startup();

		assertTrue(exception instanceof IOException);
	}

	@Test
	public void testGetVersion() {
		Agent agent = new Agent(options, this);
		assertEquals(JaCoCo.VERSION, agent.getVersion());
	}

	@Test
	public void testGetSetSessionId() throws IOException {
		Agent agent = new Agent(options, this);
		agent.startup();
		agent.setSessionId("agenttestid");
		assertEquals("agenttestid", agent.getSessionId());

		SessionInfoStore sessionStore = new SessionInfoStore();
		ExecutionDataReader reader = new ExecutionDataReader(
				new ByteArrayInputStream(agent.getExecutionData(false)));
		reader.setSessionInfoVisitor(sessionStore);
		reader.read();
		assertEquals("agenttestid", sessionStore.getInfos().get(0).getId());
	}

	@Test
	public void testReset() {
		Agent agent = new Agent(options, this);

		boolean[] probes = agent.getData()
				.getExecutionData(Long.valueOf(0x12345678), "Foo", 1)
				.getProbes();
		probes[0] = true;

		agent.reset();

		assertFalse(probes[0]);
	}

	@Test
	public void testGetExecutionData() throws IOException {
		options.setSessionId("agenttestid");
		Agent agent = new Agent(options, this);
		agent.startup();

		boolean[] probes = agent.getData()
				.getExecutionData(Long.valueOf(0x12345678), "Foo", 1)
				.getProbes();
		probes[0] = true;

		byte[] data = agent.getExecutionData(true);

		// ensure reset has been executed
		assertFalse(probes[0]);

		ExecutionDataStore execStore = new ExecutionDataStore();
		SessionInfoStore sessionStore = new SessionInfoStore();

		ExecutionDataReader reader = new ExecutionDataReader(
				new ByteArrayInputStream(data));
		reader.setExecutionDataVisitor(execStore);
		reader.setSessionInfoVisitor(sessionStore);
		reader.read();

		assertEquals("Foo", execStore.get(0x12345678).getName());
		assertEquals(1, sessionStore.getInfos().size());
		assertEquals("agenttestid", sessionStore.getInfos().get(0).getId());
	}

	@Test
	public void testDump() throws Exception {
		final boolean[] called = new boolean[1];
		Agent agent = new Agent(options, this) {
			@Override
			IAgentController createAgentController() {
				return new IAgentController() {
					public void startup(AgentOptions options, RuntimeData data) {
					}

					public void shutdown() throws Exception {
					}

					public void writeExecutionData(boolean reset) {
						assertTrue(reset);
						called[0] = true;
					}
				};
			}
		};
		agent.startup();

		agent.dump(true);

		assertTrue(called[0]);
	}

	@Test
	public void testJmx() throws Exception {
		options.setJmx(true);
		Agent agent = new Agent(options, this);

		agent.startup();

		ObjectName objectName = new ObjectName("org.jacoco:type=Runtime");
		final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		assertEquals(JaCoCo.VERSION, server.getAttribute(objectName, "Version"));

		agent.shutdown();

		try {
			server.getMBeanInfo(objectName);
			fail("InstanceNotFoundException expected");
		} catch (InstanceNotFoundException expected) {
		}
	}

	@Test(expected = InstanceNotFoundException.class)
	public void testNoJmx() throws Exception {
		Agent agent = new Agent(options, this);
		agent.startup();

		ObjectName objectName = new ObjectName("org.jacoco:type=Runtime");
		ManagementFactory.getPlatformMBeanServer().getMBeanInfo(objectName);
	}

	// === IExceptionLogger ===

	public void logExeption(Exception ex) {
		exception = ex;
	}

}