aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.cli.test/src/org/jacoco/cli/internal/commands/InstrumentTest.java
blob: f676e8d20d5a4aa6113f87fd56bcc991ea806ad6 (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
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.cli.internal.commands;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

import org.jacoco.cli.internal.CommandTestBase;
import org.jacoco.core.internal.InputStreams;
import org.jacoco.core.internal.instr.InstrSupport;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

/**
 * Unit tests for {@link Instrument}.
 */
public class InstrumentTest extends CommandTestBase {

	@Rule
	public TemporaryFolder tmp = new TemporaryFolder();

	@Test
	public void should_print_usage_when_no_options_are_given()
			throws Exception {
		execute("instrument");
		assertFailure();
		assertContains("\"--dest\"", err);
		assertContains(
				"Usage: java -jar jacococli.jar instrument [<sourcefiles> ...]",
				err);
	}

	@Test
	public void should_instrument_class_files_and_copy_resources_when_folder_is_given()
			throws Exception {
		File destdir = tmp.getRoot();

		execute("instrument", "--dest", destdir.getAbsolutePath(),
				getClassPath());

		assertOk();
		assertContains("[INFO] 14 classes instrumented to "
				+ destdir.getAbsolutePath(), out);

		// non class-file resources are copied:
		assertTrue(new File(destdir,
				"org/jacoco/cli/internal/commands/test-resource.properties")
						.isFile());

		assertInstrumented(new File(destdir,
				"org/jacoco/cli/internal/commands/InstrumentTest.class"));
	}

	@Test
	public void should_instrument_class_files_to_dest_folder_when_class_files_are_given()
			throws Exception {
		File destdir = tmp.getRoot();

		File src = new File(getClassPath(),
				"org/jacoco/cli/internal/commands/InstrumentTest.class");

		execute("instrument", "--dest", destdir.getAbsolutePath(),
				src.getAbsolutePath());

		assertOk();
		assertContains(
				"[INFO] 1 classes instrumented to " + destdir.getAbsolutePath(),
				out);

		assertInstrumented(new File(destdir, "InstrumentTest.class"));
	}

	@Test
	public void should_not_instrument_anything_when_no_source_is_given()
			throws Exception {
		File destdir = tmp.getRoot();

		execute("instrument", "--dest", destdir.getAbsolutePath());

		assertOk();
		assertArrayEquals(new String[0], destdir.list());
	}

	@Test
	public void should_not_create_dest_file_when_source_class_is_broken()
			throws Exception {
		File srcdir = new File(tmp.getRoot(), "src");
		srcdir.mkdir();
		File destdir = new File(tmp.getRoot(), "dest");
		destdir.mkdir();

		OutputStream out = new FileOutputStream(
				new File(srcdir, "Broken.class"));
		out.write((byte) 0xca);
		out.write((byte) 0xfe);
		out.write((byte) 0xba);
		out.write((byte) 0xbe);
		out.write((byte) 0x00);
		out.write((byte) 0x00);
		out.write((byte) 0x00);
		out.write((byte) 50);
		out.close();

		try {
			execute("instrument", "--dest", destdir.getAbsolutePath(),
					srcdir.getAbsolutePath());
			fail("exception expected");
		} catch (IOException expected) {
		}

		assertFalse(new File(destdir, "Broken.class").exists());
	}

	private void assertInstrumented(File classfile) throws IOException {
		InputStream in = new FileInputStream(classfile);
		final ClassReader reader = InstrSupport
				.classReaderFor(InputStreams.readFully(in));
		in.close();
		final Set<String> methods = new HashSet<String>();
		reader.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION) {
			@Override
			public MethodVisitor visitMethod(int access, String name,
					String descriptor, String signature, String[] exceptions) {
				methods.add(name);
				return null;
			}
		}, 0);
		assertTrue(methods.contains("$jacocoInit"));
	}

}