KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import junit.textui.TestRunner;
24 import org.netbeans.jmi.javamodel.Annotation;
25 import org.netbeans.jmi.javamodel.Constructor;
26 import org.netbeans.jmi.javamodel.JavaClass;
27 import org.netbeans.jmi.javamodel.JavaModelPackage;
28 import org.netbeans.jmi.javamodel.LocalVarDeclaration;
29 import org.netbeans.jmi.javamodel.LocalVariable;
30 import org.netbeans.jmi.javamodel.codegen.Utility;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.junit.NbTestSuite;
33
34 /**
35  *
36  * @author Pavel Flaska
37  */

38 public class AnnLocalVarTest extends NbTestCase {
39     
40     JavaClass clazz;
41     JavaModelPackage pkg;
42     LocalVarDeclaration[] lvdecko = new LocalVarDeclaration[4];
43     
44     Annotation[] tmpAnnotation;
45     boolean isFinal;
46     
47     /** Creates a new instance of AnnLocalVarTest */
48     public AnnLocalVarTest() {
49         super("AnnLocalVarTest");
50     }
51     
52     public static NbTestSuite suite() {
53         NbTestSuite suite = new NbTestSuite(AnnLocalVarTest.class);
54         return suite;
55     }
56     
57     protected void setUp() {
58         clazz = (JavaClass) Utility.findClass("org.netbeans.test.getters.AnnotationLocVar");
59         pkg = (JavaModelPackage) clazz.refImmediatePackage();
60         Constructor c = (Constructor) clazz.getContents().get(0);
61         int i = 0;
62         for (Iterator JavaDoc it = c.getBody().getStatements().iterator(); it.hasNext(); ) {
63             //System.err.println(it.next());
64
lvdecko[i++] = (LocalVarDeclaration) it.next();
65         }
66     }
67     
68     public void testStatement1() {
69         // final @AnnotationType(id=1, synopsis="Papaya", engineer="Snoopy", date="2004") java.util.List l;
70
logData(0);
71         assertEquals(true, isFinal);
72         assertEquals(1, tmpAnnotation.length);
73         assertEquals("AnnotationType", tmpAnnotation[0].getTypeName().getName());
74     }
75     
76     public void testStatement2() {
77         // @SuppressWarnings(value={"a", "b"}) @AnnotationType(id=2, synopsis="Flower", engineer="Duke", date="2001") int a;
78
logData(1);
79         assertEquals(false, isFinal);
80         assertEquals(2, tmpAnnotation.length);
81         assertEquals("SuppressWarnings", tmpAnnotation[0].getTypeName().getName());
82         assertEquals("AnnotationType", tmpAnnotation[1].getTypeName().getName());
83     }
84     
85     public void testStatement3() {
86         // final long c = 10;
87
logData(2);
88         assertEquals(true, isFinal);
89         assertEquals(0, tmpAnnotation.length);
90     }
91     
92     public void testStatement4() {
93         // @AnnotationType(id=3, synopsis="Aquila", engineer="Krystof", date="1851") final @SuppressWarnings(value={"c", "d"}) String d;
94
logData(3);
95         assertEquals(true, isFinal);
96         assertEquals(2, tmpAnnotation.length);
97         assertEquals("AnnotationType", tmpAnnotation[0].getTypeName().getName());
98         assertEquals("SuppressWarnings", tmpAnnotation[1].getTypeName().getName());
99     }
100     
101     private void logData(int statementIndex) {
102         Utility.beginTrans(false);
103         try {
104             LocalVarDeclaration l = lvdecko[statementIndex];
105             isFinal = l.isFinal();
106             getLog().println("Final? " + isFinal);
107             List JavaDoc/*<Annotation>*/ anns = ((LocalVariable) l.getVariables().get(0)).getAnnotations();
108             tmpAnnotation = new Annotation[anns.size()];
109             int i = 0;
110             for (Iterator JavaDoc it = anns.iterator(); it.hasNext(); ) {
111                 Annotation ann = (Annotation) it.next();
112                 tmpAnnotation[i++] = ann;
113                 getLog().println("Ann: " + ann.getTypeName().getName());
114             }
115         }
116         finally {
117             Utility.endTrans();
118         }
119     }
120     
121     /**
122      * @param args the command line arguments
123      */

124     public static void main(String JavaDoc[] args) {
125         TestRunner.run(suite());
126     }
127     
128 }
129
Popular Tags