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 java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Properties;
24  import java.util.Set;
25  import java.util.concurrent.ConcurrentLinkedDeque;
26  
27  import org.apache.maven.surefire.shared.utils.cli.CommandLineUtils;
28  
29  import static java.util.Collections.addAll;
30  
31  /**
32   * A {@link org.apache.maven.surefire.shared.utils.cli.Commandline} implementation.
33   *
34   * @author Andreas Gudian
35   */
36  public class Commandline extends org.apache.maven.surefire.shared.utils.cli.Commandline {
37      private final Collection<String> excludedEnvironmentVariables;
38      private final Set<String> addedEnvironmentVariables;
39  
40      /**
41       * for testing purposes only
42       */
43      public Commandline() {
44          this(new String[0]);
45      }
46  
47      public Commandline(String[] excludedEnvironmentVariables) {
48          this.excludedEnvironmentVariables = new ConcurrentLinkedDeque<>();
49          addedEnvironmentVariables = new HashSet<>();
50          addAll(this.excludedEnvironmentVariables, excludedEnvironmentVariables);
51      }
52  
53      @Override
54      public void addEnvironment(String name, String value) {
55          super.addEnvironment(name, value);
56          addedEnvironmentVariables.add(name);
57      }
58  
59      @Override
60      public final void addSystemEnvironment() {
61          Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
62  
63          for (String key : systemEnvVars.stringPropertyNames()) {
64              if (!addedEnvironmentVariables.contains(key) && !excludedEnvironmentVariables.contains(key)) {
65                  addEnvironment(key, systemEnvVars.getProperty(key));
66              }
67          }
68      }
69  }