1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.List ; 24 import org.netbeans.jmi.javamodel.Annotation; 25 import org.netbeans.jmi.javamodel.AttributeValueClass; 26 import org.netbeans.jmi.javamodel.BehavioralFeature; 27 import org.netbeans.jmi.javamodel.IntLiteral; 28 import org.netbeans.jmi.javamodel.JavaClass; 29 import org.netbeans.jmi.javamodel.JavaModelPackage; 30 import org.netbeans.jmi.javamodel.LocalVarDeclaration; 31 import org.netbeans.jmi.javamodel.LocalVariable; 32 import org.netbeans.jmi.javamodel.MultipartId; 33 import org.netbeans.jmi.javamodel.StatementBlock; 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 43 public class AnnotationOnLocVarTest extends NbTestCase { 44 45 JavaClass clazz; 46 JavaModelPackage pkg; 47 48 49 public AnnotationOnLocVarTest() { 50 super("AnnotationOnLocVarTest"); 51 } 52 53 public static NbTestSuite suite() { 54 NbTestSuite suite = new NbTestSuite(AnnotationOnLocVarTest.class); 55 return suite; 56 } 57 58 protected void setUp() { 59 clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.AnnOnLocalVar"); 60 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 61 } 62 63 public void testAddAnnToLocVar() throws java.io.IOException , FileStateInvalidException { 64 boolean fail = true; 65 Utility.beginTrans(true); 66 try { 67 StatementBlock sb = ((BehavioralFeature) clazz.getContents().get(0)).getBody(); 68 List l = sb.getStatements(); 69 LocalVarDeclaration lvd = (LocalVarDeclaration) l.get(0); 70 LocalVariable lv = (LocalVariable) lvd.getVariables().get(0); 71 List attrs = new ArrayList (4); 72 AttributeValueClass avc = pkg.getAttributeValue(); 73 IntLiteral id = pkg.getIntLiteral().createIntLiteral(666); 74 StringLiteral enginner = pkg.getStringLiteral().createStringLiteral("PaF"); 75 StringLiteral synopsis = pkg.getStringLiteral().createStringLiteral("fat"); 76 StringLiteral date = pkg.getStringLiteral().createStringLiteral("2005"); 77 attrs.add(avc.createAttributeValue("id", id)); 78 attrs.add(avc.createAttributeValue("synopsis", synopsis)); 79 attrs.add(avc.createAttributeValue("engineer", enginner)); 80 attrs.add(avc.createAttributeValue("date", date)); 81 Annotation a = pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("AnnotationType", null, null), attrs); 82 lv.getAnnotations().add(a); 83 fail = false; 84 } finally { 85 Utility.endTrans(fail); 86 } 87 assertFile("File is not correctly generated.", 88 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnLocalVar.java"), 89 getGoldenFile("testAddAnnToLocVar_AnnotationOnLocVarTest.pass"), 90 getWorkDir() 91 ); 92 } 93 94 public void testAddLocVarWithAnn() throws java.io.IOException , FileStateInvalidException { 95 boolean fail = true; 96 Utility.beginTrans(true); 97 try { 98 StatementBlock sb = ((BehavioralFeature) clazz.getContents().get(0)).getBody(); 99 List l = sb.getStatements(); 100 List attrs = new ArrayList (4); 101 AttributeValueClass avc = pkg.getAttributeValue(); 102 IntLiteral id = pkg.getIntLiteral().createIntLiteral(777); 103 StringLiteral enginner = pkg.getStringLiteral().createStringLiteral("Snoopy"); 104 StringLiteral synopsis = pkg.getStringLiteral().createStringLiteral("thin"); 105 StringLiteral date = pkg.getStringLiteral().createStringLiteral("2001"); 106 attrs.add(avc.createAttributeValue("id", id)); 107 attrs.add(avc.createAttributeValue("synopsis", synopsis)); 108 attrs.add(avc.createAttributeValue("engineer", enginner)); 109 attrs.add(avc.createAttributeValue("date", date)); 110 Annotation a = pkg.getAnnotation().createAnnotation(pkg.getMultipartId().createMultipartId("AnnotationType", null, null), attrs); 111 MultipartId id1 = pkg.getMultipartId().createMultipartId("java.util.List", null, null); 112 LocalVariable lv = (LocalVariable) pkg.getLocalVariable().createLocalVariable("testVar", null , true, id1, 0, null, "new java.util.ArrayList(3)"); 113 LocalVarDeclaration lvd = (LocalVarDeclaration) pkg.getLocalVarDeclaration().createLocalVarDeclaration(true, (MultipartId) id1.duplicate(), Collections.singletonList(lv)); 114 lv.getAnnotations().add(a); 115 l.add(0, lvd); 116 fail = false; 117 } finally { 118 Utility.endTrans(fail); 119 } 120 assertFile("File is not correctly generated.", 121 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnOnLocalVar.java"), 122 getGoldenFile("testAddLocVarWithAnn_AnnotationOnLocVarTest.pass"), 123 getWorkDir() 124 ); 125 } 126 } 127 | Popular Tags |