View Javadoc
1   /*
2    * Copyright 2018 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.spf4j.test.log;
17  
18  import com.google.common.annotations.Beta;
19  import com.google.common.base.Ascii;
20  import java.util.List;
21  import org.spf4j.base.Env;
22  
23  /**
24   * @author Zoltan Farkas
25   */
26  public final class TestUtils {
27    private TestUtils() { }
28  
29    public static boolean isExecutedDuringMvnRelease() {
30      return Env.getValue("MAVEN_CMD_LINE_ARGS", "").contains("release:");
31    }
32  
33    public static boolean isExecutedInCI() {
34      return isExecutedInTravis();
35    }
36  
37    public static boolean isExecutedInTravis() {
38      return Ascii.equalsIgnoreCase("true", "TRAVIS");
39    }
40  
41  
42    public static boolean isExecutedFromNetbeans() {
43      String mvnNetbeansCP = System.getProperty("maven.ext.class.path");
44      return mvnNetbeansCP != null && mvnNetbeansCP.contains("netbeans");
45    }
46  
47    @Beta
48    public static boolean isExecutedFromIntelij() {
49      for (StackTraceElement[] st : Thread.getAllStackTraces().values()) {
50        if (st != null) {
51          for (StackTraceElement ste : st) {
52            if (ste.getClassName().startsWith("com.intellij.rt.execution")) {
53              return true;
54            }
55          }
56        }
57      }
58      return false;
59    }
60  
61  
62    @Beta
63    public static boolean isExecutedFromEclipse() {
64      for (StackTraceElement[] st : Thread.getAllStackTraces().values()) {
65        if (st != null) {
66          for (StackTraceElement ste : st) {
67            if (ste.getClassName().startsWith("org.eclipse.jdt.internal")) {
68              return true;
69            }
70          }
71        }
72      }
73      return false;
74    }
75  
76  
77  
78    /**
79     * Supporting netbeans only at this time.
80     * For other IDEs you need to configure them to pass the spf4j.execFromIDE property.
81     * @return
82     */
83    public static boolean isExecutedFromIDE() {
84      return isExecutedFromNetbeans() || System.getProperty("spf4j.execFromIDE") != null
85              || isExecutedFromEclipse() || isExecutedFromIntelij();
86    }
87  
88    public static boolean isExecutedFromIDEInvididually() {
89      return isExecutedFromIDE() && System.getProperty("test") != null;
90    }
91  
92    public static boolean isExecutedWithDebuggerAgent() {
93      List<String> inputArguments = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments();
94      return inputArguments.stream()
95              .filter((arg) -> "-agentlib:jdwp".equals(arg) || "-Xdebug".equals(arg) || arg.startsWith("-Xrunjdwp"))
96              .count() > 0;
97  
98    }
99  
100 }