View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.coverage;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.nio.file.Files;
11  import java.nio.file.Path;
12  
13  public final class Configuration {
14      private static final String COVERAGE_PREFIX = "coverage-";
15  
16      private Configuration() {
17      }
18  
19      @Nullable
20      public static String getProperty(@NonNull String nameSuffix) {
21          return getProperty(nameSuffix, null);
22      }
23  
24      public static String getProperty(@NonNull String nameSuffix, @Nullable String defaultValue) {
25          return System.getProperty(COVERAGE_PREFIX + nameSuffix, defaultValue);
26      }
27  
28      @Nullable
29      public static String getOrChooseOutputDirectory(@NonNull String outputDir) {
30          if (!outputDir.isEmpty()) {
31              return outputDir;
32          }
33  
34          return isTargetSubDirectoryAvailable() ? "target" : null;
35      }
36  
37      private static boolean isTargetSubDirectoryAvailable() {
38          return System.getProperty("basedir") != null || Files.exists(Path.of("target"));
39      }
40  
41      @NonNull
42      public static String getOrChooseOutputDirectory(@NonNull String outputDir, @NonNull String defaultDir) {
43          if (!outputDir.isEmpty()) {
44              return outputDir;
45          }
46  
47          return isTargetSubDirectoryAvailable() ? "target/" + defaultDir : defaultDir;
48      }
49  }