Add test for RSS2 media:group element

This commit is contained in:
Shinokuni 2020-09-29 22:18:56 +02:00
parent f6f5f27dd4
commit 2bbc8c3e10
3 changed files with 48 additions and 5 deletions

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"
version="2.0">
<channel>
<item>
<title>title</title>
<link>link</link>
<dc:creator><![CDATA[creator]]></dc:creator>
<dc:date>2020-08-05T14:03:48Z</dc:date>
<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>
<category><![CDATA[Category 4]]></category>
<category><![CDATA[Category 5]]></category>
<category><![CDATA[Category 6]]></category>
<guid isPermaLink="false">guid</guid>
<description><![CDATA[description]]></description>
<content:encoded><![CDATA[content:encoded]]></content:encoded>
<media:group>
<media:content medium="image" url="https://image1.jpg" />
<media:content medium="image" url="https://image2.jpg">
<media:title>image2 title</media:title>
</media:content>
</media:group>
</item>
</channel>
</rss>

View File

@ -78,4 +78,12 @@ class RSS2ItemsAdapterTest {
assertEquals(item.imageLink, "https://image2.jpg")
}
@Test
fun mediaGroupTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_media_group.xml")
val item = adapter.fromXml(stream).first()
assertEquals(item.imageLink, "https://image1.jpg")
}
}

View File

@ -35,11 +35,7 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
"content:encoded" -> content = nullableTextRecursively()
"enclosure" -> parseEnclosure(this, enclosures)
"media:content" -> parseMediaContent(this, mediaContents)
"media:group" -> allChildrenAutoIgnore("content") {
when (tagName) {
"media:content" -> parseMediaContent(this, mediaContents)
}
}
"media:group" -> parseMediaGroup(this, mediaContents)
else -> skipContents() // for example media:description
}
}
@ -79,6 +75,15 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
konsume.skipContents() // ignore media content sub elements
}
private fun parseMediaGroup(konsume: Konsumer, mediaContents: MutableList<String>) {
konsume.allChildrenAutoIgnore("content") {
when (tagName) {
"media:content" -> parseMediaContent(this, mediaContents)
else -> skipContents()
}
}
}
private fun validateItem(item: Item) {
when {
item.title == null -> throw ParseException("Item title is required")