1   /*
2    * Copyright 2002-2006 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  package org.springframework.binding.support;
18  
19  import java.lang.annotation.Annotation;
20  import java.util.Map;
21  import java.util.HashMap;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * @author andy
27   * @since May 8, 2006 4:18:44 PM
28   */
29  public class ReflectionAnnotationTranslatorTests extends TestCase {
30      @NoValueAnnotation
31      public void testNoValueAnnotation() throws Exception {
32          final Annotation annotation = getClass().getDeclaredMethod("testNoValueAnnotation").getAnnotation(NoValueAnnotation.class);
33          assertNotNull(annotation);
34          
35          final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
36          final Map<String,Object> result = new HashMap<String,Object>();
37          t.translate(annotation, result);
38          assertEquals(2, result.size());
39          assertSame(Boolean.TRUE, result.get("org.springframework.binding.support.NoValueAnnotation"));
40          NoValueAnnotation nva = (NoValueAnnotation)result.get("@" + NoValueAnnotation.class.getName());
41          assertNotNull(nva);
42      }
43      
44      @SingleValueAnnotation("Hello, world!")
45      public void testSingleValueAnnotation() throws Exception {
46          final Annotation annotation = getClass().getDeclaredMethod("testSingleValueAnnotation").getAnnotation(SingleValueAnnotation.class);
47          assertNotNull(annotation);
48          
49          final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
50          final Map<String,Object> result = new HashMap<String,Object>();
51          t.translate(annotation, result);
52          assertEquals(2, result.size());
53          assertEquals("Hello, world!", result.get("org.springframework.binding.support.SingleValueAnnotation"));
54          SingleValueAnnotation sva = (SingleValueAnnotation)result.get("@" + SingleValueAnnotation.class.getName());
55          assertNotNull(sva);
56          assertEquals("Hello, world!", sva.value());
57      }
58      
59      @SingleValueAnnotation
60      public void testSingleValueAnnotationWithDefaultValue() throws Exception {
61          final Annotation annotation = getClass().getDeclaredMethod("testSingleValueAnnotationWithDefaultValue").getAnnotation(SingleValueAnnotation.class);
62          assertNotNull(annotation);
63          
64          final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
65          final Map<String,Object> result = new HashMap<String,Object>();
66          t.translate(annotation, result);
67          assertEquals(2, result.size());
68          assertEquals("This is a test", result.get("org.springframework.binding.support.SingleValueAnnotation"));
69          SingleValueAnnotation sva = (SingleValueAnnotation)result.get("@" + SingleValueAnnotation.class.getName());
70          assertNotNull(sva);
71          assertEquals("This is a test", sva.value());
72      }
73      
74      @MultiValueAnnotation(name="John Doe", age=25.5, rank=10)
75      public void testMultiValueAnnotation() throws Exception {
76          final Annotation annotation = getClass().getDeclaredMethod("testMultiValueAnnotation").getAnnotation(MultiValueAnnotation.class);
77          assertNotNull(annotation);
78          
79          final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
80          final Map<String,Object> result = new HashMap<String,Object>();
81          t.translate(annotation, result);
82          assertEquals(5, result.size());
83          assertEquals(Boolean.TRUE, result.get("org.springframework.binding.support.MultiValueAnnotation"));
84          assertEquals("John Doe", result.get("org.springframework.binding.support.MultiValueAnnotation.name"));
85          assertEquals(25.5, result.get("org.springframework.binding.support.MultiValueAnnotation.age"));
86          assertEquals(10, result.get("org.springframework.binding.support.MultiValueAnnotation.rank"));
87          MultiValueAnnotation mva = (MultiValueAnnotation)result.get("@" + MultiValueAnnotation.class.getName());
88          assertNotNull(mva);
89          assertEquals("John Doe", mva.name());
90          assertEquals(25.5, mva.age());
91          assertEquals(10, mva.rank());
92      }
93  }