quarkusio/quarkus · warning · IllegalStateException

Stored XML element is not matching expected value of '" + ex

Error message

Stored XML element is not matching expected value of '" + expectedMatch + "', but was '" + storedValue + "'

What it means

An IllegalStateException thrown when the stored XML string differs from the expected exact serialization '<?xml version="1.0" standalone="no"?><root><ele>1</ele><ele>2</ele></root>'. PostgreSQL normalizes/serializes XML, and different JVM/Transformer versions can produce different prolog formatting, so an exact-string comparison is brittle.

Source

Thrown at integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java:156

            checkWrittenXmlObject(con);
        }
        return "OK";
    }

    private void checkWrittenXmlObject(Connection con) throws SQLException {
        try (Statement stmt = con.createStatement()) {
            final ResultSet resultSet = stmt.executeQuery("SELECT val FROM xmltest");
            final boolean next = resultSet.next();
            if (!next) {
                throw new IllegalStateException("Stored XML element not found!");
            }
            final String storedValue = resultSet.getString(1);
            if (storedValue == null) {
                throw new IllegalStateException("Stored XML element was loaded as null!");
            }
            String expectedMatch = "<?xml version=\"1.0\" standalone=\"no\"?><root><ele>1</ele><ele>2</ele></root>";
            if (!expectedMatch.equals(storedValue)) {
                throw new IllegalStateException("Stored XML element is not matching expected value of '" + expectedMatch
                        + "', but was '" + storedValue + "'");
            }
        }
    }

    private void writeXmlObject(Connection con) throws SQLException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        final String _xmlDocument = "<root><ele>1</ele><ele>2</ele></root>";
        Transformer identityTransformer = factory.newTransformer();
        try (PreparedStatement ps = con.prepareStatement("INSERT INTO xmltest VALUES (?,?)")) {
            SQLXML xml = con.createSQLXML();
            Result result = xml.setResult(DOMResult.class);

            Source source = new StreamSource(new StringReader(_xmlDocument));
            identityTransformer.transform(source, result);

            ps.setInt(1, 1);
            ps.setSQLXML(2, xml);

View on GitHub (pinned to e1c734241f)

Solutions

  1. Compare canonicalized XML (e.g. with xmlunit) instead of raw strings
  2. Relax the check to compare parsed DOM content rather than exact string equality
  3. Pin/verify the JDK Transformer output matches the expected prolog, or update expectedMatch to the actual stable serialization

Example fix

// before
if (!expectedMatch.equals(storedValue)) { throw new IllegalStateException(...); }
// after
Document actual = parse(storedValue);
Diff diff = DiffBuilder.compare(parse(expectedMatch)).withTest(actual).ignoreWhitespace().build();
assertFalse(diff.hasDifferences(), diff.toString());
Defensive patterns

Strategy: validation

Validate before calling

private static boolean xmlEquivalent(String a, String b) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory f = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    f.setNamespaceAware(true);
    org.w3c.dom.Document da = f.newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(a)));
    org.w3c.dom.Document db = f.newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(b)));
    return da.isEqualNode(db);
}

Prevention

When it happens

Trigger: writeXmlObject stores a DOM-transformed XML document; the round-tripped string from the database differs in prolog attributes (standalone), whitespace, or element formatting.

Common situations: PostgreSQL XML type re-serialization dropping or changing the XML declaration, different TransformerFactory implementations across JDKs, or whitespace/encoding differences in stored XML.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/7295ec155925e428. Report an issue: GitHub.