Bug 2194752

Fix some test errors in android.database cts tests..

Some of the tests cases explicitly orphan some SQLiteStatements
that have been compiled.  When the tests are cleaned up, closing the
database would throw a SQLiteException that there are some unfinalized
statements.

Now the the tests that leave these orphaned statements explicitly
close the database, and eat that particular exception.  The database
file will be unlinked, and a new one is created for the next test,
so if the file is not close cleanly, this shoudln't be a problem

Ideally, SQLiteDatabase.removeSQLiteClosable() would be accessible by
unit tests, to clean up better, but this would either require the
package that the unit tests belong in to change or to make that
method public
diff --git a/tests/tests/database/src/android/database/sqlite/cts/SQLiteProgramTest.java b/tests/tests/database/src/android/database/sqlite/cts/SQLiteProgramTest.java
index d253a3d..c4868a1 100644
--- a/tests/tests/database/src/android/database/sqlite/cts/SQLiteProgramTest.java
+++ b/tests/tests/database/src/android/database/sqlite/cts/SQLiteProgramTest.java
@@ -73,7 +73,7 @@
         statementOne.close();
         statementTwo.close();
     }
-    
+
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         notes = "Test onAllReferencesReleased(). set compiled-sql-cache size in SQLiteDatabase " +
@@ -92,7 +92,7 @@
         assertTrue(statementOne.getUniqueId() == 0);
         statementOne.close();
     }
-    
+
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         notes = "Test onAllReferencesReleasedFromContainer(). set compiled-sql-cache size in " +
@@ -132,8 +132,12 @@
         statementOne.releaseReference();
         assertTrue(statementOne.getUniqueId() == nStatement);
         statementOne.close();
+
+        // Close the database here, because this test creates an
+        // orphaned statement
+        closeDatabaseWithOrphanedStatement();
     }
-    
+
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         notes = "Test onAllReferencesReleasedFromContainer(). set compiled-sql-cache size in " +
@@ -154,8 +158,12 @@
         statementOne.releaseReferenceFromContainer();
         assertTrue(statementOne.getUniqueId() == nStatement);
         statementOne.close();
+
+        // Close the database here, because this test creates an
+        // orphaned statement
+        closeDatabaseWithOrphanedStatement();
     }
-    
+
     @TestTargets({
         @TestTargetNew(
             level = TestLevel.COMPLETE,
@@ -377,4 +385,16 @@
     public void testProtectedMethods() {
         // cannot test
     }
+
+    private void closeDatabaseWithOrphanedStatement(){
+        try {
+            mDatabase.close();
+        } catch (SQLiteException e) {
+            // A SQLiteException is thrown if there are some unfinialized exceptions
+            // This is expected as some tests explicitly leave statements in this state
+            if (!e.getMessage().equals("Unable to close due to unfinalised statements")) {
+                throw e;
+            }
+        }
+    }
 }