5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-03 19:51:41 +08:00

SQOOP-3105: Add cleanup logic for PostgreSQL related test cases

(Boglarka Egyed via Attila Szabo)
This commit is contained in:
Attila Szabo 2017-03-18 10:56:29 +01:00
parent ac5ca7c021
commit 6d8a670d32
2 changed files with 66 additions and 19 deletions

View File

@ -73,6 +73,10 @@ protected boolean useHsqldbTestServer() {
return false; return false;
} }
private String getDropTableStatement(String tableName, String schema) {
return "DROP TABLE IF EXISTS " + quoteTableOrSchemaName(schema) + "." + quoteTableOrSchemaName(tableName);
}
@Before @Before
public void setUp() { public void setUp() {
super.setUp(); super.setUp();
@ -98,6 +102,16 @@ public void setUp() {
@Override @Override
public void tearDown() { public void tearDown() {
try {
Statement stmt = connection.createStatement();
stmt.executeUpdate(getDropTableStatement(TABLE_NAME, SCHEMA_PUBLIC));
stmt.executeUpdate(getDropTableStatement(STAGING_TABLE_NAME, SCHEMA_PUBLIC));
stmt.executeUpdate(getDropTableStatement(TABLE_NAME, SCHEMA_SPECIAL));
stmt.executeUpdate(getDropTableStatement(STAGING_TABLE_NAME, SCHEMA_SPECIAL));
} catch(SQLException e) {
LOG.error("Can't clean up the database:", e);
}
super.tearDown(); super.tearDown();
try { try {
@ -150,9 +164,9 @@ public void createIt(
+ "AS $$ " + "AS $$ "
+ "BEGIN " + "BEGIN "
+ "INSERT INTO " + "INSERT INTO "
+ escapeTableOrSchemaName(SCHEMA_PUBLIC) + quoteTableOrSchemaName(SCHEMA_PUBLIC)
+ "." + "."
+ escapeTableOrSchemaName(TABLE_NAME) + quoteTableOrSchemaName(TABLE_NAME)
+ " (" + " ("
+ manager.escapeColName("id") + manager.escapeColName("id")
+", " +", "
@ -199,7 +213,7 @@ private void create(
// Create schema if not exists in dummy way (always create and ignore // Create schema if not exists in dummy way (always create and ignore
// errors. // errors.
try { try {
st.executeUpdate("CREATE SCHEMA " + escapeTableOrSchemaName(schema)); st.executeUpdate("CREATE SCHEMA " + quoteTableOrSchemaName(schema));
connection.commit(); connection.commit();
} catch (SQLException e) { } catch (SQLException e) {
LOG.info("Couldn't create schema " + schema + " (is o.k. as long as" LOG.info("Couldn't create schema " + schema + " (is o.k. as long as"
@ -207,8 +221,8 @@ private void create(
connection.rollback(); connection.rollback();
} }
String fullTableName = escapeTableOrSchemaName(schema) String fullTableName = quoteTableOrSchemaName(schema)
+ "." + escapeTableOrSchemaName(name); + "." + quoteTableOrSchemaName(name);
try { try {
// Try to remove the table first. DROP TABLE IF EXISTS didn't // Try to remove the table first. DROP TABLE IF EXISTS didn't
@ -304,7 +318,7 @@ public void testExport() throws IOException, SQLException {
runExport(getArgv(true)); runExport(getArgv(true));
assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection); assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
} }
@Test @Test
@ -316,7 +330,7 @@ public void testExportUsingProcedure() throws IOException, SQLException {
runExport(getArgv(false)); runExport(getArgv(false));
assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection); assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
} }
@Test @Test
@ -330,7 +344,7 @@ public void testExportStaging() throws IOException, SQLException {
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection); assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
} }
@Test @Test
@ -344,7 +358,7 @@ public void testExportDirect() throws IOException, SQLException {
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection); assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
} }
@Test @Test
@ -362,8 +376,8 @@ public void testExportCustomSchema() throws IOException, SQLException {
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, assertRowCount(2,
escapeTableOrSchemaName(SCHEMA_SPECIAL) quoteTableOrSchemaName(SCHEMA_SPECIAL)
+ "." + escapeTableOrSchemaName(TABLE_NAME), + "." + quoteTableOrSchemaName(TABLE_NAME),
connection); connection);
} }
@ -385,8 +399,8 @@ public void testExportCustomSchemaStaging() throws IOException, SQLException {
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, assertRowCount(2,
escapeTableOrSchemaName(SCHEMA_SPECIAL) quoteTableOrSchemaName(SCHEMA_SPECIAL)
+ "." + escapeTableOrSchemaName(TABLE_NAME), + "." + quoteTableOrSchemaName(TABLE_NAME),
connection); connection);
} }
@ -410,8 +424,8 @@ public void testExportCustomSchemaStagingClear()
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, assertRowCount(2,
escapeTableOrSchemaName(SCHEMA_SPECIAL) quoteTableOrSchemaName(SCHEMA_SPECIAL)
+ "." + escapeTableOrSchemaName(TABLE_NAME), + "." + quoteTableOrSchemaName(TABLE_NAME),
connection); connection);
} }
@ -432,8 +446,8 @@ public void testExportCustomSchemaDirect() throws IOException, SQLException {
runExport(getArgv(true, extra)); runExport(getArgv(true, extra));
assertRowCount(2, assertRowCount(2,
escapeTableOrSchemaName(SCHEMA_SPECIAL) quoteTableOrSchemaName(SCHEMA_SPECIAL)
+ "." + escapeTableOrSchemaName(TABLE_NAME), + "." + quoteTableOrSchemaName(TABLE_NAME),
connection); connection);
} }
@ -468,7 +482,7 @@ public static void assertRowCount(long expected,
} }
} }
public String escapeTableOrSchemaName(String tableName) { public String quoteTableOrSchemaName(String tableName) {
return "\"" + tableName + "\""; return "\"" + tableName + "\"";
} }
} }

