From 8dd66745779a9a1f45a56ec1f34a752f30930067 Mon Sep 17 00:00:00 2001 From: Hideki Itakura Date: Mon, 2 Apr 2018 14:44:12 -0700 Subject: [PATCH] Fixed #1691 - Database.getPath has return type as File which is different from other platforms (#1692) --- .../java/com/couchbase/lite/DatabaseTest.java | 22 +++++++++---------- .../com/couchbase/lite/AbstractDatabase.java | 13 +++++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/DatabaseTest.java b/android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/DatabaseTest.java index 5bdabd14a..8c48329f5 100644 --- a/android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/DatabaseTest.java +++ b/android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/DatabaseTest.java @@ -57,7 +57,7 @@ private Database openDatabase(String dbName, boolean countCheck) throws Couchbas config.setDirectory(getDir().getAbsolutePath()); Database db = new Database(dbName, config); assertEquals(dbName, db.getName()); - assertTrue(db.getPath().getAbsolutePath().endsWith(".cblite2")); + assertTrue(new File(db.getPath()).getAbsolutePath().endsWith(".cblite2")); if (countCheck) assertEquals(0, db.getCount()); return db; @@ -65,7 +65,7 @@ private Database openDatabase(String dbName, boolean countCheck) throws Couchbas // helper method to delete database void deleteDatabase(Database db) throws CouchbaseLiteException { - File path = db.getPath(); + File path = db.getPath() != null ? new File(db.getPath()) : null; // if path is null, db is already closed if (path != null) assertTrue(path.exists()); @@ -193,7 +193,7 @@ public void testDatabaseConfigurationWithAndroidContect() throws CouchbaseLiteEx Database db = new Database("db", config); try { String expectedPath = context.getFilesDir().getAbsolutePath(); - assertTrue(db.getPath().getAbsolutePath().contains(expectedPath)); + assertTrue(new File(db.getPath()).getAbsolutePath().contains(expectedPath)); } finally { db.delete(); } @@ -271,8 +271,8 @@ public void testCreateWithCustomDirectory() throws CouchbaseLiteException { try { assertNotNull(db); assertEquals(dbName, db.getName()); - assertTrue(db.getPath().getAbsolutePath().endsWith(".cblite2")); - assertTrue(db.getPath().getAbsolutePath().indexOf(dir.getPath()) != -1); + assertTrue(new File(db.getPath()).getAbsolutePath().endsWith(".cblite2")); + assertTrue(new File(db.getPath()).getAbsolutePath().indexOf(dir.getPath()) != -1); assertTrue(Database.exists(dbName, dir)); assertEquals(0, db.getCount()); } finally { @@ -941,7 +941,7 @@ public void testDelete() throws CouchbaseLiteException { @Test public void testDeleteTwice() throws CouchbaseLiteException { // delete db twice - File path = db.getPath(); + File path = new File(db.getPath()); assertTrue(path.exists()); db.delete(); try { @@ -1051,7 +1051,7 @@ public void testDeleteWithDefaultDirDB() throws CouchbaseLiteException { String dbName = "db"; try { Database database = open(dbName); - File path = database.getPath(); + File path = new File(database.getPath()); assertNotNull(path); assertTrue(path.exists()); // close db before delete @@ -1077,7 +1077,7 @@ public void testDeleteOpeningDBWithDefaultDir() throws CouchbaseLiteException { // create db with custom directory Database db = openDatabase(dbName); try { - File path = db.getPath(); + File path = new File(db.getPath()); assertNotNull(path); assertTrue(path.exists()); @@ -1101,7 +1101,7 @@ public void testDeleteByStaticMethod() throws CouchbaseLiteException { // create db with custom directory Database db = openDatabase(dbName); - File path = db.getPath(); + File path = new File(db.getPath()); // close db before delete db.close(); @@ -1175,7 +1175,7 @@ public void testDatabaseExistsWithDir() throws CouchbaseLiteException { // create db with custom directory Database db = openDatabase("db"); - File path = db.getPath(); + File path = new File(db.getPath()); assertTrue(Database.exists("db", getDir())); @@ -1322,7 +1322,7 @@ public void testCopy() throws CouchbaseLiteException { Database.delete(dbName, dir); // Copy: - Database.copy(db.getPath(), dbName, config); + Database.copy(new File(db.getPath()), dbName, config); // Verify: assertTrue(Database.exists(dbName, dir)); diff --git a/shared/src/main/java/com/couchbase/lite/AbstractDatabase.java b/shared/src/main/java/com/couchbase/lite/AbstractDatabase.java index 10eb2bf86..3f50e4150 100644 --- a/shared/src/main/java/com/couchbase/lite/AbstractDatabase.java +++ b/shared/src/main/java/com/couchbase/lite/AbstractDatabase.java @@ -159,9 +159,9 @@ public String getName() { * * @return the database's path. */ - public File getPath() { + public String getPath() { synchronized (lock) { - return c4db != null ? new File(getC4Database().getPath()) : null; + return c4db != null ? getC4Database().getPath() : null; } } @@ -660,8 +660,8 @@ Object getLock() { boolean equalsWithPath(Database other) { if (other == null) return false; - File path = getPath(); - File otherPath = other.getPath(); + File path = getFilePath(); + File otherPath = other.getFilePath(); if (path == null && otherPath == null) return true; else if ((path == null && otherPath != null) || (path != null && otherPath == null)) @@ -779,6 +779,11 @@ ScheduledExecutorService getQueryExecutor() { return queryExecutor; } + File getFilePath() { + String path = getPath(); + return path != null ? new File(path) : null; + } + //--------------------------------------------- // Private (in class only) //---------------------------------------------