Commit 2a4b0da6 authored by Ricki Hirner's avatar Ricki Hirner

Refactoring

parent c1a82b27
Pipeline #15714911 (#) passed with stages
in 2 minutes and 9 seconds
...@@ -60,9 +60,9 @@ dependencies { ...@@ -60,9 +60,9 @@ dependencies {
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.9.1' androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.9.1'
androidTestCompile 'junit:junit:4.12' androidTestCompile 'junit:junit:4.12'
//noinspection GradleDynamicVersion androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:runner:+'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp3:mockwebserver:3.9.1' testCompile 'com.squareup.okhttp3:mockwebserver:3.9.1'
} }
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.xmlpull.v1.XmlPullParser
import java.io.StringReader
class XmlUtilsTest {
@Test
fun testProcessTagRoot() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test></test>"))
// now on START_DOCUMENT [0]
var processed = false
XmlUtils.processTag(parser, "", "test", {
processed = true
})
assertTrue(processed)
}
@Test
fun testProcessTagDepth1() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<root><test></test></root>"))
parser.next() // now on START_TAG <root> [1]
var processed = false
XmlUtils.processTag(parser, "", "test", {
processed = true
})
assertTrue(processed)
}
@Test
fun testReadText() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test>Test 1</test><test><garbage/>Test 2</test>"))
parser.next() // now on START_TAG <test> [1]
assertEquals("Test 1", XmlUtils.readText(parser))
assertEquals(XmlPullParser.END_TAG, parser.eventType)
parser.next()
assertEquals("Test 2", XmlUtils.readText(parser))
assertEquals(XmlPullParser.END_TAG, parser.eventType)
}
@Test
fun testReadTextCDATA() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test><![CDATA[Test 1</test><test><garbage/>Test 2]]></test>"))
parser.next() // now on START_TAG <test> [1]
assertEquals("Test 1</test><test><garbage/>Test 2", XmlUtils.readText(parser))
assertEquals(XmlPullParser.END_TAG, parser.eventType)
}
@Test
fun testReadTextPropertyRoot() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<entry>Test 1</entry><entry>Test 2</entry>"))
// now on START_DOCUMENT [0]
val entries = mutableListOf<String>()
XmlUtils.readTextPropertyList(parser, Property.Name("", "entry"), entries)
assertEquals("Test 1", entries[0])
assertEquals("Test 2", entries[1])
assertEquals(XmlPullParser.END_DOCUMENT, parser.eventType)
}
@Test
fun testReadTextPropertyListDepth1() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test><entry>Test 1</entry><entry>Test 2</entry></test>"))
parser.next() // now on START_TAG <test> [1]
val entries = mutableListOf<String>()
XmlUtils.readTextPropertyList(parser, Property.Name("", "entry"), entries)
assertEquals("Test 1", entries[0])
assertEquals("Test 2", entries[1])
assertEquals(XmlPullParser.END_TAG, parser.eventType)
assertEquals("test", parser.name)
}
}
\ No newline at end of file
...@@ -390,7 +390,7 @@ open class DavResource @JvmOverloads constructor( ...@@ -390,7 +390,7 @@ open class DavResource @JvmOverloads constructor(
var eventType = parser.eventType var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) { while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth+1) { if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
val name = Property.Name(parser.namespace, parser.name) val name = Property.Name(parser.namespace, parser.name)
val property = PropertyRegistry.create(name, parser) val property = PropertyRegistry.create(name, parser)
if (property != null) if (property != null)
......
...@@ -15,7 +15,7 @@ import java.util.logging.Level ...@@ -15,7 +15,7 @@ import java.util.logging.Level
object PropertyRegistry { object PropertyRegistry {
val factories = mutableMapOf<String /*namespace*/, MutableMap<String /*name*/, PropertyFactory>>() private val factories = mutableMapOf<Property.Name, PropertyFactory>()
init { init {
Constants.log.info("Registering DAV property factories") Constants.log.info("Registering DAV property factories")
...@@ -27,24 +27,15 @@ object PropertyRegistry { ...@@ -27,24 +27,15 @@ object PropertyRegistry {
private fun register(factory: PropertyFactory) { private fun register(factory: PropertyFactory) {
val name = factory.getName() factories[factory.getName()] = factory
var nsFactories = factories[name.namespace]
if (nsFactories == null) {
nsFactories = mutableMapOf<String, PropertyFactory>()
factories[name.namespace] = nsFactories
}
nsFactories[name.name] = factory
} }
fun create(name: Property.Name, parser: XmlPullParser): Property? { fun create(name: Property.Name, parser: XmlPullParser) =
try { try {
factories[name.namespace]?.let { nsFactories -> factories[name]?.create(parser)
nsFactories[name.name]?.let { factory -> return factory.create(parser) } } catch (e: XmlPullParserException) {
Constants.log.log(Level.WARNING, "Couldn't parse $name", e)
null
} }
} catch (e: XmlPullParserException) {
Constants.log.log(Level.WARNING, "Couldn't parse $name", e)
}
return null
}
} }
...@@ -39,14 +39,13 @@ object XmlUtils { ...@@ -39,14 +39,13 @@ object XmlUtils {
@Throws(IOException::class, XmlPullParserException::class) @Throws(IOException::class, XmlPullParserException::class)
fun processTag(parser: XmlPullParser, namespace: String, name: String, lambda: () -> Unit) { fun processTag(parser: XmlPullParser, namespace: String, name: String, processor: () -> Unit) {
val depth = parser.depth val depth = parser.depth
var eventType = parser.eventType var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) { while (!((eventType == XmlPullParser.END_TAG || eventType == XmlPullParser.END_DOCUMENT) && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 && if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 &&
parser.namespace == namespace && parser.name == name) { parser.namespace == namespace && parser.name == name)
lambda() processor()
}
eventType = parser.next() eventType = parser.next()
} }
} }
...@@ -56,7 +55,6 @@ object XmlUtils { ...@@ -56,7 +55,6 @@ object XmlUtils {
var text: String? = null var text: String? = null
val depth = parser.depth val depth = parser.depth
var eventType = parser.eventType var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) { while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.TEXT && parser.depth == depth) if (eventType == XmlPullParser.TEXT && parser.depth == depth)
...@@ -70,9 +68,8 @@ object XmlUtils { ...@@ -70,9 +68,8 @@ object XmlUtils {
@Throws(IOException::class, XmlPullParserException::class) @Throws(IOException::class, XmlPullParserException::class)
fun readTextPropertyList(parser: XmlPullParser, name: Property.Name, list: MutableCollection<String>) { fun readTextPropertyList(parser: XmlPullParser, name: Property.Name, list: MutableCollection<String>) {
val depth = parser.depth val depth = parser.depth
var eventType = parser.eventType var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) { while (!((eventType == XmlPullParser.END_TAG || eventType == XmlPullParser.END_DOCUMENT) && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 && if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 &&
Property.Name(parser.namespace, parser.name) == name) Property.Name(parser.namespace, parser.name) == name)
list.add(parser.nextText()) list.add(parser.nextText())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment