sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mau...@apache.org
Subject sqoop git commit: SQOOP-3105: Add cleanup logic for PostgreSQL related test cases
Date Sat, 18 Mar 2017 09:59:21 GMT
Repository: sqoop
Updated Branches:
  refs/heads/trunk ac5ca7c02 -> 6d8a670d3


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

(Boglarka Egyed via Attila Szabo)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/6d8a670d
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/6d8a670d
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/6d8a670d

Branch: refs/heads/trunk
Commit: 6d8a670d3246ad1d1ab95720f26297d2b4e52d27
Parents: ac5ca7c
Author: Attila Szabo <maugli@apache.org>
Authored: Sat Mar 18 10:56:29 2017 +0100
Committer: Attila Szabo <maugli@apache.org>
Committed: Sat Mar 18 10:56:29 2017 +0100

----------------------------------------------------------------------
 .../sqoop/manager/PostgresqlExportTest.java     | 50 +++++++++++++-------
 .../sqoop/manager/PostgresqlImportTest.java     | 35 +++++++++++++-
 2 files changed, 66 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/6d8a670d/src/test/com/cloudera/sqoop/manager/PostgresqlExportTest.java
----------------------------------------------------------------------
diff --git a/src/test/com/cloudera/sqoop/manager/PostgresqlExportTest.java b/src/test/com/cloudera/sqoop/manager/PostgresqlExportTest.java
index ed5917f..38d6ba6 100644
--- a/src/test/com/cloudera/sqoop/manager/PostgresqlExportTest.java
+++ b/src/test/com/cloudera/sqoop/manager/PostgresqlExportTest.java
@@ -73,6 +73,10 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     return false;
   }
 
+  private String getDropTableStatement(String tableName, String schema) {
+    return "DROP TABLE IF EXISTS " + quoteTableOrSchemaName(schema) + "." + quoteTableOrSchemaName(tableName);
+  }
+
   @Before
   public void setUp() {
     super.setUp();
@@ -98,6 +102,16 @@ public class PostgresqlExportTest extends ExportJobTestCase {
 
   @Override
   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();
 
     try {
@@ -150,9 +164,9 @@ public class PostgresqlExportTest extends ExportJobTestCase {
           + "AS $$ "
           + "BEGIN "
           + "INSERT INTO "
-          + escapeTableOrSchemaName(SCHEMA_PUBLIC)
+          + quoteTableOrSchemaName(SCHEMA_PUBLIC)
           + "."
-          + escapeTableOrSchemaName(TABLE_NAME)
+          + quoteTableOrSchemaName(TABLE_NAME)
           + " ("
           + manager.escapeColName("id")
           +", "
@@ -199,7 +213,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
       // Create schema if not exists in dummy way (always create and ignore
       // errors.
       try {
-        st.executeUpdate("CREATE SCHEMA " + escapeTableOrSchemaName(schema));
+        st.executeUpdate("CREATE SCHEMA " + quoteTableOrSchemaName(schema));
         connection.commit();
       } catch (SQLException e) {
         LOG.info("Couldn't create schema " + schema + " (is o.k. as long as"
@@ -207,8 +221,8 @@ public class PostgresqlExportTest extends ExportJobTestCase {
         connection.rollback();
       }
 
-      String fullTableName = escapeTableOrSchemaName(schema)
-        + "." + escapeTableOrSchemaName(name);
+      String fullTableName = quoteTableOrSchemaName(schema)
+        + "." + quoteTableOrSchemaName(name);
 
       try {
         // Try to remove the table first. DROP TABLE IF EXISTS didn't
@@ -304,7 +318,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
 
     runExport(getArgv(true));
 
-    assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection);
+    assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
   }
 
   @Test
@@ -316,7 +330,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
 
     runExport(getArgv(false));
 
-    assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection);
+    assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
   }
 
   @Test
@@ -330,7 +344,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
 
     runExport(getArgv(true, extra));
 
-    assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection);
+    assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
   }
 
   @Test
@@ -344,7 +358,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
 
     runExport(getArgv(true, extra));
 
-    assertRowCount(2, escapeTableOrSchemaName(TABLE_NAME), connection);
+    assertRowCount(2, quoteTableOrSchemaName(TABLE_NAME), connection);
   }
 
   @Test
@@ -362,8 +376,8 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     runExport(getArgv(true, extra));
 
     assertRowCount(2,
-      escapeTableOrSchemaName(SCHEMA_SPECIAL)
-        + "." + escapeTableOrSchemaName(TABLE_NAME),
+      quoteTableOrSchemaName(SCHEMA_SPECIAL)
+        + "." + quoteTableOrSchemaName(TABLE_NAME),
       connection);
   }
 
@@ -385,8 +399,8 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     runExport(getArgv(true, extra));
 
     assertRowCount(2,
-      escapeTableOrSchemaName(SCHEMA_SPECIAL)
-        + "." + escapeTableOrSchemaName(TABLE_NAME),
+      quoteTableOrSchemaName(SCHEMA_SPECIAL)
+        + "." + quoteTableOrSchemaName(TABLE_NAME),
       connection);
   }
 
@@ -410,8 +424,8 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     runExport(getArgv(true, extra));
 
     assertRowCount(2,
-      escapeTableOrSchemaName(SCHEMA_SPECIAL)
-        + "." + escapeTableOrSchemaName(TABLE_NAME),
+      quoteTableOrSchemaName(SCHEMA_SPECIAL)
+        + "." + quoteTableOrSchemaName(TABLE_NAME),
       connection);
   }
 
@@ -432,8 +446,8 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     runExport(getArgv(true, extra));
 
     assertRowCount(2,
-      escapeTableOrSchemaName(SCHEMA_SPECIAL)
-        + "." + escapeTableOrSchemaName(TABLE_NAME),
+      quoteTableOrSchemaName(SCHEMA_SPECIAL)
+        + "." + quoteTableOrSchemaName(TABLE_NAME),
       connection);
   }
 
@@ -468,7 +482,7 @@ public class PostgresqlExportTest extends ExportJobTestCase {
     }
   }
 
-  public String escapeTableOrSchemaName(String tableName) {
+  public String quoteTableOrSchemaName(String tableName) {
     return "\"" + tableName + "\"";
   }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/6d8a670d/src/test/com/cloudera/sqoop/manager/PostgresqlImportTest.java
----------------------------------------------------------------------
diff --git a/src/test/com/cloudera/sqoop/manager/PostgresqlImportTest.java b/src/test/com/cloudera/sqoop/manager/PostgresqlImportTest.java
index 70ee640..5872d25 100644
--- a/src/test/com/cloudera/sqoop/manager/PostgresqlImportTest.java
+++ b/src/test/com/cloudera/sqoop/manager/PostgresqlImportTest.java
@@ -31,6 +31,7 @@ import java.util.Arrays;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -109,11 +110,21 @@ public class PostgresqlImportTest extends ImportJobTestCase {
   static final String SCHEMA_SPECIAL = "special";
   static final String CONNECT_STRING = HOST_URL + DATABASE_NAME;
 
+  protected Connection connection;
+
   @Override
   protected boolean useHsqldbTestServer() {
     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
   public void setUp() {
     super.setUp();
@@ -128,13 +139,35 @@ public class PostgresqlImportTest extends ImportJobTestCase {
     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) {
     SqoopOptions options = new SqoopOptions(CONNECT_STRING, tableName);
     options.setUsername(DATABASE_USER);
     options.setPassword(PASSWORD);
 
     ConnManager manager = null;
-    Connection connection = null;
     Statement st = null;
 
     try {


Mime
View raw message