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/09/21 00:33:26 UTC

[maven-surefire] 01/04: fixed method readInt() and tests

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

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

commit 520bd6cb872e0595e335162be11621e79eef5964
Author: tibordigana <ti...@apache.org>
AuthorDate: Mon Sep 21 02:31:20 2020 +0200

    fixed method readInt() and tests
---
 .../surefire/extensions/EventConsumerThread.java   | 35 ++++++++++++-----
 .../extensions/EventConsumerThreadTest.java        | 45 +++++++++++++++++++++-
 2 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java
index d16aac0..5f38529 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java
@@ -211,8 +211,11 @@ public class EventConsumerThread extends CloseableDaemonThread
             try
             {
                 ForkedProcessEventType eventType = readEventType( eventTypes, memento );
+                if ( eventType == null )
+                {
+                    throw new MalformedFrameException( memento.line.positionByteBuffer, memento.bb.position() );
+                }
                 RunMode runMode = null;
-
                 for ( SegmentType segmentType : nextSegmentType( eventType ) )
                 {
                     if ( segmentType == null )
@@ -232,7 +235,7 @@ public class EventConsumerThread extends CloseableDaemonThread
                             memento.data.add( readString( memento ) );
                             break;
                         case DATA_INT:
-                            memento.data.add( readInt( memento ) );
+                            memento.data.add( readInteger( memento ) );
                             break;
                         case END_OF_FRAME:
                             memento.line.positionByteBuffer = memento.bb.position();
@@ -257,12 +260,10 @@ public class EventConsumerThread extends CloseableDaemonThread
             }
             catch ( RuntimeException e )
             {
-                printCorruptedStream( memento );
                 arguments.dumpStreamException( e );
             }
             catch ( IOException e )
             {
-                printCorruptedStream( memento );
                 printRemainingStream( memento );
                 throw e;
             }
@@ -287,8 +288,6 @@ public class EventConsumerThread extends CloseableDaemonThread
     protected String readString( Memento memento ) throws IOException, MalformedFrameException
     {
         memento.cb.clear();
-        read( memento, INT_LENGTH + DELIMITER_LENGTH );
-
         int readCount = readInt( memento );
         read( memento, readCount + DELIMITER_LENGTH );
 
@@ -525,9 +524,12 @@ public class EventConsumerThread extends CloseableDaemonThread
 
     private static void printCorruptedStream( Memento memento )
     {
-        if ( memento.bb.hasRemaining() )
+        ByteBuffer bb = memento.bb;
+        if ( bb.hasRemaining() )
         {
-            memento.line.write( memento.bb, memento.bb.position(), memento.bb.remaining() );
+            int bytesToWrite = bb.remaining();
+            memento.line.write( bb, bb.position(), bytesToWrite );
+            bb.position( bb.position() + bytesToWrite );
         }
     }
 
@@ -538,7 +540,9 @@ public class EventConsumerThread extends CloseableDaemonThread
      */
     private static void printRemainingStream( Memento memento )
     {
+        printCorruptedStream( memento );
         memento.line.printExistingLine();
+        memento.line.count = 0;
     }
 
     @Nonnull
@@ -670,12 +674,25 @@ public class EventConsumerThread extends CloseableDaemonThread
 
     protected int readInt( Memento memento ) throws IOException, MalformedFrameException
     {
-        read( memento, INT_LENGTH );
+        read( memento, INT_LENGTH + DELIMITER_LENGTH );
         int i = memento.bb.getInt();
         checkDelimiter( memento );
         return i;
     }
 
+    protected Integer readInteger( Memento memento ) throws IOException, MalformedFrameException
+    {
+        read( memento, BYTE_LENGTH );
+        boolean isNullObject = memento.bb.get() == 0;
+        if ( isNullObject )
+        {
+            read( memento, DELIMITER_LENGTH );
+            checkDelimiter( memento );
+            return null;
+        }
+        return readInt( memento );
+    }
+
     private static String toString( List<String> strings )
     {
         if ( strings.size() == 1 )
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThreadTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThreadTest.java
index bfc41e8..ecf23c2 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThreadTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThreadTest.java
@@ -38,6 +38,7 @@ import javax.annotation.Nonnull;
 import java.io.Closeable;
 import java.io.EOFException;
 import java.io.File;
+import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.channels.ReadableByteChannel;
@@ -217,6 +218,48 @@ public class EventConsumerThreadTest
         invokeMethod( EventConsumerThread.class, "decodeString", decoder, input, output, bytesToDecode, true, 0 );
     }
 
+    @Test
+    public void shouldReadInt() throws Exception
+    {
+        Channel channel = new Channel( new byte[] {0x01, 0x02, 0x03, 0x04, ':'}, 1 );
+
+        EventConsumerThread thread = new EventConsumerThread( "t", channel,
+            new MockEventHandler<Event>(), COUNTDOWN_CLOSEABLE, new MockForkNodeArguments() );
+
+        Memento memento = thread.new Memento();
+        memento.bb.position( 0 ).limit( 0 );
+        assertThat( thread.readInt( memento ) )
+            .isEqualTo( new BigInteger( new byte[] {0x01, 0x02, 0x03, 0x04} ).intValue() );
+    }
+
+    @Test
+    public void shouldReadInteger() throws Exception
+    {
+        Channel channel = new Channel( new byte[] {(byte) 0xff, 0x01, 0x02, 0x03, 0x04, ':'}, 1 );
+
+        EventConsumerThread thread = new EventConsumerThread( "t", channel,
+            new MockEventHandler<Event>(), COUNTDOWN_CLOSEABLE, new MockForkNodeArguments() );
+
+        Memento memento = thread.new Memento();
+        memento.bb.position( 0 ).limit( 0 );
+        assertThat( thread.readInteger( memento ) )
+            .isEqualTo( new BigInteger( new byte[] {0x01, 0x02, 0x03, 0x04} ).intValue() );
+    }
+
+    @Test
+    public void shouldReadNullInteger() throws Exception
+    {
+        Channel channel = new Channel( new byte[] {(byte) 0x00, ':'}, 1 );
+
+        EventConsumerThread thread = new EventConsumerThread( "t", channel,
+            new MockEventHandler<Event>(), COUNTDOWN_CLOSEABLE, new MockForkNodeArguments() );
+
+        Memento memento = thread.new Memento();
+        memento.bb.position( 0 ).limit( 0 );
+        assertThat( thread.readInteger( memento ) )
+            .isNull();
+    }
+
     @Test( expected = EOFException.class )
     public void shouldNotReadString() throws Exception
     {
@@ -366,7 +409,7 @@ public class EventConsumerThreadTest
             chars.clear();
         }
         long l2 = System.currentTimeMillis();
-        System.out.println( "decoded 100 bytes within " + ( l2 - l1 ) );
+        System.out.println( "decoded 100 bytes within " + ( l2 - l1 ) + " millis (10 million cycles)" );
         assertThat( s )
             .isEqualTo( PATTERN1 );
     }