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.surefire.extensions.EventHandler;
23  import org.apache.maven.surefire.extensions.util.CommandlineExecutor;
24  import org.apache.maven.surefire.extensions.util.CommandlineStreams;
25  import org.apache.maven.surefire.extensions.util.CountdownCloseable;
26  import org.apache.maven.surefire.extensions.util.LineConsumerThread;
27  import org.apache.maven.surefire.shared.utils.cli.Commandline;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  import java.io.Closeable;
33  import java.nio.file.Paths;
34  
35  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
36  import static org.fest.assertions.Assertions.assertThat;
37  import static org.fest.util.Files.delete;
38  import static org.mockito.ArgumentMatchers.contains;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.verify;
41  
42  /**
43   *
44   */
45  public class CommandlineExecutorTest
46  {
47      private CommandlineExecutor exec;
48      private CommandlineStreams streams;
49      private String baseDir;
50      private LineConsumerThread out;
51  
52      @Before
53      public void setUp() throws Exception
54      {
55          baseDir = System.getProperty( "user.dir" );
56  
57          delete( Paths.get( baseDir, "target", "CommandlineExecutorTest" ).toFile() );
58  
59          boolean createdDir = Paths.get( baseDir, "target", "CommandlineExecutorTest" )
60              .toFile()
61              .mkdirs();
62  
63          assertThat( createdDir )
64              .isTrue();
65  
66          boolean createdFile = Paths.get( baseDir, "target", "CommandlineExecutorTest", "a.txt" )
67              .toFile()
68              .createNewFile();
69  
70          assertThat( createdFile )
71              .isTrue();
72      }
73  
74      @After
75      public void tearDown() throws Exception
76      {
77          if ( out != null )
78          {
79              out.close();
80          }
81          exec.close();
82          streams.close();
83          delete( Paths.get( baseDir, "target", "CommandlineExecutorTest" ).toFile() );
84      }
85  
86      @Test
87      public void shouldExecuteNativeCommand() throws Exception
88      {
89          Closeable closer = mock( Closeable.class );
90          Commandline cli = new Commandline( IS_OS_WINDOWS ? "dir" : "ls -la" );
91          cli.setWorkingDirectory( Paths.get( baseDir, "target", "CommandlineExecutorTest" ).toFile() );
92          CountdownCloseable countdownCloseable = new CountdownCloseable( closer, 1 );
93          exec = new CommandlineExecutor( cli, countdownCloseable );
94          streams = exec.execute();
95          @SuppressWarnings( "unchecked" )
96          EventHandler<String> consumer = mock( EventHandler.class );
97  
98          out = new LineConsumerThread( "std-out-fork-1", streams.getStdOutChannel(), consumer, countdownCloseable );
99          out.start();
100         exec.awaitExit();
101         verify( consumer )
102             .handleEvent( contains( "a.txt" ) );
103     }
104 }