子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數(shù)據(jù)加載中……

          Plugin和App的統(tǒng)一路徑接口

          在Eclipse插件(Plugin)開發(fā)中,需要寫一些Test Case(by JUnit),這些Test Case不是以插件方法運行的,而是一些普通的Application。這時要注意到插件和App獲取路徑的方式是不同的,這時如果你要測試一些項目中的一些涉及到讀文件的一些類,無疑會導(dǎo)致出錯。為了讓Plugin和App路徑獲取透明化,不防寫一個工具類來提供統(tǒng)一的路徑獲取方法,示例類如下所示:

          這個思路也可以擴展到Plugin和App不一樣的地方,這樣寫起Test Case就方便多了。

          說明:示例中的AdminConsolePlugin類就是創(chuàng)建插件項目自動生成的那個類,你的項目也許叫“項目名+Plugin”

          import?java.io.IOException;
          import?java.io.InputStream;
          import?java.net.MalformedURLException;
          import?java.net.URL;

          import?org.eclipse.core.runtime.Path;
          import?org.eclipse.ui.plugin.AbstractUIPlugin;

          import?com.wxxr.management.admin.console.AdminConsolePlugin;

          /**
          ?*?用于插件項目和非插件項目,提供兩者通用的方法接口
          ?*?
          @author?chengang?2006-3-30
          ?
          */
          public?class?ProjectUtil?{

          ????
          private?static?AbstractUIPlugin?plugin?=?AdminConsolePlugin.getDefault();

          ????
          private?ProjectUtil()?{}

          ????
          /**
          ?????*?判斷當(dāng)前的運行狀態(tài)是否為插件方式
          ?????*?
          @return?true=插件方式運行
          ?????
          */
          ????
          private?static?boolean?isPlugin()?{
          ????????
          return?plugin?!=?null;
          ????}

          ????
          public?static?URL?getURL(String?path)?{
          ????????
          if?(isPlugin())//如果是插件
          ????????????//return?plugin.find(new?Path(path));
          ??????????? return?FileLocator.find(plugin.getBundle(), new?Path(path), null);??? //陳剛修改于2006-8-24,eclipse3.2已經(jīng)建議用此方法
          ????????
          else
          ????????????
          try?{
          ????????????????
          return?new?URL("file:"?+?path);
          ????????????}?
          catch?(MalformedURLException?e)?{
          ????????????????
          throw?new?RuntimeException(path?+?"?is?error",?e);
          ????????????}
          ????}

          ????
          public?static?InputStream?getInputStream(String?path)?{
          ????????URL?url?
          =?getURL(path);
          ????????
          try?{
          ????????????
          return?url.openStream();
          ????????}?
          catch?(IOException?e)?{
          ????????????
          throw?new?RuntimeException(e);
          ????????}
          ????}

          }

          posted on 2006-03-30 11:22 陳剛 閱讀(1010) 評論(0)  編輯  收藏 所屬分類: Eclipse

          主站蜘蛛池模板: 平度市| 华坪县| 井研县| 新沂市| 临颍县| 泗洪县| 潮安县| 洪雅县| 佛坪县| 青川县| 桃源县| 平乐县| 新乡市| 武宁县| 靖江市| 高尔夫| 靖边县| 隆安县| 辽中县| 桓仁| 龙陵县| 凤台县| 湟源县| 南澳县| 阿尔山市| 嘉义市| 禄丰县| 婺源县| 边坝县| 建平县| 饶平县| 南充市| 泾川县| 互助| 绥棱县| 来安县| 鄂伦春自治旗| 靖西县| 定安县| 苍溪县| 昌邑市|