[Eclipse plug-in Development Guide] 2.8 plug-in test compilation

Source: Internet
Author: User

Eclipse is a continuously developing product. When building commercial plug-ins, testing is necessary to solve product problems caused by complicated reasons. If the goal is to develop and release a one-time plug-in, manual testing is enough. However, automatic testing can improve the product as the product continues to develop.

2.8.1 test preparation

Before creating the favorites view, you must modify the favorites plug-in list so that the test plug-in can access some required classes. Double-clickPlugin. xmlOpen the plug-in List editor, and then open the runtime page (Figure 2-11 ). In the exported packages section, click Add ..., SelectCom. qualityeclipse. favorites. ViewsPackage, and then select File> Save to save all changes.

Then, add a suitable accesser so that the plug-in can verify the View content. InFavoritesviewClass, add the following method:

/** <Br/> * for testing purposes only. <br/> * @ return the table viewer in the favorites view <br/> */<br/> Public tableviewer getfavoritesviewer () {<br/> return viewer; <br/>}

Note: You can restrict the visibility of the exported package on the runtime page of the package visibility section in the plug-in List editor. You can choose to put the test in the package so that no package needs to be exported.

2.8.2 create a plug-in test project

To create a plug-in project, follow these steps:

● Name the project com. qualityeclipse. favorites. Test.

● Change the plug-in class to com. qualityeclipse. favorites. Test. favoritestestplugin.

● Do not create a plug-in using one of these templates in the check box

After the project is created, use the plug-in List editor dependencies page (Figure 2-10) to add the following plug-ins and save changes:

● Com. qualityeclipse. favorites

● Org. JUnit

2.8.3 create a test plug-in

After the project has been created and the plug-in list has been modified, it is time to perform a simple test on the favorites plug-in. The goal is to test the displayed favorites view, verify the content, and then hide it.

