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.booterclient.lazytestprovider;
20  
21  import org.apache.maven.surefire.shared.utils.cli.CommandLineException;
22  import org.assertj.core.api.Condition;
23  import org.junit.Test;
24  
25  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
26  import static org.assertj.core.api.Assertions.assertThat;
27  
28  /**
29   *
30   */
31  public class CommandlineTest {
32      @Test
33      public void shouldGetEnvironmentVariables() {
34          Commandline cli = new Commandline();
35          String[] env = cli.getEnvironmentVariables();
36  
37          assertThat(env).doesNotHaveDuplicates().satisfies(new ContainsAnyStartsWith("JAVA_HOME="));
38  
39          String[] excluded = {"JAVA_HOME"};
40          cli = new Commandline(excluded);
41          env = cli.getEnvironmentVariables();
42  
43          assertThat(env).doesNotHaveDuplicates().satisfies(new NotContainsAnyStartsWith("JAVA_HOME="));
44      }
45  
46      @Test
47      public void shouldExecute() throws CommandLineException {
48          Commandline cli = new Commandline();
49          cli.getShell().setWorkingDirectory(System.getProperty("user.dir"));
50          cli.getShell().setExecutable(IS_OS_WINDOWS ? "dir" : "ls");
51          cli.execute();
52      }
53  
54      private static final class ContainsAnyStartsWith extends Condition<Object[]> {
55          private final String expected;
56  
57          ContainsAnyStartsWith(String expected) {
58              this.expected = expected;
59          }
60  
61          @Override
62          public boolean matches(Object[] values) {
63              boolean matches = false;
64              for (Object value : values) {
65                  assertThat(value).isInstanceOf(String.class);
66                  matches |= ((String) value).startsWith(expected);
67              }
68              return matches;
69          }
70      }
71  
72      private static final class NotContainsAnyStartsWith extends Condition<Object[]> {
73          private final String expected;
74  
75          NotContainsAnyStartsWith(String expected) {
76              this.expected = expected;
77          }
78  
79          @Override
80          public boolean matches(Object[] values) {
81              boolean matches = false;
82              for (Object value : values) {
83                  assertThat(value).isInstanceOf(String.class);
84                  matches |= ((String) value).startsWith(expected);
85              }
86              return !matches;
87          }
88      }
89  }