EN · DE · RU · FR · ES

#1864: CardDavXmlUtilsTest.kt

projectforge-carddav/src/test/kotlin/org/projectforge/carddav/CardDavXmlUtilsTest.kt Unit test — org.projectforge.carddav package, projectforge-carddav/src/test/kotlin/org/projectforge/carddav/CardDavXmlUtilsTest.kt 99 lines · 70 code · 22 comments · 7 blank
Comprehensive test suite for XML utility functions in CardDavXmlUtils: root element extraction, contact ID parsing from multiget requests, XML element name/value extraction with namespace support, and XML entity escaping. These utilities form the XML parsing backbone of the CardDAV protocol handler.

Architecture

CardDavXmlUtils provides lightweight XML parsing without a full DOM/SAX parser — using regex-based extraction for CardDAV-specific XML payloads. This approach is chosen because CardDAV XML payloads are structurally predictable (defined by RFCs 4918, 6352) and performance-critical.

Imports

Tested Methods

1. getRootElement(xml) — Root Element Extraction

Extracts the XML root element name from a string, handling optional XML declaration (<?xml ... ?>) and namespace-prefixed elements (d:multistatus). Tests cover:

This is critical for the PROPFIND/REPORT response handler to dispatch to the correct response builder based on the root element type.

2. extractContactIds(xml) — Multiget Contact ID Extraction

Parses a CardDAV addressbook-multiget REPORT request body to extract ProjectForge contact IDs from <d:href> elements. Tests parse a realistic XML payload with three <d:href>{path}/ProjectForge-{id}.vcf</d:href> entries and verify correct extraction of IDs 7833476, 7858940, 7859171.

The method uses regex matching on the ProjectForge-{id}.vcf filename pattern within href elements. The result is a Sequence<Int> (lazy evaluation via .toList()).

3. getElementName(xml, name) — Namespace-Aware Element Name Resolution

Finds a named element in XML and returns its qualified name including namespace prefix:

This is used when the server needs to know the exact qualified name for response element generation.

4. extractElementValue(xml, name) — Element Content Extraction

Extracts the text content of a named element from XML, handling:

5. escapeXml(xml) — XML Entity Escaping

Escapes five XML special characters per the XML 1.0 spec:

The test input <hurzel test="dkfsld"> & ' is escaped to &lt;hurzel test=&quot;dkfsld&quot;&gt; &amp; &apos;.

The regex-based approach is fast but fragile — it cannot handle arbitrary XML with CDATA sections, processing instructions, or deeply nested structures. It works correctly only for the predictable CardDAV XML schemas defined in the relevant RFCs.

Test Data Pattern

All tests use Kotlin's let scope function with raw string literals ("""...""") for XML test data. trimIndent() and trimMargin() are used to clean up multiline test fixtures while preserving XML structure.

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
5167dab99 WIP: Carddav
6c1daac93 WIP: Carddav
0d7a81f91 WIP: Carddav
dc3936193 WIP: Carddav (Thunderbird works, readonly, including images etc.)
caee35750 WIP: Carddav
2ad0cfe66 WIP: Carddav