KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Modifier JavaDoc;
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.AttributeValue;
26 import org.netbeans.jmi.javamodel.AttributeValueClass;
27 import org.netbeans.jmi.javamodel.CallableFeature;
28 import org.netbeans.jmi.javamodel.EmptyStatement;
29 import org.netbeans.jmi.javamodel.IntLiteral;
30 import org.netbeans.jmi.javamodel.JavaClass;
31 import org.netbeans.jmi.javamodel.JavaModelPackage;
32 import org.netbeans.jmi.javamodel.MultipartId;
33 import org.netbeans.jmi.javamodel.Parameter;
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  * Tests related to generating annotation on method/constructor parameter.
41  *
42  * @author Pavel Flaska
43  */

44 public class AnnotationOnParamTest extends NbTestCase {
45     
46     JavaClass clazz;
47     JavaModelPackage pkg;
48     
49     /** Creates a new instance of AnnotationOnParamTest */
50     public AnnotationOnParamTest() {
51         super("AnnotationOnParamTest");
52     }
53     
54     public static NbTestSuite suite() {
55         NbTestSuite suite = new NbTestSuite(AnnotationOnParamTest.class);
56         return suite;
57     }
58
59     protected void setUp() {
60         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.AnnOnParam");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62     }
63     
64     public void testAnnOnPar1() throws java.io.IOException JavaDoc, FileStateInvalidException {
65         boolean fail = true;
66         Utility.beginTrans(true);
67         try {
68             CallableFeature cf = (CallableFeature) clazz.getFeatures().get(0);
69             Object JavaDoc o = ((Parameter) cf.getParameters().get(0)).getAnnotations().remove(0);
70             getLog().println(o);
71             fail = false;
72         }
73         finally {
74             Utility.endTrans(fail);
75         }
76         assertFile("File is not correctly generated.",
77             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
78             getGoldenFile("testAnnOnPar1_AnnOnParam.pass"),
79             getWorkDir()
80         );
81     }
82     
83     public void testAnnOnPar2() throws java.io.IOException JavaDoc, FileStateInvalidException {
84         boolean fail = true;
85         Utility.beginTrans(true);
86         try {
87             CallableFeature cf = (CallableFeature) clazz.getFeatures().get(1);
88             ((Parameter) cf.getParameters().get(0)).setFinal(false);
89             fail = false;
90         }
91         finally {
92             Utility.endTrans(fail);
93         }
94         assertFile("File is not correctly generated.",
95             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
96             getGoldenFile("testAnnOnPar2_AnnOnParam.pass"),
97             getWorkDir()
98         );
99     }
100     
101     public void testAnnOnPar3() throws java.io.IOException JavaDoc, FileStateInvalidException {
102         boolean fail = true;
103         Utility.beginTrans(true);
104         try {
105             CallableFeature cf = (CallableFeature) clazz.getFeatures().get(2);
106             Annotation ann = (Annotation) ((Parameter) cf.getParameters().get(0)).getAnnotations().get(0);
107             ((AttributeValue) ann.getAttributeValues().get(0)).setValue(pkg.getIntLiteral().createIntLiteral(11));
108             fail = false;
109         }
110         finally {
111             Utility.endTrans(fail);
112         }
113         assertFile("File is not correctly generated.",
114             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
115             getGoldenFile("testAnnOnPar3_AnnOnParam.pass"),
116             getWorkDir()
117         );
118     }
119     
120     public void testAnnOnPar4() throws java.io.IOException JavaDoc, FileStateInvalidException {
121         boolean fail = true;
122         Utility.beginTrans(true);
123         try {
124             CallableFeature cf = (CallableFeature) clazz.getFeatures().get(3);
125             List JavaDoc l = ((Parameter) cf.getParameters().get(0)).getAnnotations();
126             l.add(pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("Deprecated", null, null), null));
127             fail = false;
128         }
129         finally {
130             Utility.endTrans(fail);
131         }
132         assertFile("File is not correctly generated.",
133             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
134             getGoldenFile("testAnnOnPar4_AnnOnParam.pass"),
135             getWorkDir()
136         );
137     }
138
139     public void testAnnOnPar11() throws java.io.IOException JavaDoc, FileStateInvalidException {
140         boolean fail = true;
141         Utility.beginTrans(true);
142         try {
143             CallableFeature cf = (CallableFeature) clazz.getFeatures().get(0);
144             ((Parameter) cf.getParameters().get(0)).setFinal(true);
145             fail = false;
146         }
147         finally {
148             Utility.endTrans(fail);
149         }
150         assertFile("File is not correctly generated.",
151             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
152             getGoldenFile("testAnnOnPar11_AnnOnParam.pass"),
153             getWorkDir()
154         );
155     }
156     
157     public void testAddFeatureWithAnnOnPar() throws java.io.IOException JavaDoc, FileStateInvalidException {
158         boolean fail = true;
159         Utility.beginTrans(true);
160         try {
161             EmptyStatement es = pkg.getEmptyStatement().createEmptyStatement();
162             MultipartId mid = pkg.getMultipartId().createMultipartId("String", null, Collections.EMPTY_LIST);
163             List JavaDoc pl = new ArrayList JavaDoc(2);
164             pl.add(pkg.getParameter().createParameter("aName", null, false, mid, 0, false));
165             List JavaDoc attrs = new ArrayList JavaDoc(4);
166             AttributeValueClass avc = pkg.getAttributeValue();
167             IntLiteral id = pkg.getIntLiteral().createIntLiteral(666);
168             StringLiteral enginner = pkg.getStringLiteral().createStringLiteral("Dan");
169             StringLiteral synopsis = pkg.getStringLiteral().createStringLiteral("Test");
170             StringLiteral date = pkg.getStringLiteral().createStringLiteral("2005-03-30");
171             attrs.add(avc.createAttributeValue("id", id));
172             attrs.add(avc.createAttributeValue("date", date));
173             attrs.add(avc.createAttributeValue("engineer", enginner));
174             attrs.add(avc.createAttributeValue("synopsis", synopsis));
175             Annotation a = pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("AnnotationType", null, null), attrs);
176             MultipartId mid2 = pkg.getMultipartId().createMultipartId("java.util.List", null, Collections.EMPTY_LIST);
177             pl.add(pkg.getParameter().createParameter("aList", Collections.singletonList(a), true, mid2, 0, false));
178             clazz.getContents().add(pkg.getConstructor().createConstructor(null, Collections.EMPTY_LIST, Modifier.PUBLIC, null, null, null, "", null, pl, null));
179             fail = false;
180         }
181         finally {
182             Utility.endTrans(fail);
183         }
184         assertFile("File is not correctly generated.",
185             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnParam.java"),
186             getGoldenFile("testAddFeatureWithAnnOnPar_AnnOnParam.pass"),
187             getWorkDir()
188         );
189     }
190 }
191
Popular Tags