1 19 package org.netbeans.jmi.javamodel.codegen.indent; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.SortedMap ; 32 import org.netbeans.jmi.javamodel.Import; 33 import org.netbeans.jmi.javamodel.JavaClass; 34 import org.netbeans.jmi.javamodel.JavaModelPackage; 35 import org.netbeans.jmi.javamodel.codegen.Utility; 36 import org.netbeans.junit.NbTestCase; 37 import org.netbeans.junit.NbTestSuite; 38 import org.openide.filesystems.FileStateInvalidException; 39 40 44 public class ImportFormatTest extends NbTestCase { 45 46 JavaClass clazz; 47 JavaModelPackage pkg; 48 49 50 public ImportFormatTest() { 51 super("ImportFormatTest"); 52 } 53 54 public static NbTestSuite suite() { 55 NbTestSuite suite = new NbTestSuite(ImportFormatTest.class); 56 return suite; 57 } 58 59 protected void setUp() { 60 clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.imports.ImportClass1"); 61 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 62 } 63 64 public void testFirstAddition() throws IOException , FileStateInvalidException { 65 boolean fail = true; 66 Utility.beginTrans(true); 67 try { 68 List imports = clazz.getResource().getImports(); 69 imports.add(createImport("java.util.List")); 70 imports.add(createImport("java.util.Set")); 71 fail = false; 72 } 73 finally { 74 Utility.endTrans(fail); 75 } 76 assertFile("File is not correctly generated.", 77 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 78 getGoldenFile("testFirstAddition_ImportFormatTest.pass"), 79 getWorkDir() 80 ); 81 } 82 83 public void testAddFirstImport() throws IOException , FileStateInvalidException { 84 boolean fail = true; 85 Utility.beginTrans(true); 86 try { 87 List imports = clazz.getResource().getImports(); 88 imports.add(0, createImport("java.util.AbstractList")); 89 fail = false; 90 } 91 finally { 92 Utility.endTrans(fail); 93 } 94 assertFile("File is not correctly generated.", 95 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 96 getGoldenFile("testAddFirstImport_ImportFormatTest.pass"), 97 getWorkDir() 98 ); 99 } 100 101 public void testAddLastImport() throws IOException , FileStateInvalidException { 102 boolean fail = true; 103 Utility.beginTrans(true); 104 try { 105 List imports = clazz.getResource().getImports(); 106 imports.add(createImport("java.io.IOException")); 107 fail = false; 108 } 109 finally { 110 Utility.endTrans(fail); 111 } 112 assertFile("File is not correctly generated.", 113 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 114 getGoldenFile("testAddLastImport_ImportFormatTest.pass"), 115 getWorkDir() 116 ); 117 } 118 119 public void testRemoveInnerImport() throws IOException , FileStateInvalidException { 120 boolean fail = true; 121 Utility.beginTrans(true); 122 try { 123 List imports = clazz.getResource().getImports(); 124 getLog().print(imports.size()); 125 imports.remove(1); 126 fail = false; 127 } 128 finally { 129 Utility.endTrans(fail); 130 } 131 assertFile("File is not correctly generated.", 132 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 133 getGoldenFile("testRemoveInnerImport_ImportFormatTest.pass"), 134 getWorkDir() 135 ); 136 } 137 138 public void testRemoveFirstImport() throws IOException , FileStateInvalidException { 139 boolean fail = true; 140 Utility.beginTrans(true); 141 try { 142 List imports = clazz.getResource().getImports(); 143 getLog().print(imports.size()); 144 imports.remove(0); 145 fail = false; 146 } 147 finally { 148 Utility.endTrans(fail); 149 } 150 assertFile("File is not correctly generated.", 151 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 152 getGoldenFile("testRemoveFirstImport_ImportFormatTest.pass"), 153 getWorkDir() 154 ); 155 } 156 157 public void testRemoveLastImport() throws IOException , FileStateInvalidException { 158 boolean fail = true; 159 Utility.beginTrans(true); 160 try { 161 List imports = clazz.getResource().getImports(); 162 getLog().print(imports.size()); 163 imports.remove(1); 164 fail = false; 165 } 166 finally { 167 Utility.endTrans(fail); 168 } 169 assertFile("File is not correctly generated.", 170 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 171 getGoldenFile("testRemoveLastImport_ImportFormatTest.pass"), 172 getWorkDir() 173 ); 174 } 175 176 public void testRemoveAllRemaning() throws IOException , FileStateInvalidException { 177 boolean fail = true; 178 Utility.beginTrans(true); 179 try { 180 List imports = clazz.getResource().getImports(); 181 getLog().print(imports.size()); 182 imports.clear(); 183 fail = false; 184 } 185 finally { 186 Utility.endTrans(fail); 187 } 188 assertFile("File is not correctly generated.", 189 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 190 getGoldenFile("testRemoveAllRemaning_ImportFormatTest.pass"), 191 getWorkDir() 192 ); 193 } 194 195 public void testAddSeveral() throws IOException , FileStateInvalidException { 196 boolean fail = true; 197 Utility.beginTrans(true); 198 try { 199 List imports = clazz.getResource().getImports(); 200 imports.add(createImport("java.util.List")); 201 imports.add(createImport("java.util.Set")); 202 imports.add(createImport("javax.swing.CellRendererPane")); 203 imports.add(createImport("javax.swing.BorderFactory")); 204 imports.add(createImport("javax.swing.ImageIcon")); 205 imports.add(createImport("javax.swing.InputVerifier")); 206 imports.add(createImport("javax.swing.GrayFilter")); 207 imports.add(createImport("javax.swing.JFileChooser")); 208 imports.add(createImport("javax.swing.AbstractAction")); 209 fail = false; 210 } 211 finally { 212 Utility.endTrans(fail); 213 } 214 assertFile("File is not correctly generated.", 215 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 216 getGoldenFile("testAddSeveral_ImportFormatTest.pass"), 217 getWorkDir() 218 ); 219 } 220 221 public void testRemoveInside() throws IOException , FileStateInvalidException { 222 boolean fail = true; 223 Utility.beginTrans(true); 224 try { 225 List imports = clazz.getResource().getImports(); 226 imports.remove(4); 227 fail = false; 228 } 229 finally { 230 Utility.endTrans(fail); 231 } 232 assertFile("File is not correctly generated.", 233 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 234 getGoldenFile("testRemoveInside_ImportFormatTest.pass"), 235 getWorkDir() 236 ); 237 } 238 239 public void testMoveFirst() throws IOException , FileStateInvalidException { 240 boolean fail = true; 241 Utility.beginTrans(true); 242 try { 243 List imports = clazz.getResource().getImports(); 244 Object first = imports.remove(0); 245 imports.add(3, first); 246 fail = false; 247 } 248 finally { 249 Utility.endTrans(fail); 250 } 251 assertFile("File is not correctly generated.", 252 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 253 getGoldenFile("testMoveFirst_ImportFormatTest.pass"), 254 getWorkDir() 255 ); 256 } 257 258 public void testMoveLast() throws IOException , FileStateInvalidException { 259 boolean fail = true; 260 Utility.beginTrans(true); 261 try { 262 List imports = clazz.getResource().getImports(); 263 Object o = imports.remove(7); 264 imports.add(1, o); 265 fail = false; 266 } 267 finally { 268 Utility.endTrans(fail); 269 } 270 assertFile("File is not correctly generated.", 271 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 272 getGoldenFile("testMoveLast_ImportFormatTest.pass"), 273 getWorkDir() 274 ); 275 } 276 277 public void testReplaceLine() throws IOException , FileStateInvalidException { 278 boolean fail = true; 279 Utility.beginTrans(true); 280 try { 281 List imports = clazz.getResource().getImports(); 282 Object first = imports.remove(4); 283 imports.add(4, createImport("java.util.Collection")); 284 fail = false; 285 } 286 finally { 287 Utility.endTrans(fail); 288 } 289 assertFile("File is not correctly generated.", 290 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 291 getGoldenFile("testReplaceLine_ImportFormatTest.pass"), 292 getWorkDir() 293 ); 294 } 295 296 public void testSort() throws IOException , FileStateInvalidException { 297 boolean fail = true; 298 Utility.beginTrans(true); 299 try { 300 List imports = clazz.getResource().getImports(); 301 List copy = new ArrayList (imports); 302 Collections.sort(copy, new Comparator () { 303 public int compare(Object o1, Object o2) { 304 if (o1 == o2) { 305 return 0; 306 } 307 Import i1 = (Import) o1; 308 Import i2 = (Import) o2; 309 310 return i1.getIdentifier().getType().getName().compareTo(i2.getIdentifier().getType().getName()); 311 } 312 313 public boolean equals(Object obj) { 314 return this == obj ? true : false; 315 } 316 }); 317 for (Iterator it = copy.iterator(); it.hasNext(); ) { 318 getLog().println(((Import) it.next()).getIdentifier().getType().getName()); 319 } 320 imports.clear(); 321 imports.addAll(copy); 322 fail = false; 323 } 324 finally { 325 Utility.endTrans(fail); 326 } 327 assertFile("File is not correctly generated.", 328 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"), 329 getGoldenFile("testSort_ImportFormatTest.pass"), 330 getWorkDir() 331 ); 332 } 333 334 private Import createImport(String importSymbol) { 336 return pkg.getImport().createImport(importSymbol, null, false, false); 338 } 339 } 340 | Popular Tags |