1 19 20 package org.netbeans.modules.junit.wizards; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import org.netbeans.api.java.project.JavaProjectConstants; 30 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 31 import org.netbeans.api.project.FileOwnerQuery; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.modules.junit.TestUtil; 36 import org.openide.ErrorManager; 37 import org.openide.cookies.EditorCookie; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.URLMapper; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.text.Line; 44 import org.openide.util.Utilities; 45 46 50 public final class Utils { 51 52 53 private final Project project; 54 55 private boolean sourceGroupsOnly = true; 56 57 private SourceGroup[] javaSourceGroups; 58 59 private Map <SourceGroup,Object []> sourcesToTestsMap; 60 61 private Map <FileObject,Object > foldersToSourceGroupsMap; 62 63 66 public Utils(Project project) { 67 this.project = project; 68 } 69 70 71 static FileObject findTestsRoot(Project project) { 72 final SourceGroup[] sourceGroups 73 = new Utils(project).getJavaSourceGroups(); 74 for (int i = 0; i < sourceGroups.length; i++) { 75 FileObject root = sourceGroups[i].getRootFolder(); 76 if (root.getName().equals(EmptyTestCaseWizard.TESTS_ROOT_NAME)) { 77 return root; 78 } 79 } 80 return null; 81 } 82 83 84 static FileObject getPackageFolder( 85 FileObject root, 86 String pkgName) throws IOException { 87 String relativePathName = pkgName.replace('.', '/'); 88 FileObject folder = root.getFileObject(relativePathName); 89 if (folder == null) { 90 folder = FileUtil.createFolder(root, relativePathName); 91 } 92 return folder; 93 } 94 95 109 static Collection getTestTargets(Project project, 110 final boolean sourceGroupsOnly) { 111 final Utils utils = new Utils(project); 112 return utils.getTestTargets(sourceGroupsOnly); 113 } 114 115 134 static Map getSourcesToTestsMap(Project project, 135 final boolean sourceGroupsOnly) { 136 final Utils utils = new Utils(project); 137 return utils.getSourcesToTestsMap(sourceGroupsOnly); 138 } 139 140 143 Project getProject() { 144 return project; 145 } 146 147 160 private Collection <Object > getTestTargets(final boolean sourceGroupsOnly) { 161 162 171 172 173 final SourceGroup[] sourceGroups = getJavaSourceGroups(); 174 if (sourceGroups.length == 0) { 175 return Collections.<Object >emptyList(); 176 } 177 178 179 createFoldersToSourceGroupsMap(sourceGroups); 180 Object testTargetsUnion[] = new Object [sourceGroups.length]; 181 int size = 0; 182 for (int i = 0; i < sourceGroups.length; i++) { 183 Object [] testTargets = getTestTargets(sourceGroups[i], 184 sourceGroupsOnly); 185 size = merge(testTargets, testTargetsUnion, size); 186 } 187 188 if (size != testTargetsUnion.length) { 189 testTargetsUnion = TestUtil.skipNulls(testTargetsUnion, new Object [0]); 190 } 191 192 return Collections.unmodifiableCollection( 193 Arrays.asList(testTargetsUnion)); 194 } 195 196 199 Map <SourceGroup,Object []> getSourcesToTestsMap() { 200 if (sourcesToTestsMap == null) { 201 sourcesToTestsMap = createSourcesToTestsMap(sourceGroupsOnly); 202 } 203 return sourcesToTestsMap; 204 } 205 206 209 Map <SourceGroup,Object []> getSourcesToTestsMap(final boolean sourceGroupsOnly) { 210 if (sourceGroupsOnly != this.sourceGroupsOnly) { 211 sourcesToTestsMap = null; 212 this.sourceGroupsOnly = sourceGroupsOnly; 213 } 214 return getSourcesToTestsMap(); 215 } 216 217 234 private Map <SourceGroup,Object []> createSourcesToTestsMap(final boolean sourceGroupsOnly) { 235 236 242 243 244 final SourceGroup[] sourceGroups = getJavaSourceGroups(); 245 if (sourceGroups.length == 0) { 246 return Collections.<SourceGroup,Object []>emptyMap(); 247 } 248 249 250 createFoldersToSourceGroupsMap(sourceGroups); 251 Object testTargetsUnion[] = new Object [sourceGroups.length]; 252 Map <SourceGroup,Object []> map; 253 map = new HashMap <SourceGroup,Object []>( 254 (int) ((float) sourceGroups.length * 1.33f + 0.5f), 255 .75f); 256 for (int i = 0; i < sourceGroups.length; i++) { 257 Object [] testTargets = getTestTargets(sourceGroups[i], 258 sourceGroupsOnly); 259 if (testTargets.length != 0) { 260 map.put(sourceGroups[i], testTargets); 261 } 262 } 263 if (map.isEmpty()) { 264 return Collections.<SourceGroup,Object []>emptyMap(); 265 } 266 if (map.size() == 1) { 267 Map.Entry <SourceGroup,Object []> entry 268 = map.entrySet().iterator().next(); 269 return Collections.singletonMap(entry.getKey(), entry.getValue()); 270 } 271 272 final int finalMapSize = map.size(); 273 if (finalMapSize >= (sourceGroups.length - 5)) { 274 return map; 275 } 276 277 final Map <SourceGroup,Object []> targetMap; 278 targetMap = new HashMap <SourceGroup,Object []>( 279 (int) ((float) finalMapSize * 1.25f + .5f), 280 .8f); 281 targetMap.putAll(map); 282 return targetMap; 283 } 284 285 312 private static int merge(final Object [] setToAdd, 313 final Object [] targetSet, 314 final int currTargetSetSize) { 315 if (setToAdd.length == 0) { 316 return currTargetSetSize; 317 } 318 if (currTargetSetSize == 0) { 319 System.arraycopy(setToAdd, 0, targetSet, 0, setToAdd.length); 320 return setToAdd.length; 321 } 322 int targetSetSize = currTargetSetSize; 323 toAdd: 324 for (int i = 0; i < setToAdd.length; i++) { 325 final Object objToAdd = setToAdd[i]; 326 for (int j = 0; j < targetSetSize; j++) { 327 final Object chosen = chooseTarget(targetSet[j], objToAdd); 328 if (chosen != null) { targetSet[j] = chosen; 330 continue toAdd; 331 } 332 } 333 targetSet[targetSetSize++] = objToAdd; 334 } 335 return targetSetSize; 336 } 337 338 358 private static Object chooseTarget(Object target1, Object target2) { 359 final boolean isGroup1 = target1 instanceof SourceGroup; 360 final boolean isGroup2 = target2 instanceof SourceGroup; 361 362 assert isGroup1 || (target1 instanceof FileObject); 363 assert isGroup2 || (target2 instanceof FileObject); 364 365 if (isGroup1 && isGroup2 && target1.equals(target2)) { 366 return target1; 367 } 368 369 final FileObject folder1 = isGroup1 370 ? ((SourceGroup) target1).getRootFolder() 371 : ((FileObject) target1); 372 final FileObject folder2 = isGroup2 373 ? ((SourceGroup) target2).getRootFolder() 374 : ((FileObject) target2); 375 if (!(folder1.isFolder())) { 376 throw new IllegalArgumentException ("target1: not a folder"); } 378 if (!(folder2.isFolder())) { 379 throw new IllegalArgumentException ("target2: not a folder"); } 381 if (folder1.equals(folder2)) { 382 return (isGroup1 == isGroup2) ? target1 383 : (isGroup1 ? target1 : target2); 384 } 385 return null; 386 } 387 388 407 public Object [] getTestTargets(SourceGroup sourceGroup, 408 final boolean sourceGroupsOnly) { 409 410 411 final FileObject[] testFolders 412 = getTestFoldersRaw(sourceGroup.getRootFolder()); 413 414 if (testFolders.length == 0) { 415 return new Object [0]; 416 } 417 418 419 final Object [] targets = new Object [testFolders.length]; 420 for (int i = 0; i < targets.length; i++) { 421 final FileObject testFolder = testFolders[i]; 422 if (testFolder == null) { 423 continue; 424 } 425 Object srcGroup = foldersToSourceGroupsMap.get(testFolder); 426 targets[i] = (srcGroup != null) 427 ? srcGroup 428 : sourceGroupsOnly ? null : testFolder; 429 } 430 return TestUtil.skipNulls(targets, new Object [0]); 431 } 432 433 443 public FileObject[] getTestFoldersRaw(FileObject srcFolder) { 444 return getFileObjects(UnitTestForSourceQuery.findUnitTests(srcFolder), 445 true); 446 } 447 448 458 public FileObject[] getSourceFoldersRaw(FileObject testFolder) { 459 return getFileObjects(UnitTestForSourceQuery.findSources(testFolder), 460 false); 461 } 462 463 474 private FileObject[] getFileObjects(final URL [] rootURLs, 475 final boolean srcToTest) { 476 if (rootURLs.length == 0) { 477 return new FileObject[0]; 478 } 479 480 FileObject[] sourceRoots = new FileObject[rootURLs.length]; 481 for (int i = 0; i < rootURLs.length; i++) { 482 if ((sourceRoots[i] = URLMapper.findFileObject(rootURLs[i])) 483 == null) { 484 final int severity = ErrorManager.INFORMATIONAL; 485 if (ErrorManager.getDefault().isLoggable(severity)) { 486 ErrorManager.getDefault().log( 487 severity, 488 (srcToTest ? "Test" : "Source") + " directory " + rootURLs[i] + " declared by project " + ProjectUtils.getInformation(project).getName() 492 + " does not exist."); } 494 continue; 495 } 496 Project sourceRootOwner = FileOwnerQuery.getOwner(sourceRoots[i]); 497 if (!project.equals(sourceRootOwner)) { 498 sourceRoots[i] = null; 499 500 int severity = ErrorManager.INFORMATIONAL; 501 if (ErrorManager.getDefault().isNotifiable(severity)) { 502 ErrorManager.getDefault().notify( 503 severity, 504 new IllegalStateException ( 505 "Malformed project: Found test root (" + rootURLs[i] + ')' + ' ' + 507 (sourceRootOwner == null 508 ? "does not belong to any" : "belongs to a different") + " project.")); } 512 continue; 513 } 514 } 515 return sourceRoots; 516 } 517 518 520 public static FileObject[] skipNulls(final FileObject[] fileObjs) { 521 if (fileObjs.length == 0) { 522 return fileObjs; 523 } 524 525 int nullsCount = 0;; 526 for (int i = 0; i < fileObjs.length; i++) { 527 if (fileObjs[i] == null) { 528 nullsCount++; 529 } 530 } 531 532 if (nullsCount == 0) { 533 return fileObjs; 534 } 535 if (nullsCount == fileObjs.length) { 536 return new FileObject[0]; 537 } 538 539 final FileObject[] fileObjsNew 540 = new FileObject[fileObjs.length - nullsCount]; 541 int index = 0, indexNew = 0; 542 while (indexNew < fileObjsNew.length) { 543 FileObject fileObj = fileObjs[index++]; 544 if (fileObj != null) { 545 fileObjsNew[indexNew++] = fileObj; 546 } 547 } 548 return fileObjsNew; 549 } 550 551 560 private void createFoldersToSourceGroupsMap( 561 final SourceGroup[] sourceGroups) { 562 Map <FileObject,Object > result; 563 564 if (sourceGroups.length == 0) { 565 result = Collections.<FileObject,Object >emptyMap(); 566 } else { 567 result = new HashMap <FileObject,Object >(2 * sourceGroups.length, 568 .5f); 569 for (SourceGroup sourceGroup : sourceGroups) { 570 result.put(sourceGroup.getRootFolder(), sourceGroup); 571 } 572 } 573 574 foldersToSourceGroupsMap = result; 575 } 576 577 580 public SourceGroup[] getJavaSourceGroups() { 581 if (javaSourceGroups == null) { 582 javaSourceGroups = ProjectUtils.getSources(project).getSourceGroups( 583 JavaProjectConstants.SOURCES_TYPE_JAVA); 584 } 585 return javaSourceGroups; 586 } 587 588 599 private static SourceGroup findSourceGroup(SourceGroup[] sourceGroups, 600 FileObject rootFolder) { 601 for (int i = 0; i < sourceGroups.length; i++) { 602 if (sourceGroups[i].getRootFolder().equals(rootFolder)) { 603 return sourceGroups[i]; 604 } 605 } 606 return (SourceGroup) null; 607 } 608 609 static boolean isValidClassName(String className) { 610 if (className.length() == 0) { 611 return false; 612 } 613 char[] chars = className.toCharArray(); 614 int segmentStart = 0; 615 int i; 616 for (i = 0; i < chars.length; i++) { 617 if (chars[i] == '.') { 618 if (i == segmentStart) { 619 return false; } 621 if (!Utilities.isJavaIdentifier( 622 className.substring(segmentStart, i))) { 623 return false; } 625 segmentStart = i + 1; 626 } 627 } 628 if (i == segmentStart) { 629 return false; } 631 if (!Utilities.isJavaIdentifier( 632 className.substring(segmentStart, chars.length))) { 633 return false; } 635 return true; 636 } 637 638 640 public static void openFile(FileObject file, int lineNum) { 641 642 646 647 if (file == null) { 648 java.awt.Toolkit.getDefaultToolkit().beep(); 649 return; 650 } 651 652 try { 653 DataObject dob = DataObject.find(file); 654 EditorCookie ed = (EditorCookie) 655 dob.getCookie(EditorCookie.class); 656 if (ed != null && 657 file == dob.getPrimaryFile()) { 658 if (lineNum == -1) { 659 ed.open(); 661 } else { 662 ed.openDocument(); try { 664 Line l = ed.getLineSet().getOriginal(lineNum - 1); 665 if (!l.isDeleted()) { 666 l.show(Line.SHOW_GOTO); 667 } 668 } catch (IndexOutOfBoundsException ioobe) { 669 ed.open(); 671 } 672 } 673 } else { 674 java.awt.Toolkit.getDefaultToolkit().beep(); 675 } 676 } catch (DataObjectNotFoundException ex1) { 677 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex1); 678 } catch (IOException ex2) { 679 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex2); 682 } 683 } 684 685 } 686 | Popular Tags |