1 19 package org.netbeans.jmi.javamodel.codegen.FieldTest1; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import junit.textui.TestRunner; 25 import org.netbeans.jmi.javamodel.*; 26 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase; 27 import org.netbeans.jmi.javamodel.codegen.Utility; 28 import org.netbeans.junit.NbTestSuite; 29 30 35 public class FieldTest1 extends CodegenTestCase { 36 37 38 public FieldTest1(String name) { 39 super(name, "FieldTest1"); 40 } 41 42 public static NbTestSuite suite() { 43 NbTestSuite suite = new NbTestSuite(); 44 suite.addTest(new FieldTest1("testFieldModifiers")); 45 suite.addTest(new FieldTest1("testFieldType")); 46 suite.addTest(new FieldTest1("testFieldName")); 47 suite.addTest(new FieldTest1("testFieldInitialValueText")); 48 suite.addTest(new FieldTest1("testFieldInitialValue")); 49 suite.addTest(new FieldTest1("testFieldChangeInInitValue")); 50 suite.addTest(new FieldTest1("testFieldInitialValueRemoval")); 51 suite.addTest(new FieldTest1("testFieldAdd")); 52 return suite; 53 } 54 55 JavaClass clazz; 56 JavaModelPackage pkg; 57 Field[] field = new Field[7]; 58 59 protected void setUp() { 60 clazz = Utility.findClass("org.netbeans.test.codegen.FieldTest1"); 61 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 62 int i = 0; 64 for (Iterator mIt = clazz.getFeatures().iterator(); mIt.hasNext(); i++) { 65 field[i] = (Field) mIt.next(); 66 } 67 } 68 69 72 public void testFieldModifiers() { 73 boolean fail = true; 74 Utility.beginTrans(true); 75 try { 76 int modifiers = field[0].getModifiers(); 77 modifiers &= ~(Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC); 78 field[0].setModifiers(modifiers); 79 fail = false; 80 } 81 finally { 82 Utility.endTrans(fail); 83 } 84 makeDiff("testFieldModifiers"); 85 } 86 87 90 public void testFieldType() { 91 boolean fail = true; 92 Utility.beginTrans(true); 93 try { 94 field[1].setTypeName(pkg.getMultipartId().createMultipartId("short", null, null)); 95 fail = false; 96 } 97 finally { 98 Utility.endTrans(fail); 99 } 100 makeDiff("testFieldType"); 101 } 102 103 106 public void testFieldName() { 107 boolean fail = true; 108 Utility.beginTrans(true); 109 try { 110 field[2].setName("thisIsTheNewName"); 111 fail = false; 112 } 113 finally { 114 Utility.endTrans(fail); 115 } 116 makeDiff("testFieldName"); 117 } 118 119 122 public void testFieldInitialValueText() { 123 boolean fail = true; 124 Utility.beginTrans(true); 125 try { 126 field[3].setInitialValueText("\"This is a new text.\""); 127 fail = false; 128 } 129 finally { 130 Utility.endTrans(fail); 131 } 132 makeDiff("testFieldInitialValueText"); 133 } 134 135 138 public void testFieldInitialValue() { 139 boolean fail = true; 140 Utility.beginTrans(true); 141 try { 142 Literal l = pkg.getLongLiteral().createLongLiteral((long) 78); 143 field[4].setInitialValue(l); 144 fail = false; 145 } 146 finally { 147 Utility.endTrans(fail); 148 } 149 makeDiff("testFieldInitialValue"); 150 } 151 152 156 public void testFieldChangeInInitValue() { 157 boolean fail = true; 158 Utility.beginTrans(true); 159 try { 160 NewClassExpression nce = (NewClassExpression) field[5].getInitialValue(); 161 List parameters = nce.getParameters(); 162 Object o = parameters.remove(0); 163 parameters.add(pkg.getStringLiteral().createStringLiteral("NetBeans")); 164 fail = false; 165 } 166 finally { 167 Utility.endTrans(fail); 168 } 169 makeDiff("testFieldChangeInInitValue"); 170 } 171 172 175 public void testFieldInitialValueRemoval() { 176 boolean fail = true; 177 Utility.beginTrans(true); 178 try { 179 field[6].setInitialValue(null); 180 fail = false; 181 } 182 finally { 183 Utility.endTrans(fail); 184 } 185 makeDiff("testFieldInitialValueRemoval"); 186 } 187 188 191 public void testFieldAdd() { 192 boolean fail = true; 193 Field field; 194 Utility.beginTrans(true); 195 try { 196 field=pkg.getField().createField(); 197 field.setType(pkg.getType().resolve("java.math.BigDecimal")); 198 field.setName("fooo"); 199 field.setModifiers(Modifier.PRIVATE); 200 assert "java.math.BigDecimal".equals(field.getType().getName()); 201 clazz.getContents().add(field); 202 assert "java.math.BigDecimal".equals(field.getType().getName()); 203 fail=false; 204 } 205 finally { 206 Utility.endTrans(fail); 207 } 208 assert "java.math.BigDecimal".equals(field.getType().getName()); 209 } 210 211 212 215 public static void main(String [] args) { 216 TestRunner.run(suite()); 217 } 218 219 } 220 | Popular Tags |