dr29bart commented on code in PR #716:
URL: https://github.com/apache/maven-surefire/pull/716#discussion_r1449170752


##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java:
##########
@@ -451,7 +451,9 @@ private static void getTestProblems(
                     String type = delimiter == -1 ? stackTrace : 
stackTrace.substring(0, delimiter);
                     ppw.addAttribute("type", type);
                 } else {
-                    ppw.addAttribute("type", new 
StringTokenizer(stackTrace).nextToken());
+                    ppw.addAttribute(
+                            "type",
+                            isBlank(stackTrace) ? "UndefinedException" : new 
StringTokenizer(stackTrace).nextToken());

Review Comment:
   Do you mean `indexOf` approach or `to use value as is`?
   
   I didn't want to leave a chance for `type` to be null. What non-null value 
can be used here?
   
   The `stackTrace` StatelessXmlReporter#439 var may be null:
   ```
   public String getStackTrace(boolean trimStackTrace) {
           StackTraceWriter w = original.getStackTraceWriter();
           return w == null ? null : (trimStackTrace ? 
w.writeTrimmedTraceToString() : w.writeTraceToString());
       }
   ```
   At the same time surefire's XSD requires `type`
   <details>
    <summary>XSD</summary>
   
   ```xml
   <xs:element name="error" nillable="true" minOccurs="0" maxOccurs="1">
                                   <xs:complexType>
                                       <xs:simpleContent>
                                           <xs:extension base="xs:string">
                                               <xs:attribute name="message" 
type="xs:string"/>
                                               <xs:attribute name="type" 
type="xs:string" use="required"/>
                                           </xs:extension>
                                       </xs:simpleContent>
                                   </xs:complexType>
                               </xs:element>
   ```
   </details>
   
   The goal is to handle stack traces like:
   ```
   // has message
   junit.framework.ComparisonFailure: 
   Expected :fail at foo
   Actual   :faild at foo
   
   // does not have message
   java.lang.AssertionError
        at org.junit.Assert.fail(Assert.java:87)
        at org.junit.Assert.fail(Assert.java:96)
   
   // or stack trace is missing (empty String)
   
   ```
   
   Please, let me know if this alternative is more preferable:
   <details>
    <summary>Option A</summary>
   
   ```java
   if (report.getStackTraceWriter() != null) {
               //noinspection ThrowableResultOfMethodCallIgnored
               SafeThrowable t = report.getStackTraceWriter().getThrowable();
               if (t != null) {
                   int delimiter = StringUtils.indexOfAny(stackTrace, ":", 
"\t", "\n", "\r", "\f");
                   String type = delimiter == -1 ? stackTrace : 
stackTrace.substring(0, delimiter);
                   ppw.addAttribute("type", Objects.toString(type, 
"UndefinedException"));
               }
           }
   ```
   </details>
   <details>
    <summary>Option B</summary>
   
   ```java
   SafeThrowable t = report.getStackTraceWriter().getThrowable();
               if (t != null) {
                   if (t.getMessage() != null) {
                       int delimiter = stackTrace.indexOf(":");
                       String type = delimiter == -1 ? stackTrace : 
stackTrace.substring(0, delimiter);
                       ppw.addAttribute("type", type);
                   } else {
                       ppw.addAttribute(
                               "type",
                               isBlank(stackTrace) ? stackTrace : new 
StringTokenizer(stackTrace).nextToken());
                   }
               }
   ```
   </details>
   <details>
    <summary>Option C</summary>
   
   ```java
   SafeThrowable t = report.getStackTraceWriter().getThrowable();
               if (t != null) {
                   if (t.getMessage() != null) {
                       int delimiter = stackTrace.indexOf(":");
                       String type = delimiter == -1 ? stackTrace : 
stackTrace.substring(0, delimiter);
                       ppw.addAttribute("type", type);
                   } else {
                       int delimiter = StringUtils.indexOfAny(stackTrace, "\t", 
"\n", "\r", "\f");
                       String type = delimiter == -1 ? stackTrace : 
stackTrace.substring(0, delimiter);
                       ppw.addAttribute("type", type);
                   }
               }
   </details>
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to