1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.MethodTree; 23 import com.sun.source.tree.ModifiersTree; 24 import com.sun.source.tree.PrimitiveTypeTree; 25 import com.sun.source.tree.VariableTree; 26 import java.io.File ; 27 import java.util.Collections ; 28 import javax.lang.model.element.Modifier; 29 import javax.lang.model.type.TypeKind; 30 import org.netbeans.api.java.source.TestUtilities; 31 import org.netbeans.api.java.source.transform.Transformer; 32 import org.netbeans.junit.NbTestSuite; 33 34 39 public class Field6Test extends GeneratorTest { 40 41 42 public Field6Test(String testName) { 43 super(testName); 44 } 45 46 public static NbTestSuite suite() { 47 NbTestSuite suite = new NbTestSuite(); 48 suite.addTest(new Field6Test("testAddFieldToIndex0")); 50 suite.addTest(new Field6Test("testRemoveInitialValue")); 51 suite.addTest(new Field6Test("testAddFirstParameter")); 52 return suite; 53 } 54 55 public void testAddFieldToIndex0() throws Exception { 56 testFile = new File (getWorkDir(), "Test.java"); 57 TestUtilities.copyStringToFile(testFile, 58 "package hierbas.del.litoral;\n\n" + 59 "import java.io.*;\n\n" + 60 "public class Test {\n" + 61 " public void taragui() {\n" + 62 " }\n" + 63 "}\n" 64 ); 65 String golden = 66 "package hierbas.del.litoral;\n\n" + 67 "import java.io.*;\n\n" + 68 "public class Test {\n" + 69 " int field1;\n" + 70 " public void taragui() {\n" + 71 " }\n" + 72 "}\n"; 73 74 process( 75 new Transformer<Void , Object >() { 76 77 public Void visitClass(ClassTree node, Object p) { 78 super.visitClass(node, p); 79 ModifiersTree mods = make.Modifiers(Collections.<Modifier>emptySet()); 80 PrimitiveTypeTree type = make.PrimitiveType(TypeKind.INT); 81 VariableTree var = make.Variable(mods, "field1", type, null); 82 ClassTree copy = make.addClassMember(node, var); 83 changes.rewrite(node, copy); 84 return null; 85 } 86 } 87 ); 88 String res = TestUtilities.copyFileToString(testFile); 89 assertEquals(golden, res); 90 } 91 92 public void testRemoveInitialValue() throws Exception { 93 testFile = new File (getWorkDir(), "Test.java"); 94 TestUtilities.copyStringToFile(testFile, 95 "package hierbas.del.litoral;\n\n" + 96 "import java.io.*;\n\n" + 97 "public class Test {\n" + 98 " int removeInitialValueField = null;\n" + 99 "}\n" 100 ); 101 String golden = 102 "package hierbas.del.litoral;\n\n" + 103 "import java.io.*;\n\n" + 104 "public class Test {\n" + 105 " int removeInitialValueField;\n" + 106 "}\n"; 107 108 process( 109 new Transformer<Void , Object >() { 110 public Void visitVariable(VariableTree node, Object p) { 111 super.visitVariable(node, p); 112 if ("removeInitialValueField".contentEquals(node.getName())) { 113 VariableTree vt = make.Variable( 114 node.getModifiers(), 115 node.getName(), 116 node.getType(), 117 null); 118 changes.rewrite(node, vt); 119 } 120 return null; 121 } 122 } 123 ); 124 String res = TestUtilities.copyFileToString(testFile); 125 System.err.println(res); 126 assertEquals(golden, res); 127 } 128 129 public void testAddFirstParameter() throws Exception { 130 testFile = new File (getWorkDir(), "Test.java"); 131 TestUtilities.copyStringToFile(testFile, 132 "package hierbas.del.litoral;\n\n" + 133 "import java.io.*;\n\n" + 134 "public class Test {\n" + 135 " public void taragui() {\n" + 136 " }\n" + 137 "}\n" 138 ); 139 String golden = 140 "package hierbas.del.litoral;\n\n" + 141 "import java.io.*;\n\n" + 142 "public class Test {\n" + 143 " public void taragui(int equilibrio) {\n" + 144 " }\n" + 145 "}\n"; 146 147 process( 148 new Transformer<Void , Object >() { 149 public Void visitMethod(MethodTree node, Object p) { 150 super.visitMethod(node, p); 151 if ("taragui".contentEquals(node.getName())) { 152 VariableTree vt = make.Variable( 153 make.Modifiers(Collections.<Modifier>emptySet()), 154 "equilibrio", 155 make.PrimitiveType(TypeKind.INT), 156 null); 157 MethodTree copy = make.addMethodParameter(node, vt); 158 changes.rewrite(node, copy); 159 } 160 return null; 161 } 162 } 163 ); 164 String res = TestUtilities.copyFileToString(testFile); 165 System.err.println(res); 166 assertEquals(golden, res); 167 } 168 169 protected void setUp() throws Exception { 170 super.setUp(); 171 testFile = getFile(getSourceDir(), getSourcePckg() + "Test.java"); 172 } 173 174 String getGoldenPckg() { 175 return ""; 176 } 177 178 String getSourcePckg() { 179 return ""; 180 } 181 182 } 183 | Popular Tags |