As of Groovy-Eclipse 2.5.2, there is now command line support for testing DSLD descriptors. Because DSLD support us intertwined with Eclipse API, it is not possible to simply run unit tests against them. Rather, it is necessary to load up the Eclipse workbench and process the DSLD files from within the workbench in order to evaluate them.

Setting up

You can only evaluate DSLDs against an existing Eclipse project. So, you need to create an eclipse project inside an existing workspace.

  1. Start Eclipse
  2. Create a groovy project
  3. Create some Groovy classes
  4. You can add your DSLDs directly to this project, or you can add them later when you perform the type checking.
  5. Add some type annotations to assert the static types of certain expressions. Eg-
    def name = "myName"
    name // TYPE:java.lang.String
    [name] // TYPE:java.util.List<java.lang.String>
    [name:1] // TYPE:java.util.Map<java.lang.String,java.lang.Integer>
    

Shut down Eclipse and you are ready to run static type checking.

Running static type checking

The command looks like this:

eclipse -application org.codehause.groovy.eclipse.staticCheck [-data /path/to/eclipse/workspace] \
    [--help] [-h] [--extra_dslds <FILES>] [--assertions_only] [--excludes <PATH>] \
    [--includes <PATH>] <PROJECT_NAME>

Here is a description of the arguments:

--help OR -h

Prints a help message and exits.

--extra_dslds

list of extra dsld files to be included in this check. Use '|' as a file separator.

--assertions_only

Don't report unknown types. Only look for type assertions

--excludes

Project-relative exclusion filters.

--includes

Project-relative inclusion filters.

<PROJECT_NAME>

Name of a project to type check. Must be already in the workspace.

The exclusion and inclusion filters use the syntax from ant filesets. Individual filters can be concatenated using '|'.

For example:

--includes "**" --excludes "src/test/java/**/*Test.groovy|src/test/groovy/**/*Test.groovy"

includes all files, but excludes all *Test.groovy files in test/java and test/groovy.

The results

Let's assume that there is a Groovy project in an Eclipse workspace with a single file:

happiness  // TYPE:java.lang.Integer
happiness  // TYPE:java.lang.String
somethingUndefined

And this DSLD file exists outside of the workspace:

currentType().accept { property name:"happiness", type:Integer }

Then executing this command:

eclipse -application org.codehause.groovy.eclipse.staticCheck --extra_dslds /tmp/myapp.dsld MyProj

Will produce the following output:


Adding file:/tmp/other.dsld

Checking: /MyProj/src/MyScript.groovy
	Line 2: Invalid inferred type.  happiness Expected: java.lang.String Actual: java.lang.Integer
	Line 3: unknown type: somethingUndefined

Removing file:/tmp/other.dsld

If you want to suppress all of the unknown type warnings, then include the --assertions_only parameter on the command line.

You may also see some error messages sent to syserr, like this:

Job found still running after platform shutdown.  Jobs should be canceled by the plugin that scheduled them during shutdown: org.eclipse.jdt.internal.ui.InitializeAfterLoadJob

These messages can be safely ignored. These messages occur since Eclipse is being shut down early enough before all initialization jobs are complete. We'll be fixing this in future versions.

More to come

This is just a first pass at static checking. We are looking for feedback as to the best way to display failed assertions, or if there is anything else that can be improved. If there is significant interest, we can do the work to make it easy to create JUnit (or Spock!) tests for DSLDs, but we need to hear the feedback first.