Skip to content

Commit 5f1a270

Browse files
Isira-Seneviratnetheimpulson
authored andcommitted
Fix database import
1 parent aba2a38 commit 5f1a270

4 files changed

Lines changed: 19 additions & 20 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public void onAttach(@NonNull final Context context) {
6767
@Override
6868
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
6969
@Nullable final String rootKey) {
70-
final var dbDir = requireContext().getDatabasePath(BackupFileLocator.FILE_NAME_DB).toPath()
71-
.getParent();
72-
manager = new ImportExportManager(new BackupFileLocator(dbDir));
70+
manager = new ImportExportManager(new BackupFileLocator(requireContext()));
7371

7472
importExportDataPathKey = getString(R.string.import_export_data_path);
7573

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.schabi.newpipe.settings.export
22

3+
import android.content.Context
34
import java.nio.file.Path
45
import kotlin.io.path.div
56

67
/**
78
* Locates specific files of NewPipe based on the home directory of the app.
89
*/
9-
class BackupFileLocator(homeDir: Path) {
10+
class BackupFileLocator(context: Context) {
1011
companion object {
1112
const val FILE_NAME_DB = "newpipe.db"
1213

@@ -18,9 +19,8 @@ class BackupFileLocator(homeDir: Path) {
1819
const val FILE_NAME_JSON_PREFS = "preferences.json"
1920
}
2021

21-
val dbDir = homeDir / "databases"
22-
val db = homeDir / FILE_NAME_DB
23-
val dbJournal = homeDir / "$FILE_NAME_DB-journal"
24-
val dbShm = dbDir / "$FILE_NAME_DB-shm"
25-
val dbWal = dbDir / "$FILE_NAME_DB-wal"
22+
val db: Path = context.getDatabasePath(FILE_NAME_DB).toPath()
23+
val dbJournal: Path = db.resolveSibling("$FILE_NAME_DB-journal")
24+
val dbShm: Path = db.resolveSibling("$FILE_NAME_DB-shm")
25+
val dbWal: Path = db.resolveSibling("$FILE_NAME_DB-wal")
2626
}

app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.io.FileNotFoundException
99
import java.io.IOException
1010
import java.io.ObjectOutputStream
1111
import java.util.zip.ZipOutputStream
12-
import kotlin.io.path.createDirectories
12+
import kotlin.io.path.createParentDirectories
1313
import kotlin.io.path.deleteIfExists
1414
import org.schabi.newpipe.streams.io.SharpOutputStream
1515
import org.schabi.newpipe.streams.io.StoredFileHelper
@@ -63,7 +63,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
6363
*/
6464
@Throws(IOException::class)
6565
fun ensureDbDirectoryExists() {
66-
fileLocator.dbDir.createDirectories()
66+
fileLocator.db.createParentDirectories()
6767
}
6868

6969
/**

app/src/test/java/org/schabi/newpipe/settings/ImportExportManagerTest.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import android.content.SharedPreferences
44
import com.grack.nanojson.JsonParser
55
import java.io.File
66
import java.io.ObjectInputStream
7+
import java.nio.file.Paths
78
import java.util.zip.ZipFile
8-
import kotlin.io.path.Path
99
import kotlin.io.path.createTempDirectory
1010
import kotlin.io.path.createTempFile
1111
import kotlin.io.path.deleteIfExists
12+
import kotlin.io.path.div
1213
import kotlin.io.path.exists
1314
import kotlin.io.path.fileSize
1415
import kotlin.io.path.inputStream
@@ -52,7 +53,7 @@ class ImportExportManagerTest {
5253

5354
@Test
5455
fun `The settings must be exported successfully in the correct format`() {
55-
val db = Path(classloader.getResource("settings/newpipe.db")!!.file)
56+
val db = Paths.get(classloader.getResource("settings/newpipe.db")!!.toURI())
5657
`when`(fileLocator.db).thenReturn(db)
5758

5859
val expectedPreferences = mapOf("such pref" to "much wow")
@@ -87,21 +88,21 @@ class ImportExportManagerTest {
8788

8889
@Test
8990
fun `Ensuring db directory existence must work`() {
90-
val dir = createTempDirectory("newpipe_")
91-
Assume.assumeTrue(dir.deleteIfExists())
92-
`when`(fileLocator.dbDir).thenReturn(dir)
91+
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
92+
Assume.assumeTrue(path.parent.deleteIfExists())
93+
`when`(fileLocator.db).thenReturn(path)
9394

9495
ImportExportManager(fileLocator).ensureDbDirectoryExists()
95-
assertTrue(dir.exists())
96+
assertTrue(path.parent.exists())
9697
}
9798

9899
@Test
99100
fun `Ensuring db directory existence must work when the directory already exists`() {
100-
val dir = createTempDirectory("newpipe_")
101-
`when`(fileLocator.dbDir).thenReturn(dir)
101+
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
102+
`when`(fileLocator.db).thenReturn(path)
102103

103104
ImportExportManager(fileLocator).ensureDbDirectoryExists()
104-
assertTrue(dir.exists())
105+
assertTrue(path.parent.exists())
105106
}
106107

107108
@Test

0 commit comments

Comments
 (0)