001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.buildpath;
011:
012: import java.io.ByteArrayInputStream;
013: import java.lang.reflect.InvocationTargetException;
014: import java.util.ArrayList;
015: import java.util.Arrays;
016: import java.util.List;
017:
018: import junit.framework.TestCase;
019:
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IPath;
022: import org.eclipse.core.runtime.Path;
023:
024: import org.eclipse.core.resources.IFile;
025: import org.eclipse.core.resources.IFolder;
026: import org.eclipse.core.resources.IResource;
027:
028: import org.eclipse.jface.action.IAction;
029: import org.eclipse.jface.operation.IRunnableContext;
030: import org.eclipse.jface.viewers.ISelection;
031: import org.eclipse.jface.viewers.ISelectionChangedListener;
032: import org.eclipse.jface.viewers.ISelectionProvider;
033: import org.eclipse.jface.viewers.SelectionChangedEvent;
034: import org.eclipse.jface.viewers.StructuredSelection;
035:
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.part.ISetSelectionTarget;
038:
039: import org.eclipse.jdt.core.IClasspathEntry;
040: import org.eclipse.jdt.core.ICompilationUnit;
041: import org.eclipse.jdt.core.IJavaProject;
042: import org.eclipse.jdt.core.IPackageFragment;
043: import org.eclipse.jdt.core.IPackageFragmentRoot;
044: import org.eclipse.jdt.core.JavaCore;
045: import org.eclipse.jdt.core.JavaModelException;
046:
047: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
048:
049: import org.eclipse.jdt.ui.PreferenceConstants;
050:
051: import org.eclipse.jdt.internal.ui.util.CoreUtility;
052: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.AddFolderToBuildpathAction;
053: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.AddSelectedLibraryToBuildpathAction;
054: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.BuildpathModifierAction;
055: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.CreateLinkedSourceFolderAction;
056: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.EditFilterAction;
057: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.EditOutputFolderAction;
058: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ExcludeFromBuildpathAction;
059: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.IncludeToBuildpathAction;
060: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.RemoveFromBuildpathAction;
061:
062: import org.eclipse.jdt.testplugin.JavaProjectHelper;
063:
064: public class BuildpathModifierActionEnablementTest extends TestCase {
065:
066: public static final Class THIS = BuildpathModifierActionEnablementTest.class;
067:
068: private BuildpathModifierAction[] fActions;
069:
070: private AddFolderToBuildpathAction fAddFolderToBuildpathAction;
071: private RemoveFromBuildpathAction fRemoveFromBuildpathAction;
072: private ExcludeFromBuildpathAction fExcludeFromBuildpathAction;
073: private IncludeToBuildpathAction fIncludeToBuildpathAction;
074: private EditFilterAction fEditFilterAction;
075: private EditOutputFolderAction fEditOutputFolderAction;
076: private CreateLinkedSourceFolderAction fCreateLinkedSourceFolderAction;
077:
078: private IJavaProject fProject;
079: private IPackageFragmentRoot fSourceFolder;
080: private IPackageFragment fDefaultPackage;
081: private IFolder fFolder;
082: private IFile fFile;
083: private IPackageFragment fPackage;
084: private IResource fExcludedPackage;
085: private ICompilationUnit fCompilationUnit;
086: private IResource fExcludedFile;
087: private IPackageFragmentRoot fLibrary;
088: private IFile fExcludedLibrary;
089:
090: /* ### Project Structure:
091: * - DummyProject
092: * |- src
093: * |- default package
094: * |- pack1
095: * |- A.java
096: * |- B.java (excluded)
097: * |- NormalFile
098: * |- pack2 (excluded)
099: * |- archive.jar (on buildpath)
100: * |- archive.zip (excluded)
101: * |- NormalFolder
102: */
103:
104: public BuildpathModifierActionEnablementTest() {
105: super (THIS.getName());
106: }
107:
108: protected void setUp() throws Exception {
109: fActions = createActions();
110: fProject = createProject();
111: assertFalse(fProject.isOnClasspath(fProject
112: .getUnderlyingResource()));
113: }
114:
115: private BuildpathModifierAction[] createActions() {
116: ISetSelectionTarget nullSelectionTarget = new ISetSelectionTarget() {
117: public void selectReveal(ISelection selection) {
118: }
119: };
120:
121: IRunnableContext context = PlatformUI.getWorkbench()
122: .getProgressService();
123:
124: fAddFolderToBuildpathAction = new AddFolderToBuildpathAction(
125: context, nullSelectionTarget);
126: fRemoveFromBuildpathAction = new RemoveFromBuildpathAction(
127: context, nullSelectionTarget);
128: fExcludeFromBuildpathAction = new ExcludeFromBuildpathAction(
129: context, nullSelectionTarget);
130: fIncludeToBuildpathAction = new IncludeToBuildpathAction(
131: context, nullSelectionTarget);
132: fEditFilterAction = new EditFilterAction(context,
133: nullSelectionTarget);
134: fEditOutputFolderAction = new EditOutputFolderAction(context,
135: nullSelectionTarget);
136: fCreateLinkedSourceFolderAction = new CreateLinkedSourceFolderAction(
137: context, nullSelectionTarget);
138:
139: return new BuildpathModifierAction[] {
140: fAddFolderToBuildpathAction,
141: fRemoveFromBuildpathAction,
142: fExcludeFromBuildpathAction, fIncludeToBuildpathAction,
143: fEditFilterAction, fEditOutputFolderAction,
144: fCreateLinkedSourceFolderAction };
145: }
146:
147: protected void tearDown() throws Exception {
148: fProject.getProject().delete(true, true, null);
149: }
150:
151: private void assertOnlyEnabled(IAction[] enabledActions) {
152: for (int i = 0; i < fActions.length; i++) {
153: if (fActions[i].isEnabled()) {
154: assertTrue(fActions[i].getText()
155: + " is enabled but should not be.", contains(
156: enabledActions, fActions[i]));
157: } else {
158: assertTrue(fActions[i].getText()
159: + " is disabled but should not be.", !contains(
160: enabledActions, fActions[i]));
161: }
162: }
163: }
164:
165: private boolean contains(IAction[] actions, IAction action) {
166: for (int i = 0; i < actions.length; i++) {
167: if (actions[i] == action)
168: return true;
169: }
170: return false;
171: }
172:
173: private void assertAllDisabled() {
174: for (int i = 0; i < fActions.length; i++) {
175: if (fActions[i].isEnabled())
176: assertTrue(fActions[i].getText()
177: + " is enabled but should not be.", false);
178: }
179: }
180:
181: public void testProjectWithOthers() throws JavaModelException {
182: select(new Object[] { fProject });
183: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction,
184: fCreateLinkedSourceFolderAction });
185:
186: select(new Object[] { fProject, fSourceFolder });
187: assertAllDisabled();
188:
189: select(new Object[] { fProject, fFolder });
190: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
191:
192: select(new Object[] { fProject, fPackage });
193: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
194:
195: select(new Object[] { fProject, fCompilationUnit });
196: assertAllDisabled();
197:
198: select(new Object[] { fProject, fExcludedFile });
199: assertAllDisabled();
200:
201: select(new Object[] { fProject, fFile });
202: assertAllDisabled();
203:
204: select(new Object[] { fProject, fExcludedPackage });
205: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
206:
207: select(new Object[] { fProject, fDefaultPackage });
208: assertAllDisabled();
209:
210: select(new Object[] { fProject, fLibrary });
211: assertAllDisabled();
212:
213: select(new Object[] { fProject, fExcludedLibrary });
214: assertAllDisabled();
215:
216: select(new Object[] { fProject, fSourceFolder, fFolder });
217: assertAllDisabled();
218:
219: select(new Object[] { fProject, fSourceFolder, fExcludedLibrary });
220: assertAllDisabled();
221:
222: select(new Object[] { fProject, fFolder, fExcludedLibrary });
223: assertAllDisabled();
224:
225: select(new Object[] { fProject, fFolder, fLibrary });
226: assertAllDisabled();
227:
228: select(new Object[] { fProject, fLibrary, fExcludedLibrary });
229: assertAllDisabled();
230:
231: select(new Object[] { fProject, fPackage, fFolder });
232: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
233:
234: select(new Object[] { fProject, fPackage, fFolder,
235: fDefaultPackage });
236: assertAllDisabled();
237:
238: select(new Object[] { fProject, fPackage, fFolder,
239: fExcludedPackage });
240: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
241:
242: select(new Object[] { fProject, fPackage, fFolder,
243: fExcludedPackage, fExcludedLibrary });
244: assertAllDisabled();
245:
246: select(new Object[] { fProject, fPackage, fFolder,
247: fExcludedPackage, fLibrary });
248: assertAllDisabled();
249:
250: select(new Object[] { fProject, fPackage, fFolder,
251: fExcludedPackage, fDefaultPackage });
252: assertAllDisabled();
253:
254: select(new Object[] { fProject, fPackage, fFolder,
255: fCompilationUnit });
256: assertAllDisabled();
257:
258: select(new Object[] { fProject, fPackage, fFolder, fLibrary });
259: assertAllDisabled();
260:
261: select(new Object[] { fProject, fPackage, fFolder,
262: fExcludedLibrary });
263: assertAllDisabled();
264:
265: select(new Object[] { fProject, fPackage, fExcludedPackage,
266: fExcludedLibrary });
267: assertAllDisabled();
268:
269: select(new Object[] { fProject, fPackage, fExcludedPackage,
270: fLibrary });
271: assertAllDisabled();
272:
273: select(new Object[] { fProject, fSourceFolder, fPackage,
274: fFolder, fCompilationUnit });
275: assertAllDisabled();
276:
277: select(new Object[] { fProject, fPackage, fFolder,
278: fExcludedPackage, fCompilationUnit });
279: assertAllDisabled();
280:
281: select(new Object[] { fProject, fPackage, fFolder,
282: fExcludedPackage, fCompilationUnit, fDefaultPackage });
283: assertAllDisabled();
284:
285: select(new Object[] { fProject, fPackage, fFolder,
286: fExcludedPackage, fExcludedFile });
287: assertAllDisabled();
288:
289: select(new Object[] { fProject, fPackage, fFolder,
290: fExcludedPackage, fExcludedFile, fDefaultPackage });
291: assertAllDisabled();
292:
293: select(new Object[] { fProject, fPackage, fFolder,
294: fExcludedPackage, fFile });
295: assertAllDisabled();
296:
297: select(new Object[] { fProject, fPackage, fFolder,
298: fExcludedPackage, fFile, fDefaultPackage });
299: assertAllDisabled();
300:
301: select(new Object[] { fProject, fPackage, fFolder,
302: fExcludedPackage, fCompilationUnit, fExcludedFile });
303: assertAllDisabled();
304:
305: select(new Object[] { fProject, fPackage, fFolder,
306: fExcludedPackage, fCompilationUnit, fDefaultPackage });
307: assertAllDisabled();
308:
309: select(new Object[] { fProject, fPackage, fFolder,
310: fExcludedPackage, fCompilationUnit, fFile });
311: assertAllDisabled();
312:
313: select(new Object[] { fProject, fPackage, fFolder,
314: fExcludedPackage, fCompilationUnit, fFile,
315: fDefaultPackage });
316: assertAllDisabled();
317:
318: select(new Object[] { fProject, fPackage, fFolder,
319: fExcludedPackage, fExcludedFile, fFile });
320: assertAllDisabled();
321:
322: select(new Object[] { fProject, fPackage, fFolder,
323: fExcludedPackage, fExcludedFile, fFile, fDefaultPackage });
324: assertAllDisabled();
325:
326: select(new Object[] { fProject, fPackage, fFolder,
327: fExcludedPackage, fCompilationUnit, fFile,
328: fExcludedFile });
329: assertAllDisabled();
330:
331: select(new Object[] { fProject, fPackage, fFolder,
332: fExcludedPackage, fCompilationUnit, fFile,
333: fExcludedFile, fDefaultPackage });
334: assertAllDisabled();
335:
336: select(new Object[] { fProject, fPackage, fFolder,
337: fExcludedPackage, fCompilationUnit, fFile,
338: fExcludedFile, fDefaultPackage, fLibrary,
339: fExcludedLibrary });
340: assertAllDisabled();
341: }
342:
343: public void testSrcWithOthers() throws JavaModelException {
344: select(new Object[] { fSourceFolder });
345: assertOnlyEnabled(new IAction[] { fRemoveFromBuildpathAction,
346: fEditFilterAction });
347:
348: select(new Object[] { fSourceFolder, fFolder });
349: assertAllDisabled();
350:
351: select(new Object[] { fSourceFolder, fPackage });
352: assertAllDisabled();
353:
354: select(new Object[] { fSourceFolder, fCompilationUnit });
355: assertAllDisabled();
356:
357: select(new Object[] { fSourceFolder, fExcludedFile });
358: assertAllDisabled();
359:
360: select(new Object[] { fSourceFolder, fFile });
361: assertAllDisabled();
362:
363: select(new Object[] { fSourceFolder, fExcludedPackage });
364: assertAllDisabled();
365:
366: select(new Object[] { fSourceFolder, fDefaultPackage });
367: assertAllDisabled();
368:
369: select(new Object[] { fSourceFolder, fExcludedLibrary });
370: assertAllDisabled();
371:
372: select(new Object[] { fSourceFolder, fLibrary });
373: assertOnlyEnabled(new IAction[] { fRemoveFromBuildpathAction });
374:
375: select(new Object[] { fSourceFolder, fLibrary, fFolder });
376: assertAllDisabled();
377:
378: select(new Object[] { fSourceFolder, fLibrary, fPackage });
379: assertAllDisabled();
380:
381: select(new Object[] { fSourceFolder, fLibrary, fExcludedPackage });
382: assertAllDisabled();
383:
384: select(new Object[] { fSourceFolder, fFolder, fPackage,
385: fCompilationUnit, fExcludedFile, fFile,
386: fExcludedPackage, fDefaultPackage, fLibrary,
387: fExcludedLibrary });
388: assertAllDisabled();
389: }
390:
391: public void testNormalFolderWithOthers() throws JavaModelException {
392: select(new Object[] { fFolder });
393: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
394:
395: select(new Object[] { fFolder, fPackage });
396: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
397:
398: select(new Object[] { fFolder, fCompilationUnit });
399: assertAllDisabled();
400:
401: select(new Object[] { fFolder, fExcludedFile });
402: assertAllDisabled();
403:
404: select(new Object[] { fFolder, fFile });
405: assertAllDisabled();
406:
407: select(new Object[] { fFolder, fExcludedPackage });
408: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
409:
410: select(new Object[] { fFolder, fDefaultPackage });
411: assertAllDisabled();
412:
413: select(new Object[] { fFolder, fLibrary });
414: assertAllDisabled();
415:
416: select(new Object[] { fFolder, fExcludedLibrary });
417: assertAllDisabled();
418:
419: select(new Object[] { fFolder, fPackage, fCompilationUnit });
420: assertAllDisabled();
421:
422: select(new Object[] { fFolder, fPackage, fFile });
423: assertAllDisabled();
424:
425: select(new Object[] { fFolder, fPackage, fExcludedPackage });
426: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
427:
428: select(new Object[] { fFolder, fPackage, fExcludedLibrary });
429: assertAllDisabled();
430:
431: select(new Object[] { fFolder, fPackage, fLibrary });
432: assertAllDisabled();
433:
434: select(new Object[] { fFolder, fPackage, fCompilationUnit,
435: fFile });
436: assertAllDisabled();
437:
438: select(new Object[] { fFolder, fPackage, fExcludedPackage,
439: fDefaultPackage });
440: assertAllDisabled();
441:
442: select(new Object[] { fFolder, fPackage, fExcludedPackage,
443: fExcludedFile });
444: assertAllDisabled();
445:
446: select(new Object[] { fFolder, fPackage, fExcludedPackage,
447: fExcludedFile });
448: assertAllDisabled();
449:
450: select(new Object[] { fFolder, fPackage, fExcludedFile,
451: fLibrary });
452: assertAllDisabled();
453:
454: select(new Object[] { fFolder, fPackage, fExcludedFile,
455: fExcludedLibrary });
456: assertAllDisabled();
457:
458: select(new Object[] { fFolder, fPackage, fExcludedPackage,
459: fExcludedLibrary });
460: assertAllDisabled();
461:
462: select(new Object[] { fFolder, fPackage, fExcludedPackage,
463: fLibrary });
464: assertAllDisabled();
465: }
466:
467: public void testPackageWithOthers() throws JavaModelException {
468: select(new Object[] { fPackage });
469: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction,
470: fExcludeFromBuildpathAction });
471:
472: select(new Object[] { fPackage, fCompilationUnit });
473: assertOnlyEnabled(new IAction[] { fExcludeFromBuildpathAction });
474:
475: select(new Object[] { fPackage, fExcludedFile });
476: assertAllDisabled();
477:
478: select(new Object[] { fPackage, fFile });
479: assertAllDisabled();
480:
481: select(new Object[] { fPackage, fExcludedPackage });
482: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction });
483:
484: select(new Object[] { fPackage, fDefaultPackage });
485: assertAllDisabled();
486:
487: select(new Object[] { fPackage, fLibrary });
488: assertAllDisabled();
489:
490: select(new Object[] { fPackage, fExcludedLibrary });
491: assertAllDisabled();
492:
493: select(new Object[] { fPackage, fExcludedPackage,
494: fDefaultPackage });
495: assertAllDisabled();
496:
497: select(new Object[] { fPackage, fExcludedPackage, fLibrary });
498: assertAllDisabled();
499:
500: select(new Object[] { fPackage, fExcludedPackage,
501: fExcludedLibrary });
502: assertAllDisabled();
503:
504: select(new Object[] { fPackage, fExcludedPackage,
505: fCompilationUnit, fFile, fExcludedFile });
506: assertAllDisabled();
507:
508: select(new Object[] { fPackage, fExcludedPackage,
509: fCompilationUnit, fFile, fLibrary });
510: assertAllDisabled();
511:
512: select(new Object[] { fPackage, fExcludedPackage,
513: fCompilationUnit, fFile, fExcludedLibrary });
514: assertAllDisabled();
515:
516: select(new Object[] { fPackage, fExcludedPackage,
517: fCompilationUnit, fFile, fExcludedFile, fDefaultPackage });
518: assertAllDisabled();
519:
520: select(new Object[] { fPackage, fExcludedPackage,
521: fCompilationUnit, fFile, fExcludedFile,
522: fDefaultPackage, fLibrary });
523: assertAllDisabled();
524:
525: select(new Object[] { fPackage, fExcludedPackage,
526: fCompilationUnit, fFile, fExcludedFile,
527: fDefaultPackage, fExcludedLibrary });
528: assertAllDisabled();
529:
530: select(new Object[] { fPackage, fExcludedPackage,
531: fCompilationUnit, fFile, fExcludedFile,
532: fDefaultPackage, fLibrary, fExcludedLibrary });
533: assertAllDisabled();
534: }
535:
536: public void testCUWithOthers() throws JavaModelException {
537: select(new Object[] { fCompilationUnit });
538: assertOnlyEnabled(new IAction[] { fExcludeFromBuildpathAction });
539:
540: select(new Object[] { fCompilationUnit, fPackage });
541: assertOnlyEnabled(new IAction[] { fExcludeFromBuildpathAction });
542:
543: select(new Object[] { fCompilationUnit, fExcludedFile });
544: assertAllDisabled();
545:
546: select(new Object[] { fCompilationUnit, fFile });
547: assertAllDisabled();
548:
549: select(new Object[] { fCompilationUnit, fExcludedPackage });
550: assertAllDisabled();
551:
552: select(new Object[] { fCompilationUnit, fDefaultPackage });
553: assertAllDisabled();
554:
555: select(new Object[] { fCompilationUnit, fLibrary });
556: assertAllDisabled();
557:
558: select(new Object[] { fCompilationUnit, fExcludedLibrary });
559: assertAllDisabled();
560:
561: select(new Object[] { fCompilationUnit, fExcludedPackage,
562: fPackage });
563: assertAllDisabled();
564:
565: select(new Object[] { fCompilationUnit, fExcludedPackage,
566: fLibrary });
567: assertAllDisabled();
568:
569: select(new Object[] { fCompilationUnit, fExcludedPackage,
570: fExcludedLibrary });
571: assertAllDisabled();
572:
573: select(new Object[] { fCompilationUnit, fPackage, fLibrary });
574: assertAllDisabled();
575:
576: select(new Object[] { fCompilationUnit, fPackage,
577: fExcludedLibrary });
578: assertAllDisabled();
579:
580: select(new Object[] { fCompilationUnit, fExcludedFile, fFile,
581: fExcludedPackage, fDefaultPackage, fLibrary,
582: fExcludedLibrary });
583: assertAllDisabled();
584: }
585:
586: public void testExcludedFileWithOthers() throws JavaModelException {
587: select(new Object[] { fExcludedFile });
588: assertOnlyEnabled(new IAction[] { fIncludeToBuildpathAction });
589:
590: select(new Object[] { fExcludedFile, fExcludedPackage });
591: assertOnlyEnabled(new IAction[] { fIncludeToBuildpathAction });
592:
593: select(new Object[] { fExcludedFile, fFile });
594: assertAllDisabled();
595:
596: select(new Object[] { fExcludedFile, fExcludedPackage });
597: assertOnlyEnabled(new IAction[] { fIncludeToBuildpathAction });
598:
599: select(new Object[] { fExcludedFile, fDefaultPackage });
600: assertAllDisabled();
601:
602: select(new Object[] { fExcludedFile, fLibrary });
603: assertAllDisabled();
604:
605: select(new Object[] { fExcludedFile, fExcludedLibrary });
606: assertAllDisabled();
607:
608: select(new Object[] { fExcludedFile, fExcludedPackage,
609: fDefaultPackage });
610: assertAllDisabled();
611:
612: select(new Object[] { fExcludedFile, fExcludedPackage, fLibrary });
613: assertAllDisabled();
614:
615: select(new Object[] { fExcludedFile, fExcludedPackage,
616: fExcludedLibrary });
617: assertAllDisabled();
618: }
619:
620: public void testFileWithOthers() throws JavaModelException {
621: select(new Object[] { fFile });
622: assertAllDisabled();
623:
624: select(new Object[] { fFile, fExcludedPackage });
625: assertAllDisabled();
626:
627: select(new Object[] { fFile, fDefaultPackage });
628: assertAllDisabled();
629:
630: select(new Object[] { fFile, fLibrary });
631: assertAllDisabled();
632:
633: select(new Object[] { fFile, fExcludedLibrary });
634: assertAllDisabled();
635:
636: select(new Object[] { fFile, fExcludedPackage, fLibrary,
637: fExcludedLibrary });
638: assertAllDisabled();
639: }
640:
641: public void testExcludedPackWithOthers() throws JavaModelException {
642: select(new Object[] { fExcludedPackage });
643: assertOnlyEnabled(new IAction[] { fAddFolderToBuildpathAction,
644: fIncludeToBuildpathAction });
645:
646: select(new Object[] { fExcludedPackage, fDefaultPackage });
647: assertAllDisabled();
648:
649: select(new Object[] { fExcludedPackage, fLibrary });
650: assertAllDisabled();
651:
652: select(new Object[] { fExcludedPackage, fExcludedLibrary });
653: assertAllDisabled();
654:
655: select(new Object[] { fExcludedPackage, fDefaultPackage,
656: fLibrary });
657: assertAllDisabled();
658:
659: select(new Object[] { fExcludedPackage, fDefaultPackage,
660: fExcludedLibrary });
661: assertAllDisabled();
662:
663: select(new Object[] { fExcludedPackage, fLibrary,
664: fExcludedLibrary });
665: assertAllDisabled();
666:
667: select(new Object[] { fExcludedPackage, fDefaultPackage,
668: fLibrary, fExcludedLibrary });
669: assertAllDisabled();
670: }
671:
672: public void testDefaultPackWithOthers() throws JavaModelException {
673: select(new Object[] { fDefaultPackage });
674: assertAllDisabled();
675:
676: select(new Object[] { fDefaultPackage, fLibrary });
677: assertAllDisabled();
678:
679: select(new Object[] { fDefaultPackage, fExcludedLibrary });
680: assertAllDisabled();
681:
682: select(new Object[] { fDefaultPackage, fLibrary,
683: fExcludedLibrary });
684: assertAllDisabled();
685: }
686:
687: public void testDefaultJARWithOthers() throws JavaModelException {
688: select(new Object[] { fLibrary });
689: assertOnlyEnabled(new IAction[] { fRemoveFromBuildpathAction });
690:
691: select(new Object[] { fLibrary, fExcludedLibrary });
692: assertAllDisabled();
693: }
694:
695: public void testDefaultZipWithOthers() throws JavaModelException,
696: InvocationTargetException {
697: select(new Object[] { fExcludedLibrary });
698: assertAllDisabled();
699:
700: final IPackageFragmentRoot[] addedZipArchive = { null };
701: AddSelectedLibraryToBuildpathAction add = new AddSelectedLibraryToBuildpathAction(
702: PlatformUI.getWorkbench().getProgressService(),
703: new ISetSelectionTarget() {
704: public void selectReveal(ISelection selection) {
705: addedZipArchive[0] = (IPackageFragmentRoot) ((StructuredSelection) selection)
706: .getFirstElement();
707: }
708: });
709: add.selectionChanged(new SelectionChangedEvent(
710: new ISelectionProvider() {
711: public void addSelectionChangedListener(
712: ISelectionChangedListener listener) {
713: }
714:
715: public ISelection getSelection() {
716: return new StructuredSelection(fExcludedLibrary);
717: }
718:
719: public void removeSelectionChangedListener(
720: ISelectionChangedListener listener) {
721: }
722:
723: public void setSelection(ISelection selection) {
724: }
725: }, new StructuredSelection(fExcludedLibrary)));
726: add.run();
727:
728: select(new StructuredSelection(new Object[] {
729: addedZipArchive[0], fLibrary }));
730: assertOnlyEnabled(new IAction[] { fRemoveFromBuildpathAction });
731: }
732:
733: private IJavaProject createProject() throws CoreException,
734: InvocationTargetException {
735: fProject = JavaProjectHelper.createJavaProject("Dummy project",
736: "bin");
737: IPath srcPath = new Path("src");
738: IPath normalFolderPath = new Path("NormalFolder");
739: IPath packagePath = srcPath.append("pack1");
740: IPath filePath = packagePath.append("NormalFile");
741:
742: // src folder
743: IFolder folder = fProject.getProject().getFolder(srcPath);
744: CoreUtility.createFolder(folder, true, true, null);
745:
746: // one normal folder
747: IFolder folder2 = fProject.getProject().getFolder(
748: normalFolderPath);
749: CoreUtility.createFolder(folder, true, true, null);
750:
751: final IPath projectPath = fProject.getProject().getFullPath();
752:
753: // configure the classpath entries, including the default jre library.
754: List cpEntries = new ArrayList();
755: cpEntries.add(JavaCore.newSourceEntry(projectPath
756: .append(srcPath)));
757: cpEntries.addAll(Arrays.asList(PreferenceConstants
758: .getDefaultJRELibrary()));
759: IClasspathEntry[] entries = (IClasspathEntry[]) cpEntries
760: .toArray(new IClasspathEntry[cpEntries.size()]);
761: fProject.setRawClasspath(entries, null);
762:
763: // one package in src folder
764: IPackageFragmentRoot root = fProject
765: .findPackageFragmentRoot(fProject.getPath().append(
766: srcPath));
767: IPackageFragment pack1 = root.createPackageFragment("pack1",
768: true, null);
769: IPackageFragment defaultPack = root.getPackageFragment("");
770:
771: IPath libraryPath = root.getPath().append("archive.jar");
772: IPackageFragmentRoot jarRoot = JavaProjectHelper.addLibrary(
773: fProject, libraryPath);
774: assertFalse(ClasspathModifier.getClasspathEntryFor(jarRoot
775: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
776:
777: libraryPath = root.getPath().append("archive.zip");
778: IFile zipFile = fProject.getProject().getWorkspace().getRoot()
779: .getFile(libraryPath);
780: zipFile.create(new ByteArrayInputStream(new byte[] {}), true,
781: null);
782: final IPackageFragmentRoot zipRoot = JavaProjectHelper
783: .addLibrary(fProject, libraryPath);
784: assertFalse(ClasspathModifier.getClasspathEntryFor(zipRoot
785: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
786:
787: // two compilation units A and B in 'package'
788: ICompilationUnit cuA = createICompilationUnit("A", pack1);
789: final IResource excludedElements[] = { null, null };
790: final IPackageFragment pack2 = root.createPackageFragment(
791: "pack1.pack2", true, null);
792: final ICompilationUnit cuB = createICompilationUnit("B", pack1);
793: ExcludeFromBuildpathAction exclude = new ExcludeFromBuildpathAction(
794: PlatformUI.getWorkbench().getProgressService(),
795: new ISetSelectionTarget() {
796:
797: public void selectReveal(ISelection selection) {
798: StructuredSelection ss = (StructuredSelection) selection;
799: List list = ss.toList();
800: excludedElements[0] = (IResource) list.get(0);
801: excludedElements[1] = (IResource) list.get(1);
802: }
803:
804: });
805: exclude
806: .selectionChanged(new SelectionChangedEvent(
807: new ISelectionProvider() {
808: public void addSelectionChangedListener(
809: ISelectionChangedListener listener) {
810: }
811:
812: public ISelection getSelection() {
813: return new StructuredSelection(
814: new Object[] { cuB, pack2 });
815: }
816:
817: public void removeSelectionChangedListener(
818: ISelectionChangedListener listener) {
819: }
820:
821: public void setSelection(
822: ISelection selection) {
823: }
824: }, new StructuredSelection(new Object[] { cuB,
825: pack2 })));
826: exclude.run();
827:
828: IFile file = fProject.getProject().getFile(filePath);
829: file.create(null, false, null);
830:
831: final IFile[] removedZipFile = { null };
832: RemoveFromBuildpathAction remove = new RemoveFromBuildpathAction(
833: PlatformUI.getWorkbench().getProgressService(),
834: new ISetSelectionTarget() {
835:
836: public void selectReveal(ISelection selection) {
837: removedZipFile[0] = (IFile) ((StructuredSelection) selection)
838: .getFirstElement();
839: }
840:
841: });
842: remove.selectionChanged(new SelectionChangedEvent(
843: new ISelectionProvider() {
844: public void addSelectionChangedListener(
845: ISelectionChangedListener listener) {
846: }
847:
848: public ISelection getSelection() {
849: return new StructuredSelection(zipRoot);
850: }
851:
852: public void removeSelectionChangedListener(
853: ISelectionChangedListener listener) {
854: }
855:
856: public void setSelection(ISelection selection) {
857: }
858: }, new StructuredSelection(zipRoot)));
859: remove.run();
860:
861: fSourceFolder = root;
862: fFolder = folder2;
863: fPackage = pack1;
864: fCompilationUnit = cuA;
865: fExcludedFile = excludedElements[0];
866: fFile = file;
867: fExcludedPackage = excludedElements[1];
868: fDefaultPackage = defaultPack;
869: fLibrary = jarRoot;
870: fExcludedLibrary = removedZipFile[0];
871:
872: return fProject;
873: }
874:
875: private void select(Object[] objs) {
876: select(new StructuredSelection(objs));
877: }
878:
879: private void select(final StructuredSelection selection) {
880: for (int i = 0; i < fActions.length; i++) {
881: fActions[i].selectionChanged(new SelectionChangedEvent(
882: new ISelectionProvider() {
883: public void addSelectionChangedListener(
884: ISelectionChangedListener listener) {
885: }
886:
887: public ISelection getSelection() {
888: return selection;
889: }
890:
891: public void removeSelectionChangedListener(
892: ISelectionChangedListener listener) {
893: }
894:
895: public void setSelection(ISelection s) {
896: }
897: }, selection));
898: }
899: }
900:
901: private ICompilationUnit createICompilationUnit(String className,
902: IPackageFragment fragment) throws JavaModelException {
903: String packString = fragment.getElementName().equals("") ? fragment
904: .getElementName()
905: : "package " + fragment.getElementName() + ";\n";
906: StringBuffer content = getFileContent(className, packString);
907: return fragment.createCompilationUnit(className + ".java",
908: content.toString(), false, null);
909: }
910:
911: private StringBuffer getFileContent(String className,
912: String packageHeader) {
913: StringBuffer buf = new StringBuffer();
914: buf.append(packageHeader);
915: buf.append("\n");
916: buf.append("public class " + className + " {\n");
917: buf.append(" public void foo() {\n");
918: buf.append(" }\n");
919: buf.append("}\n");
920: return buf;
921: }
922: }
|