KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > AnnotationOnLocVarTest


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.codegen;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.jmi.javamodel.Annotation;
25 import org.netbeans.jmi.javamodel.AttributeValueClass;
26 import org.netbeans.jmi.javamodel.BehavioralFeature;
27 import org.netbeans.jmi.javamodel.IntLiteral;
28 import org.netbeans.jmi.javamodel.JavaClass;
29 import org.netbeans.jmi.javamodel.JavaModelPackage;
30 import org.netbeans.jmi.javamodel.LocalVarDeclaration;
31 import org.netbeans.jmi.javamodel.LocalVariable;
32 import org.netbeans.jmi.javamodel.MultipartId;
33 import org.netbeans.jmi.javamodel.StatementBlock;
34 import org.netbeans.jmi.javamodel.StringLiteral;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.junit.NbTestSuite;
37 import org.openide.filesystems.FileStateInvalidException;
38
39 /**
40  *
41  * @author Pavel Flaska
42  */

43 public class AnnotationOnLocVarTest extends NbTestCase {
44     
45     JavaClass clazz;
46     JavaModelPackage pkg;
47     
48     /** Creates a new instance of AnnotationAttributeValueTest */
49     public AnnotationOnLocVarTest() {
50         super("AnnotationOnLocVarTest");
51     }
52     
53     public static NbTestSuite suite() {
54         NbTestSuite suite = new NbTestSuite(AnnotationOnLocVarTest.class);
55         return suite;
56     }
57     
58     protected void setUp() {
59         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.AnnOnLocalVar");
60         pkg = (JavaModelPackage) clazz.refImmediatePackage();
61     }
62     
63     public void testAddAnnToLocVar() throws java.io.IOException JavaDoc, FileStateInvalidException {
64         boolean fail = true;
65         Utility.beginTrans(true);
66         try {
67             StatementBlock sb = ((BehavioralFeature) clazz.getContents().get(0)).getBody();
68             List JavaDoc l = sb.getStatements();
69             LocalVarDeclaration lvd = (LocalVarDeclaration) l.get(0);
70             LocalVariable lv = (LocalVariable) lvd.getVariables().get(0);
71             List JavaDoc attrs = new ArrayList JavaDoc(4);
72             AttributeValueClass avc = pkg.getAttributeValue();
73             IntLiteral id = pkg.getIntLiteral().createIntLiteral(666);
74             StringLiteral enginner = pkg.getStringLiteral().createStringLiteral("PaF");
75             StringLiteral synopsis = pkg.getStringLiteral().createStringLiteral("fat");
76             StringLiteral date = pkg.getStringLiteral().createStringLiteral("2005");
77             attrs.add(avc.createAttributeValue("id", id));
78             attrs.add(avc.createAttributeValue("synopsis", synopsis));
79             attrs.add(avc.createAttributeValue("engineer", enginner));
80             attrs.add(avc.createAttributeValue("date", date));
81             Annotation a = pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("AnnotationType", null, null), attrs);
82             lv.getAnnotations().add(a);
83             fail = false;
84         } finally {
85             Utility.endTrans(fail);
86         }
87         assertFile("File is not correctly generated.",
88             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnLocalVar.java"),
89             getGoldenFile("testAddAnnToLocVar_AnnotationOnLocVarTest.pass"),
90             getWorkDir()
91         );
92     }
93     
94     public void testAddLocVarWithAnn() throws java.io.IOException JavaDoc, FileStateInvalidException {
95         boolean fail = true;
96         Utility.beginTrans(true);
97         try {
98             StatementBlock sb = ((BehavioralFeature) clazz.getContents().get(0)).getBody();
99             List JavaDoc l = sb.getStatements();
100             List JavaDoc attrs = new ArrayList JavaDoc(4);
101             AttributeValueClass avc = pkg.getAttributeValue();
102             IntLiteral id = pkg.getIntLiteral().createIntLiteral(777);
103             StringLiteral enginner = pkg.getStringLiteral().createStringLiteral("Snoopy");
104             StringLiteral synopsis = pkg.getStringLiteral().createStringLiteral("thin");
105             StringLiteral date = pkg.getStringLiteral().createStringLiteral("2001");
106             attrs.add(avc.createAttributeValue("id", id));
107             attrs.add(avc.createAttributeValue("synopsis", synopsis));
108             attrs.add(avc.createAttributeValue("engineer", enginner));
109             attrs.add(avc.createAttributeValue("date", date));
110             Annotation a = pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("AnnotationType", null, null), attrs);
111             MultipartId id1 = pkg.getMultipartId().createMultipartId("java.util.List", null, null);
112             LocalVariable lv = (LocalVariable) pkg.getLocalVariable().createLocalVariable("testVar", null /* annotations here are not supported in 4.1 */, true, id1, 0, null, "new java.util.ArrayList(3)");
113             LocalVarDeclaration lvd = (LocalVarDeclaration) pkg.getLocalVarDeclaration().createLocalVarDeclaration(true, (MultipartId) id1.duplicate(), Collections.singletonList(lv));
114             lv.getAnnotations().add(a);
115             l.add(0, lvd);
116             fail = false;
117         } finally {
118             Utility.endTrans(fail);
119         }
120         assertFile("File is not correctly generated.",
121             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnLocalVar.java"),
122             getGoldenFile("testAddLocVarWithAnn_AnnotationOnLocVarTest.pass"),
123             getWorkDir()
124         );
125     }
126 }
127
Popular Tags