1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.CompilationUnitTree; 22 import com.sun.source.tree.ImportTree; 23 import com.sun.source.tree.Tree; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Comparator ; 28 import java.util.List ; 29 import org.netbeans.modules.java.source.engine.ASTModel; 30 import org.netbeans.api.java.source.transform.Transformer; 31 import org.netbeans.junit.NbTestSuite; 32 import org.openide.filesystems.FileStateInvalidException; 33 34 38 public class ImportFormatTest extends GeneratorTestMDRCompat { 39 40 41 public ImportFormatTest(String name) { 42 super(name); 43 } 44 45 public static NbTestSuite suite() { 46 NbTestSuite suite = new NbTestSuite(); 47 suite.addTest(new ImportFormatTest("testFirstAddition")); 48 suite.addTest(new ImportFormatTest("testAddFirstImport")); 49 suite.addTest(new ImportFormatTest("testAddLastImport")); 50 suite.addTest(new ImportFormatTest("testRemoveInnerImport")); 51 suite.addTest(new ImportFormatTest("testRemoveFirstImport")); 52 suite.addTest(new ImportFormatTest("testRemoveLastImport")); 53 suite.addTest(new ImportFormatTest("testRemoveAllRemaning")); 54 return suite; 55 } 56 57 protected void setUp() throws FileStateInvalidException, Exception { 58 super.setUp(); 59 testFile = getFile(getSourceDir(), getSourcePckg() + "ImportClass1.java"); 60 } 61 62 public void testFirstAddition() throws IOException , FileStateInvalidException { 63 System.err.println("testFirstAddition"); 64 process(new Transformer<Void , Object >() { 65 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 66 List <ImportTree> imports = new ArrayList <ImportTree>(); 67 imports.add(make.Import(make.Identifier("java.util.List"), false)); 68 imports.add(make.Import(make.Identifier("java.util.Set"), false)); 69 CompilationUnitTree unit = make.CompilationUnit( 70 node.getPackageName(), 71 imports, 72 node.getTypeDecls(), 73 node.getSourceFile() 74 ); 75 changes.rewrite(node, unit); 76 return null; 77 } 78 }); 79 assertFiles("testFirstAddition_ImportFormatTest.pass"); 80 } 81 82 public void testAddFirstImport() throws IOException , FileStateInvalidException { 83 System.err.println("testAddFirstImport"); 84 process(new Transformer<Void , Object >() { 85 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 86 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 87 imports.add(0, make.Import(make.Identifier("java.util.AbstractList"), false)); 88 CompilationUnitTree unit = make.CompilationUnit( 89 node.getPackageName(), 90 imports, 91 node.getTypeDecls(), 92 node.getSourceFile() 93 ); 94 changes.rewrite(node, unit); 95 return null; 96 } 97 }); 98 assertFiles("testAddFirstImport_ImportFormatTest.pass"); 99 } 100 101 102 public void testAddLastImport() throws IOException , FileStateInvalidException { 103 process(new Transformer<Void , Object >() { 104 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 105 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 106 imports.add(make.Import(make.Identifier("java.io.IOException"), false)); 107 CompilationUnitTree unit = make.CompilationUnit( 108 node.getPackageName(), 109 imports, 110 node.getTypeDecls(), 111 node.getSourceFile() 112 ); 113 changes.rewrite(node, unit); 114 return null; 115 } 116 }); 117 assertFiles("testAddLastImport_ImportFormatTest.pass"); 118 } 119 120 public void testRemoveInnerImport() throws IOException , FileStateInvalidException { 121 process(new Transformer<Void , Object >() { 122 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 123 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 124 imports.remove(1); 125 CompilationUnitTree unit = make.CompilationUnit( 126 node.getPackageName(), 127 imports, 128 node.getTypeDecls(), 129 node.getSourceFile() 130 ); 131 changes.rewrite(node, unit); 132 return null; 133 } 134 }); 135 assertFiles("testRemoveInnerImport_ImportFormatTest.pass"); 136 } 137 138 public void testRemoveFirstImport() throws IOException , FileStateInvalidException { 139 process(new Transformer<Void , Object >() { 140 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 141 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 142 imports.remove(0); 143 CompilationUnitTree unit = make.CompilationUnit( 144 node.getPackageName(), 145 imports, 146 node.getTypeDecls(), 147 node.getSourceFile() 148 ); 149 changes.rewrite(node, unit); 150 return null; 151 } 152 }); 153 assertFiles("testRemoveFirstImport_ImportFormatTest.pass"); 154 } 155 156 public void testRemoveLastImport() throws IOException , FileStateInvalidException { 157 process(new Transformer<Void , Object >() { 158 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 159 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 160 imports.remove(1); 161 CompilationUnitTree unit = make.CompilationUnit( 162 node.getPackageName(), 163 imports, 164 node.getTypeDecls(), 165 node.getSourceFile() 166 ); 167 changes.rewrite(node, unit); 168 return null; 169 } 170 }); 171 assertFiles("testRemoveLastImport_ImportFormatTest.pass"); 172 } 173 174 public void testRemoveAllRemaning() throws IOException , FileStateInvalidException { 175 process(new Transformer<Void , Object >() { 176 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 177 CompilationUnitTree unit = make.CompilationUnit( 178 node.getPackageName(), 179 Collections.EMPTY_LIST, 180 node.getTypeDecls(), 181 node.getSourceFile() 182 ); 183 changes.rewrite(node, unit); 184 return null; 185 } 186 }); 187 assertFiles("testRemoveAllRemaning_ImportFormatTest.pass"); 188 } 189 190 191 static class T extends Transformer<Void , Object > { 192 public Void visitImport(ImportTree node, Object p) { 193 make.Identifier("java.util.List"); 194 System.err.println(getParent(node, model)); 195 return null; 196 } 197 } 198 199 public void testAddSeveral() throws IOException , FileStateInvalidException { 200 process(new Transformer<Void , Object >() { 201 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 202 List <ImportTree> imports = (List <ImportTree>) node.getImports(); 203 imports.add(make.Import(make.Identifier("java.util.List"), false)); 204 imports.add(make.Import(make.Identifier("java.util.Set"), false)); 205 imports.add(make.Import(make.Identifier("javax.swing.CellRendererPane"), false)); 206 imports.add(make.Import(make.Identifier("javax.swing.BorderFactory"), false)); 207 imports.add(make.Import(make.Identifier("javax.swing.ImageIcon"), false)); 208 imports.add(make.Import(make.Identifier("javax.swing.InputVerifier"), false)); 209 imports.add(make.Import(make.Identifier("javax.swing.GrayFilter"), false)); 210 imports.add(make.Import(make.Identifier("javax.swing.JFileChooser"), false)); 211 imports.add(make.Import(make.Identifier("javax.swing.AbstractAction"), false)); 212 CompilationUnitTree unit = make.CompilationUnit( 213 node.getPackageName(), 214 imports, 215 node.getTypeDecls(), 216 node.getSourceFile() 217 ); 218 changes.rewrite(node, unit); 219 return null; 220 } 221 }); 222 assertFiles("testAddSeveral_ImportFormatTest.pass"); 223 } 224 225 public void testRemoveInside() throws IOException , FileStateInvalidException { 226 process(new Transformer<Void , Object >() { 227 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 228 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 229 imports.remove(4); 230 CompilationUnitTree unit = make.CompilationUnit( 231 node.getPackageName(), 232 imports, 233 node.getTypeDecls(), 234 node.getSourceFile() 235 ); 236 changes.rewrite(node, unit); 237 return null; 238 } 239 }); 240 assertFiles("testRemoveInside_ImportFormatTest.pass"); 241 } 242 243 public void testMoveFirst() throws IOException , FileStateInvalidException { 244 process(new Transformer<Void , Object >() { 245 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 246 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 247 ImportTree oneImport = imports.remove(0); 248 imports.add(3, oneImport); 249 CompilationUnitTree unit = make.CompilationUnit( 250 node.getPackageName(), 251 imports, 252 node.getTypeDecls(), 253 node.getSourceFile() 254 ); 255 changes.rewrite(node, unit); 256 return null; 257 } 258 }); 259 assertFiles("testMoveFirst_ImportFormatTest.pass"); 260 } 261 262 public void testMoveLast() throws IOException , FileStateInvalidException { 263 process(new Transformer<Void , Object >() { 264 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 265 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 266 ImportTree oneImport = imports.remove(7); 267 imports.add(1, oneImport); 268 CompilationUnitTree unit = make.CompilationUnit( 269 node.getPackageName(), 270 imports, 271 node.getTypeDecls(), 272 node.getSourceFile() 273 ); 274 changes.rewrite(node, unit); 275 return null; 276 } 277 }); 278 assertFiles("testMoveLast_ImportFormatTest.pass"); 279 } 280 281 public void testReplaceLine() throws IOException , FileStateInvalidException { 282 process(new Transformer<Void , Object >() { 283 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 284 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 285 ImportTree oneImport = imports.remove(4); 286 imports.add(4, make.Import(make.Identifier("java.util.Collection"), false)); 287 CompilationUnitTree unit = make.CompilationUnit( 288 node.getPackageName(), 289 imports, 290 node.getTypeDecls(), 291 node.getSourceFile() 292 ); 293 changes.rewrite(node, unit); 294 return null; 295 } 296 }); 297 assertFiles("testReplaceLine_ImportFormatTest.pass"); 298 } 299 300 public void testSort() throws IOException , FileStateInvalidException { 301 process(new Transformer<Void , Object >() { 302 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 303 List <ImportTree> imports = new ArrayList <ImportTree>(node.getImports()); 304 ImportTree oneImport = imports.remove(4); 305 imports.add(4, make.Import(make.Identifier("java.util.Collection"), false)); 306 Collections.sort(imports, new Comparator () { 307 public int compare(Object o1, Object o2) { 308 if (o1 == o2) { 309 return 0; 310 } 311 ImportTree i1 = (ImportTree) o1; 312 ImportTree i2 = (ImportTree) o2; 313 314 return i1.toString().compareTo(i2.toString()); 315 } 316 317 public boolean equals(Object obj) { 318 return this == obj ? true : false; 319 } 320 }); 321 CompilationUnitTree unit = make.CompilationUnit( 322 node.getPackageName(), 323 imports, 324 node.getTypeDecls(), 325 node.getSourceFile() 326 ); 327 changes.rewrite(node, unit); 328 return null; 329 } 330 }); 331 assertFiles("testSort_ImportFormatTest.pass"); 332 } 333 334 String getGoldenPckg() { 335 return "org/netbeans/jmi/javamodel/codegen/indent/ImportFormatTest/"; 336 } 337 338 String getSourcePckg() { 339 return "org/netbeans/test/codegen/indent/imports/"; 340 } 341 342 private static Tree getParent(Tree t, ASTModel model) { 343 Tree root = model.getRoot(); 344 Tree[] tp = model.makePath(root, t); 345 if (tp.length == 0) 346 return null; if (tp.length == 1) { 348 assert t == root; 349 return t; 350 } 351 return tp[tp.length - 2]; 352 } 353 354 } | Popular Tags |