aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2018-08-02 21:56:26 +0200
committerEvgeny Mandrikov <Godin@users.noreply.github.com>2018-08-02 21:56:26 +0200
commitb2c5ff73d24cc0c57f308f046de008568068b4c8 (patch)
treeede5f93173e1a7f9fc7fe8300b2ba94040a8a46c /org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation
parent65d1aea1bd0f85f8be9bbc5076b7191515fe5f39 (diff)
downloadjacoco-b2c5ff73d24cc0c57f308f046de008568068b4c8.tar.gz
Reorganize validation test packages (#716)
* Use module names as package names * Merge previously separate filter validation tests * Move non-version specific tests to org.jacoco.core.test
Diffstat (limited to 'org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation')
-rw-r--r--org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/StringSwitchTest.java66
-rw-r--r--org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/TryWithResourcesTest.java214
-rw-r--r--org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/StringSwitchTarget.java96
-rw-r--r--org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/TryWithResourcesTarget.java204
4 files changed, 580 insertions, 0 deletions
diff --git a/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/StringSwitchTest.java b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/StringSwitchTest.java
new file mode 100644
index 00000000..778097e0
--- /dev/null
+++ b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/StringSwitchTest.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2018 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;
+
+import org.jacoco.core.analysis.ICounter;
+import org.jacoco.core.test.validation.ValidationTestBase;
+import org.jacoco.core.test.validation.java7.targets.StringSwitchTarget;
+import org.junit.Test;
+
+/**
+ * Test of filtering of a bytecode that is generated for a String in switch
+ * statement.
+ */
+public class StringSwitchTest extends ValidationTestBase {
+
+ public StringSwitchTest() {
+ super(StringSwitchTarget.class);
+ }
+
+ /**
+ * {@link StringSwitchTarget#covered(String)}
+ */
+ @Test
+ public void covered() {
+ if (isJDKCompiler) {
+ assertLine("covered.switch", ICounter.FULLY_COVERED, 0, 4);
+ } else {
+ assertLine("covered.switch", ICounter.PARTLY_COVERED, 2, 7);
+ }
+ assertLine("covered.case1", ICounter.FULLY_COVERED, 0, 0);
+ assertLine("covered.case2", ICounter.FULLY_COVERED, 0, 0);
+ assertLine("covered.case3", ICounter.FULLY_COVERED, 0, 0);
+ assertLine("covered.default", ICounter.FULLY_COVERED, 0, 0);
+ }
+
+ /**
+ * {@link StringSwitchTarget#notCovered(String)}
+ */
+ @Test
+ public void notCovered() {
+ assertLine("notCovered", ICounter.NOT_COVERED, isJDKCompiler ? 4 : 9,
+ 0);
+ }
+
+ /**
+ * {@link StringSwitchTarget#handwritten(String)}
+ */
+ @Test
+ public void handwritten() {
+ assertLine("handwritten.firstSwitch", ICounter.FULLY_COVERED, 2, 1);
+ assertLine("handwritten.ignored", ICounter.FULLY_COVERED);
+ assertLine("handwritten.secondSwitch", ICounter.FULLY_COVERED, 3, 1);
+ assertLine("handwritten.case1", ICounter.FULLY_COVERED);
+ assertLine("handwritten.case2", ICounter.NOT_COVERED);
+ }
+
+}
diff --git a/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/TryWithResourcesTest.java b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/TryWithResourcesTest.java
new file mode 100644
index 00000000..0056a34a
--- /dev/null
+++ b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/TryWithResourcesTest.java
@@ -0,0 +1,214 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2018 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;
+
+import org.jacoco.core.analysis.ICounter;
+import org.jacoco.core.test.validation.ValidationTestBase;
+import org.jacoco.core.test.validation.java7.targets.TryWithResourcesTarget;
+import org.junit.Test;
+
+/**
+ * Test of filtering of a bytecode that is generated for a try-with-resources
+ * statement.
+ */
+public class TryWithResourcesTest extends ValidationTestBase {
+
+ public TryWithResourcesTest() {
+ super(TryWithResourcesTarget.class);
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#test()}
+ */
+ @Test
+ public void test() {
+ assertLine("test.before", ICounter.FULLY_COVERED);
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("test.try", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("test.try", ICounter.EMPTY);
+ }
+ assertLine("test.open1", ICounter.FULLY_COVERED);
+ assertLine("test.open2", ICounter.FULLY_COVERED);
+ assertLine("test.open3", ICounter.FULLY_COVERED);
+ assertLine("test.body", ICounter.FULLY_COVERED);
+ // without filter next line has branches:
+ assertLine("test.close", ICounter.EMPTY);
+ assertLine("test.catch", ICounter.NOT_COVERED);
+ assertLine("test.finally", ICounter.FULLY_COVERED);
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#test2()}
+ */
+ @Test
+ public void test2() {
+ assertLine("test2.before", ICounter.FULLY_COVERED);
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("test2.try", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("test2.try", ICounter.EMPTY);
+ }
+ assertLine("test2.open1", ICounter.FULLY_COVERED);
+ assertLine("test2.open2", ICounter.FULLY_COVERED);
+ assertLine("test2.open3", ICounter.FULLY_COVERED);
+ assertLine("test2.body", ICounter.FULLY_COVERED);
+ // without filter next line has branches:
+ assertLine("test2.close", ICounter.EMPTY);
+ assertLine("test2.catch", ICounter.NOT_COVERED);
+ assertLine("test2.finally", ICounter.FULLY_COVERED);
+ assertLine("test2.after", ICounter.FULLY_COVERED);
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#returnInBody()}
+ */
+ @Test
+ public void returnInBody() {
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("returnInBody.try", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("returnInBody.try", ICounter.EMPTY);
+ }
+ assertLine("returnInBody.open", ICounter.FULLY_COVERED);
+
+ // without filter next line has branches:
+ if (isJDKCompiler) {
+ // https://bugs.openjdk.java.net/browse/JDK-8134759
+ // javac 7 and 8 up to 8u92 are affected
+ if (JAVA_VERSION.isBefore("1.8.0_92")) {
+ assertLine("returnInBody.close", ICounter.FULLY_COVERED, 0, 0);
+ } else {
+ assertLine("returnInBody.close", ICounter.EMPTY);
+ }
+ } else {
+ assertLine("returnInBody.close", ICounter.EMPTY);
+ }
+
+ assertLine("returnInBody.return", ICounter.FULLY_COVERED);
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#nested()}
+ */
+ @Test
+ public void nested() {
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("nested.try1", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("nested.try1", ICounter.EMPTY);
+ }
+ assertLine("nested.open1", ICounter.FULLY_COVERED);
+ assertLine("nested.catch1", ICounter.NOT_COVERED);
+
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("nested.try2", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("nested.try2", ICounter.EMPTY);
+ }
+ assertLine("nested.body", ICounter.FULLY_COVERED);
+ assertLine("nested.catch2", ICounter.NOT_COVERED);
+ assertLine("nested.finally2", ICounter.FULLY_COVERED);
+
+ // next lines not covered on exceptional path:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("nested.try3", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("nested.try3", ICounter.EMPTY);
+ }
+ assertLine("nested.open3", ICounter.FULLY_COVERED, 0, 0);
+ assertLine("nested.body3", ICounter.FULLY_COVERED, 0, 0);
+ assertLine("nested.catch3", ICounter.NOT_COVERED);
+ assertLine("nested.finally3", ICounter.FULLY_COVERED, 0, 0);
+
+ // without filter next lines have branches:
+ assertLine("nested.close3", ICounter.EMPTY);
+ assertLine("nested.close2", ICounter.EMPTY);
+ assertLine("nested.close1", ICounter.EMPTY);
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#returnInCatch()}
+ */
+ @Test
+ public void returnInCatch() {
+ // without filter next line covered partly:
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("returnInCatch.try1", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("returnInCatch.try1", ICounter.EMPTY);
+ }
+ assertLine("returnInCatch.open", ICounter.FULLY_COVERED);
+ assertLine("returnInCatch.finally1", ICounter.PARTLY_COVERED, 1, 1);
+ // without filter next line has branches:
+ assertLine("returnInCatch.close", ICounter.EMPTY);
+
+ assertLine("returnInCatch.try2", ICounter.EMPTY);
+ assertLine("returnInCatch.finally2", ICounter.PARTLY_COVERED, 1, 1);
+ }
+
+ /*
+ * Corner cases
+ */
+
+ /**
+ * {@link TryWithResourcesFilterTarget#handwritten()}
+ */
+ @Test
+ public void handwritten() {
+ if (isJDKCompiler) {
+ assertLine("handwritten", /* partly when ECJ: */ICounter.EMPTY);
+ }
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#empty()}
+ */
+ @Test
+ public void empty() {
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("empty.try", ICounter.FULLY_COVERED);
+ } else {
+ assertLine("empty.try", ICounter.EMPTY);
+ }
+ assertLine("empty.open", ICounter.FULLY_COVERED);
+ // empty when EJC:
+ if (isJDKCompiler) {
+ if (JAVA_VERSION.isBefore("9")) {
+ // branches with javac 7 and 8
+ assertLine("empty.close", ICounter.PARTLY_COVERED);
+ } else {
+ assertLine("empty.close", ICounter.FULLY_COVERED, 0, 0);
+ }
+ }
+ }
+
+ /**
+ * {@link TryWithResourcesFilterTarget#throwInBody()}
+ */
+ @Test
+ public void throwInBody() {
+ // not filtered
+ assertLine("throwInBody.try", ICounter.NOT_COVERED);
+ if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) {
+ assertLine("throwInBody.close", ICounter.NOT_COVERED);
+ } else {
+ assertLine("throwInBody.close", ICounter.EMPTY);
+ }
+ }
+
+}
diff --git a/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/StringSwitchTarget.java b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/StringSwitchTarget.java
new file mode 100644
index 00000000..8e182a4f
--- /dev/null
+++ b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/StringSwitchTarget.java
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2018 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.nop;
+
+/**
+ * This test target is a switch statement with a String.
+ */
+public class StringSwitchTarget {
+
+ private static void covered(Object s) {
+ switch (String.valueOf(s)) { // $line-covered.switch$
+ case "a":
+ nop("case a"); // $line-covered.case1$
+ break;
+ case "b":
+ nop("case b"); // $line-covered.case2$
+ break;
+ case "\0a":
+ nop("case \0a"); // $line-covered.case3$
+ break;
+ default:
+ nop("case default"); // $line-covered.default$
+ break;
+ }
+ }
+
+ private static void notCovered(Object s) {
+ switch (String.valueOf(s)) { // $line-notCovered$
+ case "a":
+ nop("case a");
+ break;
+ case "b":
+ nop("case b");
+ break;
+ case "\0a":
+ nop("case \0a");
+ break;
+ default:
+ nop("default");
+ break;
+ }
+ }
+
+ private static void handwritten(String s) {
+ int c = -1;
+ switch (s.hashCode()) { // $line-handwritten.firstSwitch$
+ case 97:
+ if ("a".equals(s)) { // $line-handwritten.ignored$
+ c = 0;
+ } else if ("\0a".equals(s)) {
+ c = 1;
+ }
+ break;
+ case 98:
+ if ("b".equals(s)) {
+ c = 2;
+ }
+ break;
+ }
+ switch (c) { // $line-handwritten.secondSwitch$
+ case 0:
+ nop("case a"); // $line-handwritten.case1$
+ break;
+ case 1:
+ nop("case \0a"); // $line-handwritten.case2$
+ break;
+ case 2:
+ nop("case b");
+ break;
+ default:
+ nop("default");
+ break;
+ }
+ }
+
+ public static void main(String[] args) {
+ covered("");
+ covered("a");
+ covered("b");
+ covered("\0a");
+
+ handwritten("a");
+ }
+
+}
diff --git a/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/TryWithResourcesTarget.java b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/TryWithResourcesTarget.java
new file mode 100644
index 00000000..08d9cdab
--- /dev/null
+++ b/org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation/java7/targets/TryWithResourcesTarget.java
@@ -0,0 +1,204 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2018 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(); // $line-test.before$
+ try ( // $line-test.try$
+ Resource r1 = new Resource(); // $line-test.open1$
+ Closeable r2 = new Resource(); // $line-test.open2$
+ AutoCloseable r3 = new Resource() // $line-test.open3$
+ ) {
+ return read(r1, r2, r3); // $line-test.body$
+ } // $line-test.close$
+ catch (Exception e) {
+ nop(); // $line-test.catch$
+ throw e;
+ } finally {
+ nop(); // $line-test.finally$
+ }
+ }
+
+ private static void test2() throws Exception {
+ nop(); // $line-test2.before$
+ try ( // $line-test2.try$
+ Resource r1 = new Resource(); // $line-test2.open1$
+ Closeable r2 = new Resource(); // $line-test2.open2$
+ AutoCloseable r3 = new Resource() // $line-test2.open3$
+ ) {
+ read(r1, r2, r3); // $line-test2.body$
+ } // $line-test2.close$
+ catch (Exception e) {
+ nop(); // $line-test2.catch$
+ } finally {
+ nop(); // $line-test2.finally$
+ }
+ nop(); // $line-test2.after$
+ }
+
+ private static Object returnInBody() throws IOException {
+ try ( // $line-returnInBody.try$
+ Closeable r = new Resource() // $line-returnInBody.open$
+ ) {
+ return read(r); // $line-returnInBody.return$
+ } // $line-returnInBody.close$
+ }
+
+ private static void nested() {
+ try ( // $line-nested.try1$
+ Resource r1 = new Resource() // $line-nested.open1$
+ ) {
+
+ try ( // $line-nested.try2$
+ Resource r2 = new Resource() // $line-nested.open2$
+ ) {
+ nop(r1.toString() + r2.toString()); // $line-nested.body$
+ } // $line-nested.close2$
+ catch (Exception e) {
+ nop(); // $line-nested.catch2$
+ } finally {
+ nop(); // $line-nested.finally2$
+ }
+
+ } // $line-nested.close1$
+ catch (Exception e) {
+ nop(); // $line-nested.catch1$
+ } finally {
+
+ try ( // $line-nested.try3$
+ Resource r2 = new Resource() // $line-nested.open3$
+ ) {
+ nop(r2); // $line-nested.body3$
+ } // $line-nested.close3$
+ catch (Exception e) {
+ nop(); // $line-nested.catch3$
+ } finally {
+ nop(); // $line-nested.finally3$
+ }
+
+ }
+ }
+
+ /**
+ * 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 ( // $line-returnInCatch.try1$
+ Resource r = new Resource() // $line-returnInCatch.open$
+ ) {
+ read(r);
+ } // $line-returnInCatch.close$
+ catch (Exception e) {
+ return null;
+ } finally {
+ nop(!f()); // $line-returnInCatch.finally1$
+ }
+
+ try { // $line-returnInCatch.try2$
+ read(new Resource());
+ } catch (Exception e) {
+ return null;
+ } finally {
+ nop(!f()); // $line-returnInCatch.finally2$
+ }
+
+ 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();
+ }
+
+ public static void main(String[] args) throws Exception {
+ test();
+ test2();
+ returnInBody();
+ nested();
+
+ returnInCatch();
+
+ empty();
+ handwritten();
+ }
+
+ /*
+ * Corner cases
+ */
+
+ private static void empty() throws Exception {
+ try ( // $line-empty.try$
+ Closeable r = new Resource() // $line-empty.open$
+ ) {
+ } // $line-empty.close$
+ }
+
+ 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) { // $line-handwritten$
+ if (primaryExc != null) {
+ try {
+ r.close();
+ } catch (Throwable suppressedExc) {
+ primaryExc.addSuppressed(suppressedExc);
+ }
+ } else {
+ r.close();
+ }
+ }
+ }
+ }
+
+ private static void throwInBody() throws IOException {
+ try ( // $line-throwInBody.try$
+ Closeable r = new Resource()) {
+ nop(r);
+ throw new RuntimeException();
+ } // $line-throwInBody.close$
+ }
+
+}