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.shared.utils.cli.CommandLineException;
23 import org.fest.assertions.Condition;
24 import org.junit.Test;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28
29 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
30 import static org.fest.assertions.Assertions.assertThat;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.powermock.api.mockito.PowerMockito.mock;
34 import static org.powermock.api.mockito.PowerMockito.verifyZeroInteractions;
35
36
37
38
39 public class OutputStreamFlushableCommandlineTest
40 {
41
42 @Test
43 public void shouldGetEnvironmentVariables()
44 {
45 OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
46 String[] env = cli.getEnvironmentVariables();
47
48 assertThat( env )
49 .doesNotHaveDuplicates()
50 .satisfies( new ContainsAnyStartsWith( "JAVA_HOME=" ) );
51
52 String[] excluded = { "JAVA_HOME" };
53 cli = new OutputStreamFlushableCommandline( excluded );
54 env = cli.getEnvironmentVariables();
55
56 assertThat( env )
57 .doesNotHaveDuplicates()
58 .satisfies( new NotContainsAnyStartsWith( "JAVA_HOME=" ) );
59 }
60
61 @Test
62 public void shouldExecute() throws CommandLineException
63 {
64 OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
65 cli.getShell().setWorkingDirectory( System.getProperty( "user.dir" ) );
66 cli.getShell().setExecutable( IS_OS_WINDOWS ? "dir" : "ls" );
67 assertThat( cli.getFlushReceiver() ).isNull();
68 cli.execute();
69 assertThat( cli.getFlushReceiver() ).isNotNull();
70 }
71
72 @Test
73 public void shouldGetFlushReceiver()
74 {
75 OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
76 assertThat( cli.getFlushReceiver() ).isNull();
77 }
78
79 @Test
80 public void shouldFlush() throws IOException
81 {
82 ByteArrayOutputStream os = mock( ByteArrayOutputStream.class );
83 OutputStreamFlushReceiver flushReceiver = new OutputStreamFlushReceiver( os );
84 verifyZeroInteractions( os );
85 flushReceiver.flush();
86 verify( os, times( 1 ) ).flush();
87 }
88
89 private static final class ContainsAnyStartsWith extends Condition<Object[]>
90 {
91 private final String expected;
92
93 ContainsAnyStartsWith( String expected )
94 {
95 this.expected = expected;
96 }
97
98 @Override
99 public boolean matches( Object[] values )
100 {
101 boolean matches = false;
102 for ( Object value : values )
103 {
104 assertThat( value ).isInstanceOf( String.class );
105 matches |= ( (String) value ).startsWith( expected );
106 }
107 return matches;
108 }
109 }
110
111 private static final class NotContainsAnyStartsWith extends Condition<Object[]>
112 {
113 private final String expected;
114
115 NotContainsAnyStartsWith( String expected )
116 {
117 this.expected = expected;
118 }
119
120 @Override
121 public boolean matches( Object[] values )
122 {
123 boolean matches = false;
124 for ( Object value : values )
125 {
126 assertThat( value ).isInstanceOf( String.class );
127 matches |= ( (String) value ).startsWith( expected );
128 }
129 return !matches;
130 }
131 }
132 }