1+ /*
2+ * This file is part of bitfireAT/synctools which is released under GPLv3.
3+ * Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
4+ * SPDX-License-Identifier: GPL-3.0-or-later
5+ */
6+
7+ package at.bitfire.synctools.icalendar
8+
9+ import net.fortuna.ical4j.model.ComponentList
10+ import net.fortuna.ical4j.model.TimeZoneRegistryFactory
11+ import net.fortuna.ical4j.model.component.VAlarm
12+ import net.fortuna.ical4j.model.component.VEvent
13+ import net.fortuna.ical4j.model.property.Attendee
14+ import net.fortuna.ical4j.model.property.DtEnd
15+ import net.fortuna.ical4j.model.property.DtStart
16+ import net.fortuna.ical4j.model.property.Uid
17+ import net.fortuna.ical4j.util.TimeZones
18+ import org.junit.Assert.assertTrue
19+ import org.junit.Test
20+ import java.io.StringWriter
21+ import java.time.Duration
22+
23+ class ICalendarWriterTest {
24+
25+ private val tzRegistry = TimeZoneRegistryFactory .getInstance().createRegistry()
26+ private val tzBerlin = tzRegistry.getTimeZone(" Europe/Berlin" )!!
27+ private val tzUTC = tzRegistry.getTimeZone(TimeZones .UTC_ID )!!
28+
29+ private val writer = ICalendarWriter (prodId = javaClass.name)
30+
31+ @Test
32+ fun testGenerateEtcUTC () {
33+ val e = VEvent ().apply {
34+ properties + = Uid (" etc-utc-test@example.com" )
35+ properties + = DtStart (" 20200926T080000" , tzUTC)
36+ properties + = DtEnd (" 20200926T100000" , tzUTC)
37+ properties + = Attendee (" mailto:test@example.com" )
38+ components + = VAlarm (Duration .ofMinutes(- 30 ))
39+ }
40+ val ical = StringWriter ()
41+ writer.write(ComponentList (listOf (e)), ical)
42+
43+ assertTrue(
44+ " BEGIN:VTIMEZONE.+BEGIN:STANDARD.+END:STANDARD.+END:VTIMEZONE"
45+ .toRegex(RegexOption .DOT_MATCHES_ALL )
46+ .containsMatchIn(ical.toString())
47+ )
48+ }
49+
50+ @Test
51+ fun testWrite () {
52+ val e = VEvent ().apply {
53+ properties + = Uid (" SAMPLEUID" )
54+ properties + = DtStart (" 20190101T100000" , tzBerlin)
55+ components + = VAlarm (Duration .ofHours(- 1 ))
56+ }
57+
58+ val iCal = StringWriter ()
59+ writer.write(ComponentList (listOf (e)), iCal)
60+ val raw = iCal.toString()
61+
62+ assertTrue(raw.contains(" PRODID:${javaClass.name} " ))
63+ assertTrue(raw.contains(" UID:SAMPLEUID" ))
64+ assertTrue(raw.contains(" DTSTART;TZID=Europe/Berlin:20190101T100000" ))
65+ assertTrue(raw.contains(" DTSTAMP:" ))
66+ assertTrue(raw.contains(" BEGIN:VALARM\r\n " +
67+ " TRIGGER:-PT1H\r\n " +
68+ " END:VALARM\r\n " ))
69+ assertTrue(raw.contains(" BEGIN:VTIMEZONE" ))
70+ }
71+
72+ }
0 commit comments