Original Address: http://www.cnblogs.com/liuzhuo/archive/2010/08/17/eclipse_plugin_1_1_2.html
1. Test the plug-in using JUnit
JUnit is already embedded in eclipse, and we can use JUnit to test the plug-in as a monomer. The generic junit is not able to test the plug-in portion (the part that the Eclipse API relies on), but it makes it possible to unit-test the plug-in using JUnit in the PDE environment.
To test first add a plugin dependency, click the [Add] button on the [Dependency (Dependencies)] page of the Plugin settings page and add [Org.junit (3.8.2)] as the required plugin (Figure 3-1).
Figure 3-1 increasing the dependency of the Org.junit plugin
Next, build the test case (TestCase). The test case is the same as the generic junit use case, inheriting junit.framework.TestCase. The test case in code 1 illustrates that you open a view in the Workbench page and confirm that the open view is active.
Code 1 example of a plug-in test case
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class SampleViewTest
extends TestCase {
public SampleViewTest(String name)
throws Exception {
//取得当前活动的页面
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
//打开视图
page.showView(
"cn.sf.amateras.sample.views.SampleView"
);
//确认打开的视图处于激活状态
IWorkbenchPart activePart = page.getActivePart();
assertTrue(activePart
instanceof IViewPart);
assertEquals(
"cn.sf.amateras.sample.views.SampleView"
,
activePart.getSite().getId());
}
}
|
The current test case can be executed by right-clicking on the test class [Execute (Run as)]->[junit plug-in test (JUnit plug-in test)].
Figure 3-2 Performing the JUnit plug-in test
2. Debugging and Error handling
Debugging plug-ins
When you start the Runtime Workbench in debug mode, you can set breakpoints in your program just like normal Java programs. Debug mode startup can be from the Manifest Editor's profile page, click Debug Eclipse App (Lunach an Eclipse application in debug mode), or click the Debug button on the toolbar.
In the event of an error, the following two things need to be done:
1) Notify Users
2) Record log
Output log
Log output to the [/.log] file for the [. Metadata] folder under Eclipse. The output log uses the Org.eclipse.core.runtime.ILog interface to obtain an instance of the Ilog interface through the plug-in class. As shown in code 2
Code 2 output Log
?
1 2 3 4 5 6 7 |
ILog log = Activator.getDefault().getLog();
log.log(
new Status(IStatus.OK, Activator.PLUGIN_ID,
0
,
"正常消息"
,
null
));
log.log(
new Status(IStatus.OK, Activator.PLUGIN_ID,
0
,
"异常消息"
,
new Exception(
"输出日志的异常"
)));
|
The parameters for the constructor of the status object are shown in table 1.
Table 1 Construction parameters for status
Parameters |
Description |
int Severty |
Important degree. The candidate values are Istatus.ok, Istatus.error, Istatus.info, istatus.warning, Istatus.cancel. |
String pluginID |
The ID of the plugin |
int code |
Plug-in fixed message code, or Istatus.ok. |
String message |
The output log message. |
Throwable exception |
The exception for the output log. There is no time to add null. |
Error notification
There are many ways to notify a user after an error occurs, and the simplest way to do this is to use the JFace Org.eclipse.jface.dialogs.ErrorDialog class.
Code 3 using Errordialog
?
1 2 3 4 5 6 7 |
try
{
//.... }
catch
(Exception e){
IStatus status =
new Status(IStatus.ERROR, Activator.PLUGIN_ID,
0
, e.toString(), e);
ErrorDialog.openError(window.getShell(),
"错误"
,
"插件出错了!"
, status); }
|
Parameters at runtime when the Workbench starts
Eclipse is made up of a lot of plug-ins that run a very complex process behind it, so it also requires the appropriate memory. In particular, many plugins are prone to lack of VM resident (permanent) regions when loading large classes.
Typically, the heap area and the size of the resident area used by Eclipse can be set through the Eclipse.ini file in the Eclipse installation directory.
Code 4 default Eclipse.ini file
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
-showsplash org.eclipse.platform --launcher.XXMaxPermSize 256m --launcher.defaultAction openFile -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m |
You can also specify the size of the space when there is insufficient memory space in the plug-in development process by using the [VM parameters] item in the [Parameters]tab page of the run]->[Run Configurations] window.
Figure 3-3 VM parameter settings
Go The basics of Eclipse Plugin Development (3) testing and debugging of plug-ins