Skip to content

Commit 8d9c6c3

Browse files
committed
Add tests for ICalendarParser
1 parent 00c7aba commit 8d9c6c3

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ guava = { module = "com.google.guava:guava", version.ref = "guava" }
2626
ical4j = { module = "org.mnode.ical4j:ical4j", version.ref = "ical4j" }
2727
junit = { module = "junit:junit", version.ref = "junit" }
2828
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
29+
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
2930
mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockk" }
3031
slf4j-jdk = { module = "org.slf4j:slf4j-jdk14", version.ref = "slf4j" }
3132

lib/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,5 @@ dependencies {
135135

136136
// unit tests
137137
testImplementation(libs.junit)
138+
testImplementation(libs.mockk)
138139
}

lib/src/main/kotlin/at/bitfire/ical4android/ICalendar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ open class ICalendar {
9494
fun fromReader(reader: Reader, properties: MutableMap<String, String>? = null): Calendar {
9595
logger.fine("Parsing iCalendar stream")
9696

97-
val calendar = ICalendarParser(reader).parse()
97+
val calendar = ICalendarParser().parse(reader)
9898

9999
// fill calendar properties
100100
properties?.let {

lib/src/main/kotlin/at/bitfire/synctools/exception/InvalidRemoteResourceException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package at.bitfire.synctools.exception
99
/**
1010
* Represents an invalid remote resource (for instance, a calendar object resource).
1111
*/
12-
class InvalidRemoteResourceException: Exception {
12+
class InvalidRemoteResourceException: InvalidResourceException {
1313

1414
constructor(message: String): super(message)
1515
constructor(message: String, ex: Throwable): super(message, ex)

lib/src/main/kotlin/at/bitfire/synctools/icalendar/ICalendarParser.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import java.util.logging.Logger
2121
/**
2222
* Custom iCalendar parser that applies error correction using [ICalPreprocessor].
2323
*/
24-
class ICalendarParser(
25-
private val reader: Reader
26-
) {
24+
class ICalendarParser {
2725

2826
private val logger
2927
get() = Logger.getLogger(javaClass.name)
@@ -34,9 +32,9 @@ class ICalendarParser(
3432
* 1. The input stream from is preprocessed with [ICalPreprocessor.preprocessStream].
3533
* 2. The parsed calendar is preprocessed with [ICalPreprocessor.preprocessCalendar].
3634
*
37-
* @throws InvalidRemoteResourceException when the resource is invalid
35+
* @throws InvalidRemoteResourceException when the resource is can't be parsed
3836
*/
39-
fun parse(): Calendar {
37+
fun parse(reader: Reader): Calendar {
4038
// preprocess stream to work around problems that prevent parsing and thus can't be fixed later
4139
val preprocessed = ICalPreprocessor.preprocessStream(reader)
4240

lib/src/test/kotlin/at/bitfire/synctools/icalendar/ICalendarParserTest.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,57 @@
66

77
package at.bitfire.synctools.icalendar
88

9+
import at.bitfire.ical4android.validation.ICalPreprocessor
10+
import at.bitfire.synctools.exception.InvalidRemoteResourceException
11+
import io.mockk.junit4.MockKRule
12+
import io.mockk.mockkObject
13+
import io.mockk.verify
14+
import org.junit.Rule
15+
import org.junit.Test
16+
import java.io.StringReader
17+
918
class ICalendarParserTest {
19+
20+
@get:Rule
21+
val mockkRule = MockKRule(this)
22+
23+
@Test
24+
fun `parse() applies pre-processing`() {
25+
mockkObject(ICalPreprocessor)
26+
27+
val reader = StringReader(
28+
"BEGIN:VCALENDAR\r\n" +
29+
"BEGIN:VEVENT\r\n" +
30+
"END:VEVENT\r\n" +
31+
"END:VCALENDAR\r\n"
32+
)
33+
val cal = ICalendarParser().parse(reader)
34+
35+
verify(exactly = 1) {
36+
// verify preprocessing was applied to stream
37+
ICalPreprocessor.preprocessStream(any())
38+
39+
// verify preprocessing was applied to resulting calendar
40+
ICalPreprocessor.preprocessCalendar(cal)
41+
}
42+
}
43+
44+
@Test
45+
fun `parse() suppresses invalid properties`() {
46+
val reader = StringReader(
47+
"BEGIN:VCALENDAR\r\n" +
48+
"BEGIN:VEVENT\r\n" +
49+
"DTSTAMP:invalid\r\n" +
50+
"END:VEVENT\r\n" +
51+
"END:VCALENDAR\r\n"
52+
)
53+
ICalendarParser().parse(reader)
54+
}
55+
56+
@Test(expected = InvalidRemoteResourceException::class)
57+
fun `parse() throws exception on invalid input`() {
58+
val reader = StringReader("invalid")
59+
ICalendarParser().parse(reader)
60+
}
61+
1062
}

0 commit comments

Comments
 (0)