Additional Return Types and Complex Annotation Types


In addition to String and String[], an annotation value can be a primitive, an enum, a Class reference, an annotation type itself, or an array of any of these types.

The following test (in TestRunnerTest) sets up the requirement for an @Ignore annotation to include a date. The Date type will be an annotation; its members each return an int value.

 @TestMethod public void dateTest() {    runTests(IgnoreDateTest.class);    Map<Method, Ignore> ignoredMethods = runner.getIgnoredMethods();    Map.Entry<Method, Ignore> entry = getSoleEntry(ignoredMethods);    Ignore ignore = entry.getValue();    sis.testing.Date date = ignore.date();    assert 1 == date.month();    assert 2 == date.day();    assert 2005 == date.year(); } class IgnoreDateTest {    @Ignore(          initials=TestRunnerTest.IGNORE_INITIALS,          date=@Date(month=1, day=2, year=2005))       @TestMethod public void testC() {} } 

The annotation in IgnoreDateTest is known as a complex annotationan annotation that includes another annotation. @Ignore includes a member, date, whose value is another annotation, @Date.

The definition of the sis.testing.Date annotation type:

 package sis.testing; public @interface Date {    int month();    int day();    int year(); } 

The @Ignore annotation type can now define a date member that returns a testing.Date instance:

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

Since the Date annotation type is only used as part of another annotation, it does not need to specify a retention or a target.

You may not declare a recursive annotation typethat is, an annotation type member with the same return type as the annotation itself.

To get your tests to compile and pass, you'll also need to modify IgnoreMethodTest and DefaultIgnoreMethodTest:

 class IgnoreMethodTest {    @TestMethod public void testA() {}    @TestMethod public void testB() {}    @Ignore(       reasons={TestRunnerTest.IGNORE_REASON1,                TestRunnerTest.IGNORE_REASON2},       initials=TestRunnerTest.IGNORE_INITIALS,       date=@Date(month=1, day=2, year=2005))    @TestMethod public void testC() {} } class DefaultIgnoreMethodTest {    @TestMethod public void testA() {}    @TestMethod public void testB() {}    @Ignore(initials=TestRunnerTest.IGNORE_INITIALS,            date=@Date(month=1, day=2, year=2005))       @TestMethod public void testC() {} } 



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