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);
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);
}
}
}


+ 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.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());
}


Loading…
Cancel
Save