1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.io.File ; 23 import java.util.Collections ; 24 import javax.lang.model.element.Modifier; 25 import javax.lang.model.type.TypeKind; 26 import org.netbeans.jackpot.test.TestUtilities; 27 import org.netbeans.api.java.source.transform.Transformer; 28 import org.netbeans.junit.NbTestSuite; 29 30 34 public class FeatureAddingTest extends GeneratorTest { 35 36 37 public FeatureAddingTest(String testName) { 38 super(testName); 39 } 40 41 public static NbTestSuite suite() { 42 NbTestSuite suite = new NbTestSuite(); 43 suite.addTest(new FeatureAddingTest("testAddFieldToBeginning")); 44 suite.addTest(new FeatureAddingTest("testAddFieldToEnd")); 45 return suite; 46 } 47 48 public void testAddFieldToBeginning() throws Exception { 49 testFile = new File (getWorkDir(), "Test.java"); 50 TestUtilities.copyStringToFile(testFile, 51 "package hierbas.del.litoral;\n\n" + 52 "import java.io.File;\n\n" + 53 "public class Test {\n" + 54 " \n" + 55 " /* comment */\n" + 56 " Test(int a, long c, String s) {\n" + 57 " }\n\n" + 58 " void method() {\n" + 59 " }\n\n" + 60 "}\n" 61 ); 62 String golden = 63 "package hierbas.del.litoral;\n\n" + 64 "import java.io.File;\n\n" + 65 "public class Test {\n" + 66 " int a;\n" + 67 " \n" + 68 " /* comment */\n" + 69 " Test(int a, long c, String s) {\n" + 70 " }\n\n" + 71 " void method() {\n" + 72 " }\n\n" + 73 "}\n"; 74 75 process( 76 new Transformer<Void , Object >() { 77 public Void visitClass(ClassTree node, Object p) { 78 super.visitClass(node, p); 79 if ("Test".contentEquals(node.getSimpleName())) { 80 VariableTree member = make.Variable( 81 make.Modifiers( 82 Collections.<Modifier>emptySet(), 83 Collections.<AnnotationTree>emptyList() 84 ), 85 "a", 86 make.PrimitiveType(TypeKind.INT), null 87 ); 88 ClassTree copy = make.insertClassMember(node, 0, member); 89 changes.rewrite(node, copy); 90 } 91 return null; 92 } 93 } 94 ); 95 String res = TestUtilities.copyFileToString(testFile); 96 assertEquals(golden, res); 97 } 98 99 public void testAddFieldToEnd() throws Exception { 100 testFile = new File (getWorkDir(), "Test.java"); 101 TestUtilities.copyStringToFile(testFile, 102 "package hierbas.del.litoral;\n\n" + 103 "import java.io.File;\n\n" + 104 "public class Test {\n" + 105 " \n" + 106 " /* comment */\n" + 107 " Test(int a, long c, String s) {\n" + 108 " }\n\n" + 109 " void method() {\n" + 110 " }\n" + 111 " \n" + 112 "}\n" 113 ); 114 String golden = 115 "package hierbas.del.litoral;\n\n" + 116 "import java.io.File;\n\n" + 117 "public class Test {\n" + 118 " \n" + 119 " /* comment */\n" + 120 " Test(int a, long c, String s) {\n" + 121 " }\n\n" + 122 " void method() {\n" + 123 " }\n" + 124 " int a;\n" + 125 " \n" + 126 "}\n"; 127 128 process( 129 new Transformer<Void , Object >() { 130 public Void visitClass(ClassTree node, Object p) { 131 super.visitClass(node, p); 132 if ("Test".contentEquals(node.getSimpleName())) { 133 VariableTree member = make.Variable( 134 make.Modifiers( 135 Collections.<Modifier>emptySet(), 136 Collections.<AnnotationTree>emptyList() 137 ), 138 "a", 139 make.PrimitiveType(TypeKind.INT), null 140 ); 141 ClassTree copy = make.addClassMember(node, member); 142 changes.rewrite(node, copy); 143 } 144 return null; 145 } 146 } 147 ); 148 String res = TestUtilities.copyFileToString(testFile); 149 assertEquals(golden, res); 150 } 151 152 String getGoldenPckg() { 153 return ""; 154 } 155 156 String getSourcePckg() { 157 return ""; 158 } 159 } 160 | Popular Tags |