1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.ExpressionTree; 23 import com.sun.source.tree.NewClassTree; 24 import com.sun.source.tree.PrimitiveTypeTree; 25 import com.sun.source.tree.Tree; 26 import com.sun.source.tree.VariableTree; 27 import java.io.IOException ; 28 import java.util.Collections ; 29 import java.util.List ; 30 import javax.lang.model.type.TypeKind; 31 import junit.textui.TestRunner; 32 import org.netbeans.api.java.source.*; 33 import org.netbeans.api.java.source.JavaSource.Phase; 34 import org.netbeans.api.java.source.transform.Transformer; 35 import org.netbeans.junit.NbTestSuite; 36 import org.openide.filesystems.FileUtil; 37 38 43 public class FieldTest1 extends GeneratorTestMDRCompat { 44 45 public FieldTest1(String name) { 46 super(name); 47 } 48 49 public static NbTestSuite suite() { 50 NbTestSuite suite = new NbTestSuite(); 51 suite.addTest(new FieldTest1("testFieldModifiers")); 52 suite.addTest(new FieldTest1("testFieldType")); 53 suite.addTest(new FieldTest1("testFieldName")); 54 suite.addTest(new FieldTest1("testFieldInitialValueText")); 55 suite.addTest(new FieldTest1("testFieldInitialValue")); 56 suite.addTest(new FieldTest1("testFieldChangeInInitValue")); 57 suite.addTest(new FieldTest1("testFieldInitialValueRemoval")); 58 return suite; 59 } 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 testFile = getFile(getSourceDir(), getSourcePckg() + "FieldTest1.java"); 64 } 65 66 69 public void testFieldModifiers() throws IOException { 70 System.err.println("testFieldModifiers"); 71 process( 72 new Transformer<Void , Object >() { 73 public Void visitVariable(VariableTree node, Object p) { 74 super.visitVariable(node, p); 75 if ("modifiersField".contentEquals(node.getName())) { 76 changes.rewrite(node.getModifiers(), make.Modifiers(Collections.EMPTY_SET)); 77 } 78 return null; 79 } 80 } 81 ); 82 assertFiles("testFieldModifiers.pass"); 83 } 84 85 88 public void testFieldType() throws IOException { 89 System.err.println("testFieldType"); 90 process( 91 new Transformer<Void , Object >() { 92 public Void visitVariable(VariableTree node, Object p) { 93 super.visitVariable(node, p); 94 if ("typeField".contentEquals(node.getName())) { 95 PrimitiveTypeTree pt = make.PrimitiveType(TypeKind.SHORT); 96 VariableTree vt = make.Variable( 97 node.getModifiers(), 98 node.getName(), 99 pt, 100 node.getInitializer() 101 ); 102 model.setElement(vt, model.getElement(node)); 103 model.setType(vt, model.getType(node)); 104 model.setPos(vt, model.getPos(node)); 105 changes.rewrite(node, vt); 107 } 108 return null; 109 } 110 } 111 ); 112 assertFiles("testFieldType.pass"); 113 } 114 115 118 public void testFieldName() throws IOException { 119 System.err.println("testFieldName"); 120 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 121 CancellableTask task = new CancellableTask<WorkingCopy>() { 122 123 public void run(WorkingCopy workingCopy) throws java.io.IOException { 124 workingCopy.toPhase(Phase.RESOLVED); 125 TreeMaker make = workingCopy.getTreeMaker(); 126 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 127 VariableTree var = (VariableTree) clazz.getMembers().get(3); 128 workingCopy.rewrite(var, make.setLabel(var, "thisIsTheNewName")); 129 } 130 131 public void cancel() { 132 } 133 }; 134 testSource.runModificationTask(task).commit(); 135 String res = TestUtilities.copyFileToString(testFile); 136 System.err.println(res); 137 assertFiles("testFieldName.pass"); 138 } 139 140 143 public void testFieldInitialValueText() throws IOException { 144 System.err.println("testFieldInitialValueText"); 145 process( 146 new Transformer<Void , Object >() { 147 public Void visitVariable(VariableTree node, Object p) { 148 super.visitVariable(node, p); 149 if ("initialValueTextTester".contentEquals(node.getName())) { 150 changes.rewrite(node.getInitializer(), make.Literal("This is a new text.")); 151 } 152 return null; 153 } 154 } 155 ); 156 assertFiles("testFieldInitialValueText.pass"); 157 } 158 159 162 public void testFieldInitialValue() throws IOException { 163 System.err.println("testFieldInitialValue"); 164 process( 165 new Transformer<Void , Object >() { 166 public Void visitVariable(VariableTree node, Object p) { 167 super.visitVariable(node, p); 168 if ("initialValueChanger".contentEquals(node.getName())) { 169 changes.rewrite(node.getInitializer(), make.Literal(Long.valueOf(78))); 170 } 171 return null; 172 } 173 } 174 ); 175 assertFiles("testFieldInitialValue.pass"); 176 } 177 178 182 public void testFieldChangeInInitValue() throws IOException { 183 System.err.println("testFieldChangeInInitValue"); 184 process( 185 new Transformer<Void , Object >() { 186 public Void visitVariable(VariableTree node, Object p) { 187 super.visitVariable(node, p); 188 if ("initialValueReplacer".contentEquals(node.getName())) { 189 if (Tree.Kind.NEW_CLASS.equals(node.getInitializer().getKind())) { 190 NewClassTree nct = (NewClassTree) node.getInitializer(); 191 NewClassTree njuNct = make.NewClass( 192 nct.getEnclosingExpression(), 193 (List <ExpressionTree>) nct.getTypeArguments(), 194 nct.getIdentifier(), 195 Collections.singletonList(make.Literal("NetBeans")), 196 nct.getClassBody() 197 ); 198 changes.rewrite(nct, njuNct); 199 } 200 } 201 return null; 202 } 203 } 204 ); 205 assertFiles("testFieldChangeInInitValue.pass"); 206 } 207 208 211 public void testFieldInitialValueRemoval() throws IOException { 212 System.err.println("testFieldInitialValueRemoval"); 213 process( 214 new Transformer<Void , Object >() { 215 public Void visitVariable(VariableTree node, Object p) { 216 super.visitVariable(node, p); 217 if ("removeInitialValueField".contentEquals(node.getName())) { 218 VariableTree vt = make.Variable( 219 node.getModifiers(), 220 node.getName(), 221 node.getType(), 222 null); 223 changes.rewrite(node, vt); 224 } 225 return null; 226 } 227 } 228 ); 229 assertFiles("testFieldInitialValueRemoval.pass"); 230 } 231 232 235 public static void main(String [] args) { 236 TestRunner.run(suite()); 237 } 238 239 String getSourcePckg() { 240 return "org/netbeans/test/codegen/"; 241 } 242 243 String getGoldenPckg() { 244 return "org/netbeans/jmi/javamodel/codegen/FieldTest1/FieldTest1/"; 245 } 246 } 247 | Popular Tags |