开发者

Identify in a unit test if Jetbrains IntelliJ IDEA 8 or 9 is running

开发者 https://www.devze.com 2022-12-23 13:40 出处:网络
I need to know, in a context of a unit test, if Jetbrains IntelliJ idea is the test initiator and if it is, which version is running (or at least if it\'s \"9\" or earlier).

I need to know, in a context of a unit test, if Jetbrains IntelliJ idea is the test initiator and if it is, which version is running (or at least if it's "9" or earlier).

I don't mind doing dirty tricks (reflection, filesystem, environment...) but I do need this to work "out-of-the-box" (without each developer having to setup so开发者_StackOverflowmething special).

I've tried some reflection but couldn't find a unique identifier I could latch onto.

Any idea?

Oh - the language is Java.


Regarding the "is IDEA the test initiator" question, the answer is rather simple:

public static boolean isIdeaRunningTheTest() {
   try {
     final Class<?> aClass = Class.forName("com.intellij.rt.execution.junit.JUnitStarter");
     } catch (ClassNotFoundException e) {
        return false;
     }

     return true;
}

As of determining the IDEA version...

The following works as long as the installation directory follows the standard from the setup program (on windows, I don't know where it's installed on Mac or Linux systems).

public static String getIdeaVersionTheDumbWay() {
    String result="unknown";
    final String binPath = System.getProperty("idea.launcher.bin.path");
    if (binPath.contains("IntelliJ IDEA")) {
        final String[] strings = binPath.split(System.getProperty("file.separator")+System.getProperty("file.separator"));
        for (String s : strings) {
            final int startIndex = s.indexOf("IntelliJ IDEA");
            if (startIndex >= 0) {
                result= s.substring(startIndex + 14);
            }

        }

    }

    return result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号