[SUREFIRE-1293] Simplify org.apache.maven.plugin.surefire.report.TestSetRunListener by using the null object pattern
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/fd9f6e55 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/fd9f6e55 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/fd9f6e55 Branch: refs/heads/junit5 Commit: fd9f6e5598e7ce6cdb4168ce15cf6ca138b7753f Parents: 77cafbe Author: Benedikt Ritter <[email protected]> Authored: Sun Oct 9 16:41:49 2016 +0200 Committer: Tibor17 <[email protected]> Committed: Wed Oct 12 11:26:20 2016 +0200 ---------------------------------------------------------------------- .../surefire/report/DefaultReporterFactory.java | 46 +++++++++-- .../report/NullConsoleOutputReceiver.java | 55 +++++++++++++ .../surefire/report/NullConsoleReporter.java | 58 +++++++++++++ .../surefire/report/NullFileReporter.java | 45 ++++++++++ .../report/NullStatelessXmlReporter.java | 48 +++++++++++ .../surefire/report/NullStatisticsReporter.java | 66 +++++++++++++++ .../surefire/report/TestSetRunListener.java | 87 ++++---------------- .../surefire/runorder/StatisticsReporter.java | 11 ++- 8 files changed, 335 insertions(+), 81 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java index a2bc7ec..e011fee 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java @@ -20,9 +20,9 @@ package org.apache.maven.plugin.surefire.report; */ import org.apache.maven.plugin.surefire.StartupReportConfiguration; -import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger; +import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; import org.apache.maven.shared.utils.logging.MessageBuilder; import org.apache.maven.surefire.report.ReporterFactory; import org.apache.maven.surefire.report.RunListener; @@ -39,6 +39,7 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentLinkedQueue; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; import static org.apache.maven.plugin.surefire.report.ConsoleReporter.PLAIN; import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.error; import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.failure; @@ -64,7 +65,6 @@ public class DefaultReporterFactory { private final StartupReportConfiguration reportConfiguration; private final ConsoleLogger consoleLogger; - private final StatisticsReporter statisticsReporter; private final Collection<TestSetRunListener> listeners; private RunStatistics globalStats = new RunStatistics(); @@ -82,19 +82,17 @@ public class DefaultReporterFactory { this.reportConfiguration = reportConfiguration; this.consoleLogger = consoleLogger; - statisticsReporter = reportConfiguration.getStatisticsReporter(); listeners = new ConcurrentLinkedQueue<TestSetRunListener>(); } public RunListener createReporter() { - ConsoleReporter consoleReporter = shouldReportToConsole() ? new ConsoleReporter( consoleLogger ) : null; TestSetRunListener testSetRunListener = - new TestSetRunListener( consoleReporter, - reportConfiguration.instantiateFileReporter(), - reportConfiguration.instantiateStatelessXmlReporter(), - reportConfiguration.instantiateConsoleOutputFileReporter(), - statisticsReporter, + new TestSetRunListener( createConsoleReporter(), + createFileReporter(), + createSimpleXMLReporter(), + createConsoleOutputReceiver(), + createStatisticsReporter(), reportConfiguration.isTrimStackTrace(), PLAIN.equals( reportConfiguration.getReportFormat() ), reportConfiguration.isBriefOrPlainFormat() ); @@ -102,6 +100,36 @@ public class DefaultReporterFactory return testSetRunListener; } + private ConsoleReporter createConsoleReporter() + { + return shouldReportToConsole() ? new ConsoleReporter( consoleLogger ) : NullConsoleReporter.INSTANCE; + } + + private FileReporter createFileReporter() + { + final FileReporter fileReporter = reportConfiguration.instantiateFileReporter(); + return defaultIfNull( fileReporter, NullFileReporter.INSTANCE ); + } + + private StatelessXmlReporter createSimpleXMLReporter() + { + final StatelessXmlReporter xmlReporter = reportConfiguration.instantiateStatelessXmlReporter(); + return defaultIfNull( xmlReporter, NullStatelessXmlReporter.INSTANCE ); + } + + private TestcycleConsoleOutputReceiver createConsoleOutputReceiver() + { + final TestcycleConsoleOutputReceiver consoleOutputReceiver = + reportConfiguration.instantiateConsoleOutputFileReporter(); + return defaultIfNull( consoleOutputReceiver, NullConsoleOutputReceiver.INSTANCE ); + } + + private StatisticsReporter createStatisticsReporter() + { + final StatisticsReporter statisticsReporter = reportConfiguration.getStatisticsReporter(); + return defaultIfNull( statisticsReporter, NullStatisticsReporter.INSTANCE ); + } + private boolean shouldReportToConsole() { return reportConfiguration.isUseFile() http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleOutputReceiver.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleOutputReceiver.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleOutputReceiver.java new file mode 100644 index 0000000..55e4ee2 --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleOutputReceiver.java @@ -0,0 +1,55 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.surefire.report.ReportEntry; + +/** + * ConsoleReporter doing nothing rather than using null. + * + * @author <a href="mailto:[email protected]">Benedikt Ritter</a> + * @since 2.19.2 + */ +class NullConsoleOutputReceiver + implements TestcycleConsoleOutputReceiver +{ + + static final NullConsoleOutputReceiver INSTANCE = new NullConsoleOutputReceiver(); + + private NullConsoleOutputReceiver() + { + } + + public void testSetStarting( ReportEntry reportEntry ) + { + } + + public void testSetCompleted( ReportEntry report ) + { + } + + public void close() + { + } + + public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) + { + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleReporter.java new file mode 100644 index 0000000..1bd4e9a --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullConsoleReporter.java @@ -0,0 +1,58 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.List; + +import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger; +import org.apache.maven.surefire.report.ReportEntry; + +/** + * ConsoleReporter doing nothing rather than using null. + * + * @author <a href="mailto:[email protected]">Benedikt Ritter</a> + * @since 2.19.2 + */ +class NullConsoleReporter + extends ConsoleReporter +{ + + static final NullConsoleReporter INSTANCE = new NullConsoleReporter(); + + private NullConsoleReporter() + { + super( new NullConsoleLogger() ); + } + + @Override + public void testSetStarting( ReportEntry report ) + { + } + + @Override + public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults ) + { + } + + @Override + public void reset() + { + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullFileReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullFileReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullFileReporter.java new file mode 100644 index 0000000..bf7e3ef --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullFileReporter.java @@ -0,0 +1,45 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.List; + +/** + * FileReporter doing nothing rather than using null. + * + * @author <a href="mailto:[email protected]">Benedikt Ritter</a> + * @since 2.19.2 + */ +class NullFileReporter + extends FileReporter +{ + + static final NullFileReporter INSTANCE = new NullFileReporter(); + + private NullFileReporter() + { + super( null, null ); + } + + @Override + public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults ) + { + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java new file mode 100644 index 0000000..5895c8a --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java @@ -0,0 +1,48 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * FileReporter doing nothing rather than using null. + * + * @author <a href="mailto:[email protected]">Benedikt Ritter</a> + * @since 2.19.2 + */ +class NullStatelessXmlReporter + extends StatelessXmlReporter +{ + + static final NullStatelessXmlReporter INSTANCE = new NullStatelessXmlReporter(); + + private NullStatelessXmlReporter() + { + super( null, null, false, 0, null, null ); + } + + @Override + public void testSetCompleted( WrappedReportEntry testSetReportEntry, TestSetStats testSetStats ) + { + } + + @Override + public void cleanTestHistoryMap() + { + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatisticsReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatisticsReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatisticsReporter.java new file mode 100644 index 0000000..5e355ca --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatisticsReporter.java @@ -0,0 +1,66 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; +import org.apache.maven.surefire.report.ReportEntry; + +/** + * StatisticsReporter doing nothing rather than using null. + * + * @author <a href="mailto:[email protected]">Benedikt Ritter</a> + * @since 2.19.2 + */ +class NullStatisticsReporter + extends StatisticsReporter +{ + + static final NullStatisticsReporter INSTANCE = new NullStatisticsReporter(); + + private NullStatisticsReporter() + { + super( null, null, null ); + } + + @Override + public synchronized void testSetCompleted() + { + } + + @Override + public void testSucceeded( ReportEntry report ) + { + } + + @Override + public void testSkipped( ReportEntry report ) + { + } + + @Override + public void testError( ReportEntry report ) + { + } + + @Override + public void testFailed( ReportEntry report ) + { + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java index 4b12884..dbc802c 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java @@ -24,8 +24,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; +import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; import org.apache.maven.surefire.report.ConsoleOutputReceiver; import org.apache.maven.surefire.report.ReportEntry; import org.apache.maven.surefire.report.RunListener; @@ -88,50 +88,32 @@ public class TestSetRunListener public void debug( String message ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().debug( trimTrailingNewLine( message ) ); - } + consoleReporter.getConsoleLogger().debug( trimTrailingNewLine( message ) ); } public void info( String message ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().info( trimTrailingNewLine( message ) ); - } + consoleReporter.getConsoleLogger().info( trimTrailingNewLine( message ) ); } public void warning( String message ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().warning( trimTrailingNewLine( message ) ); - } + consoleReporter.getConsoleLogger().warning( trimTrailingNewLine( message ) ); } public void error( String message ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().error( trimTrailingNewLine( message ) ); - } + consoleReporter.getConsoleLogger().error( trimTrailingNewLine( message ) ); } public void error( String message, Throwable t ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().error( message, t ); - } + consoleReporter.getConsoleLogger().error( message, t ); } public void error( Throwable t ) { - if ( consoleReporter != null ) - { - consoleReporter.getConsoleLogger().error( t ); - } + consoleReporter.getConsoleLogger().error( t ); } public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) @@ -157,10 +139,7 @@ public class TestSetRunListener public void testSetStarting( ReportEntry report ) { detailsForThis.testSetStart(); - if ( consoleReporter != null ) - { - consoleReporter.testSetStarting( report ); - } + consoleReporter.testSetStarting( report ); consoleOutputReceiver.testSetStarting( report ); } @@ -175,27 +154,12 @@ public class TestSetRunListener final WrappedReportEntry wrap = wrapTestSet( report ); final List<String> testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : Collections.<String>emptyList(); - if ( fileReporter != null ) - { - fileReporter.testSetCompleted( wrap, detailsForThis, testResults ); - } - if ( simpleXMLReporter != null ) - { - simpleXMLReporter.testSetCompleted( wrap, detailsForThis ); - } - if ( statisticsReporter != null ) - { - statisticsReporter.testSetCompleted(); - } - if ( consoleReporter != null ) - { - consoleReporter.testSetCompleted( wrap, detailsForThis, testResults ); - } + fileReporter.testSetCompleted( wrap, detailsForThis, testResults ); + simpleXMLReporter.testSetCompleted( wrap, detailsForThis ); + statisticsReporter.testSetCompleted(); + consoleReporter.testSetCompleted( wrap, detailsForThis, testResults ); consoleOutputReceiver.testSetCompleted( wrap ); - if ( consoleReporter != null ) - { - consoleReporter.reset(); - } + consoleReporter.reset(); wrap.getStdout().free(); wrap.getStdErr().free(); @@ -218,10 +182,7 @@ public class TestSetRunListener { WrappedReportEntry wrapped = wrap( reportEntry, SUCCESS ); detailsForThis.testSucceeded( wrapped ); - if ( statisticsReporter != null ) - { - statisticsReporter.testSucceeded( reportEntry ); - } + statisticsReporter.testSucceeded( reportEntry ); clearCapture(); } @@ -229,10 +190,7 @@ public class TestSetRunListener { WrappedReportEntry wrapped = wrap( reportEntry, ERROR ); detailsForThis.testError( wrapped ); - if ( statisticsReporter != null ) - { - statisticsReporter.testError( reportEntry ); - } + statisticsReporter.testError( reportEntry ); clearCapture(); } @@ -240,10 +198,7 @@ public class TestSetRunListener { WrappedReportEntry wrapped = wrap( reportEntry, FAILURE ); detailsForThis.testFailure( wrapped ); - if ( statisticsReporter != null ) - { - statisticsReporter.testFailed( reportEntry ); - } + statisticsReporter.testFailed( reportEntry ); clearCapture(); } @@ -256,10 +211,7 @@ public class TestSetRunListener WrappedReportEntry wrapped = wrap( reportEntry, SKIPPED ); detailsForThis.testSkipped( wrapped ); - if ( statisticsReporter != null ) - { - statisticsReporter.testSkipped( reportEntry ); - } + statisticsReporter.testSkipped( reportEntry ); clearCapture(); } @@ -303,10 +255,7 @@ public class TestSetRunListener public void close() { - if ( consoleOutputReceiver != null ) - { - consoleOutputReceiver.close(); - } + consoleOutputReceiver.close(); } public void addTestMethodStats() http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/fd9f6e55/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java index 9dd3380..a53db02 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java @@ -28,7 +28,7 @@ import static org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMap.fr /** * @author Kristian Rosenvold */ -public final class StatisticsReporter +public class StatisticsReporter { private final RunEntryStatisticsMap existing; @@ -38,9 +38,14 @@ public final class StatisticsReporter public StatisticsReporter( File dataFile ) { + this( dataFile, fromFile( dataFile ), new RunEntryStatisticsMap() ); + } + + protected StatisticsReporter( File dataFile, RunEntryStatisticsMap existing, RunEntryStatisticsMap newRestuls ) + { this.dataFile = dataFile; - existing = fromFile( dataFile ); - newResults = new RunEntryStatisticsMap(); + this.existing = existing; + this.newResults = newRestuls; } public synchronized void testSetCompleted()
