1 19 20 package org.netbeans.modules.java.freeform.ui; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.modules.java.freeform.JavaProjectGenerator; 33 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 34 import org.netbeans.spi.project.support.ant.PropertyProvider; 35 import org.netbeans.spi.project.support.ant.PropertyUtils; 36 37 42 public class ProjectModelTest extends NbTestCase { 43 44 public ProjectModelTest(String testName) { 45 super(testName); 46 } 47 48 public void testCreateEmptyModel() throws Exception { 49 File baseFolder = new File (getWorkDir(), "somefolder/"); 50 File nbProjectFolder = new File (getWorkDir(), "nbprojfolder/"); 51 Map <String ,String > p = new HashMap <String ,String >(); 52 p.put("key", "value"); 53 PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, PropertyUtils.fixedPropertyProvider(p)); 54 ProjectModel pm = ProjectModel.createEmptyModel(baseFolder, nbProjectFolder, evaluator); 55 assertNotNull(pm); 56 assertEquals("Base folder incorrect", baseFolder, pm.getBaseFolder()); 57 assertEquals("NB project folder incorrect", nbProjectFolder, pm.getNBProjectFolder()); 58 assertEquals("Evaluator incorrect", evaluator, pm.getEvaluator()); 59 assertEquals("Evaluator is different", 1, pm.getEvaluator().getProperties().size()); 60 } 61 62 public void testBasicFunctionality() throws Exception { 64 ProjectModel pm = createEmptyProjectModel(); 65 pm.setSourceLevel("custom_source_level"); 66 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 67 sf.location = "loc1"; 68 sf.label = "label_loc1"; 69 sf.type = "java"; 70 pm.addSourceFolder(sf, false); 71 assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount()); 72 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 73 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 74 List <ProjectModel.CompilationUnitKey> keys = generateKeys(new Object []{"loc1"}, new String []{"label_loc1"}); 75 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 76 JavaProjectGenerator.JavaCompilationUnit cu1 = pm.getJavaCompilationUnits().get(0); 77 JavaProjectGenerator.JavaCompilationUnit cu2 = pm.getCompilationUnit(keys.get(0), false); 78 assertEquals("Must be the same instance", cu1, cu2); 79 80 sf = new JavaProjectGenerator.SourceFolder(); 81 sf.location = "loc2"; 82 sf.label = "label_loc2"; 83 sf.type = "java"; 84 pm.addSourceFolder(sf, false); 85 assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount()); 86 assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size()); 87 assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size()); 88 keys = generateKeys(new Object []{"loc1", "loc2"}, new String []{"label_loc1", "label_loc2"}); 89 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 90 cu1 = pm.getJavaCompilationUnits().get(0); 91 cu2 = pm.getCompilationUnit(keys.get(0), false); 92 assertEquals("Must be the same instance", cu1, cu2); 93 cu1 = pm.getJavaCompilationUnits().get(1); 94 cu2 = pm.getCompilationUnit(keys.get(1), false); 95 assertEquals("Must be the same instance", cu1, cu2); 96 97 assertEquals("Source level does not match", "custom_source_level", 98 pm.getJavaCompilationUnits().get(0).sourceLevel); 99 assertEquals("Source level does not match", "custom_source_level", 100 pm.getJavaCompilationUnits().get(1).sourceLevel); 101 pm.setSourceLevel("jdk15"); 102 assertEquals("Source level does not match", "jdk15", 103 pm.getJavaCompilationUnits().get(0).sourceLevel); 104 assertEquals("Source level does not match", "jdk15", 105 pm.getJavaCompilationUnits().get(1).sourceLevel); 106 107 pm.removeSourceFolder(0); 108 assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount()); 109 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 110 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 111 keys = generateKeys(new Object []{"loc2"}, new String []{"label_loc2"}); 112 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 113 } 114 115 public void testAdvancedAddSourceFolder() throws Exception { 116 ProjectModel pm = createEmptyProjectModel(); 117 pm.setSourceLevel("jdk1x"); 118 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 119 sf.location = "loc1"; 120 sf.label = "label_loc1"; 121 sf.type = "java"; 122 pm.addSourceFolder(sf, false); 123 assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount()); 124 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 125 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 126 List keys = generateKeys(new Object []{"loc1"}, new String []{"label_loc1"}); 127 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 128 129 sf = new JavaProjectGenerator.SourceFolder(); 130 sf.location = "loc2"; 131 sf.label = "label_loc2"; 132 sf.type = "java"; 133 pm.addSourceFolder(sf, false); 134 assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount()); 135 assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size()); 136 assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size()); 137 keys = generateKeys(new Object []{"loc1", "loc2"}, new String []{"label_loc1", "label_loc2"}); 138 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 139 140 sf = new JavaProjectGenerator.SourceFolder(); 141 sf.location = "locWEB"; 142 sf.label = "label_locWEB"; 143 sf.type = "web"; 144 pm.addSourceFolder(sf, false); 145 assertEquals("Number of source folders does not match", 3, pm.getSourceFoldersCount()); 146 assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size()); 147 assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size()); 148 keys = generateKeys(new Object []{"loc1", "loc2"}, new String []{"label_loc1", "label_loc2"}); 149 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 150 151 pm.removeSourceFolder(2); 152 assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount()); 153 assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size()); 154 assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size()); 155 keys = generateKeys(new Object []{"loc1", "loc2"}, new String []{"label_loc1", "label_loc2"}); 156 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 157 158 pm.updateCompilationUnits(false); 159 assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount()); 160 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 161 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 162 keys = generateKeys(new Object []{Arrays.asList("loc1", "loc2")}, new String []{null}); 163 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 164 165 sf = new JavaProjectGenerator.SourceFolder(); 166 sf.location = "loc3"; 167 sf.label = "label_loc3"; 168 sf.type = "java"; 169 pm.addSourceFolder(sf, false); 170 assertEquals("Number of source folders does not match", 3, pm.getSourceFoldersCount()); 171 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 172 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 173 keys = generateKeys(new Object []{Arrays.asList("loc1", "loc2", "loc3")}, new String []{null}); 174 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 175 176 sf = new JavaProjectGenerator.SourceFolder(); 177 sf.location = "locWEB"; 178 sf.label = "label_locWEB"; 179 sf.type = "web"; 180 pm.addSourceFolder(sf, false); 181 assertEquals("Number of source folders does not match", 4, pm.getSourceFoldersCount()); 182 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 183 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 184 keys = generateKeys(new Object []{Arrays.asList("loc1", "loc2", "loc3")}, new String []{null}); 185 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 186 187 pm.removeSourceFolder(0); 188 pm.removeSourceFolder(0); 189 assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount()); 190 assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size()); 191 assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size()); 192 keys = generateKeys(new Object []{"loc3"}, new String []{"label_loc3"}); 193 assertKeyEquals(keys, pm.createCompilationUnitKeys()); 194 } 195 196 public void testCreateCompilationUnitKeys() throws Exception { 197 List <JavaProjectGenerator.SourceFolder> sources; 198 List <JavaProjectGenerator.JavaCompilationUnit> units; 199 List <ProjectModel.CompilationUnitKey> keys; 200 List <ProjectModel.CompilationUnitKey> createdKeys; 201 202 ProjectModel pm = createEmptyProjectModel(); 203 204 sources = generateSources(new String []{"src1", "src2", "src3"}); 207 units = new ArrayList <JavaProjectGenerator.JavaCompilationUnit>(); 208 keys = generateKeys(new Object []{"src1", "src2", "src3"}, new String []{"src1", "src2", "src3"}); 209 pm.setSourceFolders(sources); 210 pm.setJavaCompilationUnits(units); 211 createdKeys = pm.createCompilationUnitKeys(); 212 assertKeyEquals(keys, createdKeys); 213 214 sources = generateSources(new String []{"src1"}); 217 units = generateUnits(new Object []{"src1"}); 218 keys = generateKeys(new Object []{"src1"}, new String []{"src1"}); 219 pm.setSourceFolders(sources); 220 pm.setJavaCompilationUnits(units); 221 createdKeys = pm.createCompilationUnitKeys(); 222 assertKeyEquals(keys, createdKeys); 223 224 sources = generateSources(new String []{"src1", "src2"}); 227 units = generateUnits(new Object []{"src1", "src2"}); 228 keys = generateKeys(new Object []{"src1", "src2"}, new String []{"src1", "src2"}); 229 pm.setSourceFolders(sources); 230 pm.setJavaCompilationUnits(units); 231 createdKeys = pm.createCompilationUnitKeys(); 232 assertKeyEquals(keys, createdKeys); 233 234 sources = generateSources(new String []{"src1", "src2"}); 237 units = generateUnits(new Object []{Arrays.asList("src1", "src2")}); 238 keys = generateKeys(new Object []{Arrays.asList("src1", "src2")}, new String []{null}); 239 pm.setSourceFolders(sources); 240 pm.setJavaCompilationUnits(units); 241 createdKeys = pm.createCompilationUnitKeys(); 242 assertKeyEquals(keys, createdKeys); 243 244 sources = generateSources(new String []{"src1", "src2"}); 246 units = generateUnits(new Object []{"src3", "src4"}); 247 keys = generateKeys(new Object []{"src3", "src4", "src1", "src2"}, new String []{null, null, "src1", "src2"}); 249 pm.setSourceFolders(sources); 250 pm.setJavaCompilationUnits(units); 251 createdKeys = pm.createCompilationUnitKeys(); 252 assertKeyEquals(keys, createdKeys); 253 254 sources = generateSources(new String []{"src1", "src2"}); 255 units = generateUnits(new Object []{Arrays.asList("src2", "src3")}); 256 keys = generateKeys(new Object []{Arrays.asList("src2", "src3"), "src1"}, new String []{null, "src1"}); 258 pm.setSourceFolders(sources); 259 pm.setJavaCompilationUnits(units); 260 createdKeys = pm.createCompilationUnitKeys(); 261 assertKeyEquals(keys, createdKeys); 262 } 263 264 public void testUpdateCompilationUnits() throws Exception { 265 List sources; 266 List units = new ArrayList (); 267 List expectedUnits; 268 269 ProjectModel pm = createEmptyProjectModel(); 270 271 sources = generateSources(new String []{"src1", "src2"}); 272 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 273 cu.packageRoots = Collections.singletonList("src1"); 274 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 275 cp.classpath = "cp1"+File.pathSeparatorChar+"cp2"; 276 cp.mode = "compile"; 277 cu.classpath = Collections.singletonList(cp); 278 cu.output = Arrays.asList(new String []{"out1", "out2"}); 279 units.add(cu); 280 cu = new JavaProjectGenerator.JavaCompilationUnit(); 281 cu.packageRoots = Collections.singletonList("src2"); 282 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 283 cp.classpath = "cp2"+File.pathSeparatorChar+"cp3"; 284 cp.mode = "compile"; 285 cu.classpath = Collections.singletonList(cp); 286 cu.output = Arrays.asList(new String []{"out3"}); 287 units.add(cu); 288 pm.setSourceFolders(sources); 289 pm.setJavaCompilationUnits(units); 290 pm.setSourceLevel("S_L_14"); 291 pm.updateCompilationUnits(false); 292 assertEquals("Compilation units has to be merged into one", 1, units.size()); 293 cu = (JavaProjectGenerator.JavaCompilationUnit)units.get(0); 294 assertEquals("Compilation unit has to have two package roots", 2, cu.packageRoots.size()); 295 assertTrue("Missing expected package root: src1", cu.packageRoots.contains("src1")); 296 assertTrue("Missing expected package root: src2", cu.packageRoots.contains("src2")); 297 assertEquals("Compilation unit has to have three classpath items", 298 "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 299 cu.classpath.get(0).classpath); 300 assertEquals("Compilation unit has to have three output items", 3, cu.output.size()); 301 assertTrue("Missing expected package root: out1", cu.output.contains("out1")); 302 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 303 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 304 assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_14")); 305 306 pm.setSourceFolders(sources); 307 pm.setJavaCompilationUnits(units); 308 pm.setSourceLevel("S_L_15"); 309 pm.updateCompilationUnits(true); 310 assertEquals("Compilation units has to be cloned into two", 2, units.size()); 311 cu = (JavaProjectGenerator.JavaCompilationUnit)units.get(0); 312 assertEquals("Compilation unit has to have one package root", 1, cu.packageRoots.size()); 313 assertTrue("Missing expected package root", cu.packageRoots.contains("src1")); 314 assertEquals("Compilation unit has to have three classpath items", 315 "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 316 cu.classpath.get(0).classpath); 317 assertEquals("Compilation unit has to have three output items", 3, cu.output.size()); 318 assertTrue("Missing expected package root: out1", cu.output.contains("out1")); 319 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 320 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 321 assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_15")); 322 cu = (JavaProjectGenerator.JavaCompilationUnit)units.get(1); 323 assertEquals("Compilation unit has to have one package root", 1, cu.packageRoots.size()); 324 assertTrue("Missing expected package root", cu.packageRoots.contains("src2")); 325 assertEquals("Compilation unit has to have three classpath items", 326 "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 327 cu.classpath.get(0).classpath); 328 assertEquals("Compilation unit has to have three output items", 3, cu.output.size()); 329 assertTrue("Missing expected package root: out1", cu.output.contains("out1")); 330 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 331 assertTrue("Missing expected package root: out2", cu.output.contains("out2")); 332 assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_15")); 333 334 } 335 336 public void testUpdatePrincipalSourceFolders() throws Exception { 337 ProjectModel pm = createEmptyProjectModel(); 338 List <JavaProjectGenerator.SourceFolder> l = pm.getSourceFolders(); 339 List <JavaProjectGenerator.SourceFolder> l2 = pm.updatePrincipalSourceFolders(l, true); 341 assertEquals("Principal source for base directory must be added", 1, l2.size()); 342 l2 = pm.updatePrincipalSourceFolders(l, false); 343 assertEquals("There are no external java source folders", 0, l2.size()); 344 345 pm = createEmptyProjectModel(); 346 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 347 sf.location = "..\\something"; 348 sf.label = "something"; 349 sf.type = "java"; 350 l = pm.getSourceFolders(); 351 pm.addSourceFolder(sf, false); 352 l2 = pm.updatePrincipalSourceFolders(l, false); 353 assertEquals("One principal source must be added", 2, l2.size()); 354 355 pm = createEmptyProjectModel(); 356 JavaProjectGenerator.SourceFolder sf2 = new JavaProjectGenerator.SourceFolder(); 357 sf2.location = "..\\something2"; 358 sf2.label = "something2"; 359 sf2.type = "java"; 360 pm.addSourceFolder(sf, false); 361 pm.addSourceFolder(sf2, false); 362 pm.removeSourceFolder(0); 363 pm.removeSourceFolder(0); 364 pm.addSourceFolder(sf, false); 365 pm.addSourceFolder(sf2, false); 366 pm.removeSourceFolder(0); 367 pm.addSourceFolder(sf, false); 368 l = pm.getSourceFolders(); 369 l2 = pm.updatePrincipalSourceFolders(l, false); 370 assertEquals("Two principal sources must be added", 4, l2.size()); 371 JavaProjectGenerator.SourceFolder addedSF = l2.get(2); 372 assertEquals("Added principal source must have the same label", addedSF.label, sf.label); 373 assertEquals("Added principal source must have the same location", addedSF.location, sf.location); 374 assertNull("Added principal source must have type==null", addedSF.type); 375 addedSF = l2.get(3); 376 assertEquals("Added principal source must have the same label", addedSF.label, sf2.label); 377 assertEquals("Added principal source must have the same location", addedSF.location, sf2.location); 378 assertNull("Added principal source must have type==null", addedSF.type); 379 pm.removeSourceFolder(0); 380 l = pm.getSourceFolders(); 381 l2 = pm.updatePrincipalSourceFolders(l, false); 382 assertEquals("One principal source must be removed", 2, l2.size()); 383 384 JavaProjectGenerator.SourceFolder sf2_ = new JavaProjectGenerator.SourceFolder(); 385 sf2_.location = "..\\something2"; 386 sf2_.label = "something2"; 387 sf2_.type = null; 388 pm = createEmptyProjectModel(); 389 pm.addSourceFolder(sf2, false); 390 pm.addSourceFolder(sf2_, false); 391 l = pm.getSourceFolders(); 392 l2 = pm.updatePrincipalSourceFolders(l, false); 393 assertEquals("No principal sources added in this case because it already exist", l.size(), l2.size()); 394 } 395 396 private List <JavaProjectGenerator.SourceFolder> generateSources(String [] locations) { 397 List <JavaProjectGenerator.SourceFolder> l = new ArrayList <JavaProjectGenerator.SourceFolder>(locations.length); 398 for (String loc : locations) { 399 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 400 sf.location = loc; 401 sf.label = loc; 402 sf.type = "java"; 403 l.add(sf); 404 } 405 return l; 406 } 407 408 411 private List generateUnits(Object [] locations) { 412 List l = new ArrayList (locations.length); 413 for (int i=0; i<locations.length; i++) { 414 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 415 if (locations[i] instanceof List ) { cu.packageRoots = (List )locations[i]; 417 } else { 418 cu.packageRoots = Collections.singletonList((String ) locations[i]); 419 } 420 l.add(cu); 421 } 422 return l; 423 } 424 425 428 private List <ProjectModel.CompilationUnitKey> generateKeys(Object [] locations, String [] labels) { 429 List <ProjectModel.CompilationUnitKey> l = new ArrayList <ProjectModel.CompilationUnitKey>(locations.length); 430 for (int i=0; i<locations.length; i++) { 431 ProjectModel.CompilationUnitKey key = new ProjectModel.CompilationUnitKey(); 432 if (locations[i] instanceof List ) { 433 key.locations = (List )locations[i]; 434 } else { 435 key.locations = Collections.singletonList((String ) locations[i]); 436 } 437 key.label = labels[i]; 438 l.add(key); 439 } 440 return l; 441 } 442 443 private void assertKeyEquals(List <ProjectModel.CompilationUnitKey> l1, List <ProjectModel.CompilationUnitKey> l2) throws Exception { 444 String param = "Keys do not match: Expected: "+l1+" Result:"+l2; assertEquals(param, l1, l2); 446 Iterator <ProjectModel.CompilationUnitKey> i1 = l1.iterator(); 447 Iterator <ProjectModel.CompilationUnitKey> i2 = l2.iterator(); 448 while (i1.hasNext()) { 449 ProjectModel.CompilationUnitKey k1 = i1.next(); 450 ProjectModel.CompilationUnitKey k2 = i2.next(); 451 assertEquals(param, k1.label, k2.label); 452 } 453 } 454 455 private ProjectModel createEmptyProjectModel() throws Exception { 456 File baseFolder = new File (getWorkDir(), "somefolder/"); 457 File nbProjectFolder = new File (getWorkDir(), "nbprojfolder/"); 458 Map <String ,String > p = new HashMap <String ,String >(); 459 p.put("key", "value"); 460 PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, PropertyUtils.fixedPropertyProvider(p)); 461 return ProjectModel.createEmptyModel(baseFolder, nbProjectFolder, evaluator); 462 } 463 464 } 465 | Popular Tags |