1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.Iterator ; 23 import junit.textui.TestRunner; 24 import org.netbeans.jmi.javamodel.*; 25 import org.netbeans.junit.NbTestCase; 26 import org.netbeans.junit.NbTestSuite; 27 import org.netbeans.modules.javacore.jmiimpl.javamodel.AttributeImpl; 28 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl; 29 import org.openide.filesystems.FileStateInvalidException; 30 31 35 public class AnnotationTypeTest extends NbTestCase { 36 37 AnnotationType clazz; 38 JavaModelPackage pkg; 39 40 41 public AnnotationTypeTest() { 42 super("AnnotationTypeTest"); 43 } 44 45 public static NbTestSuite suite() { 46 NbTestSuite suite = new NbTestSuite(AnnotationTypeTest.class); 47 return suite; 48 } 49 50 protected void setUp() { 51 clazz = (AnnotationType) Utility.findClass("org.netbeans.test.codegen.AnnotationType"); 52 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 53 } 54 55 public void testAddAttribute() throws java.io.IOException , FileStateInvalidException { 56 boolean fail = true; 57 Utility.beginTrans(true); 58 try { 59 getLog().println("Annotation type '" + clazz.getName() + "' has following attributes:"); 60 for (Iterator it = clazz.getContents().iterator(); it.hasNext(); ) { 61 AttributeImpl attr = (AttributeImpl) it.next(); 62 getLog().println("\tAttribute named '" + attr.getName() + 63 "' with type '" + attr.getTypeName().getName() + 64 "' has default value '" + attr.getDefaultValueText() + "'."); 65 } 66 Attribute newAttr = pkg.getAttribute().createAttribute( 67 "newAttribute", null, 0, null, null, pkg.getMultipartId().createMultipartId("String", null, null), pkg.getMultipartId().createMultipartId("\"Letadlo\"", null, null), null ); 76 clazz.getFeatures().add(newAttr); 77 fail = false; 78 } 79 finally { 80 Utility.endTrans(fail); 81 } 82 assertFile("File is not correctly generated.", 83 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 84 getGoldenFile("testAddAttribute_AnnotationType.pass"), 85 getWorkDir() 86 ); 87 } 88 89 public void testRemoveAttribute() throws java.io.IOException , FileStateInvalidException { 90 boolean fail = true; 91 Utility.beginTrans(true); 92 try { 93 clazz.getFeatures().remove(1); 94 fail = false; 95 } 96 finally { 97 Utility.endTrans(fail); 98 } 99 assertFile("File is not correctly generated.", 100 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 101 getGoldenFile("testRemoveAttribute_AnnotationType.pass"), 102 getWorkDir() 103 ); 104 } 105 106 public void testAttributeTypeChange() throws java.io.IOException , FileStateInvalidException { 107 boolean fail = true; 108 Utility.beginTrans(true); 109 try { 110 Attribute attr = (Attribute) clazz.getFeatures().get(0); 111 attr.setTypeName(pkg.getMultipartId().createMultipartId("long", null, null)); 112 fail = false; 113 } 114 finally { 115 Utility.endTrans(fail); 116 } 117 assertFile("File is not correctly generated.", 118 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 119 getGoldenFile("testAttributeTypeChange_AnnotationType.pass"), 120 getWorkDir() 121 ); 122 } 123 124 public void testAttributeNameChange() throws java.io.IOException , FileStateInvalidException { 125 boolean fail = true; 126 Utility.beginTrans(true); 127 try { 128 Attribute attr = (Attribute) clazz.getFeatures().get(0); 129 attr.setName("newId"); 130 fail = false; 131 } 132 finally { 133 Utility.endTrans(fail); 134 } 135 assertFile("File is not correctly generated.", 136 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 137 getGoldenFile("testAttributeNameChange_AnnotationType.pass"), 138 getWorkDir() 139 ); 140 } 141 142 public void testAttributeDefaultValueChange() throws java.io.IOException , FileStateInvalidException { 143 boolean fail = true; 144 Utility.beginTrans(true); 145 try { 146 Attribute attr = (Attribute) clazz.getFeatures().get(0); 147 attr.setDefaultValueText("666"); 148 fail = false; 149 } 150 finally { 151 Utility.endTrans(fail); 152 } 153 assertFile("File is not correctly generated.", 154 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 155 getGoldenFile("testAttributeDefaultValueChange_AnnotationType.pass"), 156 getWorkDir() 157 ); 158 } 159 160 public void testAttributeDefaultValue2Change() throws java.io.IOException , FileStateInvalidException { 161 boolean fail = true; 162 Utility.beginTrans(true); 163 try { 164 Attribute attr = (Attribute) clazz.getFeatures().get(1); 165 attr.setDefaultValue(pkg.getMultipartId().createMultipartId("\"MaM\"", null, null)); 166 fail = false; 167 } 168 finally { 169 Utility.endTrans(fail); 170 } 171 assertFile("File is not correctly generated.", 172 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 173 getGoldenFile("testAttributeDefaultValue2Change_AnnotationType.pass"), 174 getWorkDir() 175 ); 176 } 177 178 public void testAttributeDefaultValue3Change() throws java.io.IOException , FileStateInvalidException { 179 boolean fail = true; 180 Utility.beginTrans(true); 181 try { 182 Attribute attr = (Attribute) clazz.getFeatures().get(3); 183 attr.setDefaultValue(pkg.getMultipartId().createMultipartId("\"das Flugzeug\"", null, null)); 184 fail = false; 185 } 186 finally { 187 Utility.endTrans(fail); 188 } 189 assertFile("File is not correctly generated.", 190 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnnotationType.java"), 191 getGoldenFile("testAttributeDefaultValue3Change_AnnotationType.pass"), 192 getWorkDir() 193 ); 194 } 195 196 199 public static void main(String [] args) { 200 TestRunner.run(suite()); 201 } 202 } 203 | Popular Tags |