Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EventValidatorTest.kt « validation « ical4android « bitfire « at « java « test « src - github.com/bitfireAT/ical4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c64d0d7021c98ecddecb77f21a0305304122b0ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/***************************************************************************************************
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 **************************************************************************************************/

package at.bitfire.ical4android.validation

import at.bitfire.ical4android.Event
import at.bitfire.ical4android.InvalidCalendarException
import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.DateTime
import net.fortuna.ical4j.model.Recur
import net.fortuna.ical4j.model.TimeZoneRegistryFactory
import net.fortuna.ical4j.model.property.DtEnd
import net.fortuna.ical4j.model.property.DtStart
import net.fortuna.ical4j.model.property.RRule
import org.junit.Assert.*
import org.junit.Test
import java.io.StringReader
import java.util.*

class EventValidatorTest {

    val tzReg = TimeZoneRegistryFactory.getInstance().createRegistry()


    // DTSTART and DTEND

    @Test
    fun testEnsureCorrectStartAndEndTime_noDtStart() {
        assertThrows(InvalidCalendarException::class.java) {
            val event = Event().apply {
                dtEnd = DtEnd(DateTime("20000105T000000"))  // DATETIME
                // no dtStart
            }
            EventValidator.correctStartAndEndTime(event)
        }

        assertThrows(InvalidCalendarException::class.java) {
            Event.eventsFromReader(StringReader(
                "BEGIN:VCALENDAR\n" +
                "BEGIN:VEVENT\n" +
                "UID:51d8529a-5844-4609-918b-2891b855e0e8\n" +
                "DTEND;VALUE=DATE:20211116\n" +                   // DATE
                "END:VEVENT\n" +
                "END:VCALENDAR")).first()
        }
    }

    @Test
    fun testEnsureCorrectStartAndEndTime_dtEndBeforeDtStart() {
        val event = Event().apply {
            dtStart = DtStart(DateTime("20000105T001100"))              // DATETIME
            dtEnd = DtEnd(DateTime("20000105T000000"))                  // DATETIME
        }
        assertTrue(event.dtStart!!.date.time > event.dtEnd!!.date.time)
        EventValidator.correctStartAndEndTime(event)
        assertNull(event.dtEnd)

        val event1 = Event.eventsFromReader(StringReader(
            "BEGIN:VCALENDAR\n" +
               "BEGIN:VEVENT\n" +
               "UID:51d8529a-5844-4609-918b-2891b855e0e8\n" +
               "DTSTART;VALUE=DATE:20211117\n" +                           // DATE
               "DTEND;VALUE=DATE:20211116\n" +                             // DATE
               "END:VEVENT\n" +
               "END:VCALENDAR")).first()
        assertNull(event1.dtEnd)
    }


    // RRULE UNTIL and DTSTART of same type (DATETIME/DATE)

    @Test
    fun testSameTypeForDtStartAndRruleUntil_DtStartAndRruleUntilAreBothDateTimeOrDate() {
        // should do nothing when types are the same

        val event = Event().apply {
            dtStart = DtStart(DateTime("20211115T001100Z"))               // DATETIME (UTC)
            rRules.add(RRule("FREQ=MONTHLY;UNTIL=20251214T001100Z"))      // DATETIME (UTC)
        }
        assertEquals(DateTime("20211115T001100Z"), event.dtStart!!.date)
        assertEquals(DateTime("20251214T001100Z"), event.rRules.first.recur.until)
        EventValidator.sameTypeForDtStartAndRruleUntil(event.dtStart!!, event.rRules)
        assertEquals(DateTime("20211115T001100Z"), event.dtStart!!.date)
        assertEquals(DateTime("20251214T001100Z"), event.rRules.first.recur.until)

        val event1 = Event.eventsFromReader(StringReader(
            "BEGIN:VCALENDAR\n" +
               "BEGIN:VEVENT\n" +
               "UID:51d8529a-5844-4609-918b-2891b855e0e8\n" +
               "DTSTART;VALUE=DATE:20211115\n" +                               // DATE
               "RRULE:FREQ=MONTHLY;UNTIL=20231214;BYMONTHDAY=15\n" +           // DATE
               "END:VEVENT\n" +
               "END:VCALENDAR")).first()
        assertEquals(Date("20231214"), event1.rRules.first.recur.until)

        val event2 = Event.eventsFromReader(StringReader(
            "BEGIN:VCALENDAR\n" +
               "BEGIN:VEVENT\n" +
               "UID:381fb26b-2da5-4dd2-94d7-2e0874128aa7\n" +
               "DTSTART;VALUE=DATE:20080215\n" +                               // DATE
               "RRULE:FREQ=YEARLY;UNTIL=20230216;BYMONTHDAY=15\n" +            // DATE
               "END:VEVENT\n" +
               "END:VCALENDAR")).first()
        assertEquals(Date("20230216"), event2.rRules.first.recur.until)
    }

