Suppose that we have a UML profile that contains a stereotype with an attribute of a custom data type constrained by a regular expression. This tutorial steps through one way to automate tests that exercise this restriction.
Create a UML Profile:
We can use the instructions from a previous tutorial to create a UML profile with a restricted data type. In our case, a Contact is an Actor who has a US phone number. We can call our profile Contact.profile.uml:
Load the Profile Programmatically:
Suppose we need to access our profile from another Eclipse plug-in to create Contacts programmatically. In a plugins parent directory, we will then create a new Java Plug-in project, here called timezra.blog.uml_regex_validation.resources, and it will provide access to this profile from its plug-in Activator.
We can specify metadata for the plug-in in our META-INF/MANIFEST.MF:
We also want to create an additional source directory called profiles. We can put our Contact.profile.uml file in this directory so that it can be loaded from the classpath in our tests.
Our timezra.blog.uml_regex_validation.resources.Activator.java will handle loading the profile from a resource set:
Test with a Fragment:
Now that we have a way to load the UML profile programmatically, we can create a test Fragment as a sibling to the functional bundle in the plugins directory.
We will call this Fragment timezra.blog.uml_regex_validation.resources.tests, and we will again declare plug-in metadata in the META-INF/MANIFEST.MF:
The test suite will present a few challenges. For example, suppose we want to run our tests outside OSGi. We will need a way to simulate plug-in activation so that a singleton instance of our Activator is available. We will also need to initialize the resource set used to load the profile, and to redirect requests for platform resources to some other mechanism. Let's dig in our heels and get started by creating a new JUnit file, here called timezra.blog.uml_regex.validation.resources.tests.ContactTest.java:
We can start the framework and activate the resources bundle before each test runs, and dispose of them after each test finishes:
As well, we will initialize the resource set during setup;
We can use a simple mechanism that redirects platform resource and UML-specific URI requests to the classpath for loading the standard profiles as well as our custom profile:
We are now ready to write a few test cases. We can use the org.eclipse.emf.ecore.util.Diagnostician to perform validation for values set on the stereotype application itself or to check whether a value satisfies the stereotype attribute's restrictions. Our final class might look something like this:
Test With Tycho:
Suppose we want to automate our plug-in build and unit-tests. There are a few different options in the Eclipse eco-system, but my personal preference lately has been tycho. Currently, I am using tycho-0.8.0 and Maven 3-alpha-7 with Eclipse-3.5.2. Be aware that the latest version as of this first posting requires Maven 3-beta-1 and Eclipse-3.6-M7. My environment is configured according to the instructions provided by Mattias Holmqvist.
For our particular plugins, we can generate the poms for the functional resources plug-in, for the test fragment and for the parent plugins directory.
We can modify the pom generated in our fragment to include the following section, which will enable tycho to run microtests outside OSGi:
Finally we can run our unit-tests from the parent plugins directory:
The primary purpose of this tutorial has been to present a way to test Stereotype attributes that use UML Data Types constrained by regular expressions. Along the way, we have also developed techniques for simulating bundle activation outside OSGi, for loading platform plugin resources and resources registered through Eclipse extensions without Eclipse running, and for running microtests with Maven via tycho.