Multiple Parameter Annotations


You may want annotations to support multiple parameters. As an example, suppose you want developers to add their initials when ignoring a test method. A proper annotation might be:


 @Ignore(reasons={"just because", "and why not"}, initials="jjl") 

Now that you have more than one annotation parameter, you must supply member-value pairs. Each member-value pair includes the member name, which must match an annotation type member, followed by the equals (=) sign, followed by the constant value for the member.

The second member-value pair in the above example has initials as a member name and "jjl" as its value. In order to support this annotation, you must modify the @Ignore annotation type declaration to include initials as an additional member. You must also rename the value member to reasons. Each key in an annotation member-value pair must match a member name in the annotation type declaration.

 package sis.testing; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Ignore {    String[] reasons();    String initials(); } 

You can specify member-value pairs in any order within an annotation. The order need not match the member order of the annotation type declaration.

Here are the corresponding modifications to TestRunnerTest:

 package sis.testing; // ... public class TestRunnerTest {    // ...    public static final String IGNORE_INITIALS = "jjl";    // ...    private void assertIgnoreReasons() {       Map<Method, Ignore> ignoredMethods = runner.getIgnoredMethods();       Map.Entry<Method, Ignore> entry = getSoleEntry(ignoredMethods);       assert "testC".equals(entry.getKey().getName()):          "unexpected ignore method: " + entry.getKey();       Ignore ignore = entry.getValue();       String[] ignoreReasons = ignore.reasons();       assert 2 == ignoreReasons.length;       assert IGNORE_REASON1.equals(ignoreReasons[0]);       assert IGNORE_REASON2.equals(ignoreReasons[1]);       assert IGNORE_INITIALS.equals(ignore.initials());    }    // ... } class IgnoreMethodTest {    @TestMethod public void testA() {}    @TestMethod public void testB() {}    @Ignore(       reasons={TestRunnerTest.IGNORE_REASON1,        TestRunnerTest.IGNORE_REASON2},       initials=TestRunnerTest.IGNORE_INITIALS)    @TestMethod public void testC() {} } 

You do not need to make modifications to TestRunner. You will need to make a small modification to TestRunnerUI to extract the reasons and initials properly from the Ignore object.

 private void showIgnoredMethods() {    if (runner.getIgnoredMethods().isEmpty())       return;    System.out.println("\nIgnored Methods");    for (Map.Entry<Method, Ignore> entry:          runner.getIgnoredMethods().entrySet()) {       Ignore ignore = entry.getValue();       System.out.printf("%s: %s (by %s)",          entry.getKey(),          Arrays.toString(ignore.reasons()),          ignore.initials());    } } 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net