Browse Source

Add canaries in order to avoid false positives

master
Gintas Grigelionis 7 years ago
parent
commit
fc4fc40d7f
2 changed files with 19 additions and 5 deletions
  1. +7
    -0
      src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java
  2. +12
    -5
      src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java

+ 7
- 0
src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java View File

@@ -101,21 +101,28 @@ public class AntClassLoaderTest {
thrown.expect(ClassNotFoundException.class); thrown.expect(ClassNotFoundException.class);
Path path = new Path(buildRule.getProject(), "."); Path path = new Path(buildRule.getProject(), ".");
loader = buildRule.getProject().createClassLoader(path); loader = buildRule.getProject().createClassLoader(path);
boolean canary = false;
try { try {
// we don't expect to find this // we don't expect to find this
loader.findClass("fubar"); loader.findClass("fubar");
canary = true;
} finally { } finally {
assertFalse("Nonexistent class found", canary);
loader.cleanup(); loader.cleanup();
try { try {
// we don't expect to find this // we don't expect to find this
loader.findClass("fubar"); loader.findClass("fubar");
canary = true;
} finally { } finally {
assertFalse("Nonexistent class found", canary);
// tell the build it is finished // tell the build it is finished
buildRule.getProject().fireBuildFinished(null); buildRule.getProject().fireBuildFinished(null);
try { try {
// we don't expect to find this // we don't expect to find this
loader.findClass("fubar"); loader.findClass("fubar");
canary = true;
} finally { } finally {
assertFalse("Nonexistent class found", canary);
} }
} }
} }


+ 12
- 5
src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java View File

@@ -34,10 +34,12 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame; 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. * Simple testcase to test for driver caching.
@@ -77,23 +79,28 @@ public class SQLExecTest {
public void testDriverCaching() { public void testDriverCaching() {
thrown.expect(BuildException.class); thrown.expect(BuildException.class);
thrown.expectMessage("No suitable Driver"); thrown.expectMessage("No suitable Driver");
boolean canary = false;
SQLExec sql = createTask(getProperties(NULL)); SQLExec sql = createTask(getProperties(NULL));
assertFalse(SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
assertThat(SQLExec.getLoaderMap(), not(hasKey(NULL_DRIVER)));
try { try {
sql.execute(); sql.execute();
canary = true;
} finally { } 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)); assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));
ClassLoader loader1 = sql.getLoader(); ClassLoader loader1 = sql.getLoader();


// 2nd run.. // 2nd run..
sql = createTask(getProperties(NULL)); sql = createTask(getProperties(NULL));
// the driver must still be cached. // the driver must still be cached.
assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER));
assertThat(JDBCTask.getLoaderMap(), hasKey(NULL_DRIVER));
try { try {
sql.execute(); sql.execute();
canary = true;
} finally { } 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(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));
assertSame(loader1, sql.getLoader()); assertSame(loader1, sql.getLoader());
} }


Loading…
Cancel
Save