View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.extensions;
20  
21  import java.io.Closeable;
22  import java.nio.file.Paths;
23  
24  import org.apache.maven.surefire.extensions.EventHandler;
25  import org.apache.maven.surefire.extensions.util.CommandlineExecutor;
26  import org.apache.maven.surefire.extensions.util.CommandlineStreams;
27  import org.apache.maven.surefire.extensions.util.CountdownCloseable;
28  import org.apache.maven.surefire.extensions.util.LineConsumerThread;
29  import org.apache.maven.surefire.shared.utils.cli.Commandline;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
35  import static org.assertj.core.api.Assertions.assertThat;
36  import static org.assertj.core.util.Files.delete;
37  import static org.mockito.ArgumentMatchers.contains;
38  import static org.mockito.Mockito.mock;
39  import static org.mockito.Mockito.verify;
40  
41  /**
42   *
43   */
44  public class CommandlineExecutorTest {
45      private CommandlineExecutor exec;
46      private CommandlineStreams streams;
47      private String baseDir;
48      private LineConsumerThread out;
49  
50      @Before
51      public void setUp() throws Exception {
52          baseDir = System.getProperty("user.dir");
53  
54          delete(Paths.get(baseDir, "target", "CommandlineExecutorTest").toFile());
55  
56          boolean createdDir =
57                  Paths.get(baseDir, "target", "CommandlineExecutorTest").toFile().mkdirs();
58  
59          assertThat(createdDir).isTrue();
60  
61          boolean createdFile = Paths.get(baseDir, "target", "CommandlineExecutorTest", "a.txt")
62                  .toFile()
63                  .createNewFile();
64  
65          assertThat(createdFile).isTrue();
66      }
67  
68      @After
69      public void tearDown() throws Exception {
70          if (out != null) {
71              out.close();
72          }
73          exec.close();
74          streams.close();
75          delete(Paths.get(baseDir, "target", "CommandlineExecutorTest").toFile());
76      }
77  
78      @Test
79      public void shouldExecuteNativeCommand() throws Exception {
80          Closeable closer = mock(Closeable.class);
81          Commandline cli = new Commandline(IS_OS_WINDOWS ? "dir" : "ls -la");
82          cli.setWorkingDirectory(
83                  Paths.get(baseDir, "target", "CommandlineExecutorTest").toFile());
84          CountdownCloseable countdownCloseable = new CountdownCloseable(closer, 1);
85          exec = new CommandlineExecutor(cli, countdownCloseable);
86          streams = exec.execute();
87          @SuppressWarnings("unchecked")
88          EventHandler<String> consumer = mock(EventHandler.class);
89  
90          out = new LineConsumerThread("std-out-fork-1", streams.getStdOutChannel(), consumer, countdownCloseable);
91          out.start();
92          exec.awaitExit();
93          verify(consumer).handleEvent(contains("a.txt"));
94      }
95  }