2010-03-22 45 views
2

我需要知道,在單元測試的情況下,如果Jetbrains IntelliJ的想法是測試發起者,並且如果是,哪個版本正在運行(或在至少如果它是「9」或更早)。在單元測試中確定是否Jetbrains IntelliJ IDEA 8或9正在運行

我不介意做骯髒的技巧(反射,文件系統,環境...),但我確實需要這個「開箱即用」(沒有每個開發人員都需要設置一些特別的東西)。

我試過一些反射,但找不到可以鎖定的唯一標識符。

有什麼想法?

哦 - 語言是Java。

回答

2

關於「是IDEA測試始作俑者」的問題,答案很簡單:

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

    return true; 
} 

作爲確定IDEA版...

下工作,只要安裝目錄遵循安裝程序的標準(在Windows上,我不知道它在Mac或Linux系統上的安裝位置)。

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; 
} 
相關問題