From fc4fc40d7fe3b7c33010b4b64cf235710506ddd2 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Tue, 24 Apr 2018 06:37:12 +0200 Subject: [PATCH] Add canaries in order to avoid false positives --- .../apache/tools/ant/AntClassLoaderTest.java | 7 +++++++ .../apache/tools/ant/taskdefs/SQLExecTest.java | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java b/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java index bd9144ad9..9adde4a89 100644 --- a/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java +++ b/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java @@ -101,21 +101,28 @@ public class AntClassLoaderTest { thrown.expect(ClassNotFoundException.class); Path path = new Path(buildRule.getProject(), "."); loader = buildRule.getProject().createClassLoader(path); + boolean canary = false; try { // we don't expect to find this loader.findClass("fubar"); + canary = true; } finally { + assertFalse("Nonexistent class found", canary); loader.cleanup(); try { // we don't expect to find this loader.findClass("fubar"); + canary = true; } finally { + assertFalse("Nonexistent class found", canary); // tell the build it is finished buildRule.getProject().fireBuildFinished(null); try { // we don't expect to find this loader.findClass("fubar"); + canary = true; } finally { + assertFalse("Nonexistent class found", canary); } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java index b0dc876e1..5067c658f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java @@ -34,10 +34,12 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; /** * Simple testcase to test for driver caching. @@ -77,23 +79,28 @@ public class SQLExecTest { public void testDriverCaching() { thrown.expect(BuildException.class); thrown.expectMessage("No suitable Driver"); + boolean canary = false; SQLExec sql = createTask(getProperties(NULL)); - assertFalse(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); + assertThat(SQLExec.getLoaderMap(), not(hasKey(NULL_DRIVER))); try { sql.execute(); + canary = true; } finally { - assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); + assertFalse("Found some Driver", canary); + assertThat(SQLExec.getLoaderMap(), hasKey(NULL_DRIVER)); assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); ClassLoader loader1 = sql.getLoader(); // 2nd run.. sql = createTask(getProperties(NULL)); // the driver must still be cached. - assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); + assertThat(JDBCTask.getLoaderMap(), hasKey(NULL_DRIVER)); try { sql.execute(); + canary = true; } finally { - assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); + assertFalse("Found some Driver", canary); + assertThat(JDBCTask.getLoaderMap(), hasKey(NULL_DRIVER)); assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); assertSame(loader1, sql.getLoader()); }