[maven-surefire] branch base64 updated: unit tests: shouldReadEventType, shouldEventTypeReachedEndOfStream

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-surefire] branch base64 updated: unit tests: shouldReadEventType, shouldEventTypeReachedEndOfStream

Tibor Digana
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


The following commit(s) were added to refs/heads/base64 by this push:
     new 8d50c45  unit tests: shouldReadEventType, shouldEventTypeReachedEndOfStream
8d50c45 is described below

commit 8d50c453f81af9843baf6c7bb6a0fabcb4f452db
Author: tibordigana <[hidden email]>
AuthorDate: Wed Sep 16 21:15:31 2020 +0200

    unit tests: shouldReadEventType, shouldEventTypeReachedEndOfStream
---
 .../surefire/extensions/EventConsumerThread.java   | 65 +++++++---------------
 .../extensions/EventConsumerThreadTest.java        | 43 ++++++++++++++
 2 files changed, 64 insertions(+), 44 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 34787b8..0c640f3 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
@@ -74,7 +74,6 @@ import static java.lang.Math.max;
 import static java.lang.Math.min;
 import static java.nio.charset.CodingErrorAction.REPLACE;
 import static java.nio.charset.StandardCharsets.US_ASCII;
-import static org.apache.maven.plugin.surefire.extensions.EventConsumerThread.StreamReadStatus.EOF;
 import static org.apache.maven.plugin.surefire.extensions.EventConsumerThread.StreamReadStatus.OVERFLOW;
 import static org.apache.maven.plugin.surefire.extensions.EventConsumerThread.StreamReadStatus.UNDERFLOW;
 import static org.apache.maven.surefire.api.booter.Constants.MAGIC_NUMBER;
