Add tests for XmlAdapter

This commit is contained in:
Shinokuni 2020-09-20 18:44:55 +02:00
parent de21a308b6
commit 694ff6331e
3 changed files with 36 additions and 7 deletions

View File

@ -46,11 +46,11 @@ dependencies {
implementation "androidx.core:core-ktx:1.2.0" implementation "androidx.core:core-ktx:1.2.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.2.0' androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.8.1' androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.8.1'
implementation 'com.gitlab.mvysny.konsume-xml:konsume-xml:0.11' implementation 'com.gitlab.mvysny.konsume-xml:konsume-xml:0.11'

View File

@ -7,6 +7,7 @@ import com.readrops.api.localfeed.rss.RSSItemsAdapter
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import java.io.InputStream import java.io.InputStream
import java.lang.IllegalArgumentException
interface XmlAdapter<T> { interface XmlAdapter<T> {
@ -17,7 +18,7 @@ interface XmlAdapter<T> {
return when (type) { return when (type) {
LocalRSSHelper.RSSType.RSS_2 -> RSSFeedAdapter() LocalRSSHelper.RSSType.RSS_2 -> RSSFeedAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter() LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
else -> throw Exception("Unknown RSS type : $type") else -> throw IllegalArgumentException("Unknown RSS type : $type")
} }
} }
@ -25,7 +26,7 @@ interface XmlAdapter<T> {
return when (type) { return when (type) {
LocalRSSHelper.RSSType.RSS_2 -> RSSItemsAdapter() LocalRSSHelper.RSSType.RSS_2 -> RSSItemsAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMItemsAdapter() LocalRSSHelper.RSSType.ATOM -> ATOMItemsAdapter()
else -> throw Exception("Unknown RSS type : $type") else -> throw IllegalArgumentException("Unknown RSS type : $type")
} }
} }
} }

View File

@ -0,0 +1,28 @@
package com.readrops.api.localfeed
import com.readrops.api.localfeed.atom.ATOMFeedAdapter
import com.readrops.api.localfeed.atom.ATOMItemsAdapter
import com.readrops.api.localfeed.rss.RSSFeedAdapter
import com.readrops.api.localfeed.rss.RSSItemsAdapter
import junit.framework.TestCase.assertTrue
import org.junit.Assert
import org.junit.Test
class XmlAdapterTest {
@Test
fun xmlFeedAdapterFactoryTest() {
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSSFeedAdapter)
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMFeedAdapter)
Assert.assertThrows(IllegalArgumentException::class.java) { XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN) }
}
@Test
fun xmlItemsAdapterFactoryTest() {
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSSItemsAdapter)
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMItemsAdapter)
Assert.assertThrows(IllegalArgumentException::class.java) { XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN) }
}
}