テストプラグインの作成
さて、作成したプラグインのメソッドをテストして今回は終わりにしようと思います。まずはテスト用のプラグイン・プロジェクトを作成します。
- プロジェクト名
nu.mine.kino.plugin.google.core.test
テスト用プラグインには、テスト対象のプラグイン「nu.mine.kino.plugin.google.core」と「org.junit」を依存プラグインに追加しておきます。「MANIFEST.MF」は次のようになりました。
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Test プラグイン Bundle-SymbolicName: nu.mine.kino.plugin.google.core.test Bundle-Version: 1.0.0 Bundle-Activator: nu.mine.kino.plugin.google.core.test.TestPlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.ui,org.eclipse.core.runtime, nu.mine.kino.plugin.google.core, org.junit Eclipse-AutoStart: true
テストクラスを作成する
メソッドを実装したクラスnu.mine.kino.plugin.google.core.GoogleCorePluginをパッケージ・エクスプローラで右クリックし、[新規]-[その他]を選択してウィザードを開きます。ウィザードでJUnitテスト・ケースを選択します。Eclipseが「junit.jar」をビルドパスに追加するかを聞いてきますが、テストクラスはテスト用プラグインに配置するので、[いいえ]を選択します。続いてテストクラスを作成するダイアログが開くので、ソース・フォルダのプロジェクト名を「nu.mine.kino.plugin.google.core.test」に変更します。

[次へ]をクリックするとテストメソッドの追加画面になりますが、先のメソッド
getSplellingSuggestion(String myGoogleKey, String text)search(String myGoogleKey, String searchText)
にチェックして[終了]をクリックします。

テストクラスGoogleCorePluginTestには次のようにメソッドを実装しました。
public void testSearch() { try { GoogleSearchResult result = GoogleCorePlugin.getDefault().search( "xxxxxxxxxxxx", "Eclipse"); // ↑Google SOAP Search API のサイトで取得したキー値をセットする ResultElement[] resultElements = result.getResultElements(); for (int i = 0; i < resultElements.length; i++) { ResultElement element = resultElements[i]; System.out.println( element.getTitle() + ":" + element.getURL()); } } catch (CoreException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } }
public void testGetSplellingSuggestion() { String spellingSuggestion = GoogleCorePlugin.getDefault() .getSpellingSuggestion("xxxxxxxxxxxx","Eclipse"); if (spellingSuggestion != null) { System.out.println("修正候補は:" + spellingSuggestion); } else { System.out.println("修正候補はありません"); } spellingSuggestion = GoogleCorePlugin.getDefault() .getSpellingSuggestion("xxxxxxxxxxxx","Eclipsee"); if (spellingSuggestion != null) { System.out.println("修正候補は:" + spellingSuggestion); } else { System.out.println("修正候補はありません"); } }
"xxxxxxxxxxxx"の箇所は、あらかじめGoogle SOAP Search APIのサイトで取得しておいたキーコードを設定してください。
さて、このテストメソッドを実行します。パッケージ・エクスプローラで[GoogleCorePluginTest]を選択し、Eclipseのツールバーから[実行]-[JUnit プラグイン・テスト]を選択します。Eclipseが起動して、Googleプラグインのメソッドが実行され、コンソールに実行結果が表示されました。確かにプラグインが実行されていることが確認できました。
2006-06-25 00:15:55,203 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - start(BundleContext) - start 2006/06/25 0:15:55 org.apache.axis.utils.JavaUtils isAttachmentSupported 警告: 要求されたクラス(javax.activation.DataHandler と javax.
mail.internet.MimeMultipart)が見つかりません。Attachmentサポート
が利用できません。 / [en]-(Unable to find required classes (javax
.activation.DataHandler and javax.mail.internet.MimeMultipart).
Attachment support is disabled.) 2006-06-25 00:15:56,312 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - start(BundleContext) - end 2006-06-25 00:15:56,437 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - search(String) - start 2006-06-25 00:15:59,046 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - search(String) - end --- 以下、検索結果 --- エクリプス:http://eclipsewiki.net/eclipse/ 富士通テン : FUJITSU TEN Japan:http://www.fujitsu-ten.co.jp/ <b>ECLIPSE</b>:http://www.fujitsu-ten.co.jp/eclipse/index.html @IT:連載 <b>Eclipse</b>を使おう!(1):http://www.atmarkit.co
.jp/fjava/rensai2/eclipse01/eclipse01.html @IT:<b>Eclipse</b>を使おう(1):http://www.atmarkit.co.jp/fjava
/rensai3/eclipse31_01/eclipse31_01_1.html @IT:<b>Eclipse</b>ではじめるプログラミング(1):http://www
.atmarkit.co.jp/fjava/rensai3/eclipsejava01/eclipse01.html www.<b>eclipse</b>-td.com:http://www.eclipse-td.com/ LIVE! <b>ECLIPSE</b> 2006:トップページ:http://www.live-eclipse.org/ <b>eclipse</b>を使ってみよう:http://muimi.com/j/eclipse/ <b>ECLIPSE</b> 桐原いづみ+KEIG Website:http://www.uni-sex.info/ --- 以上、検索結果 --- 2006-06-25 13:36:54,281 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - start 2006-06-25 13:36:54,828 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - end 修正候補はありません 2006-06-25 13:36:54,828 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - start 2006-06-25 13:36:55,250 [main] DEBUG nu.mine.kino.plugin.google
.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - end 修正候補は:Eclipse <- スペルミスを補正してくれている
おわりに
今回はEclipseプラグイン開発のうち、GUIに依存しないコアな部分の開発手順を紹介しました。Eclipseプラグインの開発といっても、通常のJavaプロジェクトやJ2EEのプロジェクトのやり方とそんなに変わらないことがご理解いただけたと思います。また単体テスト環境がEclipseに統合されていることも紹介しました。次回は今回作成したプラグインを使用するGUIのプラグインを作成したいと思います。

.core.GoogleCorePlugin - start(BundleContext) - start
2006/06/25 0:15:55 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告: 要求されたクラス(javax.activation.DataHandler と javax.