View Javadoc

1   /*
2    * Copyright 2012 The Kuali Foundation
3    *
4    * Licensed under the the Educational Community License, Version 1.0
5    * (the "License"); you may not use this file except in compliance
6    * with the License.  You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.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.student.contract.model.impl;
17  
18  import java.io.File;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import javax.xml.bind.annotation.XmlEnum;
23  
24  import org.junit.Assert;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.BlockJUnit4ClassRunner;
29  import org.kuali.student.contract.model.util.JavaClassAnnotationUtils;
30  import org.kuali.student.model.annotation.EnumWithAnnotationValue;
31  import org.kuali.student.model.annotation.EnumWithoutAnnotationValue;
32  import org.slf4j.Logger;
33  
34  import com.thoughtworks.qdox.JavaDocBuilder;
35  import com.thoughtworks.qdox.model.DefaultDocletTagFactory;
36  import com.thoughtworks.qdox.model.JavaClass;
37  
38  /**
39   * 
40   * tests how to resolve both types of @XmlEnum annotations.  Those with and without the value defined.
41   * 
42   * @author Kuali Student Team
43   */
44  @RunWith(BlockJUnit4ClassRunner.class)
45  public class TestAnnotationValueResolution {
46  
47  	private static final Logger log = org.slf4j.LoggerFactory.getLogger(TestAnnotationValueResolution.class);
48  	
49  	private static Map<String, JavaClass> parsedJavaClassesMap;
50  	
51  	/**
52  	 * 
53  	 */
54  	public TestAnnotationValueResolution() {
55  		
56  	}
57  
58  	@BeforeClass
59  	public static void setup () {
60  		
61          DefaultDocletTagFactory dtf = new DefaultDocletTagFactory();
62          JavaDocBuilder builder = new JavaDocBuilder(dtf);
63          
64          builder.addSourceTree(new File("src/test/java/org/kuali/student/model/annotation"));
65          
66         parsedJavaClassesMap = new LinkedHashMap<String, JavaClass>();
67          
68          for (JavaClass javaClass : builder.getClasses()) {
69              
70          	parsedJavaClassesMap.put(javaClass.getName(), javaClass);
71              
72          }
73          
74  	}
75  	
76  	
77  	
78  	@Test
79  	public void checkExistence() {
80  		
81  		JavaClass withValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
82  		
83  		Assert.assertNotNull(withValue);
84  		
85  		JavaClass withOutValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
86  		
87  		Assert.assertNotNull(withOutValue);
88  		
89  	}
90  	
91  	@Test
92  	public void extractAnnotationValue() {
93  		
94  		JavaClass withValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
95  		
96  		Assert.assertTrue(JavaClassAnnotationUtils.doesAnnotationExist(XmlEnum.class.getSimpleName(), withValue));
97  		
98  		Class<?>withValueClass = JavaClassAnnotationUtils.extractXmlEnumValue(withValue);
99  		
100 		Assert.assertEquals(Integer.class, withValueClass);
101 		
102 		JavaClass withoutValue = parsedJavaClassesMap.get(EnumWithoutAnnotationValue.class.getSimpleName());
103 		
104 		Assert.assertTrue(JavaClassAnnotationUtils.doesAnnotationExist(XmlEnum.class.getSimpleName(), withValue));
105 		
106 		Class<?>withoutValueClass = JavaClassAnnotationUtils.extractXmlEnumValue(withoutValue);
107 		
108 		Assert.assertEquals(String.class, withoutValueClass);
109 			
110         
111 		
112 	}
113 }