1 19 20 package org.netbeans.api.java.source.gen; 21 22 import com.sun.source.tree.*; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.EnumSet ; 27 import java.util.List ; 28 import javax.lang.model.element.Modifier; 29 import javax.lang.model.type.TypeKind; 30 import junit.textui.TestRunner; 31 import org.netbeans.jackpot.test.TestUtilities; 32 import org.netbeans.api.java.source.transform.Transformer; 33 import org.netbeans.junit.NbTestSuite; 34 35 39 public class IndentAddedElemTest extends GeneratorTest { 40 41 public IndentAddedElemTest(String name) { 42 super(name); 43 } 44 45 49 public static NbTestSuite suite() { 50 NbTestSuite suite = new NbTestSuite(IndentAddedElemTest.class); 52 return suite; 54 } 55 56 59 public void testAddMethodToEmpty() throws Exception { 60 testFile = new File (getWorkDir(), "Test.java"); 61 TestUtilities.copyStringToFile(testFile, 62 "package com.max.test.alfa;\n\n" + 63 "public class Test {\n" + 64 "}\n" 65 ); 66 String golden = 67 "package com.max.test.alfa;\n\n" + 68 "public class Test {\n" + 69 "\n" + 70 " public double Eval(int param) {\n" + 71 " }\n" + 72 "}\n"; 73 74 process( 75 new Transformer<Void , Object >() { 76 public Void visitClass(ClassTree node, Object p) { 77 super.visitClass(node, p); 78 if ("Test".contentEquals(node.getSimpleName())) { 79 ModifiersTree parMods = make.Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList()); 80 VariableTree param = make.Variable(parMods, "param", make.PrimitiveType(TypeKind.INT), null); 81 List <VariableTree> parList = new ArrayList <VariableTree>(1); 82 parList.add(param); 83 MethodTree member = make.Method( 84 make.Modifiers( 85 Collections.singleton(Modifier.PUBLIC), Collections.EMPTY_LIST ), "Eval", make.PrimitiveType(TypeKind.DOUBLE), Collections.EMPTY_LIST, parList, Collections.EMPTY_LIST, make.Block(Collections.EMPTY_LIST, false), null ); 96 97 ClassTree copy = make.addClassMember(node, member); 98 changes.rewrite(node, copy); 99 } 100 return null; 101 } 102 } 103 ); 104 String res = TestUtilities.copyFileToString(testFile); 105 assertEquals(golden, res); 106 } 107 108 111 public void testAddFieldEmpty() throws Exception { 112 testFile = new File (getWorkDir(), "Test.java"); 113 TestUtilities.copyStringToFile(testFile, 114 "package com.max.test.alfa;\n\n" + 115 "public class Test {\n" + 116 "}\n" 117 ); 118 String golden = 119 "package com.max.test.alfa;\n\n" + 120 "public class Test {\n" + 121 " private double value;\n" + 122 "}\n"; 123 124 process( 125 new Transformer<Void , Object >() { 126 public Void visitClass(ClassTree node, Object p) { 127 super.visitClass(node, p); 128 if ("Test".contentEquals(node.getSimpleName())) { 129 ModifiersTree modTree = make.Modifiers(EnumSet.of(Modifier.PRIVATE)); 130 VariableTree member = make.Variable( 131 make.Modifiers( 132 modTree, 133 Collections.<AnnotationTree>emptyList() 134 ), 135 "value", make.PrimitiveType(TypeKind.DOUBLE), null 137 ); 138 139 ClassTree copy = make.addClassMember(node, member); 140 changes.rewrite(node, copy); 141 } 142 return null; 143 } 144 } 145 ); 146 String res = TestUtilities.copyFileToString(testFile); 147 assertEquals(golden, res); 148 } 149 150 String getGoldenPckg() { 151 return ""; 152 } 153 154 String getSourcePckg() { 155 return ""; 156 } 157 158 161 public static void main(String [] args) { 162 TestRunner.run(suite()); 163 } 164 165 } 166 | Popular Tags |