KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import junit.textui.TestRunner;
24 import org.netbeans.jmi.javamodel.*;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.junit.NbTestSuite;
27 import org.netbeans.modules.javacore.jmiimpl.javamodel.AttributeImpl;
28 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl;
29 import org.openide.filesystems.FileStateInvalidException;
30
31 /**
32  *
33  * @author Pavel Flaska
34  */

35 public class AnnotationTypeTest extends NbTestCase {
36
37     AnnotationType clazz;
38     JavaModelPackage pkg;
39     
40     /** Creates a new instance of AnnotationTypeTest */
41     public AnnotationTypeTest() {
42         super("AnnotationTypeTest");
43     }
44     
45     public static NbTestSuite suite() {
46         NbTestSuite suite = new NbTestSuite(AnnotationTypeTest.class);
47         return suite;
48     }
49     
50     protected void setUp() {
51         clazz = (AnnotationType) Utility.findClass("org.netbeans.test.codegen.AnnotationType");
52         pkg = (JavaModelPackage) clazz.refImmediatePackage();
53     }
54
55     public void testAddAttribute() throws java.io.IOException JavaDoc, FileStateInvalidException {
56         boolean fail = true;
57         Utility.beginTrans(true);
58         try {
59             getLog().println("Annotation type '" + clazz.getName() + "' has following attributes:");
60             for (Iterator JavaDoc it = clazz.getContents().iterator(); it.hasNext(); ) {
61                 AttributeImpl attr = (AttributeImpl) it.next();
62                 getLog().println("\tAttribute named '" + attr.getName() +
63                     "' with type '" + attr.getTypeName().getName() +
64                     "' has default value '" + attr.getDefaultValueText() + "'.");
65             }
66             Attribute newAttr = pkg.getAttribute().createAttribute(
67                 "newAttribute", // name
68
null, // annotations
69
0, // modifiers
70
null, // javadocText
71
null, // javadoc
72
pkg.getMultipartId().createMultipartId("String", null, null), // typeName
73
pkg.getMultipartId().createMultipartId("\"Letadlo\"", null, null), // defaultValue
74
null // default value text
75
);
76             clazz.getFeatures().add(newAttr);
77             fail = false;
78         }
79         finally {
80             Utility.endTrans(fail);
81         }
82         assertFile("File is not correctly generated.",
83             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
84             getGoldenFile("testAddAttribute_AnnotationType.pass"),
85             getWorkDir()
86         );
87     }
88     
89     public void testRemoveAttribute() throws java.io.IOException JavaDoc, FileStateInvalidException {
90         boolean fail = true;
91         Utility.beginTrans(true);
92         try {
93             clazz.getFeatures().remove(1);
94             fail = false;
95         }
96         finally {
97             Utility.endTrans(fail);
98         }
99         assertFile("File is not correctly generated.",
100             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
101             getGoldenFile("testRemoveAttribute_AnnotationType.pass"),
102             getWorkDir()
103         );
104     }
105     
106     public void testAttributeTypeChange() throws java.io.IOException JavaDoc, FileStateInvalidException {
107         boolean fail = true;
108         Utility.beginTrans(true);
109         try {
110             Attribute attr = (Attribute) clazz.getFeatures().get(0);
111             attr.setTypeName(pkg.getMultipartId().createMultipartId("long", null, null));
112             fail = false;
113         }
114         finally {
115             Utility.endTrans(fail);
116         }
117         assertFile("File is not correctly generated.",
118             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
119             getGoldenFile("testAttributeTypeChange_AnnotationType.pass"),
120             getWorkDir()
121         );
122     }
123     
124     public void testAttributeNameChange() throws java.io.IOException JavaDoc, FileStateInvalidException {
125         boolean fail = true;
126         Utility.beginTrans(true);
127         try {
128             Attribute attr = (Attribute) clazz.getFeatures().get(0);
129             attr.setName("newId");
130             fail = false;
131         }
132         finally {
133             Utility.endTrans(fail);
134         }
135         assertFile("File is not correctly generated.",
136             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
137             getGoldenFile("testAttributeNameChange_AnnotationType.pass"),
138             getWorkDir()
139         );
140     }
141     
142     public void testAttributeDefaultValueChange() throws java.io.IOException JavaDoc, FileStateInvalidException {
143         boolean fail = true;
144         Utility.beginTrans(true);
145         try {
146             Attribute attr = (Attribute) clazz.getFeatures().get(0);
147             attr.setDefaultValueText("666");
148             fail = false;
149         }
150         finally {
151             Utility.endTrans(fail);
152         }
153         assertFile("File is not correctly generated.",
154             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
155             getGoldenFile("testAttributeDefaultValueChange_AnnotationType.pass"),
156             getWorkDir()
157         );
158     }
159     
160     public void testAttributeDefaultValue2Change() throws java.io.IOException JavaDoc, FileStateInvalidException {
161         boolean fail = true;
162         Utility.beginTrans(true);
163         try {
164             Attribute attr = (Attribute) clazz.getFeatures().get(1);
165             attr.setDefaultValue(pkg.getMultipartId().createMultipartId("\"MaM\"", null, null));
166             fail = false;
167         }
168         finally {
169             Utility.endTrans(fail);
170         }
171         assertFile("File is not correctly generated.",
172             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
173             getGoldenFile("testAttributeDefaultValue2Change_AnnotationType.pass"),
174             getWorkDir()
175         );
176     }
177     
178     public void testAttributeDefaultValue3Change() throws java.io.IOException JavaDoc, FileStateInvalidException {
179         boolean fail = true;
180         Utility.beginTrans(true);
181         try {
182             Attribute attr = (Attribute) clazz.getFeatures().get(3);
183             attr.setDefaultValue(pkg.getMultipartId().createMultipartId("\"das Flugzeug\"", null, null));
184             fail = false;
185         }
186         finally {
187             Utility.endTrans(fail);
188         }
189         assertFile("File is not correctly generated.",
190             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"),
191             getGoldenFile("testAttributeDefaultValue3Change_AnnotationType.pass"),
192             getWorkDir()
193         );
194     }
195  
196     /**
197      * @param args the command line arguments
198      */

199     public static void main(String JavaDoc[] args) {
200         TestRunner.run(suite());
201     }
202 }
203
Popular Tags