KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
22 import org.netbeans.jmi.javamodel.Annotation;
23 import org.netbeans.jmi.javamodel.AttributeValue;
24 import org.netbeans.jmi.javamodel.Constructor;
25 import org.netbeans.jmi.javamodel.JavaClass;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.jmi.javamodel.StringLiteral;
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  * Tests the annotation attribute value change, i.e. changes in 'id', 'synopsis'
35  * in the following example:
36  * <code>
37  * @RequestForEnhancement(id = 333, synopsis = "implement") public abstract void foo();
38  * @Copyright("John Smith") public abstract void foo();
39  * </code>
40  *
41  * @author Pavel Flaska
42  */

43 public class AnnotationAttributeValueTest extends NbTestCase {
44     
45     JavaClass clazz;
46     JavaModelPackage pkg;
47     
48     /** Creates a new instance of AnnotationAttributeValueTest */
49     public AnnotationAttributeValueTest() {
50         super("AnnotationAttributeValueTest");
51     }
52     
53     public static NbTestSuite suite() {
54         NbTestSuite suite = new NbTestSuite(AnnotationAttributeValueTest.class);
55         return suite;
56     }
57     
58     protected void setUp() {
59         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.AnnotationAttributeValueTest");
60         pkg = (JavaModelPackage) clazz.refImmediatePackage();
61     }
62     
63     public void testChangeAttributeName() throws java.io.IOException JavaDoc, FileStateInvalidException {
64         boolean fail = true;
65         Utility.beginTrans(true);
66         try {
67             Constructor constructor = (Constructor) clazz.getContents().get(0);
68             // log the annotation information
69
getLog().println("Constructor has following annotations:");
70             for (Iterator JavaDoc it = constructor.getAnnotations().iterator(); it.hasNext(); ) {
71                 Annotation annotation = (Annotation) it.next();
72                 getLog().println("\tAnnotation named '" + annotation.getType().getName() + "'.");
73                 for (Iterator JavaDoc it2 = annotation.getAttributeValues().iterator(); it2.hasNext(); ) {
74                     AttributeValue attribute = (AttributeValue) it2.next();
75                     getLog().println("\t\tAttribute value '" + attribute.getName() + "', '" + ((MetadataElement) attribute.getValue()).getSourceText() + "'.");
76                     String JavaDoc newName = attribute.getName() + "_2";
77                     attribute.setName(newName);
78                     getLog().println("\t\tChanging its name to '" + newName + "'.");
79                 }
80             }
81             fail = false;
82        }
83         finally {
84             Utility.endTrans(fail);
85         }
86         assertFile("File is not correctly generated.",
87             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationAttributeValueTest.java"),
88             getGoldenFile("testChangeAttributeName_AnnotationAttributeValueTest.pass"),
89             getWorkDir()
90         );
91     }
92     
93     public void testChangeAttributeValue() throws java.io.IOException JavaDoc, FileStateInvalidException {
94         boolean fail = true;
95         Utility.beginTrans(true);
96         try {
97             Constructor constructor = (Constructor) clazz.getContents().get(0);
98             // log the annotation information
99
getLog().println("Constructor has following annotations:");
100             for (Iterator JavaDoc it = constructor.getAnnotations().iterator(); it.hasNext(); ) {
101                 Annotation annotation = (Annotation) it.next();
102                 getLog().println("\tAnnotation named '" + annotation.getType().getName() + "'.");
103                 AttributeValue attribute = (AttributeValue) annotation.getAttributeValues().get(1);
104                 getLog().println("\t\tAttribute value '" + attribute.getName() + "', '" + ((MetadataElement) attribute.getValue()).getSourceText() + "'.");
105                 StringLiteral sl = pkg.getStringLiteral().createStringLiteral("PaF");
106                 attribute.setValue(sl);
107                 getLog().println("\t\tChanging its value to PaF");
108             }
109             fail = false;
110        }
111         finally {
112             Utility.endTrans(fail);
113         }
114         assertFile("File is not correctly generated.",
115             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationAttributeValueTest.java"),
116             getGoldenFile("testChangeAttributeValue_AnnotationAttributeValueTest.pass"),
117             getWorkDir()
118         );
119     }
120 }
121
Popular Tags