You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/04/21 23:24:27 UTC

[maven-surefire] 08/10: [SUREFIRE-1759] NullPointerException from RunEntryStatisticsMap#serialize when there's a class-level @Ignore annotation

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit df7f08a55e1d11ae0a24e653800c28d107df9678
Author: tibordigana <ti...@apache.org>
AuthorDate: Tue Apr 21 01:04:10 2020 +0200

    [SUREFIRE-1759] NullPointerException from RunEntryStatisticsMap#serialize when there's a class-level @Ignore annotation
---
 .../runorder/RunEntryStatisticsMapTest.java        | 47 ++++++++++++++++++++++
 .../surefire/runorder/RunEntryStatisticsMap.java   | 29 ++++++++-----
 2 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMapTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMapTest.java
index b4ffd1a..0997f0a 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMapTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMapTest.java
@@ -24,18 +24,22 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.SimpleReportEntry;
 
 import junit.framework.TestCase;
+import org.apache.maven.surefire.util.internal.ClassMethod;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.maven.surefire.shared.io.IOUtils.readLines;
 import static org.apache.maven.surefire.util.internal.StringUtils.NL;
 import static org.fest.assertions.Assertions.assertThat;
+import static org.powermock.reflect.Whitebox.getInternalState;
 
 /**
  * @author Kristian Rosenvold
@@ -75,6 +79,49 @@ public class RunEntryStatisticsMapTest
     }
 
     @SuppressWarnings( "checkstyle:magicnumber" )
+    public void testSerializeClass()
+        throws Exception
+    {
+        File data = File.createTempFile( "surefire-unit", "test" );
+        RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
+        ReportEntry reportEntry = new SimpleReportEntry( "abc", null, null, null, 42 );
+        newResults.add( newResults.createNextGeneration( reportEntry ) );
+        newResults.serialize( data );
+        try ( InputStream io = new FileInputStream( data ) )
+        {
+            List<String> lines = readLines( io, UTF_8 );
+
+            assertThat( lines )
+                .hasSize( 1 );
+
+            assertThat( lines )
+                .containsSequence( "1,42,abc," );
+        }
+    }
+
+    @SuppressWarnings( "checkstyle:magicnumber" )
+    public void testDeserializeClass()
+        throws Exception
+    {
+        File data = File.createTempFile( "surefire-unit", "test" );
+        Files.write( data.toPath(), "1,42,abc".getBytes( UTF_8 ) );
+        RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
+        Map<?, ?> runEntryStatistics = getInternalState( existingEntries, "runEntryStatistics" );
+        assertThat( runEntryStatistics )
+            .hasSize( 1 );
+        ClassMethod cm = (ClassMethod) runEntryStatistics.keySet().iterator().next();
+        assertThat( cm.getClazz() )
+            .isEqualTo( "abc" );
+        assertThat( cm.getMethod() )
+            .isNull();
+        RunEntryStatistics statistics = (RunEntryStatistics) runEntryStatistics.values().iterator().next();
+        assertThat( statistics.getRunTime() )
+            .isEqualTo( 42 );
+        assertThat( statistics.getSuccessfulBuilds() )
+            .isEqualTo( 1 );
+    }
+
+    @SuppressWarnings( "checkstyle:magicnumber" )
     public void testSerialize()
         throws Exception
     {
diff --git a/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java b/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java
index 09a4445..fb80dd0 100644
--- a/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java
+++ b/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java
@@ -38,7 +38,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Scanner;
-import java.util.StringTokenizer;
 import java.util.concurrent.ConcurrentHashMap;
 
 import static java.lang.Integer.parseInt;
@@ -105,25 +104,26 @@ public final class RunEntryStatisticsMap
                     {
                         result.put( previous.getClassMethod(), previous );
                     }
-                    StringTokenizer tokenizer = new StringTokenizer( line, "," );
 
-                    int methodIndex = 3;
+                    int to = line.indexOf( ',' );
 
-                    String successfulBuildsString = tokenizer.nextToken();
+                    String successfulBuildsString = line.substring( 0, to );
                     int successfulBuilds = parseInt( successfulBuildsString );
 
-                    methodIndex += successfulBuildsString.length();
+                    int from = 1 + to;
+                    to = line.indexOf( ',', from + 1 );
 
-                    String runTimeString = tokenizer.nextToken();
+                    String runTimeString = line.substring( from, to );
                     int runTime = parseInt( runTimeString );
 
-                    methodIndex += runTimeString.length();
+                    from = 1 + to;
+                    to = line.indexOf( ',', from + 1 );
 
-                    String className = tokenizer.nextToken();
+                    String className = to == -1 ? line.substring( from ) : line.substring( from, to );
 
-                    methodIndex += className.length();
+                    from = 1 + to;
 
-                    String methodName = line.substring( methodIndex );
+                    String methodName = to == -1 ? null : line.substring( from );
 
                     ClassMethod classMethod = new ClassMethod( className, methodName );
                     previous = new RunEntryStatistics( runTime, successfulBuilds, classMethod );
@@ -157,7 +157,13 @@ public final class RunEntryStatisticsMap
                 String line = item.getSuccessfulBuilds() + "," + item.getRunTime() + "," + test.getClazz() + ",";
                 writer.write( line );
                 boolean wasFirstLine = false;
-                for ( Scanner scanner = new Scanner( test.getMethod() ); scanner.hasNextLine(); wasFirstLine = true )
+                String method = test.getMethod();
+                if ( method == null )
+                {
+                    continue;
+                }
+
+                for ( Scanner scanner = new Scanner( method ); scanner.hasNextLine(); wasFirstLine = true )
                 {
                     String methodLine = scanner.nextLine();
                     if ( wasFirstLine )
@@ -170,6 +176,7 @@ public final class RunEntryStatisticsMap
                         writer.newLine();
                     }
                 }
+
                 if ( it.hasNext() )
                 {
                     writer.newLine();