KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class AnnotationTest extends NbTestCase {
38
39     JavaClass clazz;
40     JavaModelPackage pkg;
41     
42     /** Creates a new instance of AnnotationTest */
43     public AnnotationTest() {
44         super("AnnotationTest");
45     }
46
47     public static NbTestSuite suite() {
48         NbTestSuite suite = new NbTestSuite(AnnotationTest.class);
49         return suite;
50     }
51
52     protected void setUp() {
53         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.AnnotationClass");
54         pkg = (JavaModelPackage) clazz.refImmediatePackage();
55     }
56     
57     public void testAddAnnotation() throws java.io.IOException JavaDoc, FileStateInvalidException {
58         boolean fail = true;
59         Utility.beginTrans(true);
60         try {
61             Method method = (Method) clazz.getContents().get(0);
62             // log the annotation information
63
getLog().println("Method '" + method.getName() + "' has following annotations:");
64             for (Iterator JavaDoc it = method.getAnnotations().iterator(); it.hasNext(); ) {
65                 Annotation annotation = (Annotation) it.next();
66                 getLog().println("\tAnnotation named '" + annotation.getType().getName() + "'.");
67                 for (Iterator JavaDoc it2 = annotation.getAttributeValues().iterator(); it2.hasNext(); ) {
68                     AttributeValue attribute = (AttributeValue) it2.next();
69                     getLog().println("\t\tAttribute value '" + attribute.getName() + "', '" + ((MetadataElement) attribute.getValue()).getSourceText() + "'.");
70                 }
71             }
72             Annotation ann = createSimpleAnn("\"2004-06-21\"", "\"Lojza\"", "\"Unknown\"", "46");
73             method = (Method) clazz.getContents().get(1);
74             method.getAnnotations().add(ann);
75             fail = false;
76        }
77         finally {
78             Utility.endTrans(fail);
79         }
80         assertFile("File is not correctly generated.",
81             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationClass.java"),
82             getGoldenFile("testAddAnnotation_AnnotationClass.pass"),
83             getWorkDir()
84         );
85     }
86     
87     public void testAddAnnWithField() throws java.io.IOException JavaDoc, FileStateInvalidException {
88         boolean fail = true;
89         Utility.beginTrans(true);
90         try {
91             Annotation ann = createSimpleAnn("\"2004-07-21\"", "\"Karlik\"", "\"Neznamy\"", "12");
92             Field f = pkg.getField().createField(
93                 "newField",
94                 Collections.singletonList(ann),
95                 0,
96                 "Neue",
97                 null,
98                 false,
99                 pkg.getMultipartId().createMultipartId("String", null, null),
100                 0,
101                 null,
102                 "\"novy field\""
103             );
104             clazz.getContents().add(0, f);
105             fail = false;
106         }
107         finally {
108             Utility.endTrans(fail);
109         }
110         assertFile("File is not correctly generated.",
111             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationClass.java"),
112             getGoldenFile("testAddAnnWithField_AnnotationClass.pass"),
113             getWorkDir()
114         );
115     }
116
117     public void testAddAnnWithConstr() throws java.io.IOException JavaDoc, FileStateInvalidException {
118         boolean fail = true;
119         Utility.beginTrans(true);
120         try {
121             Annotation ann = createSimpleAnn("\"2004-07-22\"", "\"Davidek\"", "\"inedit\"", "666");
122             Constructor constructor = pkg.getConstructor().createConstructor(
123                 null,
124                 Collections.singletonList(ann),
125                 Modifier.PUBLIC,
126                 "Added constructor",
127                 null,
128                 null,
129                 "// nothing",
130                 null,
131                 null,
132                 null
133             );
134             clazz.getContents().add(1, constructor);
135             fail = false;
136         }
137         finally {
138             Utility.endTrans(fail);
139         }
140         assertFile("File is not correctly generated.",
141             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationClass.java"),
142             getGoldenFile("testAddAnnWithConstr_AnnotationClass.pass"),
143             getWorkDir()
144         );
145     }
146     
147     /**
148      * @param args the command line arguments
149      */

150     public static void main(String JavaDoc[] args) {
151         TestRunner.run(suite());
152     }
153     
154     private Annotation createSimpleAnn(String JavaDoc attr1, String JavaDoc attr2, String JavaDoc attr3, String JavaDoc attr4) {
155         MultipartIdClass multiProxy = pkg.getMultipartId();
156         Annotation ann = pkg.getAnnotation().createAnnotation();
157         ann.setTypeName(multiProxy.createMultipartId("AnnotationType", null, null));
158         List JavaDoc attrList = new ArrayList JavaDoc();
159         AttributeValueClass avProxy = pkg.getAttributeValue();
160         attrList.add(avProxy.createAttributeValue("date", multiProxy.createMultipartId(attr1, null, null)));
161         attrList.add(avProxy.createAttributeValue("engineer", multiProxy.createMultipartId(attr2, null, null)));
162         attrList.add(avProxy.createAttributeValue("synopsis", multiProxy.createMultipartId(attr3, null, null)));
163         attrList.add(avProxy.createAttributeValue("id", multiProxy.createMultipartId(attr4, null, null)));
164         ann.getAttributeValues().addAll(attrList);
165         return ann;
166     }
167 }
168
Popular Tags