1 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.booter.Command;
23 import org.apache.maven.surefire.booter.MasterProcessCommand;
24 import org.junit.Test;
25
26 import java.io.DataInputStream;
27 import java.io.IOException;
28 import java.util.ArrayDeque;
29 import java.util.Queue;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.ConcurrentLinkedQueue;
32 import java.util.concurrent.FutureTask;
33 import java.util.concurrent.TimeUnit;
34
35 import static org.apache.maven.surefire.booter.MasterProcessCommand.BYE_ACK;
36 import static org.apache.maven.surefire.booter.MasterProcessCommand.decode;
37 import static org.hamcrest.MatcherAssert.assertThat;
38 import static org.hamcrest.Matchers.*;
39
40
41
42
43
44
45
46 public class TestProvidingInputStreamTest
47 {
48 @Test
49 public void closedStreamShouldReturnEndOfStream()
50 throws IOException
51 {
52 Queue<String> commands = new ArrayDeque<String>();
53 TestProvidingInputStream is = new TestProvidingInputStream( commands );
54 is.close();
55 assertThat( is.read(), is( -1 ) );
56 }
57
58 @Test
59 public void emptyStreamShouldWaitUntilClosed()
60 throws Exception
61 {
62 Queue<String> commands = new ArrayDeque<String>();
63 final TestProvidingInputStream is = new TestProvidingInputStream( commands );
64 final Thread streamThread = Thread.currentThread();
65 FutureTask<Thread.State> futureTask = new FutureTask<Thread.State>( new Callable<Thread.State>()
66 {
67 @Override
68 public Thread.State call()
69 {
70 sleep( 1000 );
71 Thread.State state = streamThread.getState();
72 is.close();
73 return state;
74 }
75 } );
76 Thread assertionThread = new Thread( futureTask );
77 assertionThread.start();
78 assertThat( is.read(), is( -1 ) );
79 Thread.State state = futureTask.get();
80 assertThat( state, is( Thread.State.WAITING ) );
81 }
82
83 @Test
84 public void finishedTestsetShouldNotBlock()
85 throws IOException
86 {
87 Queue<String> commands = new ArrayDeque<String>();
88 final TestProvidingInputStream is = new TestProvidingInputStream( commands );
89 is.testSetFinished();
90 new Thread( new Runnable()
91 {
92 @Override
93 public void run()
94 {
95 is.provideNewTest();
96 }
97 } ).start();
98 assertThat( is.read(), is( 0 ) );
99 assertThat( is.read(), is( 0 ) );
100 assertThat( is.read(), is( 0 ) );
101 assertThat( is.read(), is( 1 ) );
102 assertThat( is.read(), is( 0 ) );
103 assertThat( is.read(), is( 0 ) );
104 assertThat( is.read(), is( 0 ) );
105 assertThat( is.read(), is( 0 ) );
106 is.close();
107 assertThat( is.read(), is( -1 ) );
108 }
109
110 @Test
111 public void shouldReadTest()
112 throws IOException
113 {
114 Queue<String> commands = new ArrayDeque<String>();
115 commands.add( "Test" );
116 final TestProvidingInputStream is = new TestProvidingInputStream( commands );
117 new Thread( new Runnable()
118 {
119 @Override
120 public void run()
121 {
122 is.provideNewTest();
123 }
124 } ).start();
125 assertThat( is.read(), is( 0 ) );
126 assertThat( is.read(), is( 0 ) );
127 assertThat( is.read(), is( 0 ) );
128 assertThat( is.read(), is( 0 ) );
129 assertThat( is.read(), is( 0 ) );
130 assertThat( is.read(), is( 0 ) );
131 assertThat( is.read(), is( 0 ) );
132 assertThat( is.read(), is( 4 ) );
133 assertThat( is.read(), is( (int) 'T' ) );
134 assertThat( is.read(), is( (int) 'e' ) );
135 assertThat( is.read(), is( (int) 's' ) );
136 assertThat( is.read(), is( (int) 't' ) );
137 }
138
139 @Test
140 public void shouldDecodeTwoCommands()
141 throws IOException
142 {
143 TestProvidingInputStream pluginIs = new TestProvidingInputStream( new ConcurrentLinkedQueue<String>() );
144 pluginIs.acknowledgeByeEventReceived();
145 pluginIs.noop();
146 DataInputStream is = new DataInputStream( pluginIs );
147 Command bye = decode( is );
148 assertThat( bye, is( notNullValue() ) );
149 assertThat( bye.getCommandType(), is( BYE_ACK ) );
150 Command noop = decode( is );
151 assertThat( noop, is( notNullValue() ) );
152 assertThat( noop.getCommandType(), is( MasterProcessCommand.NOOP ) );
153 }
154
155 private static void sleep( long millis )
156 {
157 try
158 {
159 TimeUnit.MILLISECONDS.sleep( millis );
160 }
161 catch ( InterruptedException e )
162 {
163
164 }
165 }
166 }