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