views:

114

answers:

1

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 something special).

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

Any idea?

Oh - the language is Java.

+1  A: 

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;
}
Gerd Klima
oooh. nice idea. didn't think of going there.Thanks! Will mark "accepted" once I test this :)
Ran Biron
as promised - accepted :) This works perfectly.
Ran Biron