001    /**
002     * Copyright 2004-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.student.contract.model.impl;
017    
018    import java.io.File;
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    
022    import javax.xml.bind.annotation.XmlEnum;
023    
024    import org.junit.Assert;
025    import org.junit.BeforeClass;
026    import org.junit.Test;
027    import org.junit.runner.RunWith;
028    import org.junit.runners.BlockJUnit4ClassRunner;
029    import org.kuali.student.contract.model.util.JavaClassAnnotationUtils;
030    import org.kuali.student.model.annotation.EnumWithAnnotationValue;
031    import org.kuali.student.model.annotation.EnumWithoutAnnotationValue;
032    import org.slf4j.Logger;
033    
034    import com.thoughtworks.qdox.JavaDocBuilder;
035    import com.thoughtworks.qdox.model.DefaultDocletTagFactory;
036    import com.thoughtworks.qdox.model.JavaClass;
037    
038    /**
039     * 
040     * tests how to resolve both types of @XmlEnum annotations.  Those with and without the value defined.
041     * 
042     * @author Kuali Student Team
043     */
044    @RunWith(BlockJUnit4ClassRunner.class)
045    public class TestAnnotationValueResolution {
046    
047            private static final Logger log = org.slf4j.LoggerFactory.getLogger(TestAnnotationValueResolution.class);
048            
049            private static Map<String, JavaClass> parsedJavaClassesMap;
050            
051            /**
052             * 
053             */
054            public TestAnnotationValueResolution() {
055                    
056            }
057    
058            @BeforeClass
059            public static void setup () {
060                    
061            DefaultDocletTagFactory dtf = new DefaultDocletTagFactory();
062            JavaDocBuilder builder = new JavaDocBuilder(dtf);
063            
064            builder.addSourceTree(new File("src/test/java/org/kuali/student/model/annotation"));
065            
066           parsedJavaClassesMap = new LinkedHashMap<String, JavaClass>();
067            
068            for (JavaClass javaClass : builder.getClasses()) {
069                
070                    parsedJavaClassesMap.put(javaClass.getName(), javaClass);
071                
072            }
073            
074            }
075            
076            
077            
078            @Test
079            public void checkExistence() {
080                    
081                    JavaClass withValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
082                    
083                    Assert.assertNotNull(withValue);
084                    
085                    JavaClass withOutValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
086                    
087                    Assert.assertNotNull(withOutValue);
088                    
089            }
090            
091            @Test
092            public void extractAnnotationValue() {
093                    
094                    JavaClass withValue = parsedJavaClassesMap.get(EnumWithAnnotationValue.class.getSimpleName());
095                    
096                    Assert.assertTrue(JavaClassAnnotationUtils.doesAnnotationExist(XmlEnum.class.getSimpleName(), withValue));
097                    
098                    Class<?>withValueClass = JavaClassAnnotationUtils.extractXmlEnumValue(withValue);
099                    
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    }