Skip to content
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

[SUREFIRE-2011] Updated abstractions which helps associating standard out/err with a test #469

Merged
merged 1 commit into from Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,9 +23,11 @@
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
import org.apache.maven.surefire.api.event.Event;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.extensions.EventHandler;
import org.apache.maven.surefire.api.booter.MasterProcessChannelEncoder;
import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.api.report.TestOutputReceiver;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.RunListener;
import org.apache.maven.surefire.api.report.RunMode;
Expand Down Expand Up @@ -78,7 +80,7 @@ public class ForkClient

private final int forkNumber;

private volatile RunListener testSetReporter;
private volatile TestReportListener testSetReporter;

/**
* Written by one Thread and read by another: Main Thread and ForkStarter's Thread.
Expand Down Expand Up @@ -379,15 +381,15 @@ public final boolean hadTimeout()
/**
* Only {@link #getConsoleOutputReceiver()} may call this method in another Thread.
*/
private RunListener getTestSetReporter()
private TestReportListener getTestSetReporter()
{
if ( testSetReporter == null )
{
synchronized ( this )
{
if ( testSetReporter == null )
{
testSetReporter = defaultReporterFactory.createReporter();
testSetReporter = defaultReporterFactory.createTestReportListener();
}
}
}
Expand All @@ -404,7 +406,7 @@ void dumpToLoFile( String msg )
private void writeTestOutput( String output, boolean newLine, boolean isStdout )
{
getConsoleOutputReceiver()
.writeTestOutput( output, newLine, isStdout );
.writeTestOutput( new TestOutputReportEntry( output, isStdout, newLine, /*todo*/ null, null ) );
}

public final Map<String, String> getTestVmSystemProperties()
Expand All @@ -423,14 +425,14 @@ public final RunListener getReporter()
return getTestSetReporter();
}

public ConsoleOutputReceiver getConsoleOutputReceiver()
public TestOutputReceiver getConsoleOutputReceiver()
{
return (ConsoleOutputReceiver) getTestSetReporter();
return getTestSetReporter();
}

private ConsoleLogger getOrCreateConsoleLogger()
{
return (ConsoleLogger) getTestSetReporter();
return getTestSetReporter();
}
Comment on lines 433 to 436
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use getTestSetReporter() instead of getOrCreateConsoleLogger() - private method

Copy link
Contributor Author

@Tibor17 Tibor17 Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this PR has only limited scope of refactoring. This PR is aming for refactoring of the interfaces of the listeners and the reason is the fact that we are not able to associate system output with the Thread of the test without refactoring of these interfaces.


public void close( boolean hadTimeout )
Expand Down
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -88,7 +89,7 @@ public synchronized void close()
}

