View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.test;
17  
18  import org.junit.Assert;
19  import org.junit.Test;
20  
21  import java.lang.annotation.*;
22  import java.lang.reflect.Method;
23  
24  /**
25   * Tests that ordering of annotation definitions is preserved 
26   * 
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class AnnotationOrderTest {
30      @Target({ElementType.TYPE, ElementType.METHOD})
31      @Retention(RetentionPolicy.RUNTIME)
32      @interface TestAnnotation {
33          String value();
34      }
35  
36      @Target({ElementType.TYPE, ElementType.METHOD})
37      @Retention(RetentionPolicy.RUNTIME)
38      @interface TestAnnotation1 {
39          String value();
40      }
41      
42      @Target({ElementType.TYPE, ElementType.METHOD})
43      @Retention(RetentionPolicy.RUNTIME)
44      @interface TestAnnotation2 {
45          String value();
46      }
47      
48      @Target({ElementType.TYPE, ElementType.METHOD})
49      @Retention(RetentionPolicy.RUNTIME)
50      @interface TestAnnotationHolder {
51          TestAnnotation[] value();
52      }
53  
54  
55      @Test
56      @TestAnnotation1("11")
57      @TestAnnotationHolder({
58              @TestAnnotation("1"),
59              @TestAnnotation("2"),
60              @TestAnnotation("3"),
61              @TestAnnotation("4"),
62              @TestAnnotation("5"),
63              @TestAnnotation("6")
64      })
65      @TestAnnotation2("22")
66      public void testAnnotationOrder() throws Exception {
67          Method me = getClass().getMethod("testAnnotationOrder");
68          Annotation[] annotations = me.getAnnotations();
69          Assert.assertEquals(4, annotations.length);
70          Assert.assertEquals(Test.class, annotations[0].annotationType());
71          Assert.assertEquals(TestAnnotation1.class, annotations[1].annotationType());
72          Assert.assertEquals(TestAnnotationHolder.class, annotations[2].annotationType());
73          Assert.assertEquals(TestAnnotation2.class, annotations[3].annotationType());
74          
75          TestAnnotationHolder holder = me.getAnnotation(TestAnnotationHolder.class);
76          TestAnnotation[] children = holder.value();
77          Assert.assertEquals(6, children.length);
78          Assert.assertEquals("1", children[0].value());
79          Assert.assertEquals("2", children[1].value());
80          Assert.assertEquals("3", children[2].value());
81          Assert.assertEquals("4", children[3].value());
82          Assert.assertEquals("5", children[4].value());
83          Assert.assertEquals("6", children[5].value());
84      }
85  }