Repository: maven-surefire Updated Branches: refs/heads/master 27fbe2c19 -> 66bc4c083
[SUREFIRE-1322] Surefire and Failsafe should dump critical errors in dump file and console Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/66bc4c08 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/66bc4c08 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/66bc4c08 Branch: refs/heads/master Commit: 66bc4c0839ba11af7a8915930f76abf3cd58ee53 Parents: 27fbe2c Author: Tibor17 <tibo...@lycos.com> Authored: Sat Dec 31 10:25:00 2016 +0100 Committer: Tibor17 <tibo...@lycos.com> Committed: Sat Dec 31 10:25:00 2016 +0100 ---------------------------------------------------------------------- .../surefire/util/internal/DumpFileUtils.java | 2 +- ...e735ForkFailWithRedirectConsoleOutputIT.java | 32 +++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/66bc4c08/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DumpFileUtils.java ---------------------------------------------------------------------- diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DumpFileUtils.java b/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DumpFileUtils.java index 47a1386..2c110f5 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DumpFileUtils.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DumpFileUtils.java @@ -1 +1 @@ -package org.apache.maven.surefire.util.internal; /* * 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.ReporterConfiguration; import java.io.File; import java.io.FileOutputStream; import java.io.IOE xception; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; /** * Dumps a text or exception in dump file. * Each call logs a date when it was written to the dump file. * * @author <a href="mailto:tibordig...@apache.org">Tibor Digana (tibor17)</a> * @since 2.19.2 */ public final class DumpFileUtils { private DumpFileUtils() { throw new IllegalStateException( "no instantiable constructor" ); } /** * New dump file. Synchronized object appears in main memory and perfectly visible in other threads. */ public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration ) { return new File( configuration.getReportsDirectory(), dumpFileName ); } public static void dumpException( Throwable t, File dumpFile ) { dumpException( t, null, dumpFile ); } public static void dumpException ( Throwable t, String msg, File dumpFile ) { try { if ( t != null && dumpFile != null && ( dumpFile.exists() || dumpFile.createNewFile() ) ) { Writer fw = createWriter( dumpFile ); if ( msg != null ) { fw.append( msg ) .append( StringUtils.NL ); } PrintWriter pw = new PrintWriter( fw ); t.printStackTrace( pw ); pw.flush(); fw.append( StringUtils.NL ) .append( StringUtils.NL ); fw.flush(); fw.close(); } } catch ( Exception e ) { // do nothing } } public static void dumpText( String msg, File dumpFile ) { try { if ( msg != null && dumpFile != null && ( dumpFile.exists() || dumpFile.createNewFile() ) ) { Writer fw = createWriter( dumpFile ) .append( msg ) .append( StringUtils.NL ) .append( StringUtils.NL ); fw.flush(); fw.close(); } } catch ( Exception e ) { // do nothing } } public static String newFormattedDateFileName() { return new SimpleDateFormat( "yyyy-MM-dd'T'hh-mm-ss_SSS" ).format( new Date() ); } private static Writer createWriter( File dumpFile ) throws IOException { return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), "UTF-8" ) .append( "# Created on " ) .append( new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss.SSS" ).format( new Date() ) ) .append( StringUtils.NL ); } } \ No newline at end of file +package org.apache.maven.surefire.util.internal; /* * 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.ReporterConfiguration; import java.io.File; import java.io.FileOutputStream; import java.io.IOE xception; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; /** * Dumps a text or exception in dump file. * Each call logs a date when it was written to the dump file. * * @author <a href="mailto:tibordig...@apache.org">Tibor Digana (tibor17)</a> * @since 2.19.2 */ public final class DumpFileUtils { private DumpFileUtils() { throw new IllegalStateException( "no instantiable constructor" ); } /** * New dump file. Synchronized object appears in main memory and perfectly visible in other threads. */ public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration ) { return new File( configuration.getReportsDirectory(), dumpFileName ); } public static void dumpException( Throwable t, File dumpFile ) { dumpException( t, null, dumpFile ); } public static void dumpException ( Throwable t, String msg, File dumpFile ) { try { if ( t != null && dumpFile != null && ( dumpFile.exists() || dumpFile.createNewFile() ) ) { Writer fw = createWriter( dumpFile ); if ( msg != null ) { fw.append( msg ) .append( StringUtils.NL ); } PrintWriter pw = new PrintWriter( fw ); t.printStackTrace( pw ); pw.flush(); fw.append( StringUtils.NL ) .append( StringUtils.NL ); fw.flush(); fw.close(); } } catch ( Exception e ) { // do nothing } } public static void dumpText( String msg, File dumpFile ) { try { if ( msg != null && dumpFile != null && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) ) { Writer fw = createWriter( dumpFile ) .append( msg ) .append( StringUtils.NL ) .append( StringUtils.NL ); fw.flush(); fw.close(); } } catch ( Exception e ) { // do nothing } } public static String newFormattedDateFileName() { return new SimpleDateFormat( "yyyy-MM-dd'T'hh-mm-ss_SSS" ).format( new Date() ); } private static Writer createWriter( File dumpFile ) throws IOException { return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), "UTF-8" ) .append( "# Created on " ) .append( new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss.SSS" ).format( new Date() ) ) .append( StringUtils.NL ); } private static boolean mkdirs( File dumpFile ) { return dumpFile.getParentFile().mkdirs(); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/66bc4c08/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire735ForkFailWithRedirectConsoleOutputIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire735ForkFailWithRedirectConsoleOutputIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire735ForkFailWithRedirectConsoleOutputIT.java index cabb90e..4117d8c 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire735ForkFailWithRedirectConsoleOutputIT.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire735ForkFailWithRedirectConsoleOutputIT.java @@ -19,28 +19,50 @@ package org.apache.maven.surefire.its.jiras; * under the License. */ +import org.apache.maven.surefire.its.fixture.OutputValidator; import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; import org.apache.maven.surefire.its.fixture.SurefireLauncher; - import org.junit.Test; +import java.io.File; +import java.io.FilenameFilter; + +import static org.fest.assertions.Assertions.assertThat; + /** * @author Kristian Rosenvold */ public class Surefire735ForkFailWithRedirectConsoleOutputIT - extends SurefireJUnit4IntegrationTestCase + extends SurefireJUnit4IntegrationTestCase { @Test public void vmStartFail() - throws Exception + throws Exception { - unpack().failNever().executeTest().verifyTextInLog( "Invalid maximum heap size: -Xmxxxx712743m" ); + OutputValidator outputValidator = unpack().failNever().executeTest(); + + File reportDir = outputValidator.getSurefireReportsDirectory(); + String[] dumpFiles = reportDir.list( new FilenameFilter() + { + @Override + public boolean accept( File dir, String name ) + { + return name.endsWith( ".dumpstream" ); + } + } + ); + assertThat( dumpFiles ).isNotEmpty(); + for ( String dump : dumpFiles ) + { + outputValidator.getSurefireReportsFile( dump ) + .assertContainsText( "Invalid maximum heap size: -Xmxxxx712743m" ); + } } @Test public void vmStartFailShouldFailBuildk() - throws Exception + throws Exception { unpack().maven().withFailure().executeTest(); }