@Test public void testError() throws IOException { String stackTrace = "trace line 1\ntrace line 2"; String trimmedStackTrace = "trace line 1"; SafeThrowable safeThrowable = new SafeThrowable( new Exception() ); StackTraceWriter stackTraceWriter = mock( StackTraceWriter.class ); when( stackTraceWriter.getThrowable() ).thenReturn( safeThrowable ); when( stackTraceWriter.smartTrimmedStackTrace() ).thenReturn( null ); when( stackTraceWriter.writeTrimmedTraceToString() ).thenReturn( trimmedStackTrace ); when( stackTraceWriter.writeTraceToString() ).thenReturn( stackTrace ); ReportEntry reportEntry = mock( ReportEntry.class ); when( reportEntry.getElapsed() ).thenReturn( ELAPSED_TIME ); when( reportEntry.getGroup() ).thenReturn( "this group" ); when( reportEntry.getMessage() ).thenReturn( "skipped test" ); when( reportEntry.getName() ).thenReturn( "my test" ); when( reportEntry.getNameWithGroup() ).thenReturn( "name with group" ); when( reportEntry.getSourceName() ).thenReturn( "pkg.MyTest" ); when( reportEntry.getStackTraceWriter() ).thenReturn( stackTraceWriter ); Stream out = Stream.newStream(); LegacyMasterProcessChannelEncoder encoder = new LegacyMasterProcessChannelEncoder( newBufferedChannel( out ) ); encoder.testError( reportEntry, false ); ByteArrayOutputStream expectedFrame = new ByteArrayOutputStream(); expectedFrame.write( ":maven-surefire-event:".getBytes( UTF_8 ) ); expectedFrame.write( (byte) 10 ); expectedFrame.write( ":test-error:".getBytes( UTF_8 ) ); expectedFrame.write( (byte) 10 ); expectedFrame.write( ":normal-run:".getBytes( UTF_8 ) ); expectedFrame.write( (byte) 5 ); expectedFrame.write( ":UTF-8:".getBytes( UTF_8 ) ); expectedFrame.write( new byte[] {0, 0, 0, 10} ); expectedFrame.write( ':' ); expectedFrame.write( reportEntry.getSourceName().getBytes( UTF_8 ) ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 1} ); expectedFrame.write( ':' ); expectedFrame.write( 0 ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 7} ); expectedFrame.write( ':' ); expectedFrame.write( reportEntry.getName().getBytes( UTF_8 ) ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 1} ); expectedFrame.write( ':' ); expectedFrame.write( 0 ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 10} ); expectedFrame.write( ':' ); expectedFrame.write( reportEntry.getGroup().getBytes( UTF_8 ) ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 12} ); expectedFrame.write( ':' ); expectedFrame.write( reportEntry.getMessage().getBytes( UTF_8 ) ); expectedFrame.write( ':' ); expectedFrame.write( 0xff ); expectedFrame.write( ELAPSED_TIME_HEXA ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 1} ); expectedFrame.write( ':' ); expectedFrame.write( 0 ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 1} ); expectedFrame.write( ':' ); expectedFrame.write( 0 ); expectedFrame.write( ':' ); expectedFrame.write( new byte[] {0, 0, 0, 25} ); expectedFrame.write( ':' ); expectedFrame.write( stackTrace.getBytes( UTF_8 ) ); expectedFrame.write( ':' ); assertThat( out.toByteArray() ) .isEqualTo( expectedFrame.toByteArray() ); }
private void error( StackTraceWriter stackTraceWriter, boolean trimStackTraces, ForkedProcessEventType eventType, @SuppressWarnings( "SameParameterValue" ) boolean sendImmediately ) { CharsetEncoder encoder = DEFAULT_STREAM_ENCODING.newEncoder(); StackTrace stackTraceWrapper = new StackTrace( stackTraceWriter, trimStackTraces ); int bufferMaxLength = estimateBufferLength( eventType, null, encoder, 0, stackTraceWrapper.message, stackTraceWrapper.smartTrimmedStackTrace, stackTraceWrapper.stackTrace ); ByteBuffer result = ByteBuffer.allocate( bufferMaxLength ); encodeHeader( encoder, result, eventType, null ); encode( encoder, result, stackTraceWrapper ); encodeAndPrintEvent( result, sendImmediately ); }
LegacyMasterProcessChannelEncoder implements MasterProcessChannelEncoder { private void error( StackTraceWriter stackTraceWriter, boolean trimStackTraces, ForkedProcessEventType eventType, @SuppressWarnings( "SameParameterValue" ) boolean sendImmediately ) { CharsetEncoder encoder = DEFAULT_STREAM_ENCODING.newEncoder(); StackTrace stackTraceWrapper = new StackTrace( stackTraceWriter, trimStackTraces ); int bufferMaxLength = estimateBufferLength( eventType, null, encoder, 0, stackTraceWrapper.message, stackTraceWrapper.smartTrimmedStackTrace, stackTraceWrapper.stackTrace ); ByteBuffer result = ByteBuffer.allocate( bufferMaxLength ); encodeHeader( encoder, result, eventType, null ); encode( encoder, result, stackTraceWrapper ); encodeAndPrintEvent( result, sendImmediately ); } }
LegacyMasterProcessChannelEncoder implements MasterProcessChannelEncoder { private void error( StackTraceWriter stackTraceWriter, boolean trimStackTraces, ForkedProcessEventType eventType, @SuppressWarnings( "SameParameterValue" ) boolean sendImmediately ) { CharsetEncoder encoder = DEFAULT_STREAM_ENCODING.newEncoder(); StackTrace stackTraceWrapper = new StackTrace( stackTraceWriter, trimStackTraces ); int bufferMaxLength = estimateBufferLength( eventType, null, encoder, 0, stackTraceWrapper.message, stackTraceWrapper.smartTrimmedStackTrace, stackTraceWrapper.stackTrace ); ByteBuffer result = ByteBuffer.allocate( bufferMaxLength ); encodeHeader( encoder, result, eventType, null ); encode( encoder, result, stackTraceWrapper ); encodeAndPrintEvent( result, sendImmediately ); } LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out ); protected  LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out, @Nonnull RunMode runMode ); }
LegacyMasterProcessChannelEncoder implements MasterProcessChannelEncoder { private void error( StackTraceWriter stackTraceWriter, boolean trimStackTraces, ForkedProcessEventType eventType, @SuppressWarnings( "SameParameterValue" ) boolean sendImmediately ) { CharsetEncoder encoder = DEFAULT_STREAM_ENCODING.newEncoder(); StackTrace stackTraceWrapper = new StackTrace( stackTraceWriter, trimStackTraces ); int bufferMaxLength = estimateBufferLength( eventType, null, encoder, 0, stackTraceWrapper.message, stackTraceWrapper.smartTrimmedStackTrace, stackTraceWrapper.stackTrace ); ByteBuffer result = ByteBuffer.allocate( bufferMaxLength ); encodeHeader( encoder, result, eventType, null ); encode( encoder, result, stackTraceWrapper ); encodeAndPrintEvent( result, sendImmediately ); } LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out ); protected  LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out, @Nonnull RunMode runMode ); @Override MasterProcessChannelEncoder asRerunMode(); @Override MasterProcessChannelEncoder asNormalMode(); @Override boolean checkError(); @Override void onJvmExit(); @Override void sendSystemProperties( Map<String, String> sysProps ); @Override void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testStarting( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSucceeded( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testFailed( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSkipped( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testError( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testAssumptionFailure( ReportEntry reportEntry, boolean trimStackTraces ); @Override void stdOut( String msg, boolean newLine ); @Override void stdErr( String msg, boolean newLine ); @Override void consoleInfoLog( String message ); @Override void consoleErrorLog( String message ); @Override void consoleErrorLog( Throwable t ); @Override void consoleErrorLog( String message, Throwable t ); @Override void consoleErrorLog( StackTraceWriter stackTraceWriter, boolean trimStackTraces ); @Override void consoleDebugLog( String message ); @Override void consoleWarningLog( String message ); @Override void bye(); @Override void stopOnNextTest(); @Override void acquireNextTest(); @Override void sendExitError( StackTraceWriter stackTraceWriter, boolean trimStackTraces ); }
LegacyMasterProcessChannelEncoder implements MasterProcessChannelEncoder { private void error( StackTraceWriter stackTraceWriter, boolean trimStackTraces, ForkedProcessEventType eventType, @SuppressWarnings( "SameParameterValue" ) boolean sendImmediately ) { CharsetEncoder encoder = DEFAULT_STREAM_ENCODING.newEncoder(); StackTrace stackTraceWrapper = new StackTrace( stackTraceWriter, trimStackTraces ); int bufferMaxLength = estimateBufferLength( eventType, null, encoder, 0, stackTraceWrapper.message, stackTraceWrapper.smartTrimmedStackTrace, stackTraceWrapper.stackTrace ); ByteBuffer result = ByteBuffer.allocate( bufferMaxLength ); encodeHeader( encoder, result, eventType, null ); encode( encoder, result, stackTraceWrapper ); encodeAndPrintEvent( result, sendImmediately ); } LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out ); protected  LegacyMasterProcessChannelEncoder( @Nonnull WritableBufferedByteChannel out, @Nonnull RunMode runMode ); @Override MasterProcessChannelEncoder asRerunMode(); @Override MasterProcessChannelEncoder asNormalMode(); @Override boolean checkError(); @Override void onJvmExit(); @Override void sendSystemProperties( Map<String, String> sysProps ); @Override void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testStarting( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSucceeded( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testFailed( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testSkipped( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testError( ReportEntry reportEntry, boolean trimStackTraces ); @Override void testAssumptionFailure( ReportEntry reportEntry, boolean trimStackTraces ); @Override void stdOut( String msg, boolean newLine ); @Override void stdErr( String msg, boolean newLine ); @Override void consoleInfoLog( String message ); @Override void consoleErrorLog( String message ); @Override void consoleErrorLog( Throwable t ); @Override void consoleErrorLog( String message, Throwable t ); @Override void consoleErrorLog( StackTraceWriter stackTraceWriter, boolean trimStackTraces ); @Override void consoleDebugLog( String message ); @Override void consoleWarningLog( String message ); @Override void bye(); @Override void stopOnNextTest(); @Override void acquireNextTest(); @Override void sendExitError( StackTraceWriter stackTraceWriter, boolean trimStackTraces );  }