-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract export database logic into own class #5059
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well from refactoring too much stuff around, i apparently created a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a separate PR for this |
||
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) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
such db much wow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That causes the build to fail. Can you fix that, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, how is this possible? I ran the tests successfully on my machine...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WOW, i see where this is coming from. PR coming