View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
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.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 }