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.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.NoSuchElementException;
32
33 import static org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestLessInputStream.TestLessInputStreamBuilder;
34 import static org.apache.maven.surefire.booter.Command.NOOP;
35 import static org.apache.maven.surefire.booter.Command.SKIP_SINCE_NEXT_TEST;
36 import static org.apache.maven.surefire.booter.MasterProcessCommand.BYE_ACK;
37 import static org.apache.maven.surefire.booter.MasterProcessCommand.SHUTDOWN;
38 import static org.apache.maven.surefire.booter.MasterProcessCommand.decode;
39 import static org.apache.maven.surefire.booter.Shutdown.EXIT;
40 import static org.hamcrest.MatcherAssert.assertThat;
41 import static org.hamcrest.Matchers.is;
42 import static org.hamcrest.Matchers.notNullValue;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertTrue;
45
46
47
48
49
50
51
52 public class TestLessInputStreamBuilderTest
53 {
54 @Rule
55 public final ExpectedException e = ExpectedException.none();
56
57 @Test
58 public void cachableCommandsShouldBeIterableWithStillOpenIterator()
59 {
60 TestLessInputStreamBuilder builder = new TestLessInputStreamBuilder();
61 TestLessInputStream is = builder.build();
62 Iterator<Command> iterator = builder.getIterableCachable().iterator();
63
64 assertFalse( iterator.hasNext() );
65
66 builder.getCachableCommands().skipSinceNextTest();
67 assertTrue( iterator.hasNext() );
68 assertThat( iterator.next(), is( SKIP_SINCE_NEXT_TEST ) );
69
70 assertFalse( iterator.hasNext() );
71
72 builder.getCachableCommands().noop();
73 assertTrue( iterator.hasNext() );
74 assertThat( iterator.next(), is( NOOP ) );
75
76 builder.removeStream( is );
77 }
78
79 @Test
80 public void immediateCommands()
81 throws IOException
82 {
83 TestLessInputStreamBuilder builder = new TestLessInputStreamBuilder();
84 TestLessInputStream is = builder.build();
85 assertThat( is.availablePermits(), is( 0 ) );
86 is.noop();
87 assertThat( is.availablePermits(), is( 1 ) );
88 is.beforeNextCommand();
89 assertThat( is.availablePermits(), is( 0 ) );
90 assertThat( is.nextCommand(), is( NOOP ) );
91 assertThat( is.availablePermits(), is( 0 ) );
92 e.expect( NoSuchElementException.class );
93 is.nextCommand();
94 }
95
96 @Test
97 public void combinedCommands()
98 throws IOException
99 {
100 TestLessInputStreamBuilder builder = new TestLessInputStreamBuilder();
101 TestLessInputStream is = builder.build();
102 assertThat( is.availablePermits(), is( 0 ) );
103 builder.getCachableCommands().skipSinceNextTest();
104 is.noop();
105 assertThat( is.availablePermits(), is( 2 ) );
106 is.beforeNextCommand();
107 assertThat( is.availablePermits(), is( 1 ) );
108 assertThat( is.nextCommand(), is( NOOP ) );
109 assertThat( is.availablePermits(), is( 1 ) );
110 builder.getCachableCommands().skipSinceNextTest();
111 assertThat( is.availablePermits(), is( 1 ) );
112 builder.getImmediateCommands().shutdown( EXIT );
113 assertThat( is.availablePermits(), is( 2 ) );
114 is.beforeNextCommand();
115 assertThat( is.nextCommand().getCommandType(), is( SHUTDOWN ) );
116 assertThat( is.availablePermits(), is( 1 ) );
117 is.beforeNextCommand();
118 assertThat( is.nextCommand(), is( SKIP_SINCE_NEXT_TEST ) );
119 assertThat( is.availablePermits(), is( 0 ) );
120 builder.getImmediateCommands().noop();
121 assertThat( is.availablePermits(), is( 1 ) );
122 builder.getCachableCommands().shutdown( EXIT );
123 builder.getCachableCommands().shutdown( EXIT );
124 assertThat( is.availablePermits(), is( 2 ) );
125 is.beforeNextCommand();
126 assertThat( is.nextCommand(), is( NOOP ) );
127 assertThat( is.availablePermits(), is( 1 ) );
128 is.beforeNextCommand();
129 assertThat( is.nextCommand().getCommandType(), is( SHUTDOWN ) );
130 assertThat( is.availablePermits(), is( 0 ) );
131 e.expect( NoSuchElementException.class );
132 is.nextCommand();
133 }
134
135 @Test
136 public void shouldDecodeTwoCommands()
137 throws IOException
138 {
139 TestLessInputStreamBuilder builder = new TestLessInputStreamBuilder();
140 TestLessInputStream pluginIs = builder.build();
141 builder.getImmediateCommands().acknowledgeByeEventReceived();
142 builder.getImmediateCommands().noop();
143 DataInputStream is = new DataInputStream( pluginIs );
144 Command bye = decode( is );
145 assertThat( bye, is( notNullValue() ) );
146 assertThat( bye.getCommandType(), is( BYE_ACK ) );
147 Command noop = decode( is );
148 assertThat( noop, is( notNullValue() ) );
149 assertThat( noop.getCommandType(), is( MasterProcessCommand.NOOP ) );
150 }
151 }