Skip to content

Commit

Permalink
TestSetReportEntry already contains system properties and systemPrope…
Browse files Browse the repository at this point in the history
…rties() can be removed in MasterProcessChannelEncoder
  • Loading branch information
Tibor17 committed Feb 25, 2022
1 parent 2840bfa commit 7f6b017
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 142 deletions.
Expand Up @@ -72,7 +72,7 @@ public final class ForkClient

/**
* <em>testSetStartedAt</em> is set to non-zero after received
* {@link MasterProcessChannelEncoder#testSetStarting(ReportEntry, boolean)}.
* {@link MasterProcessChannelEncoder#testSetStarting(TestSetReportEntry, boolean)}.
*/
private final AtomicLong testSetStartedAt = new AtomicLong( START_TIME_ZERO );

Expand Down
Expand Up @@ -36,7 +36,7 @@
import org.apache.maven.surefire.api.report.RunMode;
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.util.internal.ObjectUtils;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
import org.apache.maven.surefire.booter.spi.EventChannelEncoder;
import org.apache.maven.surefire.extensions.EventHandler;
Expand Down Expand Up @@ -79,6 +79,7 @@
import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newChannel;
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.rules.ExpectedException.none;
Expand All @@ -104,48 +105,6 @@ public static class DecoderOperationsTest
@Rule
public final ExpectedException rule = none();

@Test
public void shouldHaveSystemProperty() throws Exception
{
final Stream out = Stream.newStream();
WritableBufferedByteChannel wChannel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( wChannel );
Map<String, String> props = ObjectUtils.systemProps();
encoder.systemProperties( props, NORMAL_RUN, 1L );
wChannel.close();

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
PropertyEventAssertionListener listener = new PropertyEventAssertionListener();
notifier.setSystemPropertiesListener( listener );

ReadableByteChannel channel = newChannel( new ByteArrayInputStream( out.toByteArray() ) );

EH eventHandler = new EH();
CountdownCloseable countdown = new CountdownCloseable( mock( Closeable.class ), 0 );
ConsoleLoggerMock logger = new ConsoleLoggerMock( false, false, false, false );
ForkNodeArgumentsMock arguments = new ForkNodeArgumentsMock( logger, new File( "" ) );
try ( EventConsumerThread t = new EventConsumerThread( "t", channel, eventHandler, countdown, arguments ) )
{
t.start();
for ( int i = 0; i < props.size(); i++ )
{
notifier.notifyEvent( eventHandler.pullEvent() );
}
}

assertThat( logger.error ).isEmpty();
assertThat( logger.warning ).isEmpty();
assertThat( logger.info ).isEmpty();
assertThat( logger.debug ).isEmpty();

assertThat( logger.isCalled() )
.isFalse();
assertThat( arguments.isCalled() )
.isFalse();
assertThat( listener.counter.get() )
.isEqualTo( props.size() );
}

@Test
public void shouldSendByeEvent() throws Exception
{
Expand Down Expand Up @@ -799,44 +758,6 @@ public void testStdErrStream() throws Exception
.isTrue();
}

@Test
public void shouldCountSameNumberOfSystemProperties() throws Exception
{
final Stream out = Stream.newStream();
WritableBufferedByteChannel wChannel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( wChannel );
encoder.systemProperties( ObjectUtils.systemProps(), NORMAL_RUN, 1L );
wChannel.close();

ReadableByteChannel channel = newChannel( new ByteArrayInputStream( out.toByteArray() ) );

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
PropertyEventAssertionListener listener = new PropertyEventAssertionListener();
notifier.setSystemPropertiesListener( listener );

EH eventHandler = new EH();
CountdownCloseable countdown = new CountdownCloseable( mock( Closeable.class ), 0 );
ConsoleLoggerMock logger = new ConsoleLoggerMock( false, false, false, false );
ForkNodeArgumentsMock arguments = new ForkNodeArgumentsMock( logger, new File( "" ) );
try ( EventConsumerThread t = new EventConsumerThread( "t", channel, eventHandler, countdown, arguments ) )
{
t.start();
notifier.notifyEvent( eventHandler.pullEvent() );
}

assertThat( logger.error ).isEmpty();
assertThat( logger.warning ).isEmpty();
assertThat( logger.info ).isEmpty();
assertThat( logger.debug ).isEmpty();

assertThat( logger.isCalled() )
.isFalse();
assertThat( arguments.isCalled() )
.isFalse();
assertThat( listener.called.get() )
.isTrue();
}