@@ -273,28 +272,18 @@ public class EventConsumerThread extends CloseableDaemonThread
     {
         int readCount = DELIMITER_LENGTH + MAGIC_NUMBER_BYTES.length + DELIMITER_LENGTH
             + BYTE_LENGTH + DELIMITER_LENGTH;
-        if ( read( memento, readCount ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, readCount );
         checkHeader( memento );
-
         return eventTypes.get( readSegment( memento ) );
     }
 
     protected String readString( Memento memento ) throws IOException, MalformedFrameException
     {
         memento.cb.clear();
-        if ( read( memento, INT_LENGTH + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, INT_LENGTH + DELIMITER_LENGTH );
 
         int readCount = readInt( memento );
-        if ( read( memento, readCount + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, readCount + DELIMITER_LENGTH );
 
         final String string;
         if ( readCount == 0 )
@@ -303,10 +292,7 @@ public class EventConsumerThread extends CloseableDaemonThread
         }
         else if ( readCount == 1 )
         {
-            if ( read( memento, 1 ) == EOF )
-            {
-                throw new EOFException();
-            }
+            read( memento, 1 );
             byte oneChar = memento.bb.get();
             string = oneChar == 0 ? null : String.valueOf( (char) oneChar );
         }
@@ -322,16 +308,10 @@ public class EventConsumerThread extends CloseableDaemonThread
     @Nonnull
     protected Segment readSegment( Memento memento ) throws IOException, MalformedFrameException
     {
-        if ( read( memento, BYTE_LENGTH + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
-        ByteBuffer bb = memento.bb;
+        read( memento, BYTE_LENGTH + DELIMITER_LENGTH );
         int readCount = readByte( memento ) & 0xff;
-        if ( read( memento, readCount + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, readCount + DELIMITER_LENGTH );
+        ByteBuffer bb = memento.bb;
         Segment segment = new Segment( bb.array(), bb.arrayOffset() + bb.position(), readCount );
         bb.position( bb.position() + readCount );
         checkDelimiter( memento );
@@ -341,16 +321,10 @@ public class EventConsumerThread extends CloseableDaemonThread
     @Nonnull
     protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException
     {
-        if ( read( memento, BYTE_LENGTH + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, BYTE_LENGTH + DELIMITER_LENGTH );
         ByteBuffer bb = memento.bb;
         int length = readByte( memento ) & 0xff;
-        if ( read( memento, length + DELIMITER_LENGTH ) == EOF )
-        {
-            throw new EOFException();
-        }
+        read( memento, length + DELIMITER_LENGTH );
         byte[] array = bb.array();
         int offset = bb.arrayOffset() + bb.position();
         bb.position( bb.position() + length );
@@ -473,10 +447,19 @@ public class EventConsumerThread extends CloseableDaemonThread
             {
                 isEnd = channel.read( buffer ) == -1;
             }
+
             buffer.limit( buffer.position() );
             buffer.position( mark );
             int readBytes = buffer.remaining();
-            return readBytes == 0 && isEnd ? EOF : ( readBytes >= recommendedCount ? OVERFLOW : UNDERFLOW );
+
+            if ( isEnd && readBytes < recommendedCount )
+            {
+                throw new EOFException();
+            }
+            else
+            {
+                return readBytes >= recommendedCount ? OVERFLOW : UNDERFLOW;
+            }
         }
     }
 
@@ -623,15 +606,9 @@ public class EventConsumerThread extends CloseableDaemonThread
         for ( boolean endOfInput = false; !endOfInput; )
         {
             final int bytesToRead = totalBytes - countDecodedBytes;
-
-            if ( read( memento, bytesToRead - input.remaining() ) == EOF )
-            {
-                throw new EOFException();
-            }
-
+            read( memento, bytesToRead - input.remaining() );
             int bytesToDecode = min( input.remaining(), bytesToRead );
             final boolean isLastChunk = bytesToDecode == bytesToRead;
-
             endOfInput = countDecodedBytes + bytesToDecode >= totalBytes;
             do
             {
@@ -877,7 +854,7 @@ public class EventConsumerThread extends CloseableDaemonThread
         }
     }
 
-    private static class Segment
+    static class Segment
     {
         private final byte[] array;
         private final int fromIndex;
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 a599e54..6775040 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
@@ -1,7 +1,9 @@
 package org.apache.maven.plugin.surefire.extensions;
 
 import org.apache.maven.plugin.surefire.extensions.EventConsumerThread.Memento;
+import org.apache.maven.plugin.surefire.extensions.EventConsumerThread.Segment;
 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
+import org.apache.maven.surefire.api.booter.ForkedProcessEventType;
 import org.apache.maven.surefire.api.event.Event;
 import org.apache.maven.surefire.extensions.EventHandler;
 import org.apache.maven.surefire.extensions.ForkNodeArguments;
@@ -16,10 +18,12 @@ import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.CharsetDecoder;
+import java.util.Map;
 
 import static java.lang.Math.min;
 import static java.nio.charset.CodingErrorAction.REPLACE;
 import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.apache.maven.plugin.surefire.extensions.EventConsumerThread.mapEventTypes;
 import static org.apache.maven.surefire.api.booter.Constants.DEFAULT_STREAM_ENCODING;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.powermock.reflect.Whitebox.invokeMethod;
@@ -306,6 +310,45 @@ public class EventConsumerThreadTest
             .isEqualTo( PATTERN1 );
     }
 
+    @Test
+    public void shouldReadEventType() throws Exception
+    {
+        Map<Segment, ForkedProcessEventType> eventTypes = mapEventTypes();
+        assertThat( eventTypes )
+            .hasSize( ForkedProcessEventType.values().length );
+
+        byte[] stream = ":maven-surefire-event:\u000E:std-out-stream:".getBytes( UTF_8 );
+        Channel channel = new Channel( stream, 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 );
+        memento.setCharset( UTF_8 );
+
+        ForkedProcessEventType eventType = thread.readEventType( eventTypes, memento );
+        assertThat( eventType )
+            .isEqualTo( ForkedProcessEventType.BOOTERCODE_STDOUT );
+    }
+
+    @Test( expected = EOFException.class )
+    public void shouldEventTypeReachedEndOfStream() throws Exception
+    {
+        Map<Segment, ForkedProcessEventType> eventTypes = mapEventTypes();
+        assertThat( eventTypes )
+            .hasSize( ForkedProcessEventType.values().length );
+
+        byte[] stream = ":maven-surefire-event:\u000E:xxx".getBytes( UTF_8 );
+        Channel channel = new Channel( stream, 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 );
+        memento.setCharset( UTF_8 );
+        thread.readEventType( eventTypes, memento );
+    }
+
     private static class Channel implements ReadableByteChannel
     {
         private final byte[] bytes;