@Override
public synchronized void writeTestOutput( String output, boolean newLine, boolean stdout )
public synchronized void writeTestOutput( TestOutputReportEntry reportEntry )
{
try
{
Expand All @@ -109,14 +110,14 @@ public synchronized void writeTestOutput( String output, boolean newLine, boolea
os = new BufferedOutputStream( new FileOutputStream( file ), STREAM_BUFFER_SIZE );
fileOutputStream.set( os, OPEN );
}

String output = reportEntry.getLog();
if ( output == null )
{
output = "null";
}
Charset charset = Charset.forName( encoding );
os.write( output.getBytes( charset ) );
if ( newLine )
if ( reportEntry.isNewLine() )
{
os.write( NL.getBytes( charset ) );
}
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
import org.apache.maven.surefire.extensions.StatelessTestsetInfoFileReportEventListener;
import org.apache.maven.surefire.api.report.ReporterFactory;
import org.apache.maven.surefire.api.report.RunListener;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.report.RunStatistics;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.suite.RunResult;
Expand Down Expand Up @@ -97,7 +97,7 @@ public DefaultReporterFactory( StartupReportConfiguration reportConfiguration, C
}

@Override
public RunListener createReporter()
public TestReportListener createTestReportListener()
{
TestSetRunListener testSetRunListener =
new TestSetRunListener( createConsoleReporter(),
Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

import java.io.PrintStream;
Expand Down Expand Up @@ -47,16 +48,16 @@ public DirectConsoleOutput( PrintStream out, PrintStream err )
}

@Override
public void writeTestOutput( String output, boolean newLine, boolean stdout )
public void writeTestOutput( TestOutputReportEntry reportEntry )
{
PrintStream stream = stdout ? out : err;
if ( newLine )
PrintStream stream = reportEntry.isStdOut() ? out : err;
if ( reportEntry.isNewLine() )
{
stream.println( output );
stream.println( reportEntry.getLog() );
}
else
{
stream.print( output );
stream.print( reportEntry.getLog() );
}
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public void close()
}

@Override
public void writeTestOutput( String output, boolean newLine, boolean stdout )
public void writeTestOutput( TestOutputReportEntry reportEntry )
{

}
Expand Down
Expand Up @@ -25,16 +25,15 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.extensions.ConsoleOutputReportEventListener;
import org.apache.maven.surefire.extensions.StatelessReportEventListener;
import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
import org.apache.maven.surefire.extensions.StatelessTestsetInfoFileReportEventListener;
import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.RunListener;
import org.apache.maven.surefire.api.report.RunMode;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

import static org.apache.maven.plugin.surefire.report.ReportEntryType.ERROR;
Expand All @@ -51,13 +50,13 @@
* @author Kristian Rosenvold
*/
public class TestSetRunListener
implements RunListener, ConsoleOutputReceiver, ConsoleLogger
implements TestReportListener
{
private final Queue<TestMethodStats> testMethodStats = new ConcurrentLinkedQueue<>();

private final TestSetStats detailsForThis;

private final ConsoleOutputReportEventListener consoleOutputReceiver;
private final ConsoleOutputReportEventListener testOutputReceiver;

private final boolean briefOrPlainFormat;

Expand All @@ -83,15 +82,15 @@ public TestSetRunListener( StatelessTestsetInfoConsoleReportEventListener<Wrappe
StatelessTestsetInfoFileReportEventListener<WrappedReportEntry, TestSetStats>
fileReporter,
StatelessReportEventListener<WrappedReportEntry, TestSetStats> simpleXMLReporter,
ConsoleOutputReportEventListener consoleOutputReceiver,
ConsoleOutputReportEventListener testOutputReceiver,
StatisticsReporter statisticsReporter, boolean trimStackTrace,
boolean isPlainFormat, boolean briefOrPlainFormat, Object lock )
{
this.consoleReporter = consoleReporter;
this.fileReporter = fileReporter;
this.statisticsReporter = statisticsReporter;
this.simpleXMLReporter = simpleXMLReporter;
this.consoleOutputReceiver = consoleOutputReceiver;
this.testOutputReceiver = testOutputReceiver;
this.briefOrPlainFormat = briefOrPlainFormat;
detailsForThis = new TestSetStats( trimStackTrace, isPlainFormat );
this.lock = lock;
Expand Down Expand Up @@ -176,15 +175,15 @@ public void error( Throwable t )
}

@Override
public void writeTestOutput( String output, boolean newLine, boolean stdout )
public void writeTestOutput( TestOutputReportEntry reportEntry )
{
try
{
synchronized ( lock )
{
Utf8RecodingDeferredFileOutputStream stream = stdout ? testStdOut : testStdErr;
stream.write( output, newLine );
consoleOutputReceiver.writeTestOutput( output, newLine, stdout );
Utf8RecodingDeferredFileOutputStream stream = reportEntry.isStdOut() ? testStdOut : testStdErr;
stream.write( reportEntry.getLog(), reportEntry.isNewLine() );
testOutputReceiver.writeTestOutput( reportEntry );
}
}
catch ( IOException e )
Expand All @@ -198,7 +197,7 @@ public void testSetStarting( TestSetReportEntry report )
{
detailsForThis.testSetStart();
consoleReporter.testSetStarting( report );
consoleOutputReceiver.testSetStarting( report );
testOutputReceiver.testSetStarting( report );
}

private void clearCapture()
Expand All @@ -217,7 +216,7 @@ public void testSetCompleted( TestSetReportEntry report )
simpleXMLReporter.testSetCompleted( wrap, detailsForThis );
statisticsReporter.testSetCompleted();
consoleReporter.testSetCompleted( wrap, detailsForThis, testResults );
consoleOutputReceiver.testSetCompleted( wrap );
testOutputReceiver.testSetCompleted( wrap );
consoleReporter.reset();

wrap.getStdout().free();
Expand Down Expand Up @@ -317,7 +316,7 @@ private WrappedReportEntry wrapTestSet( TestSetReportEntry other )

public void close()
{
consoleOutputReceiver.close();
testOutputReceiver.close();
}

private void addTestMethodStats()
Expand Down
Expand Up @@ -19,15 +19,15 @@
* under the License.
*/

import org.apache.maven.surefire.api.report.TestOutputReceiver;
import org.apache.maven.surefire.extensions.ConsoleOutputReportEventListener;
import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

/**
* @author Kristian Rosenvold
*/
public interface TestcycleConsoleOutputReceiver
extends ConsoleOutputReceiver, ConsoleOutputReportEventListener
extends TestOutputReceiver, ConsoleOutputReportEventListener
{
void testSetStarting( TestSetReportEntry reportEntry );

Expand Down
Expand Up @@ -31,14 +31,15 @@
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
import org.apache.maven.surefire.api.report.CategorizedReportEntry;
import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.api.report.LegacyPojoStackTraceWriter;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.ReporterException;
import org.apache.maven.surefire.api.report.RunListener;
import org.apache.maven.surefire.api.report.SimpleReportEntry;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReceiver;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;

import javax.annotation.Nonnull;
Expand All @@ -57,6 +58,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOut;
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.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -172,16 +174,16 @@ public void testAssumptionFailure() throws Exception
public void testConsole() throws Exception
{
final StandardTestRun standardTestRun = new StandardTestRun();
ConsoleLogger directConsoleReporter = (ConsoleLogger) standardTestRun.run();
ConsoleLogger directConsoleReporter = standardTestRun.run();
directConsoleReporter.info( "HeyYou" );
standardTestRun.assertExpected( MockReporter.CONSOLE_INFO, "HeyYou" );
}

public void testConsoleOutput() throws Exception
{
final StandardTestRun standardTestRun = new StandardTestRun();
ConsoleOutputReceiver directConsoleReporter = (ConsoleOutputReceiver) standardTestRun.run();
directConsoleReporter.writeTestOutput( "HeyYou", false, true );
TestOutputReceiver directConsoleReporter = standardTestRun.run();
directConsoleReporter.writeTestOutput( stdOut( "HeyYou" ) );
standardTestRun.assertExpected( MockReporter.STDOUT, "HeyYou" );
}

Expand Down Expand Up @@ -461,7 +463,7 @@ private SimpleReportEntry createReportEntryWithSpecialMessage( String message )
}
}

private RunListener createForkingRunListener()
private TestReportListener createForkingRunListener()
{
WritableBufferedByteChannel channel = (WritableBufferedByteChannel) newChannel( printStream );
return new ForkingRunListener( new EventChannelEncoder( channel ), false );
Expand All @@ -471,7 +473,7 @@ private class StandardTestRun
{
private MockReporter reporter;

public RunListener run()
public TestReportListener run()
throws ReporterException
{
reset();
Expand Down
Expand Up @@ -19,10 +19,9 @@
* under the License.
*/

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.RunListener;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.report.RunMode;

Expand All @@ -34,7 +33,7 @@
* Internal tests use only.
*/
public class MockReporter
implements RunListener, ConsoleLogger, ConsoleOutputReceiver
implements TestReportListener
{
private final List<String> events = new ArrayList<>();

Expand Down Expand Up @@ -236,9 +235,10 @@ public void error( Throwable t )
}

@Override
public void writeTestOutput( String output, boolean newLine, boolean stdout )
public void writeTestOutput( TestOutputReportEntry reportEntry )
{
events.add( stdout ? STDOUT : STDERR );
data.add( newLine ? output + "\n" : output );
events.add( reportEntry.isStdOut() ? STDOUT : STDERR );
String output = reportEntry.getLog();
data.add( reportEntry.isNewLine() ? output + "\n" : output );
}
}