-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5059 from XiangRongLin/content_settings_manager
Extract export database logic into own class
- Loading branch information
Showing
6 changed files
with
127 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.schabi.newpipe.settings | ||
|
||
import android.content.SharedPreferences | ||
import org.schabi.newpipe.util.ZipHelper | ||
import java.io.BufferedOutputStream | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.io.ObjectOutputStream | ||
import java.lang.Exception | ||
import java.util.zip.ZipOutputStream | ||
|
||
class ContentSettingsManager( | ||
private val newpipeDb: File, | ||
private val newpipeSettings: File | ||
) { | ||
|
||
constructor(homeDir: String) : this( | ||
File("$homeDir/databases/newpipe.db"), | ||
File("$homeDir/databases/newpipe.settings") | ||
) | ||
|
||
/** | ||
* Exports given [SharedPreferences] to the file in given outputPath. | ||
* It also creates the file. | ||
*/ | ||
@Throws(Exception::class) | ||
fun exportDatabase(preferences: SharedPreferences, outputPath: String) { | ||
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputPath))) | ||
.use { outZip -> | ||
ZipHelper.addFileToZip(outZip, newpipeDb.path, "newpipe.db") | ||
|
||
try { | ||
ObjectOutputStream(FileOutputStream(newpipeSettings)).use { output -> | ||
output.writeObject(preferences.all) | ||
output.flush() | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
|
||
ZipHelper.addFileToZip(outZip, newpipeSettings.path, "newpipe.settings") | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
app/src/test/java/org/schabi/newpipe/settings/ContentSettingsManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.schabi.newpipe.settings | ||
|
||
import android.content.SharedPreferences | ||
import org.junit.Assert | ||
import org.junit.Assume | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Suite | ||
import org.mockito.Mockito | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.schabi.newpipe.settings.ContentSettingsManagerTest.ExportTest | ||
import java.io.File | ||
import java.io.ObjectInputStream | ||
import java.util.zip.ZipFile | ||
|
||
@RunWith(Suite::class) | ||
@Suite.SuiteClasses(ExportTest::class) | ||
class ContentSettingsManagerTest { | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class ExportTest { | ||
|
||
private lateinit var preferences: SharedPreferences | ||
private lateinit var newpipeDb: File | ||
private lateinit var newpipeSettings: File | ||
|
||
@Before | ||
fun beforeClass() { | ||
|
||
val dbPath = javaClass.classLoader?.getResource("settings/newpipe.db")?.file | ||
val settingsPath = javaClass.classLoader?.getResource("settings/newpipe.settings")?.path | ||
Assume.assumeNotNull(dbPath) | ||
Assume.assumeNotNull(settingsPath) | ||
|
||
newpipeDb = File(dbPath!!) | ||
newpipeSettings = File(settingsPath!!) | ||
} | ||
|
||
@Before | ||
fun before() { | ||
preferences = Mockito.mock(SharedPreferences::class.java, Mockito.withSettings().stubOnly()) | ||
} | ||
|
||
@Test | ||
fun `The settings must be exported successfully in the correct format`() { | ||
val expectedPreferences = mapOf("such pref" to "much wow") | ||
`when`(preferences.all).thenReturn(expectedPreferences) | ||
|
||
val manager = ContentSettingsManager(newpipeDb, newpipeSettings) | ||
|
||
val output = File.createTempFile("newpipe_", "") | ||
manager.exportDatabase(preferences, output.absolutePath) | ||
|
||
val zipFile = ZipFile(output.absoluteFile) | ||
val entries = zipFile.entries().toList() | ||
Assert.assertEquals(2, entries.size) | ||
|
||
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual -> | ||
newpipeDb.inputStream().use { expected -> | ||
Assert.assertEquals(expected.reader().readText(), actual.reader().readText()) | ||
} | ||
} | ||
|
||
zipFile.getInputStream(entries.first { it.name == "newpipe.settings" }).use { actual -> | ||
val actualPreferences = ObjectInputStream(actual).readObject() | ||
Assert.assertEquals(expectedPreferences, actualPreferences) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
such db much wow |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters