[CALCITE-1067] Test failures due to clashing temporary table names
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/4ec47270 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/4ec47270 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/4ec47270 Branch: refs/heads/master Commit: 4ec47270b199cef58c39f6bf07209c5fb40875ef Parents: 82b5860 Author: Julian Hyde <[email protected]> Authored: Mon Jan 25 18:08:52 2016 -0800 Committer: Julian Hyde <[email protected]> Committed: Tue Jan 26 11:53:33 2016 -0800 ---------------------------------------------------------------------- .../calcite/avatica/RemoteDriverTest.java | 23 ++++++++++++-------- .../calcite/avatica/remote/RemoteMetaTest.java | 15 ++++++++----- .../apache/calcite/avatica/AvaticaUtils.java | 18 +++++++++++++++ .../calcite/avatica/test/AvaticaUtilsTest.java | 22 +++++++++++++++++++ 4 files changed, 63 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/4ec47270/avatica-server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java ---------------------------------------------------------------------- diff --git a/avatica-server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java b/avatica-server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java index 7e50dee..228ba8d 100644 --- a/avatica-server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java +++ b/avatica-server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java @@ -346,10 +346,12 @@ public class RemoteDriverTest { } @Test public void testInsertDrop() throws Exception { - final String create = "create table if not exists TEST_TABLE2 (" - + "id int not null, " - + "msg varchar(3) not null)"; - final String insert = "insert into TEST_TABLE2 values(1, 'foo')"; + final String t = AvaticaUtils.unique("TEST_TABLE2"); + final String create = + String.format("create table if not exists %s (" + + "id int not null, " + + "msg varchar(3) not null)", t); + final String insert = String.format("insert into %s values(1, 'foo')", t); Connection connection = ljs(); Statement statement = connection.createStatement(); statement.execute(create); @@ -540,12 +542,15 @@ public class RemoteDriverTest { @Test public void testCreateInsertUpdateDrop() throws Exception { ConnectionSpec.getDatabaseLock().lock(); - final String drop = "drop table TEST_TABLE if exists"; - final String create = "create table TEST_TABLE(" + final String t = AvaticaUtils.unique("TEST_TABLE"); + final String drop = String.format("drop table %s if exists", t); + final String create = String.format("create table %s(" + "id int not null, " - + "msg varchar(3) not null)"; - final String insert = "insert into TEST_TABLE values(1, 'foo')"; - final String update = "update TEST_TABLE set msg='bar' where id=1"; + + "msg varchar(3) not null)", + t); + final String insert = String.format("insert into %s values(1, 'foo')", t); + final String update = + String.format("update %s set msg='bar' where id=1", t); try (Connection connection = getLocalConnection(); Statement statement = connection.createStatement(); PreparedStatement pstmt = connection.prepareStatement("values 1")) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4ec47270/avatica-server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java ---------------------------------------------------------------------- diff --git a/avatica-server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java b/avatica-server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java index 49a88e7..ff95a45 100644 --- a/avatica-server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java +++ b/avatica-server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java @@ -19,6 +19,7 @@ package org.apache.calcite.avatica.remote; import org.apache.calcite.avatica.AvaticaConnection; import org.apache.calcite.avatica.AvaticaSqlException; import org.apache.calcite.avatica.AvaticaStatement; +import org.apache.calcite.avatica.AvaticaUtils; import org.apache.calcite.avatica.ConnectionPropertiesImpl; import org.apache.calcite.avatica.ConnectionSpec; import org.apache.calcite.avatica.Meta; @@ -262,17 +263,19 @@ public class RemoteMetaTest { } @Test public void testRemoteStatementInsert() throws Exception { - System.out.println(url); + final String t = AvaticaUtils.unique("TEST_TABLE2"); AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url); Statement statement = conn.createStatement(); - int status = statement.executeUpdate( - "create table if not exists " - + "TEST_TABLE2 (id int not null, msg varchar(255) not null)"); + final String create = + String.format("create table if not exists %s (" + + " id int not null, msg varchar(255) not null)", t); + int status = statement.executeUpdate(create); assertEquals(status, 0); statement = conn.createStatement(); - status = statement.executeUpdate("insert into TEST_TABLE2 values (" - + "'" + RANDOM.nextInt(Integer.MAX_VALUE) + "', '" + UUID.randomUUID() + "')"); + final String update = String.format("insert into %s values ('%d', '%s')", + t, RANDOM.nextInt(Integer.MAX_VALUE), UUID.randomUUID()); + status = statement.executeUpdate(update); assertEquals(status, 1); } http://git-wip-us.apache.org/repos/asf/calcite/blob/4ec47270/avatica/src/main/java/org/apache/calcite/avatica/AvaticaUtils.java ---------------------------------------------------------------------- diff --git a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaUtils.java b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaUtils.java index 6dd076b..9382f87 100644 --- a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaUtils.java +++ b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaUtils.java @@ -27,8 +27,10 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.AbstractList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** Avatica utilities. */ public class AvaticaUtils { @@ -41,6 +43,8 @@ public class AvaticaUtils { private static final MethodHandle GET_LARGE_UPDATE_COUNT = method(void.class, Statement.class, "getLargeUpdateCount"); + private static final Set<String> UNIQUE_STRINGS = new HashSet<>(); + private AvaticaUtils() {} static { @@ -277,6 +281,20 @@ public class AvaticaUtils { } return statement.getUpdateCount(); } + + /** Generates a string that is unique in the execution of the JVM. + * It is used by tests to ensure that they create distinct temporary tables. + * The strings are never thrown away, so don't put too much in there! + * Thread safe. */ + public static String unique(String base) { + synchronized (UNIQUE_STRINGS) { + String s = base; + while (!UNIQUE_STRINGS.add(s)) { + s = base + "_" + UNIQUE_STRINGS.size(); + } + return s; + } + } } // End AvaticaUtils.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4ec47270/avatica/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java ---------------------------------------------------------------------- diff --git a/avatica/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java b/avatica/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java index ca548a6..850cbfd 100644 --- a/avatica/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java +++ b/avatica/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java @@ -21,6 +21,8 @@ import org.apache.calcite.avatica.AvaticaUtils; import org.junit.Test; import java.math.BigInteger; +import java.util.LinkedHashSet; +import java.util.Set; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -59,6 +61,26 @@ public class AvaticaUtilsTest { is("Property 'java.math.BigInteger.ONE' not valid for plugin type java.math.BigInteger")); } } + + /** Unit test for + * {@link org.apache.calcite.avatica.AvaticaUtils#unique(java.lang.String)}. */ + @Test public void testUnique() { + // Below, the "probably" comments indicate the strings that will be + // generated the first time you run the test. + final Set<String> list = new LinkedHashSet<>(); + list.add(AvaticaUtils.unique("a")); // probably "a" + assertThat(list.size(), is(1)); + list.add(AvaticaUtils.unique("a")); // probably "a_1" + assertThat(list.size(), is(2)); + list.add(AvaticaUtils.unique("b")); // probably "b" + assertThat(list.size(), is(3)); + list.add(AvaticaUtils.unique("a_1")); // probably "a_1_3" + assertThat(list.size(), is(4)); + list.add(AvaticaUtils.unique("A")); // probably "A" + assertThat(list.size(), is(5)); + list.add(AvaticaUtils.unique("a")); // probably "a_5" + assertThat(list.size(), is(6)); + } } // End AvaticaUtilsTest.java
