1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 37 public class AnnotationTest extends NbTestCase { 38 39 JavaClass clazz; 40 JavaModelPackage pkg; 41 42 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 , FileStateInvalidException { 58 boolean fail = true; 59 Utility.beginTrans(true); 60 try { 61 Method method = (Method) clazz.getContents().get(0); 62 getLog().println("Method '" + method.getName() + "' has following annotations:"); 64 for (Iterator it = method.getAnnotations().iterator(); it.hasNext(); ) { 65 Annotation annotation = (Annotation) it.next(); 66 getLog().println("\tAnnotation named '" + annotation.getType().getName() + "'."); 67 for (Iterator 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 , 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 , 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 150 public static void main(String [] args) { 151 TestRunner.run(suite()); 152 } 153 154 private Annotation createSimpleAnn(String attr1, String attr2, String attr3, String attr4) { 155 MultipartIdClass multiProxy = pkg.getMultipartId(); 156 Annotation ann = pkg.getAnnotation().createAnnotation(); 157 ann.setTypeName(multiProxy.createMultipartId("AnnotationType", null, null)); 158 List attrList = new ArrayList (); 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 |