1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.util.Iterator ; 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 43 public class AnnotationAttributeValueTest extends NbTestCase { 44 45 JavaClass clazz; 46 JavaModelPackage pkg; 47 48 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 , FileStateInvalidException { 64 boolean fail = true; 65 Utility.beginTrans(true); 66 try { 67 Constructor constructor = (Constructor) clazz.getContents().get(0); 68 getLog().println("Constructor has following annotations:"); 70 for (Iterator it = constructor.getAnnotations().iterator(); it.hasNext(); ) { 71 Annotation annotation = (Annotation) it.next(); 72 getLog().println("\tAnnotation named '" + annotation.getType().getName() + "'."); 73 for (Iterator 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 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 , FileStateInvalidException { 94 boolean fail = true; 95 Utility.beginTrans(true); 96 try { 97 Constructor constructor = (Constructor) clazz.getContents().get(0); 98 getLog().println("Constructor has following annotations:"); 100 for (Iterator 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 |