KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > getters > GetAnnotationTests


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.jmi.javamodel.getters;
20
21 import java.io.PrintStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import junit.textui.TestRunner;
25 import org.netbeans.jmi.javamodel.Constructor;
26 import org.netbeans.jmi.javamodel.codegen.Utility;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.netbeans.jmi.javamodel.*;
30
31 /**
32  * Test getAnnotation() on elements.
33  *
34  * @author Pavel Flaska
35  */

36 public class GetAnnotationTests extends NbTestCase {
37
38     JavaClass clazz;
39     JavaModelPackage pkg;
40
41     /** Creates a new instance of GetAnnotationTests */
42     public GetAnnotationTests() {
43         super("GetAnnotationTests");
44     }
45     
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite(GetAnnotationTests.class);
48         return suite;
49     }
50     
51     protected void setUp() {
52         clazz = (JavaClass) Utility.findClass("org.netbeans.test.getters.AnnotationEverywhere");
53         pkg = (JavaModelPackage) clazz.refImmediatePackage();
54     }
55     
56     private String JavaDoc[] attrsNames = {
57         "id",
58         "synopsis",
59         "engineer",
60         "date"
61     };
62     
63     public void testFieldAnnotation() {
64         PrintStream JavaDoc log = getLog();
65         Field f = clazz.getField("a", false);
66         for (Iterator JavaDoc it0 = f.getAnnotations().iterator(); it0.hasNext(); ) {
67             Annotation ann = (Annotation) it0.next();
68             log.println("Annotation: '" + ann.getTypeName().getName() + "'");
69             int i = 0;
70             for (Iterator JavaDoc it1 = ann.getAttributeValues().iterator(); it1.hasNext(); i++) {
71                 AttributeValue attr = (AttributeValue) it1.next();
72                 log.println("\tname = '" + attr.getName() + "', value = '" + attr.getValue());
73                 assertEquals(attrsNames[i], attr.getName());
74             }
75         }
76     }
77     
78     public void testConstructorAnnotation() {
79         PrintStream JavaDoc log = getLog();
80         Constructor c = (Constructor) clazz.getContents().get(1);
81         log.println("Constructor: '" + c.getDeclaringClass().getName() + "':");
82         int i = 0;
83         for (Iterator JavaDoc it0 = c.getAnnotations().iterator(); it0.hasNext(); i++) {
84             String JavaDoc name = ((Annotation) it0.next()).getTypeName().getName();
85             log.println("\t" + name + ".");
86             assertEquals(parAnnNames[i], name);
87         }
88     }
89     
90     private String JavaDoc[] parAnnNames = {
91         "Deprecated",
92         "AnnotationType"
93     };
94     
95     public void testParameterAnnotation() {
96         PrintStream JavaDoc log = getLog();
97         Method m = (Method) clazz.getContents().get(2);
98         List JavaDoc/*<Parameter>*/ l = m.getParameters();
99         for (Iterator JavaDoc it0 = l.iterator(); it0.hasNext(); ) {
100             Parameter p = (Parameter) it0.next();
101             log.println("Parameter: '" + p.getName() + "':");
102             int i = 0;
103             for (Iterator JavaDoc it1 = p.getAnnotations().iterator(); it1.hasNext(); i++) {
104                 String JavaDoc name = ((Annotation) it1.next()).getTypeName().getName();
105                 log.println("\tannotation: '" + name + "'");
106                 assertEquals(parAnnNames[i], name);
107             }
108         }
109     }
110     
111     /**
112      * @param args the command line arguments
113      */

114     public static void main(String JavaDoc[] args) {
115         TestRunner.run(suite());
116     }
117     
118 }
119
Popular Tags