1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.io.*; 23 import java.util.Collections ; 24 import java.util.EnumSet ; 25 import javax.lang.model.element.Modifier; 26 import javax.lang.model.type.TypeKind; 27 import static org.netbeans.api.java.lexer.JavaTokenId.*; 28 import org.netbeans.api.java.source.*; 29 import org.netbeans.api.java.source.query.Query; 30 import static org.netbeans.api.java.source.JavaSource.*; 31 import org.netbeans.junit.NbTestSuite; 32 import org.openide.filesystems.FileUtil; 33 34 38 public class CommentsTest extends GeneratorTestMDRCompat { 39 40 41 public CommentsTest(String name) { 42 super(name); 43 } 44 45 public static NbTestSuite suite() { 46 NbTestSuite suite = new NbTestSuite(); 47 suite.addTestSuite(CommentsTest.class); 48 return suite; 53 } 54 55 public void testAddStatement() throws Exception { 56 testFile = new File(getWorkDir(), "Test.java"); 57 TestUtilities.copyStringToFile(testFile, 58 "package hierbas.del.litoral;\n" + 59 "\n" + 60 "import java.io.File;\n" + 61 "\n" + 62 "public class Test {\n" + 63 "\n" + 64 " void method() {\n" + 65 " }\n\n" + 66 "}\n" 67 ); 68 String golden = 69 "package hierbas.del.litoral;\n" + 70 "\n" + 71 "import java.io.File;\n" + 72 "\n" + 73 "public class Test {\n" + 74 "\n" + 75 " void method() {" + 76 " // test\n" + 77 " int a;\n" + 78 "\n" + 79 " /**\n" + 80 " * becko\n" + 81 " */\n" + 82 " int b;\n" + 83 " // cecko\n" + 84 " int c;\n" + 85 "\n" + 86 " }\n\n" + 87 "}\n"; 88 89 JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 90 91 CancellableTask task = new CancellableTask<WorkingCopy>() { 92 93 public void run(WorkingCopy workingCopy) throws IOException { 94 workingCopy.toPhase(Phase.RESOLVED); 95 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 96 TreeMaker make = workingCopy.getTreeMaker(); 97 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 98 MethodTree method = (MethodTree) clazz.getMembers().get(1); 99 String bodyText = 100 "{" + 101 " // test\n" + 102 " int a;\n" + 103 " /** becko */\n" + 104 " int b;\n" + 105 " /* cecko\n */\n" + 106 " int c; // trail\n" + 107 "}"; 108 BlockTree block = make.createMethodBody(method, bodyText); 109 workingCopy.rewrite(method.getBody(), block); 110 } 111 112 public void cancel() { 113 } 114 }; 115 src.runModificationTask(task).commit(); 116 String res = TestUtilities.copyFileToString(testFile); 117 System.err.println(res); 118 assertEquals(golden, res); 119 } 120 121 public void testGetComment1() throws Exception { 122 testFile = new File(getWorkDir(), "Test.java"); 123 TestUtilities.copyStringToFile(testFile, 124 "package hierbas.del.litoral;\n" + 125 "\n" + 126 "import java.io.File;\n" + 127 "\n" + 128 "public class Test {\n" + 129 "\n" + 130 " void method() {\n" + 131 " // preceding comment\n" + 132 " int a; // trailing comment\n" + 133 " // what's up?" + 134 " }\n" + 135 "\n" + 136 "}\n" 137 ); 138 JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 139 140 CancellableTask task = new CancellableTask<WorkingCopy>() { 141 142 public void run(WorkingCopy workingCopy) throws IOException { 143 workingCopy.toPhase(Phase.RESOLVED); 144 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 145 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 146 MethodTree method = (MethodTree) clazz.getMembers().get(1); 147 } 151 152 public void cancel() { 153 } 154 }; 155 src.runModificationTask(task).commit(); 156 } 157 158 public void testAddJavaDocToMethod() throws Exception { 159 File testFile = new File(getWorkDir(), "Test.java"); 160 TestUtilities.copyStringToFile(testFile, 161 "package hierbas.del.litoral;\n" + 162 "\n" + 163 "public class Test {\n" + 164 " Test() {\n" + 165 " }\n" + 166 "\n" + 167 "}\n" 168 ); 169 170 JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 171 CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { 172 173 public void run(final WorkingCopy copy) throws Exception { 174 copy.toPhase(Phase.RESOLVED); 175 176 TreeMaker make = copy.getTreeMaker(); 177 ClassTree node = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); 178 MethodTree method = make.Method( 179 make.Modifiers(EnumSet.of(Modifier.PUBLIC)), 180 "nuevoMetodo", 181 make.PrimitiveType(TypeKind.VOID), 182 Collections.<TypeParameterTree>emptyList(), 183 Collections.<VariableTree>emptyList(), 184 Collections.<ExpressionTree>emptyList(), 185 "{ }", 186 null 187 ); 188 make.addComment(method, Comment.create( 189 Comment.Style.JAVADOC, 190 Query.NOPOS, 191 Query.NOPOS, 192 Query.NOPOS, 193 "Comentario"), 194 true 195 ); 196 ClassTree clazz = make.addClassMember(node, method); 197 copy.rewrite(node, clazz); 198 } 199 200 public void cancel() { 201 } 202 }; 203 src.runModificationTask(task).commit(); 204 System.err.println(TestUtilities.copyFileToString(testFile)); 205 assertTrue(TestUtilities.copyFileToString(testFile).contains("nuevoMetodo")); 206 assertTrue(TestUtilities.copyFileToString(testFile).contains("Comentario")); 207 } 208 209 public void testAddJavaDocToExistingMethod() throws Exception { 210 File testFile = new File(getWorkDir(), "Test.java"); 211 TestUtilities.copyStringToFile(testFile, 212 "package hierbas.del.litoral;\n" + 213 "\n" + 214 "public class Test {\n" + 215 " public void test(int a) {\n" + 216 " }\n\n" + 217 "}\n" 218 ); 219 220 JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 221 CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { 222 223 public void run(final WorkingCopy workingCopy) throws Exception { 224 workingCopy.toPhase(Phase.RESOLVED); 225 226 TreeMaker make = workingCopy.getTreeMaker(); 227 ClassTree node = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 228 MethodTree method = (MethodTree) node.getMembers().get(1); 229 MethodTree copy = make.Method(method.getModifiers(), 230 method.getName(), 231 method.getReturnType(), 232 method.getTypeParameters(), 233 method.getParameters(), 234 method.getThrows(), 235 method.getBody(), 236 (ExpressionTree) method.getDefaultValue() 237 ); 238 make.addComment(copy, Comment.create( 239 Comment.Style.JAVADOC, 240 Query.NOPOS, 241 Query.NOPOS, 242 Query.NOPOS, 243 "/** Comentario \n*/"), 244 true 245 ); 246 workingCopy.rewrite(method, copy); 247 } 248 249 public void cancel() { 250 } 251 }; 252 src.runModificationTask(task).commit(); 253 System.err.println(TestUtilities.copyFileToString(testFile)); 254 assertTrue(TestUtilities.copyFileToString(testFile), TestUtilities.copyFileToString(testFile).contains("Comentario")); 255 } 256 257 String getGoldenPckg() { 258 return ""; 259 } 260 261 String getSourcePckg() { 262 return ""; 263 } 264 265 } 266 | Popular Tags |