View File

@ -31,6 +31,7 @@
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -109,11 +110,21 @@ public class PostgresqlImportTest extends ImportJobTestCase {
static final String SCHEMA_SPECIAL = "special"; static final String SCHEMA_SPECIAL = "special";
static final String CONNECT_STRING = HOST_URL + DATABASE_NAME; static final String CONNECT_STRING = HOST_URL + DATABASE_NAME;
protected Connection connection;
@Override @Override
protected boolean useHsqldbTestServer() { protected boolean useHsqldbTestServer() {
return false; return false;
} }
public String quoteTableOrSchemaName(String tableName) {
return "\"" + tableName + "\"";
}
private String getDropTableStatement(String tableName, String schema) {
return "DROP TABLE IF EXISTS " + quoteTableOrSchemaName(schema) + "." + quoteTableOrSchemaName(tableName);
}
@Before @Before
public void setUp() { public void setUp() {
super.setUp(); super.setUp();
@ -128,13 +139,35 @@ public void setUp() {
LOG.debug("setUp complete."); LOG.debug("setUp complete.");
} }
@After
public void tearDown() {
try {
Statement stmt = connection.createStatement();
stmt.executeUpdate(getDropTableStatement(TABLE_NAME, SCHEMA_PUBLIC));
stmt.executeUpdate(getDropTableStatement(NULL_TABLE_NAME, SCHEMA_PUBLIC));
stmt.executeUpdate(getDropTableStatement(SPECIAL_TABLE_NAME, SCHEMA_PUBLIC));
stmt.executeUpdate(getDropTableStatement(DIFFERENT_TABLE_NAME, SCHEMA_SPECIAL));
} catch (SQLException e) {
LOG.error("Can't clean up the database:", e);
}
super.tearDown();
try {
connection.close();
} catch (SQLException e) {
LOG.error("Ignoring exception in tearDown", e);
}
}
public void setUpData(String tableName, String schema, boolean nullEntry) { public void setUpData(String tableName, String schema, boolean nullEntry) {
SqoopOptions options = new SqoopOptions(CONNECT_STRING, tableName); SqoopOptions options = new SqoopOptions(CONNECT_STRING, tableName);
options.setUsername(DATABASE_USER); options.setUsername(DATABASE_USER);
options.setPassword(PASSWORD); options.setPassword(PASSWORD);
ConnManager manager = null; ConnManager manager = null;
Connection connection = null;
Statement st = null; Statement st = null;
try { try {