aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/TryWithResourcesTarget.java
blob: 061db23284300f9d9be658335dd2d87a821d5bb0 (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
/*******************************************************************************
 * Copyright (c) 2009, 2019 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.core.test.validation.java7.targets;

import static org.jacoco.core.test.validation.targets.Stubs.f;
import static org.jacoco.core.test.validation.targets.Stubs.nop;

import java.io.Closeable;
import java.io.IOException;

/**
 * This test target is a try-with-resources statement.
 */
public class TryWithResourcesTarget {

	private static class Resource implements Closeable {
		@Override
		public void close() {
		}
	}

	/**
	 * Closing performed using {@link org.objectweb.asm.Opcodes#INVOKEVIRTUAL}
	 * or {@link org.objectweb.asm.Opcodes#INVOKEINTERFACE} depending on a class
	 * of resource.
	 */
	private static Object test() throws Exception {
		nop(); // assertFullyCovered()
		try ( // assertTry()
				Resource r1 = new Resource(); // assertFullyCovered()
				Closeable r2 = new Resource(); // assertFullyCovered()
				AutoCloseable r3 = new Resource() // assertFullyCovered()
		) {
			return read(r1, r2, r3); // assertFullyCovered()
			/* without filter next line has branches: */
		} // assertEmpty()
		catch (Exception e) {
			nop(); // assertNotCovered()
			throw e;
		} finally {
			nop(); // assertFullyCovered()
		}
	}

	private static void test2() throws Exception {
		nop(); // assertFullyCovered()
		try ( // assertTry()
				Resource r1 = new Resource(); // assertFullyCovered()
				Closeable r2 = new Resource(); // assertFullyCovered()
				AutoCloseable r3 = new Resource() // assertFullyCovered()
		) {
			read(r1, r2, r3); // assertFullyCovered()
			/* without filter next line has branches: */
		} // assertEmpty()
		catch (Exception e) {
			nop(); // assertNotCovered()
		} finally {
			nop(); // assertFullyCovered()
		}
		nop(); // assertFullyCovered()
	}

	private static Object returnInBody() throws IOException {
		try ( // assertTry()
				Closeable r = new Resource() // assertFullyCovered()
		) {
			return read(r); // assertFullyCovered()
		} // assertReturnInBodyClose()
	}

	private static void nested() {
		try ( // assertTry()
				Resource r1 = new Resource() // assertFullyCovered()
		) {

			try ( // assertTry()
					Resource r2 = new Resource() // assertFullyCovered()
			) {
				nop(r1.toString() + r2.toString()); // assertFullyCovered()
			} // assertEmpty()
			catch (Exception e) {
				nop(); // assertNotCovered()
			} finally {
				nop(); // assertFullyCovered()
			}

		} // assertEmpty()
		catch (Exception e) {
			nop(); // assertNotCovered()
		} finally {

			try ( // assertTry()
					Resource r2 = new Resource() // assertFullyCovered()
			) {
				nop(r2); // assertFullyCovered()
			} // assertEmpty()
			catch (Exception e) {
				nop(); // assertNotCovered()
			} finally {
				nop(); // assertFullyCovered()
			}

		}
	}

	/**
	 * In this case bytecode will contain 3 copies of <code>finally</code>
	 * block, each containing 2 branches, resulting in 6 branches in total. One
	 * could think that this is artifact of try-with-resources, but the same
	 * happens without it.
	 */
	private static Object returnInCatch() {
		try ( // assertTry()
				Resource r = new Resource() // assertFullyCovered()
		) {
			read(r);
			/* without filter next line has branches: */
		} // assertEmpty()
		catch (Exception e) {
			return null;
		} finally {
			nop(!f()); // assertPartlyCovered(1, 1)
		}

		try { // assertEmpty()
			read(new Resource());
		} catch (Exception e) {
			return null;
		} finally {
			nop(!f()); // assertPartlyCovered(1, 1)
		}

		return null;
	}

	private static Object read(Object r1, Object r2, Object r3) {
		return r1.toString() + r2.toString() + r3.toString();
	}

	private static Object read(Object r1) {
		return r1.toString();
	}

	/*
	 * Corner cases
	 */

	private static void empty() throws Exception {
		try ( // assertTry()
				Closeable r = new Resource() // assertFullyCovered()
		) {
		} // assertEmptyClose()
	}

	private static void handwritten() throws IOException {
		Closeable r = new Resource();
		Throwable primaryExc = null;
		try {
			nop(r);
		} catch (Throwable t) {
			primaryExc = t;
			throw t;
		} finally {
			if (r != null) { // assertHandwritten()
				if (primaryExc != null) {
					try {
						r.close();
					} catch (Throwable suppressedExc) {
						primaryExc.addSuppressed(suppressedExc);
					}
				} else {
					r.close();
				}
			}
		}
	}

	private static void throwInBody() throws IOException {
		try ( // assertNotCovered()
				Closeable r = new Resource()) {
			nop(r);
			throw new RuntimeException();
		} // assertThrowInBodyClose()
	}

	public static void main(String[] args) throws Exception {
		test();
		test2();
		returnInBody();
		nested();

		returnInCatch();

		empty();
		handwritten();
	}

}