Package COM. qualityeclipse. favorites. test; </P> <p> Import JUnit. framework. assertionfailederror; <br/> Import JUnit. framework. testcase; </P> <p> Import Org. eclipse. core. runtime. platform; <br/> Import Org. eclipse. jface. viewers. ilabelprovider; <br/> Import Org. eclipse. jface. viewers. istructuredcontentprovider; <br/> Import Org. eclipse. jface. viewers. itablelabelprovider; <br/> Import Org. eclipse. jface. viewers. tabl Eviewer; <br/> Import Org. eclipse. SWT. widgets. display; <br/> Import Org. eclipse. UI. platformui; </P> <p> Import COM. qualityeclipse. favorites. views. favoritesview; </P> <p>/** <br/> * The class <code> favoritesviewtest </code> contains tests <br/> * for the class {@ link <br /> * COM. qualityeclipse. favorites. views. favoritesview }. <br/> * @ pattern JUnit test case <br/> * @ generatedby codepro studio <br/> */<Br/> public class favoritesviewtest extends testcase <br/>{< br/> Private Static final string view_id = <br/> "com. qualityeclipse. favorites. views. favoritesview "; </P> <p>/** <br/> * the object that is being tested. <br/> * @ see COM. qualityeclipse. favorites. views. favoritesview <br/> */<br/> private favoritesview testview; <br/>/** <br/> * construct new test instance. <br/> * @ P Aram name the test name <br/> */<br/> Public favoritesviewtest (string name) {<br/> super (name ); <br/>}</P> <p>/** <br/> * perform pre-test initialization. <br/> * @ throws exception <br/> * @ see testcase # setup () <br/> */<br/> protected void setup () throws exception {<br/> super. setup (); <br/> // initialize the test fixture for each test <br/> // that is run. <br/> waitforjobs (); <Br/> testview = (favoritesview) <br/> platformui <br/>. getworkbench () <br/>. getactiveworkbenchwindow () <br/>. getactivepage () <br/>. showview (view_id); </P> <p> // delay for 3 seconds so that <br/> // The favorites view can be seen. <br/> waitforjobs (); <br/> delay( 3000); </P> <p> // Add additional setup code here. <br/>}</P> <p>/** <br/> * perform post-test cleanup. <br/> * @ throws exce Ption <br/> * @ see testcase # teardown () <br/> */<br/> protected void teardown () throws exception {<br/> super. teardown (); <br/> // dispose of test fixture. <br/> waitforjobs (); <br/> platformui <br/>. getworkbench () <br/>. getactiveworkbenchwindow () <br/>. getactivepage () <br/>. hideview (testview); </P> <p> // Add additional teardown code here. <br/>}< br/>/** <br/> * run the view test. <B R/> */<br/> Public void testview () {<br/> tableviewer viewer = testview. getfavoritesviewer (); <br/> object [] expectedcontent = <br/> new object [] {"one", "two", "three "}; <br/> object [] expectedlabels = <br/> New String [] {"one", "two", "three "}; </P> <p> // assert valid content. <br/> istructuredcontentprovider contentprovider = <br/> (istructuredcontentprovider) <br/> viewer. getcontentprovi Der (); <br/> assertequals (expectedcontent, <br/> contentprovider. getelements (viewer. getinput (); </P> <p> // assert valid labels. <br/> itablelabelprovider labelprovider = <br/> (itablelabelprovider) viewer. getlabelprovider (); <br/> for (INT I = 0; I <expectedlabels. length; I ++) <br/> assertequals (expectedlabels [I], <br/> labelprovider. getcolumntext (expectedcontent [I], 1); <br/>}</P> <p>/** <br/> * Process UI input but do not return for the <br/> * specified time interval. <br/> * @ Param waittimemillis the number of milliseconds <br/> */<br/> private void delay (long waittimemillis) {<br/> display = display. getcurrent (); </P> <p> // if this is the UI thread, <br/> // then process input. <br/> If (display! = NULL) {<br/> long endtimemillis = <br/> system. currenttimemillis () + waittimemillis; <br/> while (system. currenttimemillis () <endtimemillis) <br/>{< br/> If (! Display. readanddispatch () <br/> display. sleep (); <br/>}< br/> display. update (); <br/>}< br/> // otherwise, perform a simple sleep. <br/> else {<br/> try {<br/> thread. sleep (waittimemillis); <br/>}< br/> catch (interruptedexception e) {<br/> // ignored. <br/>}< br/>/** <br/> * wait until all background tasks are complete. <br/> */<br/> Public void waitforjobs () {<br/> whil E (platform. getjobmanager (). currentjob ()! = NULL) <br/> delay (1000); <br/>}</P> <p>/** <br/> * assert that the two arrays are equal. <br/> * Throw an assertionexception if they are not. <br/> * @ Param expected first array <br/> * @ Param actual second array <br/> */<br/> private void assertequals (Object [] expected, object [] actual) {<br/> If (expected = NULL) {<br/> If (actual = NULL) <br/> return; <br/> throw new assertionfailederror (<br/> "expected is null, but actual is not "); <br/>}< br/> else {<br/> If (actual = NULL) <br/> throw new assertionfailederror (<br/> "actual is null, but expected is not "); <br/>}</P> <p> assertequals (<br/>" expected. length "<br/> + expected. length <br/> + ", but actual. length "<br/> + actual. length, <br/> expected. length, <br/> actual. length); </P> <p> for (INT I = 0; I <actual. length; I ++) <br/> assertequals (<br/> "expected [" + I + <br/> "] is not equal to actual [" + <br/> I + "]", <br/> expected [I], <br/> actual [I]); <br/>}< br/>

2.8.4 run a test plug-in

After the plug-in class is created, configure and execute the test. Familiar with creating running configurations (section 2.6.1), right-clickFavoritesviewtest: Select Run as> JUnit plug-in test. In this way, the test configuration is automatically built and you can perform this test. The runtime workbench is displayed, the favorites view is opened, and the runtime workbench is closed. The JUnit view shows that your test is successful, and the content of the favorites view is displayed correctly (Figure 2-27 ).

Figure 2-27 Test Configuration Wizard Page

Right-clickFavoritesviewtest, select Run as> Run... to open the Configuration Wizard Page (Figure 2-28 ). You can specify whether to run a single test or all tests in the project at the same time.

Figure 2-28 Test Configuration Wizard Page

2.8.5 uninstall favorites

In the Development workspace, follow these steps to delete the favorites Plugin:

1. Disable the favorites view.

2. Disable eclipse

3. Delete the plug-in DirectoryCom. Quality. favorites_1.0.0.jar.

4. Restart eclipse. If an error occurs during restart (Figure 2-29), the favorites view is not closed at Step 2.

Figure 2-29 the problem pop-up window during eclipse restart

5. open the show view pop-up window (Figure 2-18) to verify that the favorites view is no longer in use. At the same time, check whether the quality eclipse category is missing.

This article is the original article of eclipselight.org (eclipse light). For more information, see the source.
Fixed Link: http://www.eclipselight.org/eclipse-plugin-tutorial/742/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.