diff --git a/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java b/jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java index ddfc087..e667aa6 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; @@ -39,8 +45,6 @@ import java.util.Set; import java.util.regex.Pattern; -import junit.framework.TestCase; - import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.TableType; @@ -50,13 +54,17 @@ import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; import org.apache.hive.service.cli.operation.HiveTableTypeMapping; import org.apache.hive.service.cli.operation.TableTypeMappingFactory.TableTypeMappings; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /** * 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"; @@ -72,10 +80,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:", ""); @@ -85,19 +93,36 @@ public TestJdbcDriver2(String name) { .getProperty("test.service.standalone.server")); } - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeClass + public static void setUpBeforeClass() throws SQLException, ClassNotFoundException{ Class.forName(driverName); - if (standAloneServer) { - // get connection - con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", - "", ""); - } else { - con = DriverManager.getConnection("jdbc:hive2://", "", ""); + Connection con1 = getConnection(); + + Statement stmt1 = con1.createStatement(); + assertNotNull("Statement is null", stmt1); + + stmt1.execute("set hive.support.concurrency = false"); + + DatabaseMetaData metadata = con1.getMetaData(); + + // Drop databases created by other test cases + ResultSet databaseRes = metadata.getSchemas(); + + while(databaseRes.next()){ + String db = databaseRes.getString(1); + if(!db.equals("default")){ + System.err.println("Dropping database " + db); + stmt1.execute("DROP DATABASE " + db + " CASCADE"); + } } - assertNotNull("Connection is null", con); - assertFalse("Connection should not be closed", con.isClosed()); + stmt1.close(); + con1.close(); + } + + @Before + public void setUp() throws Exception { + con = getConnection(); + Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -110,7 +135,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 '" @@ -179,10 +203,23 @@ protected void setUp() throws Exception { +"' as select * from "+ tableName); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); + private static Connection getConnection() throws SQLException { + Connection con1; + if (standAloneServer) { + // get connection + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", + "", ""); + } else { + con1 = DriverManager.getConnection("jdbc:hive2://", "", ""); + } + assertNotNull("Connection is null", con1); + assertFalse("Connection should not be closed", con1.isClosed()); + return con1; + } + + @After + public void tearDown() throws Exception { // drop table Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -205,6 +242,7 @@ protected void tearDown() throws Exception { expectedException); } + @Test public void testBadURL() throws Exception { checkBadUrl("jdbc:hive2://localhost:10000;principal=test"); checkBadUrl("jdbc:hive2://localhost:10000;" + @@ -223,6 +261,7 @@ private void checkBadUrl(String url) throws SQLException { } } + @Test public void testDataTypes2() throws Exception { Statement stmt = con.createStatement(); @@ -238,6 +277,8 @@ public void testDataTypes2() throws Exception { } } + + @Test public void testErrorDiag() throws SQLException { Statement stmt = con.createStatement(); @@ -268,6 +309,7 @@ public void testErrorDiag() throws SQLException { * verify 'explain ...' resultset * @throws SQLException */ + @Test public void testExplainStmt() throws SQLException { Statement stmt = con.createStatement(); @@ -276,12 +318,15 @@ public void testExplainStmt() throws SQLException { "c1*2, sentences(null, null, null) as b from " + dataTypeTableName + " limit 1"); ResultSetMetaData md = res.getMetaData(); - assertEquals(md.getColumnCount(), 1); // only one result column - assertEquals(md.getColumnLabel(1), EXPL_COLUMN_NAME); // verify the column name + // only one result column + assertEquals(md.getColumnCount(), 1); + // verify the column name + assertEquals(md.getColumnLabel(1), EXPL_COLUMN_NAME); //verify that there is data in the resultset assertTrue("Nothing returned explain", res.next()); } + @Test public void testPrepareStatement() { String sql = "from (select count(1) from " @@ -398,6 +443,7 @@ public void testPrepareStatement() { * of PreparedStatement interface * @throws Exception */ + @Test public void testExecutePreparedStatement() throws Exception { String key = "testKey"; String val1 = "val1"; @@ -433,11 +479,13 @@ private void verifyConfValue(String key, String expectedVal) throws Exception { assertEquals("Conf value should be set by execute()", expectedVal, result); } + @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) @@ -445,14 +493,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 testNullType() throws Exception { Statement stmt = con.createStatement(); try { @@ -466,6 +517,7 @@ public void testNullType() throws Exception { // executeQuery should always throw a SQLException, // when it executes a non-ResultSet query (like create) + @Test public void testExecuteQueryException() throws Exception { Statement stmt = con.createStatement(); try { @@ -506,6 +558,7 @@ private void failWithExceptionMsg(Exception e) { fail(e.toString()); } + @Test public void testNullResultSet() throws Exception { List setupQueries = new ArrayList(); String testQuery; @@ -541,6 +594,7 @@ public void testNullResultSet() throws Exception { stmt.close(); } + @Test public void testCloseResultSet() throws Exception { Statement stmt = con.createStatement(); @@ -574,6 +628,7 @@ public void testCloseResultSet() throws Exception { assertTrue(stmt.isClosed()); } + @Test public void testDataTypes() throws Exception { Statement stmt = con.createStatement(); @@ -590,7 +645,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)); @@ -598,7 +653,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)); @@ -615,7 +670,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)); @@ -623,7 +678,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)); @@ -641,7 +696,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)); @@ -649,7 +704,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)); @@ -759,6 +814,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"; @@ -807,6 +863,7 @@ private void doTestErrorCase(String sql, String expectedMessage, exceptionFound); } + @Test public void testShowTables() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -825,10 +882,12 @@ public void testShowTables() throws SQLException { + " not found in SHOW TABLES result set", testTableExists); } + @Test public void testMetaDataGetTables() throws SQLException { getTablesTest(ClassicTableTypes.TABLE.toString(), ClassicTableTypes.VIEW.toString()); } + @Test public void testMetaDataGetTablesHive() throws SQLException { Statement stmt = con.createStatement(); stmt.execute("set " + HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING.varname + @@ -836,6 +895,7 @@ public void testMetaDataGetTablesHive() throws SQLException { getTablesTest(TableType.MANAGED_TABLE.toString(), TableType.VIRTUAL_VIEW.toString()); } + @Test public void testMetaDataGetTablesClassic() throws SQLException { Statement stmt = con.createStatement(); stmt.execute("set " + HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING.varname + @@ -907,6 +967,7 @@ private void getTablesTest(String tableTypeName, String viewTypeName) throws SQL assertEquals("Incorrect number of views found.", 1, cnt); } + @Test public void testMetaDataGetCatalogs() throws SQLException { ResultSet rs = (ResultSet)con.getMetaData().getCatalogs(); ResultSetMetaData resMeta = rs.getMetaData(); @@ -916,6 +977,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(); @@ -925,20 +987,21 @@ public void testMetaDataGetSchemas() throws SQLException { assertTrue(rs.next()); assertEquals("default", rs.getString(1)); - // assertNull(rs.getString(2)); assertFalse(rs.next()); rs.close(); } - //test default table types returned in + // test default table types returned in // Connection.getMetaData().getTableTypes() + @Test public void testMetaDataGetTableTypes() throws SQLException { metaDataGetTableTypeTest(new ClassicTableTypeMapping().getTableTypeNames()); } - //test default table types returned in + // test default table types returned in // Connection.getMetaData().getTableTypes() when type config is set to "HIVE" + @Test public void testMetaDataGetHiveTableTypes() throws SQLException { Statement stmt = con.createStatement(); stmt.execute("set " + HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING.varname + @@ -947,8 +1010,9 @@ public void testMetaDataGetHiveTableTypes() throws SQLException { metaDataGetTableTypeTest(new HiveTableTypeMapping().getTableTypeNames()); } - //test default table types returned in + // test default table types returned in // Connection.getMetaData().getTableTypes() when type config is set to "CLASSIC" + @Test public void testMetaDataGetClassicTableTypes() throws SQLException { Statement stmt = con.createStatement(); stmt.execute("set " + HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING.varname + @@ -979,6 +1043,7 @@ private void metaDataGetTableTypeTest(Set tabletypes) 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); @@ -1027,6 +1092,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); @@ -1084,6 +1150,7 @@ public boolean next() throws SQLException { } */ + @Test public void testDescribeTable() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); @@ -1102,6 +1169,7 @@ public void testDescribeTable() throws SQLException { assertFalse("More results found than expected", res.next()); } + @Test public void testDatabaseMetaData() throws SQLException { DatabaseMetaData meta = con.getMetaData(); @@ -1120,12 +1188,13 @@ public void testDatabaseMetaData() throws SQLException { assertTrue(meta.supportsAlterTableWithAddColumn()); } + @Test public void testResultSetMetaData() throws SQLException { Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery( "select c1, c2, c3, c4, c5 as a, c6, c7, c8, c9, c10, c11, c12, " + - "c1*2, sentences(null, null, null) as b, c17, c18, c20, c21 from " + dataTypeTableName + + "c1*2, sentences(null, null, null) as b, c17, c18, c20, c21 from " + dataTypeTableName + " limit 1"); ResultSetMetaData meta = res.getMetaData(); @@ -1370,6 +1439,7 @@ public void testResultSetMetaData() throws SQLException { "server", "10002", "db"}, }; + @Test public void testDriverProperties() throws SQLException { HiveDriver driver = new HiveDriver(); for (String[] testValues : URL_PROPERTIES) { @@ -1386,12 +1456,13 @@ public void testDriverProperties() throws SQLException { "user=foo;password=bar?" + "hive.server2.transport.mode=http;" + "hive.server2.thrift.http.path=hs2", "server", "10002", "db", "http", "hs2"}, - {"jdbc:hive2://server:10000/testdb;" + - "user=foo;password=bar?" + - "hive.server2.transport.mode=binary;" + - "hive.server2.thrift.http.path=", "server", "10000", "testdb", "binary", ""}, + {"jdbc:hive2://server:10000/testdb;" + + "user=foo;password=bar?" + + "hive.server2.transport.mode=binary;" + + "hive.server2.thrift.http.path=", "server", "10000", "testdb", "binary", ""}, }; + @Test public void testParseUrlHttpMode() throws SQLException { HiveDriver driver = new HiveDriver(); for (String[] testValues : HTTP_URL_PROPERTIES) { @@ -1416,6 +1487,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"; @@ -1438,6 +1510,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); @@ -1455,6 +1528,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); @@ -1467,6 +1541,7 @@ public void testDuplicateColumnNameOrder() throws SQLException { * Test bad args to getXXX() * @throws SQLException */ + @Test public void testOutOfBoundCols() throws SQLException { Statement stmt = con.createStatement(); @@ -1492,6 +1567,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 @@ -1509,6 +1585,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 @@ -1527,6 +1604,7 @@ public void testExprCol() throws SQLException { * test getProcedureColumns() * @throws SQLException */ + @Test public void testProcCols() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1541,6 +1619,7 @@ public void testProcCols() throws SQLException { * test testProccedures() * @throws SQLException */ + @Test public void testProccedures() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1555,6 +1634,7 @@ public void testProccedures() throws SQLException { * test getPrimaryKeys() * @throws SQLException */ + @Test public void testPrimaryKeys() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1569,6 +1649,7 @@ public void testPrimaryKeys() throws SQLException { * test getImportedKeys() * @throws SQLException */ + @Test public void testImportedKeys() throws SQLException { DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1583,12 +1664,10 @@ public void testImportedKeys() throws SQLException { * If the Driver implementation understands the URL, it will return a Connection object; * otherwise it returns null */ + @Test public void testInvalidURL() throws Exception { HiveDriver driver = new HiveDriver(); Connection conn = driver.connect("jdbc:derby://localhost:10000/default", new Properties()); assertNull(conn); } - - - } 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..69d1896 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 @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -47,8 +48,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 +77,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.dropDatabase(dbName); + hive.dropTable(dbName, tableName, true, true); + hive.dropDatabase(dbName, true, true, true); } catch (NoSuchObjectException e) { // ignore + } catch (HiveException e) { + // ignore } } @Override protected void tearDown() throws Exception { + dropDbTable(); super.tearDown(); Hive.closeCurrent(); } @@ -97,19 +106,19 @@ public void testTableCheck() throws HiveException, MetaException, CheckResult result = new CheckResult(); checker.checkMetastore(dbName, null, null, result); // we haven't added anything so should return an all ok - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // check table only, should not exist in ms result = new CheckResult(); checker.checkMetastore(dbName, tableName, null, result); assertEquals(1, result.getTablesNotInMs().size()); assertEquals(tableName, result.getTablesNotInMs().get(0)); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); Database db = new Database(); db.setName(dbName); @@ -125,18 +134,18 @@ public void testTableCheck() throws HiveException, MetaException, // first check all (1) tables result = new CheckResult(); checker.checkMetastore(dbName, null, null, result); - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // then let's check the one we know about result = new CheckResult(); checker.checkMetastore(dbName, tableName, null, result); - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // remove the table folder fs = table.getPath().getFileSystem(hive.getConf()); @@ -145,26 +154,27 @@ public void testTableCheck() throws HiveException, MetaException, // now this shouldn't find the path on the fs result = new CheckResult(); checker.checkMetastore(dbName, tableName, null, result); - assertTrue(result.getTablesNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs());; assertEquals(1, result.getTablesNotOnFs().size()); assertEquals(tableName, result.getTablesNotOnFs().get(0)); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // put it back and one additional table fs.mkdirs(table.getPath()); Path fakeTable = table.getPath().getParent().suffix( Path.SEPARATOR + "faketable"); fs.mkdirs(fakeTable); + fs.deleteOnExit(fakeTable); // find the extra table result = new CheckResult(); checker.checkMetastore(dbName, null, null, result); assertEquals(1, result.getTablesNotInMs().size()); assertEquals(fakeTable.getName(), result.getTablesNotInMs().get(0)); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // create a new external table hive.dropTable(dbName, tableName); @@ -174,10 +184,10 @@ public void testTableCheck() throws HiveException, MetaException, // should return all ok result = new CheckResult(); checker.checkMetastore(dbName, null, null, result); - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); } public void testPartitionsCheck() throws HiveException, MetaException, @@ -203,10 +213,10 @@ public void testPartitionsCheck() throws HiveException, MetaException, CheckResult result = new CheckResult(); checker.checkMetastore(dbName, tableName, null, result); // all is well - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); List partitions = hive.getPartitions(table); assertEquals(2, partitions.size()); @@ -218,24 +228,24 @@ public void testPartitionsCheck() throws HiveException, MetaException, result = new CheckResult(); checker.checkMetastore(dbName, tableName, null, result); // missing one partition on fs - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); assertEquals(1, result.getPartitionsNotOnFs().size()); assertEquals(partToRemove.getName(), result.getPartitionsNotOnFs().get(0) .getPartitionName()); assertEquals(partToRemove.getTable().getTableName(), result .getPartitionsNotOnFs().get(0).getTableName()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); List> partsCopy = new ArrayList>(); partsCopy.add(partitions.get(1).getSpec()); // check only the partition that exists, all should be well result = new CheckResult(); checker.checkMetastore(dbName, tableName, partsCopy, result); - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotInMs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); // put the other one back fs.mkdirs(partToRemovePath); @@ -244,15 +254,60 @@ public void testPartitionsCheck() throws HiveException, MetaException, Path fakePart = new Path(table.getDataLocation().toString(), "fakepartition=fakevalue"); fs.mkdirs(fakePart); + fs.deleteOnExit(fakePart); checker.checkMetastore(dbName, tableName, null, result); // one extra partition - assertTrue(result.getTablesNotInMs().isEmpty()); - assertTrue(result.getTablesNotOnFs().isEmpty()); - assertTrue(result.getPartitionsNotOnFs().isEmpty()); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); assertEquals(1, result.getPartitionsNotInMs().size()); assertEquals(fakePart.getName(), result.getPartitionsNotInMs().get(0) .getPartitionName()); + + // cleanup + hive.dropTable(dbName, tableName, true, true); + hive.createTable(table); + result = new CheckResult(); + checker.checkMetastore(dbName, null, null, result); + assertEquals(Collections.emptyList(), result.getTablesNotInMs()); + assertEquals(Collections.emptyList(), result.getTablesNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotOnFs()); + assertEquals(Collections.emptyList(), result.getPartitionsNotInMs()); //--0e + System.err.println("Test completed - partition check"); + } + public void testDataDeletion() throws HiveException, MetaException, + IOException, TException, AlreadyExistsException, NoSuchObjectException { + + Database db = new Database(); + db.setName(dbName); + hive.createDatabase(db); + + Table table = new Table(dbName, tableName); + table.setDbName(dbName); + table.setInputFormatClass(TextInputFormat.class); + table.setOutputFormatClass(HiveIgnoreKeyTextOutputFormat.class); + table.setPartCols(partCols); + + hive.createTable(table); + table = hive.getTable(dbName, tableName); + + Path fakeTable = table.getPath().getParent().suffix( + Path.SEPARATOR + "faketable"); + fs = fakeTable.getFileSystem(hive.getConf()); + fs.mkdirs(fakeTable); + fs.deleteOnExit(fakeTable); + + Path fakePart = new Path(table.getDataLocation().toString(), + "fakepartition=fakevalue"); + fs.mkdirs(fakePart); + fs.deleteOnExit(fakePart); + + hive.dropTable(dbName, tableName, true, true); + assertFalse(fs.exists(fakePart)); + hive.dropDatabase(dbName); + assertFalse(fs.exists(fakeTable)); + } }