1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.util;
17  
18  import java.lang.annotation.ElementType;
19  import java.lang.annotation.Retention;
20  import java.lang.annotation.RetentionPolicy;
21  import java.lang.annotation.Target;
22  import java.lang.reflect.Field;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.seasar.framework.beans.BeanDesc;
28  import org.seasar.framework.beans.PropertyDesc;
29  import org.seasar.framework.beans.factory.BeanDescFactory;
30  
31  /**
32   * {@link AnnotationUtil} のためのテストクラスです。<br />
33   * 
34   * @author y-komori
35   */
36  public class AnnotationUtilTest extends TestCase {
37  
38      @Override
39      protected void setUp() throws Exception {
40          super.setUp();
41      }
42  
43      public void testGetAnnotatedFields() {
44          List<Field> fields = AnnotationUtil.getAnnotatedFields(
45                  TargetClass.class, TestAnnotation.class);
46          try {
47              assertEquals("1", TargetClass.class.getField("field3"), fields
48                      .get(0));
49              assertEquals("2", TargetClass.class.getField("field4"), fields
50                      .get(1));
51              assertEquals("3", TargetClass.class.getField("field1"), fields
52                      .get(2));
53          } catch (Exception ex) {
54          }
55  
56          fields = AnnotationUtil.getAnnotatedFields(TargetClass.class,
57                  TestAnnotation.class);
58          try {
59              assertEquals("4", TargetClass.class.getField("field3"), fields
60                      .get(0));
61              assertEquals("5", TargetClass.class.getField("field4"), fields
62                      .get(1));
63              assertEquals("6", TargetClass.class.getField("field1"), fields
64                      .get(2));
65          } catch (Exception ex) {
66          }
67      }
68  
69      public void testGetAnnotatedPropertyDescs() {
70          BeanDesc desc = BeanDescFactory.getBeanDesc(TargetClass.class);
71          List<PropertyDesc> pds = AnnotationUtil.getAnnotatedPropertyDescs(
72                  TargetClass.class, TestAnnotation.class);
73          try {
74              assertEquals("1", desc.getPropertyDesc("field3"), pds.get(0));
75              assertEquals("2", desc.getPropertyDesc("field4"), pds.get(1));
76              assertEquals("3", desc.getPropertyDesc("field1"), pds.get(2));
77          } catch (Exception ex) {
78          }
79  
80          pds = AnnotationUtil.getAnnotatedPropertyDescs(TargetClass.class,
81                  TestAnnotation.class);
82          try {
83              assertEquals("4", desc.getPropertyDesc("field3"), pds.get(0));
84              assertEquals("5", desc.getPropertyDesc("field4"), pds.get(1));
85              assertEquals("6", desc.getPropertyDesc("field1"), pds.get(2));
86          } catch (Exception ex) {
87          }
88      }
89  
90      @Retention(RetentionPolicy.RUNTIME)
91      @Target(ElementType.FIELD)
92      @interface TestAnnotation {
93      }
94  
95      class TargetSuperClass {
96          @TestAnnotation
97          private boolean field1;
98  
99          private int field2;
100 
101         public boolean getField1() {
102             return this.field1;
103         }
104 
105         public void setField1(final boolean field1) {
106             this.field1 = field1;
107         }
108 
109         public int getField2() {
110             return this.field2;
111         }
112 
113         public void setField2(final int field2) {
114             this.field2 = field2;
115         }
116     }
117 
118     class TargetClass extends TargetSuperClass {
119         @TestAnnotation
120         private long field3;
121 
122         @TestAnnotation
123         private String field4;
124 
125         private Object field5;
126 
127         public long getField3() {
128             return this.field3;
129         }
130 
131         public void setField3(final long field3) {
132             this.field3 = field3;
133         }
134 
135         public String getField4() {
136             return this.field4;
137         }
138 
139         public void setField4(final String field4) {
140             this.field4 = field4;
141         }
142 
143         public Object getField5() {
144             return this.field5;
145         }
146 
147         public void setField5(final Object field5) {
148             this.field5 = field5;
149         }
150     }
151 }