-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Legacy XML reports should preserve new line characters in message attributes #4174
Comments
@gtach2o I'm not sure what you mean. In your example, there are newline characters in the attribute value. While Gradle seems to replace |
Well, that xml is processed by some other services and message value extracted with
Can console launcher do this as well? |
While XML attribute values may contain whitespace such as line breaks, the XML spec [1] dictates that XML processors must replace them with spaces which causes downstream tools to misrepresent the original value. [1] https://www.w3.org/TR/xml/#AVNormalize Resolves #4174.
I've looked into it in #4180. However, the closest I can get with the current STaX-based infrastructure is this: <failure message="line 1&#10; line 2">...</failure> That's because when passing @gtach2o Could you please check whether that kind of "double-escaping" works with your |
I think we only need to replace <failure message="line 1 line 2 ">...</failure> One more note: if message contains quotes |
How gradle with test {
useJUnitPlatform()
} does it? |
Maybe some double-escape avoiding mechanism can be applied String escapeXmlAttribute(String input) {
StringBuilder result = new StringBuilder();
int length = input.length();
for (int i = 0; i < length; i++) {
char c = input.charAt(i);
if (c == '&' && input.startsWith(" ", i)) {
result.append(" ");
i += 4;
} else if (c == '\n') {
result.append(" ");
} else {
result.append(StringEscapeUtils.escapeXml10(String.valueOf(c)));
}
}
return result.toString();
} or something like that, but to not reinvent the wheel would be better to look up how it s done during |
I did some more digging. The problem is that the default implementation of However, adding Woodstox to the classpath fixes the problem since it does escape those characters by default. It would even allow to configure a custom escaping strategy since it's STaX2-compatible but we don't need that. I don't think we should add Woodstox as a dependency to junit-platform-reporting, though. Doing so could affect the code under test which would suddenly use a different XML parser/writer implementation. Thus, I've added test coverage and documentation in #4184. @gtach2o Could you please verify that adding Woodstox to your test runtime classpath solves the problem for you as well? |
FWIW, Gradle uses its own XML writer. |
There is no way to do it without adding third party dependency ? |
Can you create a custom wrapper class around XMLStreamWriter that overrides the writeAttribute method to handle escaping these characters? The rest of methods can use the original writer. public class CustomXMLStreamWriter implements XMLStreamWriter {
private final XMLStreamWriter delegate;
public CustomXMLStreamWriter(XMLStreamWriter delegate) {
this.delegate = delegate;
}
@Override
public void writeAttribute(String localName, String value) throws XMLStreamException {
String escapedValue = value
.replace("\n", " ")
.replace("\r", " ")
.replace("\t", "	");
delegate.writeAttribute(localName, escapedValue);
}
. . . Then use it in legacy.xml.XmlReportWriter XMLStreamWriter xmlWriter = new CustomXMLStreamWriter(factory.createXMLStreamWriter(out)); Maybe this and no need in woodstox? |
No, that wouldn't help since the delegate would then escape But maybe I can wrap the underlying |
While XML attribute values may contain whitespace such as line breaks, the XML spec [1] dictates that XML processors must replace them with spaces which causes downstream tools to misrepresent the original value. [1] w3.org/TR/xml#AVNormalize Resolves #4174.
Seems to work: |
Cool! |
Yes, Woodstox is only used for testing (as an additional test task). I've now merged the PR and you can consume 5.12.0-SNAPSHOT versions from https://oss.sonatype.org/content/repositories/snapshots/ |
@marcphilipp Thank you! It works as expected! I presume 5.12 will be released in ~Feb 2025 ? |
We will include this in 5.11.4 which will be released toward the end of this week. |
Hello!
After switching test execution from using gradle with junit platform to console launcher I noticed that
value of message attribute in JUnit xml report loosing new line characters.
e.g.
Using gradle xml :
Using Console launcher xml:
Steps to reproduce
Context
org.junit.platform:junit-platform-console-standalone:1.11.3
org.junit.jupiter:junit-jupiter-engine:5.11.3
JRE 23
As I understand console launcher uses new open/xml report generator and gradle platform launcher uses legacy report generator, right?
The text was updated successfully, but these errors were encountered: