1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import junit.textui.TestRunner; 28 import org.netbeans.jmi.javamodel.*; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.modules.javacore.jmiimpl.javamodel.MetadataElement; 32 import org.openide.filesystems.FileStateInvalidException; 33 34 38 public class ArrayAttrValueTest extends NbTestCase { 39 40 JavaClass clazz; 41 JavaModelPackage pkg; 42 43 44 public ArrayAttrValueTest() { 45 super("ArrayAttrValueTest"); 46 } 47 48 public static NbTestSuite suite() { 49 NbTestSuite suite = new NbTestSuite(ArrayAttrValueTest.class); 50 return suite; 51 } 52 53 protected void setUp() { 54 clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.ArrayAttrValueClass"); 55 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 56 } 57 58 public void testAddAnnotation() throws java.io.IOException , FileStateInvalidException { 59 boolean fail = true; 60 Utility.beginTrans(true); 61 try { 62 Method method = (Method) clazz.getContents().get(0); 63 Annotation ann1 = createNamedQueryAnn("\"foo1\"", "\"query1\"", true); 64 Annotation ann2 = createNamedQueryAnn("\"foo2\"", "\"query2\"", true); 65 Annotation ann = createNamedQueriesAnn(new Object [] { ann1, ann2 }, true); 66 method.getAnnotations().add(ann); 67 fail = false; 68 } 69 finally { 70 Utility.endTrans(fail); 71 } 72 assertFile("File is not correctly generated.", 73 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ArrayAttrValueClass.java"), 74 getGoldenFile("testAddAnnotation_ArrayAttrValueClass.pass"), 75 getWorkDir() 76 ); 77 } 78 79 public void testAddAnnotationWithoutAttrName() throws java.io.IOException , FileStateInvalidException { 80 boolean fail = true; 81 Utility.beginTrans(true); 82 try { 83 Method method = (Method) clazz.getContents().get(1); 84 Annotation ann1 = createNamedQueryAnn("\"foo1\"", "\"query1\"", false); 85 Annotation ann2 = createNamedQueryAnn("\"foo2\"", "\"query2\"", false); 86 Annotation ann = createNamedQueriesAnn(new Object [] { ann1, ann2 }, false); 87 method.getAnnotations().add(ann); 88 fail = false; 89 } 90 finally { 91 Utility.endTrans(fail); 92 } 93 assertFile("File is not correctly generated.", 94 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ArrayAttrValueClass.java"), 95 getGoldenFile("testAddAnnotationWithoutAttrName_ArrayAttrValueClass.pass"), 96 getWorkDir() 97 ); 98 } 99 100 103 public static void main(String [] args) { 104 TestRunner.run(suite()); 105 } 106 107 private Annotation createNamedQueryAnn(String attr1, String attr2, boolean useAttrName) { 108 MultipartIdClass multiProxy = pkg.getMultipartId(); 109 Annotation ann = pkg.getAnnotation().createAnnotation(); 110 ann.setTypeName(multiProxy.createMultipartId("NamedQuery", null, null)); 111 List attrList = new ArrayList (); 112 AttributeValueClass avProxy = pkg.getAttributeValue(); 113 attrList.add(avProxy.createAttributeValue(useAttrName ? "name" : null, multiProxy.createMultipartId(attr1, null, null))); 114 attrList.add(avProxy.createAttributeValue(useAttrName ? "query" : null, multiProxy.createMultipartId(attr2, null, null))); 115 ann.getAttributeValues().addAll(attrList); 116 return ann; 117 } 118 119 private Annotation createNamedQueriesAnn(Object [] annotation, boolean useAttrName) { 120 MultipartIdClass multiProxy = pkg.getMultipartId(); 121 Annotation ann = pkg.getAnnotation().createAnnotation(); 122 ann.setTypeName(multiProxy.createMultipartId("NamedQueries", null, null)); 123 List attrList = new ArrayList (); 124 AttributeValueClass avProxy = pkg.getAttributeValue(); 125 ArrayInitialization ai = pkg.getArrayInitialization().createArrayInitialization(Arrays.asList(annotation)); 126 attrList.add(avProxy.createAttributeValue(useAttrName ? "value" : null, ai)); 127 ann.getAttributeValues().addAll(attrList); 128 return ann; 129 } 130 } 131 | Popular Tags |