@Test
public void shouldHandleErrorAfterNullLine()
{
Expand Down Expand Up @@ -999,7 +920,7 @@ public void testReportEntryOperations( @FromDataPoints( "operation" ) String[] o
when( stackTraceWriter.writeTraceToString() ).thenReturn( exceptionStackTrace );
}

ReportEntry reportEntry = mock( ReportEntry.class );
TestSetReportEntry reportEntry = mock( TestSetReportEntry.class );
when( reportEntry.getElapsed() ).thenReturn( elapsed );
when( reportEntry.getGroup() ).thenReturn( "this group" );
when( reportEntry.getMessage() ).thenReturn( reportedMessage );
Expand All @@ -1009,12 +930,14 @@ public void testReportEntryOperations( @FromDataPoints( "operation" ) String[] o
when( reportEntry.getSourceName() ).thenReturn( "pkg.MyTest" );
when( reportEntry.getSourceText() ).thenReturn( "test class display name" );
when( reportEntry.getStackTraceWriter() ).thenReturn( stackTraceWriter );
when( reportEntry.getSystemProperties() ).thenReturn( systemProps() );

final Stream out = Stream.newStream();

EventChannelEncoder encoder = new EventChannelEncoder( newBufferedChannel( out ) );

EventChannelEncoder.class.getMethod( operation[0], ReportEntry.class, boolean.class )
Class<?> reportType = operation[0].startsWith( "testSet" ) ? TestSetReportEntry.class : ReportEntry.class;
EventChannelEncoder.class.getMethod( operation[0], reportType, boolean.class )
.invoke( encoder, reportEntry, trim );

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
Expand Down
Expand Up @@ -68,7 +68,6 @@ public void testSetStarting( TestSetReportEntry report )
@Override
public void testSetCompleted( TestSetReportEntry report )
{
target.systemProperties( report.getSystemProperties(), null, null );
target.testSetCompleted( report, trim );
}

Expand Down
Expand Up @@ -20,11 +20,9 @@
*/

import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.RunMode;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;

import java.util.Map;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

/**
* An abstraction for physical encoder of events.
Expand All @@ -44,30 +42,21 @@ public interface MasterProcessChannelEncoder
*/
void onJvmExit();

/**
* System properties after the test set has finished.
*
* @param sysProps system properties handled after the test set tear down
* @param runMode the run mode of particular test set
* @param testRunId the test set identification
*/
void systemProperties( Map<String, String> sysProps, RunMode runMode, Long testRunId );

/**
* The test set has started.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces );
void testSetStarting( TestSetReportEntry reportEntry, boolean trimStackTraces );

/**
* The test set has finished.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces );
void testSetCompleted( TestSetReportEntry reportEntry, boolean trimStackTraces );

/**
* The test has started.
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
import org.apache.maven.surefire.booter.stream.EventEncoder;

Expand Down Expand Up @@ -127,8 +128,7 @@ public void onJvmExit()
write( ByteBuffer.wrap( new byte[] {'\n'} ), true );
}

@Override
public void systemProperties( Map<String, String> sysProps, RunMode rm, Long testRunId )
private void encodeSystemProperties( Map<String, String> sysProps, RunMode rm, Long testRunId )
{
CharsetEncoder encoder = newCharsetEncoder();
ByteBuffer result = null;
Expand All @@ -145,19 +145,20 @@ public void systemProperties( Map<String, String> sysProps, RunMode rm, Long tes
// :maven-surefire-event:sys-prop:rerun-test-after-failure:UTF-8:<integer>:<key>:<integer>:<value>:
encode( encoder, result, BOOTERCODE_SYSPROPS, runMode, key, value );
boolean sync = !it.hasNext();
write( result, sync );
write( result, true );
}
}

@Override
public void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces )
public void testSetStarting( TestSetReportEntry reportEntry, boolean trimStackTraces )
{
encode( BOOTERCODE_TESTSET_STARTING, runMode, reportEntry, trimStackTraces, true );
}

@Override
public void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces )
public void testSetCompleted( TestSetReportEntry reportEntry, boolean trimStackTraces )
{
encodeSystemProperties( reportEntry.getSystemProperties(), null, null ); // todo in next commit
encode( BOOTERCODE_TESTSET_COMPLETED, runMode, reportEntry, trimStackTraces, true );
}

Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.util.internal.ObjectUtils;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
import org.junit.Test;

Expand All @@ -44,6 +44,7 @@
import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOut;
import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void reportEntry() throws IOException
when( stackTraceWriter.writeTrimmedTraceToString() ).thenReturn( trimmedStackTrace );
when( stackTraceWriter.writeTraceToString() ).thenReturn( stackTrace );

ReportEntry reportEntry = mock( ReportEntry.class );
TestSetReportEntry reportEntry = mock( TestSetReportEntry.class );
when( reportEntry.getElapsed() ).thenReturn( ELAPSED_TIME );
when( reportEntry.getGroup() ).thenReturn( "this group" );
when( reportEntry.getMessage() ).thenReturn( "skipped test" );
Expand Down Expand Up @@ -317,20 +318,51 @@ public void testSetCompleted() throws IOException
when( stackTraceWriter.writeTrimmedTraceToString() ).thenReturn( trimmedStackTrace );
when( stackTraceWriter.writeTraceToString() ).thenReturn( stackTrace );

ReportEntry reportEntry = mock( ReportEntry.class );
Map<String, String> props = systemProps();

TestSetReportEntry reportEntry = mock( TestSetReportEntry.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 );
when( reportEntry.getSystemProperties() ).thenReturn( props );

Stream out = Stream.newStream();
EventChannelEncoder encoder = new EventChannelEncoder( newBufferedChannel( out ) );

encoder.testSetCompleted( reportEntry, false );

ByteArrayOutputStream expectedFrame = new ByteArrayOutputStream();
for ( Entry<String, String> entry : props.entrySet() )
{
expectedFrame.write( ":maven-surefire-event:".getBytes() );
expectedFrame.write( 8 );
expectedFrame.write( ":sys-prop:".getBytes() );
expectedFrame.write( 10 );
expectedFrame.write( ":normal-run:".getBytes() );
expectedFrame.write( 5 );
expectedFrame.write( ":UTF-8:".getBytes() );
int[] k = toBytes( entry.getKey().length() );
expectedFrame.write( k[0] );
expectedFrame.write( k[1] );
expectedFrame.write( k[2] );
expectedFrame.write( k[3] );
expectedFrame.write( ':' );
expectedFrame.write( entry.getKey().getBytes( UTF_8 ) );
expectedFrame.write( ':' );
int[] v = toBytes( entry.getValue() == null ? 1 : entry.getValue().getBytes( UTF_8 ).length );
expectedFrame.write( v[0] );
expectedFrame.write( v[1] );
expectedFrame.write( v[2] );
expectedFrame.write( v[3] );
expectedFrame.write( ':' );
expectedFrame.write( ( entry.getValue() == null ? "\u0000" : entry.getValue() ).getBytes( UTF_8 ) );
expectedFrame.write( ':' );
}

expectedFrame.write( ":maven-surefire-event:".getBytes( UTF_8 ) );
expectedFrame.write( (byte) 17 );
expectedFrame.write( ":testset-completed:".getBytes( UTF_8 ) );
Expand Down Expand Up @@ -377,6 +409,7 @@ public void testSetCompleted() throws IOException
expectedFrame.write( ':' );
expectedFrame.write( stackTrace.getBytes( UTF_8 ) );
expectedFrame.write( ':' );

assertThat( out.toByteArray() )
.isEqualTo( expectedFrame.toByteArray() );
}
Expand Down Expand Up @@ -1142,41 +1175,6 @@ public void testStdErrStreamLn() throws IOException
.isEqualTo( expected );
}

@Test
@SuppressWarnings( "checkstyle:innerassignment" )
public void shouldCountSameNumberOfSystemProperties() throws IOException
{
Stream stream = Stream.newStream();
WritableBufferedByteChannel channel = newBufferedChannel( stream );
EventChannelEncoder encoder = new EventChannelEncoder( channel );

Map<String, String> sysProps = ObjectUtils.systemProps();
encoder.systemProperties( sysProps, NORMAL_RUN, 1L );
channel.close();

for ( Entry<String, String> entry : sysProps.entrySet() )
{
int[] k = toBytes( entry.getKey().length() );
int[] v = toBytes( entry.getValue() == null ? 1 : entry.getValue().getBytes( UTF_8 ).length );
ByteArrayOutputStream expectedFrame = new ByteArrayOutputStream();
expectedFrame.write( ":maven-surefire-event:sys-prop:normal-run:UTF-8:".getBytes( UTF_8 ) );
expectedFrame.write( k[0] );
expectedFrame.write( k[1] );
expectedFrame.write( k[2] );
expectedFrame.write( k[3] );
expectedFrame.write( ':' );
expectedFrame.write( v[0] );
expectedFrame.write( v[1] );
expectedFrame.write( v[2] );
expectedFrame.write( v[3] );
expectedFrame.write( ':' );
expectedFrame.write( ( entry.getValue() == null ? "\u0000" : entry.getValue() ).getBytes( UTF_8 ) );
expectedFrame.write( ':' );
assertThat( stream.toByteArray() )
.contains( expectedFrame.toByteArray() );
}
}

@Test
public void shouldHandleExit()
{
Expand Down

0 comments on commit 7f6b017

Please sign in to comment.