1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.util.*; 23 import java.io.IOException ; 24 import javax.lang.model.element.Modifier; 25 import org.netbeans.junit.NbTestSuite; 26 import junit.textui.TestRunner; 27 import org.netbeans.api.java.source.*; 28 import static org.netbeans.api.java.source.JavaSource.*; 29 import org.openide.filesystems.FileUtil; 30 31 36 public class Method1Test extends GeneratorTestMDRCompat { 37 38 39 public Method1Test(String name) { 40 super(name); 41 } 42 43 public static NbTestSuite suite() { 44 NbTestSuite suite = new NbTestSuite(); 45 suite.addTest(new Method1Test("testMethodModifiers")); 46 suite.addTest(new Method1Test("testMethodName")); 47 suite.addTest(new Method1Test("testMethodParameters")); 48 suite.addTest(new Method1Test("testMethodParameterChange")); 49 suite.addTest(new Method1Test("testMethodThrows")); 50 suite.addTest(new Method1Test("testMethodReturnType")); 51 return suite; 56 } 57 58 protected void setUp() throws Exception { 59 super.setUp(); 60 testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest1.java"); 61 } 62 63 67 public void testMethodModifiers() throws IOException { 68 System.err.println("testMethodModifiers"); 69 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 70 CancellableTask task = new CancellableTask<WorkingCopy>() { 71 72 public void run(WorkingCopy workingCopy) throws java.io.IOException { 73 workingCopy.toPhase(Phase.RESOLVED); 74 TreeMaker make = workingCopy.getTreeMaker(); 75 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 76 MethodTree method = (MethodTree) clazz.getMembers().get(1); 77 ModifiersTree origMods = method.getModifiers(); 78 Set<Modifier> njuMods = new HashSet<Modifier>(); 79 njuMods.add(Modifier.PRIVATE); 80 njuMods.add(Modifier.STATIC); 81 workingCopy.rewrite(origMods, make.Modifiers(njuMods)); 82 } 83 84 public void cancel() { 85 } 86 }; 87 testSource.runModificationTask(task).commit(); 88 String res = TestUtilities.copyFileToString(testFile); 89 System.err.println(res); 90 assertFiles("testMethodModifiers.pass"); 91 } 92 93 96 public void testMethodName() throws IOException { 97 System.err.println("testMethodName"); 98 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 99 CancellableTask task = new CancellableTask<WorkingCopy>() { 100 101 public void run(WorkingCopy workingCopy) throws java.io.IOException { 102 workingCopy.toPhase(Phase.RESOLVED); 103 TreeMaker make = workingCopy.getTreeMaker(); 104 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 105 MethodTree method = (MethodTree) clazz.getMembers().get(2); 106 workingCopy.rewrite(method, make.setLabel(method, "druhaMetoda")); 107 } 108 109 public void cancel() { 110 } 111 }; 112 testSource.runModificationTask(task).commit(); 113 String res = TestUtilities.copyFileToString(testFile); 114 System.err.println(res); 115 assertFiles("testMethodName.pass"); 116 } 117 118 121 public void testMethodParameters() throws IOException { 122 System.err.println("testMethodParameters"); 123 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 124 CancellableTask task = new CancellableTask<WorkingCopy>() { 125 126 public void run(WorkingCopy workingCopy) throws java.io.IOException { 127 workingCopy.toPhase(Phase.RESOLVED); 128 TreeMaker make = workingCopy.getTreeMaker(); 129 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 130 MethodTree method = (MethodTree) clazz.getMembers().get(3); 131 VariableTree vt = method.getParameters().get(0); 132 VariableTree vtCopy = make.Variable(vt.getModifiers(), vt.getName(), vt.getType(), vt.getInitializer()); 133 MethodTree njuMethod = make.removeMethodParameter(method, 0); 134 njuMethod = make.addMethodParameter(njuMethod, vt); 135 workingCopy.rewrite(method, njuMethod); 136 } 137 138 public void cancel() { 139 } 140 }; 141 testSource.runModificationTask(task).commit(); 142 String res = TestUtilities.copyFileToString(testFile); 143 System.err.println(res); 144 assertFiles("testMethodParameters.pass"); 145 } 146 147 150 public void testMethodParameterChange() throws IOException { 151 System.err.println("testMethodParameters"); 152 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 153 CancellableTask task = new CancellableTask<WorkingCopy>() { 154 155 public void run(WorkingCopy workingCopy) throws java.io.IOException { 156 workingCopy.toPhase(Phase.RESOLVED); 157 TreeMaker make = workingCopy.getTreeMaker(); 158 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 159 MethodTree method = (MethodTree) clazz.getMembers().get(4); 160 List<? extends VariableTree> parameters = method.getParameters(); 161 VariableTree vt = parameters.get(0); 162 MethodTree njuMethod = make.removeMethodParameter(method, 0); 163 VariableTree njuParameter = make.setLabel(vt, "aParNewName"); 164 workingCopy.rewrite(vt, njuParameter); 165 njuMethod = make.insertMethodParameter(njuMethod, 0, njuParameter); 168 workingCopy.rewrite(method, njuMethod); 169 } 170 171 public void cancel() { 172 } 173 }; 174 testSource.runModificationTask(task).commit(); 175 String res = TestUtilities.copyFileToString(testFile); 176 System.err.println(res); 177 assertFiles("testMethodParameterChange.pass"); 178 } 179 180 183 public void testMethodThrows() throws IOException { 184 System.err.println("testMethodThrows"); 185 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 186 CancellableTask task = new CancellableTask<WorkingCopy>() { 187 188 public void run(WorkingCopy workingCopy) throws java.io.IOException { 189 workingCopy.toPhase(Phase.RESOLVED); 190 TreeMaker make = workingCopy.getTreeMaker(); 191 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 192 MethodTree method = (MethodTree) clazz.getMembers().get(5); 193 ExpressionTree ident = make.Identifier("java.lang.IllegalMonitorStateException"); 194 MethodTree njuMethod = make.removeMethodThrows(method, 0); 195 njuMethod = make.addMethodThrows(njuMethod, ident); 196 workingCopy.rewrite(method, njuMethod); 197 } 198 199 public void cancel() { 200 } 201 }; 202 testSource.runModificationTask(task).commit(); 203 String res = TestUtilities.copyFileToString(testFile); 204 System.err.println(res); 205 assertFiles("testMethodThrows.pass"); 206 } 207 208 211 public void testMethodReturnType() throws IOException { 212 System.err.println("testMethodReturnType"); 213 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 214 CancellableTask task = new CancellableTask<WorkingCopy>() { 215 216 public void run(WorkingCopy workingCopy) throws java.io.IOException { 217 workingCopy.toPhase(Phase.RESOLVED); 218 TreeMaker make = workingCopy.getTreeMaker(); 219 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 220 MethodTree method = (MethodTree) clazz.getMembers().get(6); 221 Tree returnType = method.getReturnType(); 222 IdentifierTree identifier = make.Identifier("String"); 223 workingCopy.rewrite(returnType, identifier); 224 } 225 226 public void cancel() { 227 } 228 }; 229 testSource.runModificationTask(task).commit(); 230 String res = TestUtilities.copyFileToString(testFile); 231 System.err.println(res); 232 assertFiles("testMethodReturnType.pass"); 233 } 234 235 238 406 410 public static void main(String [] args) { 411 TestRunner.run(suite()); 412 } 413 414 String getSourcePckg() { 415 return "org/netbeans/test/codegen/"; 416 } 417 418 String getGoldenPckg() { 419 return "org/netbeans/jmi/javamodel/codegen/MethodTest1/MethodTest1/"; 420 } 421 422 } 423 | Popular Tags |