-
Notifications
You must be signed in to change notification settings - Fork 92
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 #1408 from nextcloud/backport-1349
Backport 1349
- Loading branch information
Showing
7 changed files
with
240 additions
and
27 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
98 changes: 98 additions & 0 deletions
98
library/src/androidTest/java/com/nextcloud/extensions/DavPropertyProcessorTest.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,98 @@ | ||
package com.nextcloud.extensions | ||
|
||
import com.google.gson.Gson | ||
import org.apache.jackrabbit.webdav.property.DavProperty | ||
import org.apache.jackrabbit.webdav.property.DavPropertyName | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.w3c.dom.Document | ||
import org.w3c.dom.Element | ||
|
||
class DavPropertyProcessorTest { | ||
data class TestData(val name: String, val age: Int) | ||
|
||
private val gson = Gson() | ||
|
||
@Test | ||
fun testFromDavPropertyWhenGivenValidDataShouldReturnExpectedData() { | ||
val result = | ||
gson.fromDavProperty<TestData>( | ||
object : DavProperty<String> { | ||
override fun toXml(document: Document?): Element { | ||
return createElement("TestData", value) | ||
} | ||
|
||
override fun getName(): DavPropertyName { | ||
return DavPropertyName.DISPLAYNAME | ||
} | ||
|
||
override fun getValue(): String { | ||
return "{\"name\":\"John\",\"age\":55}" | ||
} | ||
|
||
override fun isInvisibleInAllprop(): Boolean { | ||
return true | ||
} | ||
} | ||
) | ||
val expected = TestData("John", 55) | ||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun testFromDavPropertyWhenGivenValidDataAndExpectDifferentTypeShouldReturnNull() { | ||
val result = | ||
gson.fromDavProperty<ArrayList<String>>( | ||
object : DavProperty<String> { | ||
override fun toXml(document: Document?): Element { | ||
return createElement("TestData", value) | ||
} | ||
|
||
override fun getName(): DavPropertyName { | ||
return DavPropertyName.DISPLAYNAME | ||
} | ||
|
||
override fun getValue(): String { | ||
return "{\"name\":\"John\",\"age\":55}" | ||
} | ||
|
||
override fun isInvisibleInAllprop(): Boolean { | ||
return true | ||
} | ||
} | ||
) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testFromDavPropertyWhenGivenInvalidDataShouldReturnNull() { | ||
val result = | ||
gson.fromDavProperty<TestData>( | ||
object : DavProperty<String?> { | ||
override fun toXml(document: Document?): Element { | ||
return createElement("TestData", "") | ||
} | ||
|
||
override fun getName(): DavPropertyName { | ||
return DavPropertyName.DISPLAYNAME | ||
} | ||
|
||
override fun getValue(): String? { | ||
return null | ||
} | ||
|
||
override fun isInvisibleInAllprop(): Boolean { | ||
return true | ||
} | ||
} | ||
) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testFromDavPropertyWhenGivenNullDataShouldReturnNull() { | ||
val result = gson.fromDavProperty<TestData>(null) | ||
assertNull(result) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
library/src/androidTest/java/com/nextcloud/extensions/ElementCreator.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,21 @@ | ||
package com.nextcloud.extensions | ||
|
||
import org.w3c.dom.Element | ||
import javax.xml.parsers.DocumentBuilderFactory | ||
|
||
fun createElement( | ||
xml: String, | ||
value: String | ||
): Element { | ||
val builder = | ||
DocumentBuilderFactory.newInstance().run { | ||
newDocumentBuilder() | ||
} | ||
val document = builder.newDocument() | ||
val element = | ||
document.createElement(xml).apply { | ||
textContent = value | ||
} | ||
document.appendChild(element) | ||
return element | ||
} |
67 changes: 67 additions & 0 deletions
67
library/src/androidTest/java/com/nextcloud/extensions/XmlDataProcessorTest.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,67 @@ | ||
package com.nextcloud.extensions | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.w3c.dom.Element | ||
|
||
class XmlDataProcessorTest { | ||
@Test | ||
fun testProcessXmlDataWhenGivenEmptyArrayListShouldReturnNull() { | ||
val tag = "width" | ||
val xmlData: ArrayList<Element> = arrayListOf() | ||
val result = xmlData.processXmlData<Float>(tag) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenWrongArrayListShouldReturnNull() { | ||
val tag = "width" | ||
val xmlData: ArrayList<String> = arrayListOf("element") | ||
val result = xmlData.processXmlData<Float>(tag) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenValidDataShouldReturnFloat() { | ||
val tag = "width" | ||
val element = createElement(tag, "220") | ||
val xmlData: ArrayList<Element> = arrayListOf(element) | ||
val result = xmlData.processXmlData<Float>(tag) | ||
assertEquals(220f, result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenValidDataAndWrongTagShouldReturnNull() { | ||
val element = createElement("width", "220") | ||
val xmlData: ArrayList<Element> = arrayListOf(element) | ||
val result = xmlData.processXmlData<Float>("latitude") | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenNullElementShouldReturnNull() { | ||
val tag = "width" | ||
val xmlData: ArrayList<Element?> = arrayListOf(null) | ||
val result = xmlData.processXmlData<Float>(tag) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenValidDataShouldReturnDouble() { | ||
val tag = "latitude" | ||
val element = createElement(tag, "12.4231") | ||
val xmlData: ArrayList<Element> = arrayListOf(element) | ||
val result = xmlData.processXmlData<Double>(tag) | ||
assertEquals(12.4231, result) | ||
} | ||
|
||
@Test | ||
fun testProcessXmlDataWhenGivenDifferentValueTypeShouldReturnNull() { | ||
val tag = "latitude" | ||
val element = createElement(tag, "StringData") | ||
val xmlData: ArrayList<Element> = arrayListOf(element) | ||
val result = xmlData.processXmlData<Float>(tag) | ||
assertNull(result) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
library/src/main/java/com/nextcloud/extensions/ArrayListExtensions.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,27 @@ | ||
package com.nextcloud.extensions | ||
|
||
import org.w3c.dom.Element | ||
|
||
@Suppress("ReturnCount", "NestedBlockDepth") | ||
inline fun <reified T> ArrayList<*>.processXmlData(tagName: String): T? { | ||
this.forEach { | ||
val element = it as? Element | ||
if (element != null && element.tagName == tagName) { | ||
val textContent = element.firstChild.textContent | ||
|
||
return when (T::class) { | ||
Float::class -> { | ||
val floatValue = textContent.toFloatOrNull() | ||
if (floatValue != null) floatValue as T else null | ||
} | ||
Double::class -> { | ||
val doubleValue = textContent.toDoubleOrNull() | ||
if (doubleValue != null) doubleValue as T else null | ||
} | ||
else -> return null | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} |
7 changes: 6 additions & 1 deletion
7
library/src/main/java/com/nextcloud/extensions/GsonExtensions.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
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