{"record":{"id":"7295ec155925e428","repo":"quarkusio/quarkus","slug":"stored-xml-element-is-not-matching-expected-value","errorCode":null,"errorMessage":"Stored XML element is not matching expected value of '\" + expectedMatch + \"', but was '\" + storedValue + \"'","messagePattern":"Stored XML element is not matching expected value of '\" \\+ expectedMatch \\+ \"', but was '\" \\+ storedValue \\+ \"'","errorType":"http","errorClass":"IllegalStateException","httpStatus":500,"severity":"warning","filePath":"integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java","lineNumber":156,"sourceCode":"            checkWrittenXmlObject(con);\n        }\n        return \"OK\";\n    }\n\n    private void checkWrittenXmlObject(Connection con) throws SQLException {\n        try (Statement stmt = con.createStatement()) {\n            final ResultSet resultSet = stmt.executeQuery(\"SELECT val FROM xmltest\");\n            final boolean next = resultSet.next();\n            if (!next) {\n                throw new IllegalStateException(\"Stored XML element not found!\");\n            }\n            final String storedValue = resultSet.getString(1);\n            if (storedValue == null) {\n                throw new IllegalStateException(\"Stored XML element was loaded as null!\");\n            }\n            String expectedMatch = \"<?xml version=\\\"1.0\\\" standalone=\\\"no\\\"?><root><ele>1</ele><ele>2</ele></root>\";\n            if (!expectedMatch.equals(storedValue)) {\n                throw new IllegalStateException(\"Stored XML element is not matching expected value of '\" + expectedMatch\n                        + \"', but was '\" + storedValue + \"'\");\n            }\n        }\n    }\n\n    private void writeXmlObject(Connection con) throws SQLException, TransformerException {\n        TransformerFactory factory = TransformerFactory.newInstance();\n        final String _xmlDocument = \"<root><ele>1</ele><ele>2</ele></root>\";\n        Transformer identityTransformer = factory.newTransformer();\n        try (PreparedStatement ps = con.prepareStatement(\"INSERT INTO xmltest VALUES (?,?)\")) {\n            SQLXML xml = con.createSQLXML();\n            Result result = xml.setResult(DOMResult.class);\n\n            Source source = new StreamSource(new StringReader(_xmlDocument));\n            identityTransformer.transform(source, result);\n\n            ps.setInt(1, 1);\n            ps.setSQLXML(2, xml);","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java#L138-L174","documentation":"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.","triggerScenarios":"writeXmlObject stores a DOM-transformed XML document; the round-tripped string from the database differs in prolog attributes (standalone), whitespace, or element formatting.","commonSituations":"PostgreSQL XML type re-serialization dropping or changing the XML declaration, different TransformerFactory implementations across JDKs, or whitespace/encoding differences in stored XML.","solutions":["Compare canonicalized XML (e.g. with xmlunit) instead of raw strings","Relax the check to compare parsed DOM content rather than exact string equality","Pin/verify the JDK Transformer output matches the expected prolog, or update expectedMatch to the actual stable serialization"],"exampleFix":"// before\nif (!expectedMatch.equals(storedValue)) { throw new IllegalStateException(...); }\n// after\nDocument actual = parse(storedValue);\nDiff diff = DiffBuilder.compare(parse(expectedMatch)).withTest(actual).ignoreWhitespace().build();\nassertFalse(diff.hasDifferences(), diff.toString());","handlingStrategy":"validation","validationCode":"private static boolean xmlEquivalent(String a, String b) throws Exception {\n    javax.xml.parsers.DocumentBuilderFactory f = javax.xml.parsers.DocumentBuilderFactory.newInstance();\n    f.setNamespaceAware(true);\n    org.w3c.dom.Document da = f.newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(a)));\n    org.w3c.dom.Document db = f.newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(b)));\n    return da.isEqualNode(db);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never compare serialized XML with exact string equality","Use XMLUnit or DOM canonicalization for round-trip assertions","Pin JDK/Transformer versions in CI to keep output stable"],"tags":["xml","serialization","postgresql","brittle-assertion"],"backgroundTag":"xml-serialization-mismatch","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}