View Javadoc
1   package org.apache.maven.plugin.surefire.extensions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
23  import org.apache.maven.surefire.api.booter.Command;
24  import org.apache.maven.surefire.extensions.CommandReader;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.mockito.invocation.InvocationOnMock;
29  import org.mockito.stubbing.Answer;
30  
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.nio.ByteBuffer;
34  import java.nio.channels.WritableByteChannel;
35  import java.util.Iterator;
36  
37  import static java.util.Arrays.asList;
38  import static org.apache.maven.surefire.api.booter.Command.TEST_SET_FINISHED;
39  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.NOOP;
40  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.RUN_CLASS;
41  import static org.fest.assertions.Assertions.assertThat;
42  import static org.mockito.ArgumentMatchers.any;
43  import static org.mockito.Mockito.mock;
44  import static org.mockito.Mockito.times;
45  import static org.mockito.Mockito.verify;
46  import static org.mockito.Mockito.verifyZeroInteractions;
47  import static org.mockito.Mockito.when;
48  
49  /**
50   * Tests for {@link StreamFeeder}.
51   */
52  public class StreamFeederTest
53  {
54      private final ByteArrayOutputStream out = new ByteArrayOutputStream();
55      private final WritableByteChannel channel = mock( WritableByteChannel.class );
56      private final CommandReader commandReader = mock( CommandReader.class );
57      private StreamFeeder streamFeeder;
58  
59      @Before
60      public void setup() throws IOException
61      {
62          final Iterator<Command> it = asList( new Command( RUN_CLASS, "pkg.ATest" ), TEST_SET_FINISHED ).iterator();
63          when( commandReader.readNextCommand() )
64              .thenAnswer( new Answer<Command>()
65              {
66                  @Override
67                  public Command answer( InvocationOnMock invocation )
68                  {
69                      return it.hasNext() ? it.next() : null;
70                  }
71              } );
72      }
73  
74      @After
75      public void close() throws IOException
76      {
77          if ( streamFeeder != null )
78          {
79              streamFeeder.disable();
80              streamFeeder.close();
81          }
82      }
83  
84      @Test
85      public void shouldEncodeCommandToStream() throws Exception
86      {
87          when( channel.write( any( ByteBuffer.class ) ) )
88              .thenAnswer( new Answer<Object>()
89              {
90                  @Override
91                  public Object answer( InvocationOnMock invocation ) throws IOException
92                  {
93                      ByteBuffer bb = invocation.getArgument( 0 );
94                      bb.flip();
95                      out.write( bb.array() );
96                      return 0;
97                  }
98              } );
99  
100         ConsoleLogger logger = mock( ConsoleLogger.class );
101         streamFeeder = new StreamFeeder( "t", channel, commandReader, logger );
102         streamFeeder.start();
103 
104         streamFeeder.join();
105         String commands = out.toString();
106 
107         assertThat( commands )
108             .isEqualTo( ":maven-surefire-command:run-testclass:pkg.ATest::maven-surefire-command:testset-finished:" );
109 
110         verify( channel, times( 1 ) )
111             .close();
112 
113         assertThat( streamFeeder.getException() )
114             .isNull();
115 
116         verifyZeroInteractions( logger );
117     }
118 
119     @Test
120     public void shouldFailThread() throws Exception
121     {
122         when( channel.write( any( ByteBuffer.class ) ) )
123             .thenAnswer( new Answer<Object>()
124             {
125                 @Override
126                 public Object answer( InvocationOnMock invocation ) throws IOException
127                 {
128                     throw new IOException();
129                 }
130             } );
131 
132         ConsoleLogger logger = mock( ConsoleLogger.class );
133         streamFeeder = new StreamFeeder( "t", channel, commandReader, logger );
134         streamFeeder.start();
135 
136         streamFeeder.join();
137 
138         assertThat( out.size() )
139             .isZero();
140 
141         verify( channel, times( 1 ) )
142             .close();
143 
144         assertThat( streamFeeder.getException() )
145             .isNotNull()
146             .isInstanceOf( IOException.class );
147 
148         verifyZeroInteractions( logger );
149     }
150 
151     @Test( expected = IllegalArgumentException.class )
152     public void shouldFailWithoutData()
153     {
154         StreamFeeder.encode( RUN_CLASS );
155     }
156 
157     @Test( expected = IllegalArgumentException.class )
158     public void shouldFailWithData()
159     {
160         StreamFeeder.encode( NOOP, "" );
161     }
162 }