1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.io.File ; 23 import java.io.IOException ; 24 import org.netbeans.api.java.source.*; 25 import static org.netbeans.api.java.source.JavaSource.*; 26 import org.netbeans.jackpot.test.TestUtilities; 27 import org.netbeans.junit.NbTestSuite; 28 29 34 public class PackageTest extends GeneratorTestMDRCompat { 35 36 37 public PackageTest(String testName) { 38 super(testName); 39 } 40 41 public static NbTestSuite suite() { 42 NbTestSuite suite = new NbTestSuite(); 43 suite.addTestSuite(PackageTest.class); 44 return suite; 50 } 51 52 56 public void testChangePackage() throws Exception { 57 testFile = new File (getWorkDir(), "Test.java"); 58 TestUtilities.copyStringToFile(testFile, 59 "/**\n" + 60 " * What?\n" + 61 " */\n" + 62 "package org.nothing;\n\n" + 63 "class Test {\n" + 64 "}\n" 65 ); 66 String golden = 67 "/**\n" + 68 " * What?\n" + 69 " */\n" + 70 "package com.unit;\n\n" + 71 "class Test {\n" + 72 "}\n"; 73 74 JavaSource src = getJavaSource(testFile); 75 CancellableTask task = new CancellableTask<WorkingCopy>() { 76 77 public void run(WorkingCopy workingCopy) throws IOException { 78 workingCopy.toPhase(Phase.RESOLVED); 79 TreeMaker make = workingCopy.getTreeMaker(); 80 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 81 CompilationUnitTree copy = make.CompilationUnit( 82 make.Identifier("com.unit"), 83 cut.getImports(), 84 cut.getTypeDecls(), 85 cut.getSourceFile() 86 ); 87 workingCopy.rewrite(cut, copy); 88 } 89 90 public void cancel() { 91 } 92 }; 93 src.runModificationTask(task).commit(); 94 String res = TestUtilities.copyFileToString(testFile); 95 System.err.println(res); 96 assertEquals(golden, res); 97 } 98 99 103 public void testChangeToDefPackage() throws Exception { 104 testFile = new File (getWorkDir(), "Test.java"); 105 TestUtilities.copyStringToFile(testFile, 106 "/**\n" + 107 " * What?\n" + 108 " */\n" + 109 "package org.nothing;\n\n" + 110 "class Test {\n" + 111 "}\n" 112 ); 113 String golden = 114 "/**\n" + 115 " * What?\n" + 116 " */\n" + 117 "\n\n" + 118 "class Test {\n" + 119 "}\n"; 120 121 JavaSource src = getJavaSource(testFile); 122 CancellableTask task = new CancellableTask<WorkingCopy>() { 123 124 public void run(WorkingCopy workingCopy) throws IOException { 125 workingCopy.toPhase(Phase.RESOLVED); 126 TreeMaker make = workingCopy.getTreeMaker(); 127 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 128 CompilationUnitTree copy = make.CompilationUnit( 129 null, 130 cut.getImports(), 131 cut.getTypeDecls(), 132 cut.getSourceFile() 133 ); 134 workingCopy.rewrite(cut, copy); 135 } 136 137 public void cancel() { 138 } 139 }; 140 src.runModificationTask(task).commit(); 141 String res = TestUtilities.copyFileToString(testFile); 142 System.err.println(res); 143 assertEquals(golden, res); 144 } 145 146 150 public void testChangeDefToNamedPackage() throws Exception { 151 testFile = new File (getWorkDir(), "Test.java"); 152 TestUtilities.copyStringToFile(testFile, 153 "/**\n" + 154 " * What?\n" + 155 " */\n" + 156 "class Test {\n" + 157 "}\n" 158 ); 159 String golden = 160 "package gro.snaebten.seludom.avaj;\n" + 161 "/**\n" + 162 " * What?\n" + 163 " */\n" + 164 "class Test {\n" + 165 "}\n"; 166 167 JavaSource src = getJavaSource(testFile); 168 CancellableTask task = new CancellableTask<WorkingCopy>() { 169 170 public void run(WorkingCopy workingCopy) throws IOException { 171 workingCopy.toPhase(Phase.RESOLVED); 172 TreeMaker make = workingCopy.getTreeMaker(); 173 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 174 CompilationUnitTree copy = make.CompilationUnit( 175 make.Identifier("gro.snaebten.seludom.avaj"), 176 cut.getImports(), 177 cut.getTypeDecls(), 178 cut.getSourceFile() 179 ); 180 workingCopy.rewrite(cut, copy); 181 } 182 183 public void cancel() { 184 } 185 }; 186 src.runModificationTask(task).commit(); 187 String res = TestUtilities.copyFileToString(testFile); 188 System.err.println(res); 189 assertEquals(golden, res); 190 } 191 192 196 public void testChangeDefToNamedPackageWithImport() throws Exception { 197 testFile = new File (getWorkDir(), "Test.java"); 198 TestUtilities.copyStringToFile(testFile, 199 "/**\n" + 200 " * What?\n" + 201 " */\n" + 202 "import gro;\n\n" + 203 "class Test {\n" + 204 "}\n" 205 ); 206 String golden = 207 "package gro.snaebten.seludom.avaj;\n" + 208 "/**\n" + 209 " * What?\n" + 210 " */\n" + 211 "import gro;\n\n" + 212 "class Test {\n" + 213 "}\n"; 214 215 JavaSource src = getJavaSource(testFile); 216 CancellableTask task = new CancellableTask<WorkingCopy>() { 217 218 public void run(WorkingCopy workingCopy) throws IOException { 219 workingCopy.toPhase(Phase.RESOLVED); 220 TreeMaker make = workingCopy.getTreeMaker(); 221 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 222 CompilationUnitTree copy = make.CompilationUnit( 223 make.Identifier("gro.snaebten.seludom.avaj"), 224 cut.getImports(), 225 cut.getTypeDecls(), 226 cut.getSourceFile() 227 ); 228 workingCopy.rewrite(cut, copy); 229 } 230 231 public void cancel() { 232 } 233 }; 234 src.runModificationTask(task).commit(); 235 String res = TestUtilities.copyFileToString(testFile); 236 System.err.println(res); 237 assertEquals(golden, res); 238 } 239 240 243 public void testPackageRenameWithAnnotation() throws Exception { 244 testFile = new File (getWorkDir(), "Test.java"); 245 TestUtilities.copyStringToFile(testFile, 246 "@SuppressWarnings()\n" + 247 "package javaapplication1;\n"); 248 String golden = 249 "@SuppressWarnings()\n" + 250 "package app;\n"; 251 252 JavaSource src = getJavaSource(testFile); 253 CancellableTask task = new CancellableTask<WorkingCopy>() { 254 255 public void run(WorkingCopy workingCopy) throws IOException { 256 workingCopy.toPhase(Phase.RESOLVED); 257 TreeMaker make = workingCopy.getTreeMaker(); 258 ExpressionTree pckgName = workingCopy.getCompilationUnit().getPackageName(); 259 workingCopy.rewrite(pckgName, make.setLabel(pckgName, "app")); 260 } 261 262 public void cancel() { 263 } 264 }; 265 src.runModificationTask(task).commit(); 266 String res = TestUtilities.copyFileToString(testFile); 267 System.err.println(res); 268 assertEquals(golden, res); 269 } 270 271 String getGoldenPckg() { 273 return ""; 274 } 275 276 String getSourcePckg() { 277 return ""; 278 } 279 280 281 } 282 | Popular Tags |