aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java
blob: e98dcfedb9d14670f5c3c02b23f5b9c5959247f8 (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
/*******************************************************************************
 * 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.assertTrue;

import java.io.File;
import java.io.IOException;

import org.jacoco.agent.rt.internal.Agent;
import org.jacoco.agent.rt.internal.IExceptionLogger;
import org.jacoco.agent.rt.internal.controller.LocalController;
import org.jacoco.agent.rt.internal.controller.MBeanController;
import org.jacoco.agent.rt.internal.controller.TcpClientController;
import org.jacoco.agent.rt.internal.controller.TcpServerController;
import org.jacoco.core.runtime.AgentOptions;
import org.jacoco.core.runtime.AgentOptions.OutputMode;
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 Exception exception;

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

		options.setOutput(OutputMode.file);
		assertTrue(agent.createAgentController() instanceof LocalController);

		options.setOutput(OutputMode.tcpserver);
		assertTrue(agent.createAgentController() instanceof TcpServerController);

		options.setOutput(OutputMode.tcpclient);
		assertTrue(agent.createAgentController() instanceof TcpClientController);

		options.setOutput(OutputMode.mbean);
		assertTrue(agent.createAgentController() instanceof MBeanController);
	}

	@Test
	public void testStartupShutdown() throws Exception {
		final File execfile = new File(folder.getRoot(), "jacoco.exec");
		AgentOptions options = new AgentOptions();
		options.setOutput(OutputMode.file);
		options.setDestfile(execfile.getAbsolutePath());
		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 testNoSessionId() throws Exception {
		final File execfile = new File(folder.getRoot(), "jacoco.exec");
		AgentOptions options = new AgentOptions();
		options.setOutput(OutputMode.file);
		options.setDestfile(execfile.getAbsolutePath());
		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 {
		final File execfile = new File(folder.getRoot(), "jacoco.exec");
		AgentOptions options = new AgentOptions();
		options.setOutput(OutputMode.file);
		options.setDestfile(execfile.getAbsolutePath());
		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 {
		AgentOptions options = new AgentOptions();
		options.setOutput(OutputMode.file);
		options.setDestfile(folder.getRoot().getAbsolutePath());
		Agent agent = new Agent(options, this);

		agent.startup();

		assertTrue(exception instanceof IOException);
	}

	// === IExceptionLogger ===

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

}