0

junit テストに使用する eclipse プラグイン プロジェクトと対応するフラグメント プロジェクトを作成しました。

フラグメントでは、プラグイン プロジェクトを「ホスト プラグイン」として指定します。さらに、build.properties ペインで次のように指定します。

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               my.properties

my.properties は、フラグメント プロジェクトのルートにあるファイルです。次に、次のように my.properties ファイルをロードしようとするテストを作成しました。

Properties properties = new Properties();
InputStream istream = this.getClass().getClassLoader()
    .getResourceAsStream("my.properties");

try {
  properties.load(istream);
} catch (IOException e) {
  e.printStackTrace();
}

しかしistreamnull であり、try ブロックで load を呼び出すと、テストは NullPointerException で失敗します。

ホストプラグインで同じことをしようとしましたが、正常に動作します。Junit を使用しているときに PDE フラグメントのリソースを読み取れない理由について何か考えはありますか?

4

3 に答える 3

0

Bundle#getEntryを使用してみてください。プラグインにActivatorがある場合、プラグインの起動時にBundleContextオブジェクトを取得Bundle-ActivationPolicy: lazyします(マニフェストで使用)。BundleObjectはBundleContextから取得できます。

public class Activator implements BundleActivator {
   private static Bundle bundle;

   public static Bundle getBundle() {
      return myBundle;
   }
   public void start(BundleContext context) throws Exception {
      bundle = context.getBundle();
   }
}

...
URL url = Activator.getBundle().getEntry("my.properties");
InputStream stream = url.openStream();
properties.load(stream);
于 2010-08-07T16:14:51.183 に答える
0

アンドリュー・ニーファーは方向性を示しましたが、解決策は間違っています。それは機能するものです:

super();1) Activator コンストラクターに追加します。
2) これをプラグインのコンストラクターに入れます。

    Properties properties = new Properties();

    try {
        Bundle bundle=Activator.getDefault().getBundle();
        URL url = bundle.getEntry("plugin.properties");
        InputStream stream;
        stream = url.openStream();
        properties.load(stream);
    } catch (Exception e) {
        e.printStackTrace();
    }

そして、機能する「プロパティ」があります。


説明:

(1) を実行すると、そのすべての機能にアクセスできます。

public class Activator implements BundleActivator {
   private static Bundle bundle;

   public static Bundle getBundle() {
      return myBundle;
   }
   public void start(BundleContext context) throws Exception {
      bundle = context.getBundle();
   }
}

これは、事前親クラス Plugin に既に存在します。また、プラグインでは getBundle() が final であるため、単に Activator に入れることはできません。

(2) の Activator.getDefault() に注目してください。バンドルがなければ到達できず、静的ではありません。また、単にアクティベーターの新しいインスタンスを作成すると、そのバンドルはnullになります。


バンドルを取得する方法がもう 1 つあります。

Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);

Activator.PLUGIN_IDプラグインの概要ページの ID フィールドにあるように、正しい文字列に設定されていることのみを確認してください。ところで、とにかくActivator.PLUGIN_IDプラグイン ID を変更するたびにこれを確認する必要があります。

于 2012-05-18T15:04:31.350 に答える
0

あなたが抱えているかもしれない問題の1つは、

InputStream istream = this.getClass().getClassLoader().
getResourceAsStream("my.properties");

"this" が別のパッケージにある 2 つの状況では、動作が異なります。先頭に「/」を追加しなかったため、Java はリソースのクラスパス ルートではなく、パッケージ ルートを自動的に検索し始めます。プラグイン プロジェクトとフラグメント プロジェクトのコードが異なるパッケージに存在する場合、問題が発生します。

于 2010-08-06T21:55:36.183 に答える