001 /*
002 * Copyright 2002-2006 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016
017 package org.springframework.binding.support;
018
019 import java.lang.annotation.Annotation;
020 import java.util.Map;
021 import java.util.HashMap;
022
023 import junit.framework.TestCase;
024
025 /**
026 * @author andy
027 * @since May 8, 2006 4:18:44 PM
028 */
029 public class ReflectionAnnotationTranslatorTests extends TestCase {
030 @NoValueAnnotation
031 public void testNoValueAnnotation() throws Exception {
032 final Annotation annotation = getClass().getDeclaredMethod("testNoValueAnnotation").getAnnotation(NoValueAnnotation.class);
033 assertNotNull(annotation);
034
035 final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
036 final Map<String,Object> result = new HashMap<String,Object>();
037 t.translate(annotation, result);
038 assertEquals(2, result.size());
039 assertSame(Boolean.TRUE, result.get("org.springframework.binding.support.NoValueAnnotation"));
040 NoValueAnnotation nva = (NoValueAnnotation)result.get("@" + NoValueAnnotation.class.getName());
041 assertNotNull(nva);
042 }
043
044 @SingleValueAnnotation("Hello, world!")
045 public void testSingleValueAnnotation() throws Exception {
046 final Annotation annotation = getClass().getDeclaredMethod("testSingleValueAnnotation").getAnnotation(SingleValueAnnotation.class);
047 assertNotNull(annotation);
048
049 final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
050 final Map<String,Object> result = new HashMap<String,Object>();
051 t.translate(annotation, result);
052 assertEquals(2, result.size());
053 assertEquals("Hello, world!", result.get("org.springframework.binding.support.SingleValueAnnotation"));
054 SingleValueAnnotation sva = (SingleValueAnnotation)result.get("@" + SingleValueAnnotation.class.getName());
055 assertNotNull(sva);
056 assertEquals("Hello, world!", sva.value());
057 }
058
059 @SingleValueAnnotation
060 public void testSingleValueAnnotationWithDefaultValue() throws Exception {
061 final Annotation annotation = getClass().getDeclaredMethod("testSingleValueAnnotationWithDefaultValue").getAnnotation(SingleValueAnnotation.class);
062 assertNotNull(annotation);
063
064 final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
065 final Map<String,Object> result = new HashMap<String,Object>();
066 t.translate(annotation, result);
067 assertEquals(2, result.size());
068 assertEquals("This is a test", result.get("org.springframework.binding.support.SingleValueAnnotation"));
069 SingleValueAnnotation sva = (SingleValueAnnotation)result.get("@" + SingleValueAnnotation.class.getName());
070 assertNotNull(sva);
071 assertEquals("This is a test", sva.value());
072 }
073
074 @MultiValueAnnotation(name="John Doe", age=25.5, rank=10)
075 public void testMultiValueAnnotation() throws Exception {
076 final Annotation annotation = getClass().getDeclaredMethod("testMultiValueAnnotation").getAnnotation(MultiValueAnnotation.class);
077 assertNotNull(annotation);
078
079 final ReflectionAnnotationTranslator t = new ReflectionAnnotationTranslator();
080 final Map<String,Object> result = new HashMap<String,Object>();
081 t.translate(annotation, result);
082 assertEquals(5, result.size());
083 assertEquals(Boolean.TRUE, result.get("org.springframework.binding.support.MultiValueAnnotation"));
084 assertEquals("John Doe", result.get("org.springframework.binding.support.MultiValueAnnotation.name"));
085 assertEquals(25.5, result.get("org.springframework.binding.support.MultiValueAnnotation.age"));
086 assertEquals(10, result.get("org.springframework.binding.support.MultiValueAnnotation.rank"));
087 MultiValueAnnotation mva = (MultiValueAnnotation)result.get("@" + MultiValueAnnotation.class.getName());
088 assertNotNull(mva);
089 assertEquals("John Doe", mva.name());
090 assertEquals(25.5, mva.age());
091 assertEquals(10, mva.rank());
092 }
093 }