diff --git a/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java b/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java index 73a4be0..1efde3d 100644 --- a/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -20,6 +20,12 @@ import static org.apache.hadoop.hive.ql.exec.ExplainTask.EXPL_COLUMN_NAME; import static org.apache.hadoop.hive.ql.processors.SetProcessor.SET_COLUMN_NAME; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.sql.Connection; import java.sql.DatabaseMetaData; @@ -36,7 +42,10 @@ import java.util.Map; import java.util.Set; -import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; @@ -45,7 +54,7 @@ * TestJdbcDriver2 * */ -public class TestJdbcDriver2 extends TestCase { +public class TestJdbcDriver2 { private static final String driverName = "org.apache.hive.jdbc.HiveDriver"; private static final String tableName = "testHiveJdbcDriver_Table"; private static final String tableComment = "Simple table"; @@ -61,10 +70,10 @@ private final Path dataFilePath; private final Path dataTypeDataFilePath; private Connection con; - private boolean standAloneServer = false; + private static boolean standAloneServer = false; + private static final float floatCompareDelta = 0.0001f; - public TestJdbcDriver2(String name) { - super(name); + public TestJdbcDriver2() { conf = new HiveConf(TestJdbcDriver2.class); String dataFileDir = conf.get("test.data.files").replace('\\', '/') .replace("c:", ""); @@ -74,23 +83,48 @@ public TestJdbcDriver2(String name) { .getProperty("test.service.standalone.server")); } - protected void setupConnection() throws SQLException { + + @BeforeClass + public static void oneTimeSetup() throws SQLException, ClassNotFoundException{ + Class.forName(driverName); + + Connection con1 = getConnection(); + DatabaseMetaData metadata = con1.getMetaData(); + + //drop databases created by other test cases + ResultSet databaseRes = metadata.getSchemas(); + Statement stmt = con1.createStatement(); + while(databaseRes.next()){ + String db = databaseRes.getString(1); + if(!db.equals("default")){ + System.err.println("Dropping database " + db); + stmt.execute("DROP DATABASE " + db + " CASCADE"); + } + } + stmt.close(); + con1.close(); + } + + + protected static Connection getConnection() throws SQLException { + Connection con1; if (standAloneServer) { // get connection - con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", ""); } else { - con = DriverManager.getConnection("jdbc:hive2://", "", ""); + con1 = DriverManager.getConnection("jdbc:hive2://", "", ""); } - assertNotNull("Connection is null", con); - assertFalse("Connection should not be closed", con.isClosed()); + assertNotNull("Connection is null", con1); + assertFalse("Connection should not be closed", con1.isClosed()); - Statement stmt = con.createStatement(); + Statement stmt = con1.createStatement(); assertNotNull("Statement is null", stmt); stmt.execute("set hive.support.concurrency = false"); stmt.close(); + return con1; } protected void resetConnection() throws SQLException { @@ -100,20 +134,17 @@ protected void resetConnection() throws SQLException { con = null; } - setupConnection(); + con = getConnection(); } catch (SQLException e) { e.printStackTrace(); throw e; } } - @Override - protected void setUp() throws Exception { - super.setUp(); - - Class.forName(driverName); + @Before + public void setUp() throws Exception { - setupConnection(); + con = getConnection(); Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -127,7 +158,6 @@ protected void setUp() throws Exception { fail(ex.toString()); } - ResultSet res; // create table stmt.execute("create table " + tableName + " (under_col int comment 'the under column', value string) comment '" @@ -193,9 +223,8 @@ protected void setUp() throws Exception { +"' as select * from "+ tableName); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); + @After + public void tearDown() throws Exception { // drop table Statement stmt = con.createStatement(); @@ -219,6 +248,7 @@ protected void tearDown() throws Exception { expectedException); } + @Test public void testDataTypes2() throws Exception { Statement stmt = con.createStatement(); @@ -234,6 +264,8 @@ public void testDataTypes2() throws Exception { } } + + @Test public void testErrorDiag() throws SQLException { Statement stmt = con.createStatement(); @@ -264,6 +296,7 @@ public void testErrorDiag() throws SQLException { * verify 'explain ...' resultset * @throws SQLException */ + @Test public void testExplainStmt() throws SQLException { Statement stmt = con.createStatement(); @@ -278,6 +311,7 @@ public void testExplainStmt() throws SQLException { assertTrue("Nothing returned explain", res.next()); } + @Test public void testPrepareStatement() { String sql = "from (select count(1) from " @@ -387,11 +421,13 @@ public void testPrepareStatement() { expectedException); } + @Test public final void testSelectAll() throws Exception { doTestSelectAll(tableName, -1, -1); // tests not setting maxRows (return all) doTestSelectAll(tableName, 0, -1); // tests setting maxRows to 0 (return all) } + @Test public final void testSelectAllPartioned() throws Exception { doTestSelectAll(partitionedTableName, -1, -1); // tests not setting maxRows // (return all) @@ -399,14 +435,17 @@ public final void testSelectAllPartioned() throws Exception { // (return all) } + @Test public final void testSelectAllMaxRows() throws Exception { doTestSelectAll(tableName, 100, -1); } + @Test public final void testSelectAllFetchSize() throws Exception { doTestSelectAll(tableName, 100, 20); } + @Test public void testDataTypes() throws Exception { Statement stmt = con.createStatement(); @@ -423,7 +462,7 @@ public void testDataTypes() throws Exception { // getXXX returns 0 for numeric types, false for boolean and null for other assertEquals(0, res.getInt(1)); assertEquals(false, res.getBoolean(2)); - assertEquals(0d, res.getDouble(3)); + assertEquals(0d, res.getDouble(3), floatCompareDelta); assertEquals(null, res.getString(4)); assertEquals(null, res.getString(5)); assertEquals(null, res.getString(6)); @@ -431,7 +470,7 @@ public void testDataTypes() throws Exception { assertEquals(null, res.getString(8)); assertEquals(0, res.getByte(9)); assertEquals(0, res.getShort(10)); - assertEquals(0f, res.getFloat(11)); + assertEquals(0f, res.getFloat(11), floatCompareDelta); assertEquals(0L, res.getLong(12)); assertEquals(null, res.getString(13)); assertEquals(null, res.getString(14)); @@ -445,7 +484,7 @@ public void testDataTypes() throws Exception { assertTrue(res.next()); assertEquals(-1, res.getInt(1)); assertEquals(false, res.getBoolean(2)); - assertEquals(-1.1d, res.getDouble(3)); + assertEquals(-1.1d, res.getDouble(3), floatCompareDelta); assertEquals("", res.getString(4)); assertEquals("[]", res.getString(5)); assertEquals("{}", res.getString(6)); @@ -453,7 +492,7 @@ public void testDataTypes() throws Exception { assertEquals("{\"r\":null,\"s\":null,\"t\":null}", res.getString(8)); assertEquals(-1, res.getByte(9)); assertEquals(-1, res.getShort(10)); - assertEquals(-1.0f, res.getFloat(11)); + assertEquals(-1.0f, res.getFloat(11), floatCompareDelta); assertEquals(-1, res.getLong(12)); assertEquals("[]", res.getString(13)); assertEquals("{}", res.getString(14)); @@ -468,7 +507,7 @@ public void testDataTypes() throws Exception { assertTrue(res.next()); assertEquals(1, res.getInt(1)); assertEquals(true, res.getBoolean(2)); - assertEquals(1.1d, res.getDouble(3)); + assertEquals(1.1d, res.getDouble(3), floatCompareDelta); assertEquals("1", res.getString(4)); assertEquals("[1,2]", res.getString(5)); assertEquals("{1:\"x\",2:\"y\"}", res.getString(6)); @@ -476,7 +515,7 @@ public void testDataTypes() throws Exception { assertEquals("{\"r\":\"a\",\"s\":9,\"t\":2.2}", res.getString(8)); assertEquals(1, res.getByte(9)); assertEquals(1, res.getShort(10)); - assertEquals(1.0f, res.getFloat(11)); + assertEquals(1.0f, res.getFloat(11), floatCompareDelta); assertEquals(1, res.getLong(12)); assertEquals("[[\"a\",\"b\"],[\"c\",\"d\"]]", res.getString(13)); assertEquals("{1:{11:12,13:14},2:{21:22}}", res.getString(14)); @@ -577,6 +616,7 @@ private void doTestSelectAll(String tableName, int maxRows, int fetchSize) throw assertTrue("Statement should be closed", stmt.isClosed()); } + @Test public void testErrorMessages() throws SQLException { String invalidSyntaxSQLState = "42000"; @@ -625,6 +665,7 @@ private void doTestErrorCase(String sql, String expectedMessage, exceptionFound); } + @Test public void testShowTables() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -643,6 +684,7 @@ public void testShowTables() throws SQLException { + " not found in SHOW TABLES result set", testTableExists); } + @Test public void testMetaDataGetTables() throws SQLException { Map tests = new HashMap(); tests.put("test%jdbc%", new Object[]{"testhivejdbcdriver_table" @@ -689,6 +731,7 @@ public void testMetaDataGetTables() throws SQLException { assertEquals("Incorrect number of views found.", 1, cnt); } + @Test public void testMetaDataGetCatalogs() throws SQLException { ResultSet rs = (ResultSet)con.getMetaData().getCatalogs(); ResultSetMetaData resMeta = rs.getMetaData(); @@ -698,6 +741,7 @@ public void testMetaDataGetCatalogs() throws SQLException { assertFalse(rs.next()); } + @Test public void testMetaDataGetSchemas() throws SQLException { ResultSet rs = (ResultSet)con.getMetaData().getSchemas(); ResultSetMetaData resMeta = rs.getMetaData(); @@ -707,12 +751,11 @@ public void testMetaDataGetSchemas() throws SQLException { assertTrue(rs.next()); assertEquals("default", rs.getString(1)); -// assertNull(rs.getString(2)); - assertFalse(rs.next()); rs.close(); } + @Test public void testMetaDataGetTableTypes() throws SQLException { ResultSet rs = (ResultSet)con.getMetaData().getTableTypes(); @@ -734,6 +777,7 @@ public void testMetaDataGetTableTypes() throws SQLException { assertTrue("Found less tabletypes then we test for.", cnt >= tabletypes.size()); } + @Test public void testMetaDataGetColumns() throws SQLException { Map tests = new HashMap(); tests.put(new String[]{"testhivejdbcdriver\\_table", null}, 2); @@ -782,6 +826,7 @@ public void testMetaDataGetColumns() throws SQLException { /** * Validate the Metadata for the result set of a metadata getColumns call. */ + @Test public void testMetaDataGetColumnsMetaData() throws SQLException { ResultSet rs = (ResultSet)con.getMetaData().getColumns(null, null , "testhivejdbcdriver\\_table", null); @@ -839,6 +884,7 @@ public boolean next() throws SQLException { } */ + @Test public void testDescribeTable() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -878,6 +924,7 @@ private boolean hasTable(String t, Statement stmt) throws SQLException { * Test if default database gets reset on connection reset * @throws SQLException */ + @Test public void testConnectionReset() throws SQLException { Statement stmt = null; @@ -915,6 +962,7 @@ public void testConnectionReset() throws SQLException { } } + @Test public void testDatabaseMetaData() throws SQLException { DatabaseMetaData meta = con.getMetaData(); @@ -929,6 +977,7 @@ public void testDatabaseMetaData() throws SQLException { assertTrue(meta.supportsAlterTableWithAddColumn()); } + @Test public void testResultSetMetaData() throws SQLException { Statement stmt = con.createStatement(); @@ -1155,6 +1204,7 @@ public void testResultSetMetaData() throws SQLException { {"jdbc:hive2://localhost/notdefault", "localhost", "10000", "notdefault"}, {"jdbc:hive2://foo:1243", "foo", "1243", "default"}}; + @Test public void testDriverProperties() throws SQLException { HiveDriver driver = new HiveDriver(); @@ -1180,6 +1230,7 @@ private static void assertDpi(DriverPropertyInfo dpi, String name, * validate schema generated by "set" command * @throws SQLException */ + @Test public void testSetCommand() throws SQLException { // execute set command String sql = "set -v"; @@ -1202,6 +1253,7 @@ public void testSetCommand() throws SQLException { * Validate error on closed resultset * @throws SQLException */ + @Test public void testPostClose() throws SQLException { Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery("select * from " + tableName); @@ -1219,6 +1271,7 @@ public void testPostClose() throws SQLException { * The JDBC spec says when you have duplicate column names, * the first one should be returned. */ + @Test public void testDuplicateColumnNameOrder() throws SQLException { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT 1 AS a, 2 AS a from " + tableName); @@ -1231,6 +1284,7 @@ public void testDuplicateColumnNameOrder() throws SQLException { * Test bad args to getXXX() * @throws SQLException */ + @Test public void testOutOfBoundCols() throws SQLException { Statement stmt = con.createStatement(); @@ -1256,6 +1310,7 @@ public void testOutOfBoundCols() throws SQLException { * Verify selecting using builtin UDFs * @throws SQLException */ + @Test public void testBuiltInUDFCol() throws SQLException { Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery("select c12, bin(c12) from " + dataTypeTableName @@ -1273,6 +1328,7 @@ public void testBuiltInUDFCol() throws SQLException { * Verify selecting named expression columns * @throws SQLException */ + @Test public void testExprCol() throws SQLException { Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery("select c1+1 as col1, length(c4) as len from " + dataTypeTableName @@ -1291,6 +1347,7 @@ public void testExprCol() throws SQLException { * test getProcedureColumns() * @throws SQLException */ + @Test public void testProcCols() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1305,6 +1362,7 @@ public void testProcCols() throws SQLException { * test testProccedures() * @throws SQLException */ + @Test public void testProccedures() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1319,6 +1377,7 @@ public void testProccedures() throws SQLException { * test getPrimaryKeys() * @throws SQLException */ + @Test public void testPrimaryKeys() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1333,6 +1392,7 @@ public void testPrimaryKeys() throws SQLException { * test getImportedKeys() * @throws SQLException */ + @Test public void testImportedKeys() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); diff --git a/ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHiveMetaStoreChecker.java b/ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHiveMetaStoreChecker.java index 6490b89..3963148 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHiveMetaStoreChecker.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHiveMetaStoreChecker.java @@ -47,8 +47,8 @@ private FileSystem fs; private HiveMetaStoreChecker checker = null; - private final String dbName = "dbname"; - private final String tableName = "tablename"; + private final String dbName = "testhivemetastorechecker_db"; + private final String tableName = "testhivemetastorechecker_table"; private final String partDateName = "partdate"; private final String partCityName = "partcity"; @@ -76,17 +76,25 @@ protected void setUp() throws Exception { part2.put(partCityName, "stockholm"); parts.add(part2); + //cleanup just in case something is left over from previous run + dropDbTable(); + } + + private void dropDbTable() { // cleanup - hive.dropTable(dbName, tableName, true, true); try { + hive.dropTable(dbName, tableName, true, true); hive.dropDatabase(dbName); } catch (NoSuchObjectException e) { // ignore + } catch (HiveException e) { + // ignore } } @Override protected void tearDown() throws Exception { + dropDbTable(); super.tearDown(); Hive.closeCurrent(); }