    @Test
    fun testSameTypeForDtStartAndRruleUntil_DtStartIsDateAndRruleUntilIsDateTime() {
        // should remove (possibly existing) time in RRULE if DTSTART value is of type DATE (not DATETIME)
        // we want time to be cut off hard, not taking time zones into account risking the date could flip when time is close to midnight

        val event = Event().apply {
            dtStart = DtStart(Date("20211115"))                         // DATE
            rRules.add(RRule("FREQ=MONTHLY;UNTIL=20211214T235959Z"))    // DATETIME (UTC), close to flip up
        }
        assertEquals(
            DateTime("20211214T235959Z"),
            event.rRules.first.recur.until
        )
        EventValidator.sameTypeForDtStartAndRruleUntil(event.dtStart!!, event.rRules)
        assertEquals(Date("20211214"), event.rRules.first.recur.until)

        val event1 = Event.eventsFromReader(
            StringReader(
                "BEGIN:VCALENDAR\n" +
                        "BEGIN:VEVENT\n" +
                        "UID:51d8529a-5844-4609-918b-2891b855e0e8\n" +
                        "DTSTART;VALUE=DATE:20211115\n" +                             // DATE
                        "RRULE:FREQ=MONTHLY;UNTIL=20211214T235959;BYMONTHDAY=15\n" +  // DATETIME (no timezone), close to flip up
                        "END:VEVENT\n" +
                        "END:VCALENDAR"
            )
        ).first()
        assertEquals(1639440000000, event1.rRules.first.recur.until.time)
        assertEquals(Date("20211214"), event1.rRules.first.recur.until)
    }

    @Test
    fun testSameTypeForDtStartAndRruleUntil_DtStartIsDateAndRruleUntilIsDateTime_2() {
        val event2 = Event().apply {
            dtStart = DtStart(Date("20080215"))                                                     // DATE (local/system time zone)
            rRules.add(RRule("FREQ=YEARLY;TZID=Europe/Vienna;UNTIL=20230218T000000;BYMONTHDAY=15")) // DATETIME (with timezone), close to flip down
        }
        // NOTE: ical4j will:
        //  - ignore the time zone of the RRULE (TZID=Europe/Vienna)
        //  - use the timezone from DTSTART to determine the time value (timezone of DTSTART is local/system for a DATE)
        //  - take DST into account
        // Because of this when running the test in a different timezone the date may flip down before we cut off time, making the test hard to predict.
        // As it does not happen often, for the sake of simplicity we just accept either
        EventValidator.sameTypeForDtStartAndRruleUntil(event2.dtStart!!, event2.rRules)
        assertTrue(
            Date("20230218") == event2.rRules.first.recur.until ||
            Date("20230217") == event2.rRules.first.recur.until
        )
    }

