diff options
Diffstat (limited to 'org.jacoco.core.test.validation.java7/src/org/jacoco/core/test/validation')
4 files changed, 113 insertions, 263 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 index 04fe6f5d..f291c174 100644 --- 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 @@ -11,10 +11,9 @@ *******************************************************************************/ package org.jacoco.core.test.validation.java7; -import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.Source.Line; 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 @@ -26,41 +25,22 @@ public class StringSwitchTest extends ValidationTestBase { super(StringSwitchTarget.class); } - /** - * {@link StringSwitchTarget#covered(Object)} - */ - @Test - public void covered() { + public void assertSwitchCovered(final Line line) { if (isJDKCompiler) { - assertLine("covered.switch", ICounter.FULLY_COVERED, 0, 4); + assertFullyCovered(line, 0, 4); } else { - assertLine("covered.switch", ICounter.PARTLY_COVERED, 2, 7); + // Filtering for ECJ not yet implemented: + assertPartlyCovered(line, 2, 7); } - assertLine("covered.case1", ICounter.FULLY_COVERED); - assertLine("covered.case2", ICounter.FULLY_COVERED); - assertLine("covered.case3", ICounter.FULLY_COVERED); - assertLine("covered.default", ICounter.FULLY_COVERED); } - /** - * {@link StringSwitchTarget#notCovered(Object)} - */ - @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, 1, 1); - assertLine("handwritten.secondSwitch", ICounter.FULLY_COVERED, 3, 1); - assertLine("handwritten.case1", ICounter.FULLY_COVERED); - assertLine("handwritten.case2", ICounter.NOT_COVERED); + public void assertSwitchNotCovered(final Line line) { + if (isJDKCompiler) { + assertNotCovered(line, 4, 0); + } else { + // Filtering for ECJ not yet implemented: + assertNotCovered(line, 9, 0); + } } } 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 index 9eb02571..0078258a 100644 --- 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 @@ -11,10 +11,9 @@ *******************************************************************************/ package org.jacoco.core.test.validation.java7; -import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.Source.Line; 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 @@ -26,192 +25,60 @@ public class TryWithResourcesTest extends ValidationTestBase { super(TryWithResourcesTarget.class); } - /** - * {@link TryWithResourcesTarget#test()} - */ - @Test - public void test() { - assertLine("test.before", ICounter.FULLY_COVERED); - // without filter next line covered partly: + public void assertTry(final Line line) { + // without filter this line is covered partly: if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) { - assertLine("test.try", ICounter.FULLY_COVERED); + assertFullyCovered(line); } else { - assertLine("test.try", ICounter.EMPTY); + assertEmpty(line); } - 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 TryWithResourcesTarget#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 TryWithResourcesTarget#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); - + public void assertReturnInBodyClose(final Line line) { // 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); + assertFullyCovered(line); } else { - assertLine("returnInBody.close", ICounter.EMPTY); + assertEmpty(line); } } else { - assertLine("returnInBody.close", ICounter.EMPTY); + assertEmpty(line); } - - assertLine("returnInBody.return", ICounter.FULLY_COVERED); } - /** - * {@link TryWithResourcesTarget#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); - assertLine("nested.body3", ICounter.FULLY_COVERED); - assertLine("nested.catch3", ICounter.NOT_COVERED); - assertLine("nested.finally3", ICounter.FULLY_COVERED); - - // without filter next lines have branches: - assertLine("nested.close3", ICounter.EMPTY); - assertLine("nested.close2", ICounter.EMPTY); - assertLine("nested.close1", ICounter.EMPTY); - } - - /** - * {@link TryWithResourcesTarget#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 TryWithResourcesTarget#handwritten()} - */ - @Test - public void handwritten() { + public void assertHandwritten(final Line line) { if (isJDKCompiler) { - assertLine("handwritten", /* partly when ECJ: */ICounter.EMPTY); + assertEmpty(line); + } else { + assertFullyCovered(line, 1, 1); } } - /** - * {@link TryWithResourcesTarget#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); + public void assertEmptyClose(final Line line) { if (!isJDKCompiler) { - assertLine("empty.close", ICounter.PARTLY_COVERED, 7, 1); + assertPartlyCovered(line, 7, 1); } else if (JAVA_VERSION.isBefore("8")) { - assertLine("empty.close", ICounter.PARTLY_COVERED, 6, 2); + assertPartlyCovered(line, 6, 2); } else if (JAVA_VERSION.isBefore("9")) { - assertLine("empty.close", ICounter.PARTLY_COVERED, 2, 2); + assertPartlyCovered(line, 2, 2); } else { - assertLine("empty.close", ICounter.FULLY_COVERED); + assertFullyCovered(line); } } - /** - * {@link TryWithResourcesTarget#throwInBody()} - */ - @Test - public void throwInBody() { + public void assertThrowInBodyClose(final Line line) { // not filtered - assertLine("throwInBody.try", ICounter.NOT_COVERED); - if (!isJDKCompiler){ - assertLine("throwInBody.close", ICounter.NOT_COVERED, 6, 0); + if (!isJDKCompiler) { + assertNotCovered(line, 6, 0); } else if (JAVA_VERSION.isBefore("9")) { - assertLine("throwInBody.close", ICounter.NOT_COVERED, 4, 0); + assertNotCovered(line, 4, 0); } else if (JAVA_VERSION.isBefore("11")) { - assertLine("throwInBody.close", ICounter.NOT_COVERED); + assertNotCovered(line); } else { - assertLine("throwInBody.close", ICounter.EMPTY); + assertEmpty(line); } } 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 index 8e182a4f..cdbf9f51 100644 --- 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 @@ -19,24 +19,24 @@ import static org.jacoco.core.test.validation.targets.Stubs.nop; public class StringSwitchTarget { private static void covered(Object s) { - switch (String.valueOf(s)) { // $line-covered.switch$ + switch (String.valueOf(s)) { // assertSwitchCovered() case "a": - nop("case a"); // $line-covered.case1$ + nop("case a"); // assertFullyCovered() break; case "b": - nop("case b"); // $line-covered.case2$ + nop("case b"); // assertFullyCovered() break; case "\0a": - nop("case \0a"); // $line-covered.case3$ + nop("case \0a"); // assertFullyCovered() break; default: - nop("case default"); // $line-covered.default$ + nop("case default"); // assertFullyCovered() break; } } private static void notCovered(Object s) { - switch (String.valueOf(s)) { // $line-notCovered$ + switch (String.valueOf(s)) { // assertSwitchNotCovered() case "a": nop("case a"); break; @@ -54,9 +54,9 @@ public class StringSwitchTarget { private static void handwritten(String s) { int c = -1; - switch (s.hashCode()) { // $line-handwritten.firstSwitch$ + switch (s.hashCode()) { // assertFullyCovered(2, 1) case 97: - if ("a".equals(s)) { // $line-handwritten.ignored$ + if ("a".equals(s)) { // assertFullyCovered(1, 1) c = 0; } else if ("\0a".equals(s)) { c = 1; @@ -68,12 +68,12 @@ public class StringSwitchTarget { } break; } - switch (c) { // $line-handwritten.secondSwitch$ + switch (c) { // assertFullyCovered(3, 1) case 0: - nop("case a"); // $line-handwritten.case1$ + nop("case a"); // assertFullyCovered() break; case 1: - nop("case \0a"); // $line-handwritten.case2$ + nop("case \0a"); // assertNotCovered() break; case 2: nop("case b"); 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 index 08d9cdab..cdb5b06d 100644 --- 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 @@ -34,77 +34,79 @@ public class TryWithResourcesTarget { * 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$ + nop(); // assertFullyCovered() + try ( // assertTry() + Resource r1 = new Resource(); // assertFullyCovered() + Closeable r2 = new Resource(); // assertFullyCovered() + AutoCloseable r3 = new Resource() // assertFullyCovered() ) { - return read(r1, r2, r3); // $line-test.body$ - } // $line-test.close$ + return read(r1, r2, r3); // assertFullyCovered() + /* without filter next line has branches: */ + } // assertEmpty() catch (Exception e) { - nop(); // $line-test.catch$ + nop(); // assertNotCovered() throw e; } finally { - nop(); // $line-test.finally$ + nop(); // assertFullyCovered() } } 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$ + nop(); // assertFullyCovered() + try ( // assertTry() + Resource r1 = new Resource(); // assertFullyCovered() + Closeable r2 = new Resource(); // assertFullyCovered() + AutoCloseable r3 = new Resource() // assertFullyCovered() ) { - read(r1, r2, r3); // $line-test2.body$ - } // $line-test2.close$ + read(r1, r2, r3); // assertFullyCovered() + /* without filter next line has branches: */ + } // assertEmpty() catch (Exception e) { - nop(); // $line-test2.catch$ + nop(); // assertNotCovered() } finally { - nop(); // $line-test2.finally$ + nop(); // assertFullyCovered() } - nop(); // $line-test2.after$ + nop(); // assertFullyCovered() } private static Object returnInBody() throws IOException { - try ( // $line-returnInBody.try$ - Closeable r = new Resource() // $line-returnInBody.open$ + try ( // assertTry() + Closeable r = new Resource() // assertFullyCovered() ) { - return read(r); // $line-returnInBody.return$ - } // $line-returnInBody.close$ + return read(r); // assertFullyCovered() + } // assertReturnInBodyClose() } private static void nested() { - try ( // $line-nested.try1$ - Resource r1 = new Resource() // $line-nested.open1$ + try ( // assertTry() + Resource r1 = new Resource() // assertFullyCovered() ) { - try ( // $line-nested.try2$ - Resource r2 = new Resource() // $line-nested.open2$ + try ( // assertTry() + Resource r2 = new Resource() // assertFullyCovered() ) { - nop(r1.toString() + r2.toString()); // $line-nested.body$ - } // $line-nested.close2$ + nop(r1.toString() + r2.toString()); // assertFullyCovered() + } // assertEmpty() catch (Exception e) { - nop(); // $line-nested.catch2$ + nop(); // assertNotCovered() } finally { - nop(); // $line-nested.finally2$ + nop(); // assertFullyCovered() } - } // $line-nested.close1$ + } // assertEmpty() catch (Exception e) { - nop(); // $line-nested.catch1$ + nop(); // assertNotCovered() } finally { - try ( // $line-nested.try3$ - Resource r2 = new Resource() // $line-nested.open3$ + try ( // assertTry() + Resource r2 = new Resource() // assertFullyCovered() ) { - nop(r2); // $line-nested.body3$ - } // $line-nested.close3$ + nop(r2); // assertFullyCovered() + } // assertEmpty() catch (Exception e) { - nop(); // $line-nested.catch3$ + nop(); // assertNotCovered() } finally { - nop(); // $line-nested.finally3$ + nop(); // assertFullyCovered() } } @@ -117,23 +119,24 @@ public class TryWithResourcesTarget { * happens without it. */ private static Object returnInCatch() { - try ( // $line-returnInCatch.try1$ - Resource r = new Resource() // $line-returnInCatch.open$ + try ( // assertTry() + Resource r = new Resource() // assertFullyCovered() ) { read(r); - } // $line-returnInCatch.close$ + /* without filter next line has branches: */ + } // assertEmpty() catch (Exception e) { return null; } finally { - nop(!f()); // $line-returnInCatch.finally1$ + nop(!f()); // assertPartlyCovered(1, 1) } - try { // $line-returnInCatch.try2$ + try { // assertEmpty() read(new Resource()); } catch (Exception e) { return null; } finally { - nop(!f()); // $line-returnInCatch.finally2$ + nop(!f()); // assertPartlyCovered(1, 1) } return null; @@ -147,27 +150,15 @@ public class TryWithResourcesTarget { 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$ + try ( // assertTry() + Closeable r = new Resource() // assertFullyCovered() ) { - } // $line-empty.close$ + } // assertEmptyClose() } private static void handwritten() throws IOException { @@ -179,7 +170,7 @@ public class TryWithResourcesTarget { primaryExc = t; throw t; } finally { - if (r != null) { // $line-handwritten$ + if (r != null) { // assertHandwritten() if (primaryExc != null) { try { r.close(); @@ -194,11 +185,23 @@ public class TryWithResourcesTarget { } private static void throwInBody() throws IOException { - try ( // $line-throwInBody.try$ + try ( // assertNotCovered() Closeable r = new Resource()) { nop(r); throw new RuntimeException(); - } // $line-throwInBody.close$ + } // assertThrowInBodyClose() + } + + public static void main(String[] args) throws Exception { + test(); + test2(); + returnInBody(); + nested(); + + returnInCatch(); + + empty(); + handwritten(); } } |