Eclipse Plug-In Testing with JUnit Plug-In Tests

I recently mentioned that my current research project is a subset of AADL and an associated Eclipse plug-in which translates from that subset into Java.  Since both my advisors and I are interested in following recommended software engineering practices, I needed to figure out how to programmatically test my plug-in’s functionality. Unfortunately, testing an Eclipse plug-in can be sort of complicated, since some of your code may depend on Eclipse itself — either Eclipse services or the UI — running.  Fortunately, Eclipse’s Plug-in Development Environment (PDE) provides a launcher for JUnit tests that makes the process more straightforward.

Plug-In Test Run Configuration
The JUnit Plug-In Test Run Configuration

The functionality I relied on in OSATE2 (the AADL-focused Eclipse distribution) was, unfortunately, deeply tied to the UI thread. This meant that I needed to launch Eclipse as part of my test suite, initialize the project(s) I needed for compilation, and then run my tests. Unlike some of the other tasks I’d eventually work on, I didn’t find any super-clear tutorials on this stuff, so while it wasn’t super difficult, I had to sort of hack my way through it.

The testing vision

At a high-level, the basic outline of what I needed to do was (steps 2-4 are repeated for each test):

  1. Initialize the environment (using JUnit’s @BeforeClass annotation)
    1. Execute a command (which creates a built-in project) in the running version of Eclipse provided by the launcher
    2. Create a test project
    3. Add XText and AADL natures to the project
    4. Mark the built-in project as a dependency of the test project
    5. Create folders and copy in source files
    6. Build the project
  2. Run pre-test setup (using JUnit’s @Before annotation)
  3. Run the test (using JUnit’s @Test annotation)
    1. Specify which files are needed for this test
    2. Run the translator on the specified files
    3. Inspect the model and compare it to expected values
  4. Run post-test teardown (using JUnit’s @After annotation)

I ended up structuring my test suite so that one class contained all the initialization logic common to each test, and then dividing the actual tests between a number of files depending on their functionality. This is pretty easy to do with the  @RunWith  and  @Suite.SuiteClasses  JUnit annotations:

Lessons Learned

As I built and tweaked my test suite, I learned a number of things that may help other people working on plug-in tests:

  • In my list of steps, steps 1 and 2 should not be combined. This is because OSATE2 uses an XtextResourceSet to store the files contained in a project, and that class does substantial behind-the-scenes caching. I was unable to get around this caching, and I probably shouldn’t even have been trying to defeat the optimizations in the first place — there’s no reason to recreate the various files that are re-used between tests.
  • All AADL projects can use certain built-in properties.  These properties are typically created by running an OSATE2 command (via a right-click menu). I found the command’s id by sifting through the various OSATE2 component’s plugin.xml files, and ran it (this code will need to be within a try / catch block):

    The downside to this code  is that it relies on the Eclipse UI.  The PDE’s JUnit Plug-In test launch configuration is smart in that it won’t launch a UI if it doesn’t need to, so using the UI should be avoided if possible.  Unfortunately, the functionality of this command couldn’t be recreated without getting seriously hack-y.
  • Forgetting step 1.3 (adding project natures) will lead to some really screwy errors.  Natures are easy to add — using IProjectDescription — once you have their ids, which are again found by sifting through plugin.xml files:
  • Same thing with not compiling the project — it sounds basic, but since my translator doesn’t explicitly require Xtext validation / compilation, I didn’t know that it would be required.  Fortunately, once the project is defined, it’s just a single command:

The full testing package is available over on github, and most of the interesting initialization code can be found in AllTests.java. Let me know if you have any questions or suggestions in the comments below!

One Reply to “Eclipse Plug-In Testing with JUnit Plug-In Tests”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.