private String getCodeSnippetHtml ( Throwable cause ) { StringBuilder codeSnippet = new StringBuilder ( ) ; BufferedReader reader = null ; try { if ( cause instanceof CitrusRuntimeException ) { CitrusRuntimeException ex = ( CitrusRuntimeException ) cause ; if ( ! ex . getFailureStack ( ) . isEmpty ( ) ) { FailureStackElement stackElement = ex . getFailureStack ( ) . pop ( ) ; if ( stackElement . getLineNumberStart ( ) > 0 ) { reader = new BufferedReader ( new FileReader ( new ClassPathResource ( stackElement . getTestFilePath ( ) + ".xml" ) . getFile ( ) ) ) ; codeSnippet . append ( "<div class=\"code-snippet\">" ) ; codeSnippet . append ( "<h2 class=\"code-title\">" + stackElement . getTestFilePath ( ) + ".xml</h2>" ) ; String line ; String codeStyle ; int lineIndex = 1 ; int snippetOffset = 5 ; while ( ( line = reader . readLine ( ) ) != null ) { if ( lineIndex >= stackElement . getLineNumberStart ( ) - snippetOffset && lineIndex < stackElement . getLineNumberStart ( ) || lineIndex > stackElement . getLineNumberEnd ( ) && lineIndex <= stackElement . getLineNumberEnd ( ) + snippetOffset ) { codeStyle = "code" ; } else if ( lineIndex >= stackElement . getLineNumberStart ( ) && lineIndex <= stackElement . getLineNumberEnd ( ) ) { codeStyle = "code-failed" ; } else { codeStyle = "" ; } if ( StringUtils . hasText ( codeStyle ) ) { codeSnippet . append ( "<pre class=\"" + codeStyle + "\"><span class=\"line-number\">" + lineIndex + ":</span>" + line . replaceAll ( ">" , "&gt;" ) . replaceAll ( "<" , "&lt;" ) + "</pre>" ) ; } lineIndex ++ ; } codeSnippet . append ( "</div>" ) ; } } } } catch ( IOException e ) { log . error ( "Failed to construct HTML code snippet" , e ) ; } finally { if ( reader != null ) { try { reader . close ( ) ; } catch ( IOException e ) { log . warn ( "Failed to close test file" , e ) ; } } } return codeSnippet . toString ( ) ; }
Gets the code section from test case XML which is responsible for the error .