1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Evgeny Mandrikov - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.core.test.filter; 13 14 import org.jacoco.core.analysis.ICounter; 15 import org.jacoco.core.test.filter.targets.TryWithResources; 16 import org.jacoco.core.test.validation.ValidationTestBase; 17 import org.junit.Test; 18 19 import java.util.regex.Matcher; 20 import java.util.regex.Pattern; 21 22 /** 23 * Test of filtering of a bytecode that is generated for a try-with-resources 24 * statement. 25 */ 26 public class TryWithResourcesTest extends ValidationTestBase { 27 28 public TryWithResourcesTest() { 29 super("src-java7", TryWithResources.class); 30 } 31 32 /** 33 * {@link TryWithResources#test()} 34 */ 35 @Test 36 public void test() { 37 assertLine("test.before", ICounter.FULLY_COVERED); 38 // without filter next line covered partly: 39 assertLine("test.try", ICounter.FULLY_COVERED); 40 assertLine("test.open1", ICounter.FULLY_COVERED); 41 assertLine("test.open2", ICounter.FULLY_COVERED); 42 assertLine("test.open3", ICounter.FULLY_COVERED); 43 assertLine("test.body", ICounter.FULLY_COVERED); 44 // without filter next line has branches: 45 assertLine("test.close", ICounter.EMPTY); 46 assertLine("test.catch", ICounter.NOT_COVERED); 47 assertLine("test.finally", ICounter.FULLY_COVERED); 48 } 49 50 /** 51 * {@link TryWithResources#test2()} 52 */ 53 @Test 54 public void test2() { 55 assertLine("test2.before", ICounter.FULLY_COVERED); 56 // without filter next line covered partly: 57 assertLine("test2.try", ICounter.FULLY_COVERED); 58 assertLine("test2.open1", ICounter.FULLY_COVERED); 59 assertLine("test2.open2", ICounter.FULLY_COVERED); 60 assertLine("test2.open3", ICounter.FULLY_COVERED); 61 assertLine("test2.body", ICounter.FULLY_COVERED); 62 // without filter next line has branches: 63 assertLine("test2.close", ICounter.EMPTY); 64 assertLine("test2.catch", ICounter.NOT_COVERED); 65 assertLine("test2.finally", ICounter.FULLY_COVERED); 66 assertLine("test2.after", ICounter.FULLY_COVERED); 67 } 68 69 /** 70 * {@link TryWithResources#returnInBody()} 71 */ 72 @Test 73 public void returnInBody() { 74 // without filter next line covered partly: 75 assertLine("returnInBody.try", ICounter.FULLY_COVERED); 76 assertLine("returnInBody.open", ICounter.FULLY_COVERED); 77 78 // without filter next line has branches: 79 if (isJDKCompiler) { 80 // https://bugs.openjdk.java.net/browse/JDK-8134759 81 // javac 7 and 8 up to 8u92 are affected 82 final String jdkVersion = System.getProperty("java.version"); 83 final Matcher m = Pattern.compile("1\\.8\\.0_(\\d++)(-ea)?") 84 .matcher(jdkVersion); 85 if (jdkVersion.startsWith("1.7.0_") 86 || (m.matches() && Integer.parseInt(m.group(1)) < 92)) { 87 assertLine("returnInBody.close", ICounter.FULLY_COVERED, 0, 0); 88 } else { 89 assertLine("returnInBody.close", ICounter.EMPTY); 90 } 91 } else { 92 assertLine("returnInBody.close", ICounter.EMPTY); 93 } 94 95 assertLine("returnInBody.return", ICounter.FULLY_COVERED); 96 } 97 98 /** 99 * {@link TryWithResources#nested()} 100 */ 101 @Test 102 public void nested() { 103 // without filter next line covered partly: 104 assertLine("nested.try1", ICounter.FULLY_COVERED); 105 assertLine("nested.open1", ICounter.FULLY_COVERED); 106 assertLine("nested.catch1", ICounter.NOT_COVERED); 107 108 // without filter next line covered partly: 109 assertLine("nested.try2", ICounter.FULLY_COVERED); 110 assertLine("nested.body", ICounter.FULLY_COVERED); 111 assertLine("nested.catch2", ICounter.NOT_COVERED); 112 assertLine("nested.finally2", ICounter.FULLY_COVERED); 113 114 // next lines not covered on exceptional path: 115 assertLine("nested.try3", ICounter.FULLY_COVERED, 0, 0); 116 assertLine("nested.open3", ICounter.FULLY_COVERED, 0, 0); 117 assertLine("nested.body3", ICounter.FULLY_COVERED, 0, 0); 118 assertLine("nested.catch3", ICounter.NOT_COVERED); 119 assertLine("nested.finally3", ICounter.FULLY_COVERED, 0, 0); 120 121 // without filter next lines have branches: 122 assertLine("nested.close3", ICounter.EMPTY); 123 assertLine("nested.close2", ICounter.EMPTY); 124 assertLine("nested.close1", ICounter.EMPTY); 125 } 126 127 /** 128 * {@link TryWithResources#returnInCatch()} 129 */ 130 @Test 131 public void returnInCatch() { 132 // without filter next line covered partly: 133 assertLine("returnInCatch.try1", ICounter.FULLY_COVERED); 134 assertLine("returnInCatch.open", ICounter.FULLY_COVERED); 135 assertLine("returnInCatch.finally1", ICounter.PARTLY_COVERED, 1, 1); 136 // without filter next line has branches: 137 assertLine("returnInCatch.close", ICounter.EMPTY); 138 139 assertLine("returnInCatch.try2", ICounter.EMPTY); 140 assertLine("returnInCatch.finally2", ICounter.PARTLY_COVERED, 1, 1); 141 } 142 143 /* 144 * Corner cases 145 */ 146 147 /** 148 * {@link TryWithResources#handwritten()} 149 */ 150 @Test 151 public void handwritten() { 152 if (isJDKCompiler) { 153 assertLine("handwritten", /* partly when ECJ: */ICounter.EMPTY); 154 } 155 } 156 157 /** 158 * {@link TryWithResources#empty()} 159 */ 160 @Test 161 public void empty() { 162 assertLine("empty.try", ICounter.FULLY_COVERED, 0, 0); 163 assertLine("empty.open", ICounter.FULLY_COVERED); 164 // empty when EJC: 165 if (isJDKCompiler) { 166 final String jdkVersion = System.getProperty("java.version"); 167 if (jdkVersion.startsWith("9") || jdkVersion.startsWith("10")) { 168 assertLine("empty.close", ICounter.FULLY_COVERED, 0, 0); 169 } else { 170 // branches with javac 7 and 8 171 assertLine("empty.close", ICounter.PARTLY_COVERED); 172 } 173 } 174 } 175 176 /** 177 * {@link TryWithResources#throwInBody()} 178 */ 179 @Test 180 public void throwInBody() { 181 // not filtered 182 assertLine("throwInBody.try", ICounter.NOT_COVERED); 183 assertLine("throwInBody.close", ICounter.NOT_COVERED); 184 } 185 186 } 187