    @Test
    fun testSameTypeForDtStartAndRruleUntil_DtStartIsDateTimeAndRruleUntilIsDate() {
        // should add (possibly missing) time in UNTIL if DTSTART value is of type DATETIME (not just DATE)

        val event = Event().apply {
            dtStart = DtStart(DateTime("20110605T001100Z"))         // DATETIME (UTC)
            rRules.add(RRule("FREQ=MONTHLY;UNTIL=20211214"))        // DATE
        }
        assertEquals(Date("20211214"), event.rRules.first.recur.until)
        EventValidator.sameTypeForDtStartAndRruleUntil(event.dtStart!!, event.rRules)
        assertEquals(DateTime("20211214T001100Z"), event.rRules.first.recur.until)

        val event1 = Event.eventsFromReader(StringReader(
            "BEGIN:VCALENDAR\n" +
                "BEGIN:VEVENT\n" +
                "UID:51d8529a-5844-4609-918b-2891b855e0e8\n" +
                "DTSTART;TZID=America/New_York:20211111T053000\n" +     // DATETIME (with timezone)
                "RRULE:FREQ=MONTHLY;UNTIL=20211214;BYMONTHDAY=15\n" +   // DATE
                "END:VEVENT\n" +
                "END:VCALENDAR")).first()
        assertEquals(DateTime("20211214T053000", tzReg.getTimeZone("America/New_York")), event1.rRules.first.recur.until)

        val event2 = Event.eventsFromReader(StringReader(
            "BEGIN:VCALENDAR\n" +
                "BEGIN:VEVENT\n" +
                "UID:381fb26b-2da5-4dd2-94d7-2e0874128aa7\n" +
                "DTSTART;VALUE=DATETIME:20080214T001100\n" +            // DATETIME (no timezone)
                "RRULE:FREQ=YEARLY;UNTIL=20110214;BYMONTHDAY=15\n" +    // DATE
                "END:VEVENT\n" +
                "END:VCALENDAR")).first()
        assertEquals(DateTime("20110214T001100"), event2.rRules.first.recur.until)
    }


    // RRULE UNTIL time before DTSTART time

    @Test
    fun testHasUntilBeforeDtStart_DtStartTime_RRuleNoUntil() {
        assertFalse(
            EventValidator.hasUntilBeforeDtStart(
                DtStart(DateTime("20220531T010203")), RRule())
        )
    }


    @Test
    fun testHasUntilBeforeDtStart_DtStartDate_RRuleUntil_TimeBeforeDtStart_UTC() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart("20220912", tzReg.getTimeZone("UTC")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220911T235959Z"))
                .build())))
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartDate_RRuleUntil_TimeBeforeDtStart_noTimezone() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart("20220912"), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220911T235959"))
                .build())))
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartDate_RRuleUntil_TimeBeforeDtStart_withTimezone() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart("20220912", tzReg.getTimeZone("America/New_York")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220911T235959", tzReg.getTimeZone("America/New_York")))
                .build())))
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartDate_RRuleUntil_DateBeforeDtStart() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart("20220531"), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220530T000000"))
                .build())))
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartDate_RRuleUntil_TimeAfterDtStart() {
        assertFalse(
            EventValidator.hasUntilBeforeDtStart(DtStart("20200912"), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220912T000001Z"))
                .build()))
        )
    }


    @Test
    fun testHasUntilBeforeDtStart_DtStartTime_RRuleUntil_DateBeforeDtStart() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart(DateTime("20220531T010203")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(Date("20220530"))
                .build()))
        )
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartTime_RRuleUntil_TimeBeforeDtStart() {
        assertTrue(
            EventValidator.hasUntilBeforeDtStart(DtStart(DateTime("20220531T010203")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220531T010202"))
                .build()))
        )
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartTime_RRuleUntil_TimeAtDtStart() {
        assertFalse(
            EventValidator.hasUntilBeforeDtStart(DtStart(DateTime("20220531T010203")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220531T010203"))
                .build()))
        )
    }

    @Test
    fun testHasUntilBeforeDtStart_DtStartTime_RRuleUntil_TimeAfterDtStart() {
        assertFalse(
            EventValidator.hasUntilBeforeDtStart(DtStart(DateTime("20220531T010203")), RRule(Recur.Builder()
                .frequency(Recur.Frequency.DAILY)
                .until(DateTime("20220531T010204"))
                .build()))
        )
    }


    @Test
    fun testRemoveRRulesWithUntilBeforeDtStart() {
        val dtStart = DtStart(DateTime("20220531T125304"))
        val rruleBefore = RRule(Recur.Builder()
            .frequency(Recur.Frequency.DAILY)
            .until(DateTime("20220531T125303"))
            .build())
        val rruleAfter = RRule(Recur.Builder()
            .frequency(Recur.Frequency.DAILY)
            .until(DateTime("20220531T125305"))
            .build())

        val rrules = mutableListOf(
            rruleBefore,
            rruleAfter
        )
        EventValidator.removeRRulesWithUntilBeforeDtStart(dtStart, rrules)
        assertArrayEquals(arrayOf(
            // rRuleBefore has been removed because RRULE UNTIL is before DTSTART
            rruleAfter
        ), rrules.toTypedArray())
    }

}