0001: /*******************************************************************************
0002: * Copyright (c) 2000, 2006 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.ui.tests.dialogs;
0011:
0012: import java.io.IOException;
0013: import java.lang.reflect.InvocationTargetException;
0014:
0015: import junit.framework.Test;
0016: import junit.framework.TestCase;
0017: import junit.framework.TestSuite;
0018:
0019: import org.eclipse.core.runtime.CoreException;
0020: import org.eclipse.core.runtime.IPath;
0021: import org.eclipse.core.runtime.IProgressMonitor;
0022: import org.eclipse.core.runtime.IStatus;
0023: import org.eclipse.core.runtime.NullProgressMonitor;
0024: import org.eclipse.core.runtime.Path;
0025:
0026: import org.eclipse.core.resources.IFile;
0027: import org.eclipse.core.resources.IFolder;
0028: import org.eclipse.core.resources.IResource;
0029:
0030: import org.eclipse.jdt.core.IClasspathEntry;
0031: import org.eclipse.jdt.core.ICompilationUnit;
0032: import org.eclipse.jdt.core.IJavaElement;
0033: import org.eclipse.jdt.core.IJavaModelStatus;
0034: import org.eclipse.jdt.core.IJavaProject;
0035: import org.eclipse.jdt.core.IPackageFragment;
0036: import org.eclipse.jdt.core.IPackageFragmentRoot;
0037: import org.eclipse.jdt.core.JavaConventions;
0038: import org.eclipse.jdt.core.JavaModelException;
0039:
0040: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
0041:
0042: import org.eclipse.jdt.ui.PreferenceConstants;
0043:
0044: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
0045: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
0046: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
0047: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
0048: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.BuildpathModifierAction;
0049: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries;
0050: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderValidator;
0051:
0052: import org.eclipse.jdt.testplugin.JavaProjectHelper;
0053:
0054: public class NewProjectWizardTest extends TestCase {
0055:
0056: private static final Class THIS = NewProjectWizardTest.class;
0057:
0058: protected IJavaProject fProject;
0059: protected static NewProjectTestSetup fTestSetup;
0060: protected String fNormalFolder = "NormalFolder";
0061: protected String fSubFolder = "SubFolder";
0062:
0063: public NewProjectWizardTest(String name) {
0064: super (name);
0065: }
0066:
0067: public static Test allTests() {
0068: return setUpTest(new TestSuite(THIS));
0069: }
0070:
0071: public static Test setUpTest(Test test) {
0072: fTestSetup = new NewProjectTestSetup(test);
0073: return fTestSetup;
0074: }
0075:
0076: public static Test suite() {
0077: TestSuite suite = new TestSuite(THIS);
0078: fTestSetup = new NewProjectTestSetup(suite);
0079: suite.addTestSuite(NewEmptyProjectWizardTest.THIS);
0080: return fTestSetup;
0081: }
0082:
0083: protected void setUp() throws Exception {
0084: fProject = fTestSetup.getWorkspaceProjectWithSrc();
0085: assertFalse(fProject.isOnClasspath(fProject
0086: .getUnderlyingResource()));
0087: }
0088:
0089: protected void tearDown() throws Exception {
0090: fProject.getProject().delete(true, true, null);
0091: }
0092:
0093: // Test folder creation (on project, on fragment root, on fragment; as source folder, as normal folder)
0094: public void testCreateNormalFolderOnProject() throws CoreException,
0095: InvocationTargetException, InterruptedException {
0096: IFolder folder = (IFolder) executeOperation(
0097: BuildpathModifierAction.CREATE_FOLDER, null,
0098: getOutputFolderQueryInternal(fProject
0099: .getOutputLocation()), null,
0100: getNormalFolderCreationQuery(), null);
0101:
0102: assertTrue(folder.exists());
0103: assertTrue(folder.getName().equals(fNormalFolder));
0104: assertTrue(ClasspathModifier.getClasspathEntryFor(folder
0105: .getFullPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0106:
0107: validateClasspath();
0108: }
0109:
0110: public void testCreateSourceFolderOnProject() throws CoreException,
0111: InvocationTargetException, InterruptedException {
0112: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
0113: BuildpathModifierAction.CREATE_FOLDER, null,
0114: getOutputFolderQueryInternal(fProject
0115: .getOutputLocation()), null,
0116: getSourceFolderCreationQuery(), null);
0117:
0118: assertTrue(root.getUnderlyingResource().exists());
0119: assertTrue(root.getElementName().equals(fSubFolder));
0120: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0121: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0122:
0123: validateClasspath();
0124: }
0125:
0126: public void testCreateNormalFolderOnFragRoot()
0127: throws CoreException, InvocationTargetException,
0128: InterruptedException {
0129: IPackageFragmentRoot parentRoot = addToClasspath(new Path(
0130: fSubFolder));
0131: ClasspathModifierQueries.ICreateFolderQuery folderQuery = new ClasspathModifierQueries.ICreateFolderQuery() {
0132:
0133: public boolean doQuery() {
0134: return true;
0135: }
0136:
0137: public boolean isSourceFolder() {
0138: return false;
0139: }
0140:
0141: public IFolder getCreatedFolder() {
0142: return getFolderHandle(new Path(fSubFolder)
0143: .append(fNormalFolder));
0144: }
0145:
0146: };
0147: IFolder folder = (IFolder) executeOperation(
0148: BuildpathModifierAction.CREATE_FOLDER, parentRoot,
0149: getOutputFolderQueryInternal(fProject
0150: .getOutputLocation()), null, folderQuery, null);
0151:
0152: assertTrue(folder.exists());
0153: assertTrue(folder.getName().equals(fNormalFolder));
0154: assertTrue(ClasspathModifier.getClasspathEntryFor(new Path(
0155: fNormalFolder), fProject, IClasspathEntry.CPE_SOURCE) == null);
0156: assertTrue(contains(new Path(fNormalFolder), parentRoot
0157: .getRawClasspathEntry().getExclusionPatterns(), null));
0158:
0159: validateClasspath();
0160: }
0161:
0162: public void testCreateSourceFolderOnFragRoot()
0163: throws CoreException, InvocationTargetException,
0164: InterruptedException {
0165: IPackageFragmentRoot parentRoot = addToClasspath(new Path(
0166: fNormalFolder));
0167: ClasspathModifierQueries.ICreateFolderQuery folderQuery = new ClasspathModifierQueries.ICreateFolderQuery() {
0168:
0169: public boolean doQuery() {
0170: return true;
0171: }
0172:
0173: public boolean isSourceFolder() {
0174: return true;
0175: }
0176:
0177: public IFolder getCreatedFolder() {
0178: return getFolderHandle(new Path(fNormalFolder)
0179: .append(fSubFolder));
0180: }
0181:
0182: };
0183: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
0184: BuildpathModifierAction.CREATE_FOLDER, parentRoot,
0185: getOutputFolderQueryInternal(fProject
0186: .getOutputLocation()), null, folderQuery, null);
0187:
0188: assertTrue(root.getUnderlyingResource().exists());
0189: assertTrue(root.getElementName().equals(fSubFolder));
0190: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0191: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0192: assertTrue(contains(new Path(fSubFolder), parentRoot
0193: .getRawClasspathEntry().getExclusionPatterns(), null));
0194:
0195: validateClasspath();
0196: }
0197:
0198: public void testCreateNormalFolderOnFrag() throws CoreException,
0199: InvocationTargetException, InterruptedException {
0200: IPackageFragmentRoot root = addToClasspath(new Path(
0201: fNormalFolder));
0202: final IPath nfFolder = new Path(fNormalFolder).append(
0203: fSubFolder).append("nfFolder");
0204: IFolder fragment = getFolderHandle(new Path(fNormalFolder)
0205: .append(fSubFolder));
0206: ClasspathModifierQueries.ICreateFolderQuery folderQuery = new ClasspathModifierQueries.ICreateFolderQuery() {
0207:
0208: public boolean doQuery() {
0209: return true;
0210: }
0211:
0212: public boolean isSourceFolder() {
0213: return false;
0214: }
0215:
0216: public IFolder getCreatedFolder() {
0217: return getFolderHandle(nfFolder);
0218: }
0219:
0220: };
0221: IFolder folder = (IFolder) executeOperation(
0222: BuildpathModifierAction.CREATE_FOLDER, fragment,
0223: getOutputFolderQueryInternal(fProject
0224: .getOutputLocation()), null, folderQuery, null);
0225:
0226: assertTrue(folder.exists());
0227: assertTrue(folder.getParent().equals(fragment));
0228: assertTrue(folder.getName().equals(nfFolder.lastSegment()));
0229: assertTrue(ClasspathModifier.getClasspathEntryFor(folder
0230: .getFullPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0231: assertTrue(contains(
0232: folder.getFullPath().removeFirstSegments(2), root
0233: .getRawClasspathEntry().getExclusionPatterns(),
0234: null));
0235:
0236: validateClasspath();
0237: }
0238:
0239: public void testCreateSourceFolderOnFrag() throws CoreException,
0240: InvocationTargetException, InterruptedException {
0241: IPackageFragmentRoot parentRoot = addToClasspath(new Path(
0242: fNormalFolder));
0243: IFolder fragment = getFolderHandle(new Path(fNormalFolder)
0244: .append(fSubFolder));
0245: final IPath srcFolder = new Path(fNormalFolder).append(
0246: fSubFolder).append("srcFolder");
0247:
0248: ClasspathModifierQueries.ICreateFolderQuery folderQuery = new ClasspathModifierQueries.ICreateFolderQuery() {
0249:
0250: public boolean doQuery() {
0251: return true;
0252: }
0253:
0254: public boolean isSourceFolder() {
0255: return true;
0256: }
0257:
0258: public IFolder getCreatedFolder() {
0259: return getFolderHandle(srcFolder);
0260: }
0261:
0262: };
0263: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
0264: BuildpathModifierAction.CREATE_FOLDER, fragment,
0265: getOutputFolderQueryInternal(fProject
0266: .getOutputLocation()), null, folderQuery, null);
0267:
0268: assertTrue(root.getUnderlyingResource().exists());
0269: assertTrue(root.getUnderlyingResource().getParent().equals(
0270: fragment));
0271: assertTrue(root.getElementName()
0272: .equals(srcFolder.lastSegment()));
0273: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0274: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0275: assertTrue(contains(root.getPath().removeFirstSegments(2),
0276: parentRoot.getRawClasspathEntry()
0277: .getExclusionPatterns(), null));
0278:
0279: validateClasspath();
0280: }
0281:
0282: public void testAddNormalFolderToCP() throws JavaModelException,
0283: CoreException, InvocationTargetException,
0284: InterruptedException {
0285: int numberOfEntries = fProject.getRawClasspath().length;
0286:
0287: addToClasspath(new Path(fNormalFolder));
0288:
0289: int newNumberOfEntries = fProject.getRawClasspath().length;
0290: assertTrue(numberOfEntries + 1 == newNumberOfEntries);
0291:
0292: validateClasspath();
0293: }
0294:
0295: public void testAddNestedNormalFolderToCP()
0296: throws JavaModelException, CoreException,
0297: InvocationTargetException, InterruptedException {
0298: IFolder cpFolder = excludePackage();
0299:
0300: IPackageFragmentRoot root = fProject
0301: .findPackageFragmentRoot(cpFolder.getFullPath());
0302: IFolder folder = getFolderHandle(cpFolder
0303: .getProjectRelativePath().append(fSubFolder));
0304:
0305: IClasspathEntry entry = root.getRawClasspathEntry();
0306: assertTrue(contains(new Path(folder.getName()), entry
0307: .getExclusionPatterns(), null));
0308:
0309: addToClasspath(folder.getProjectRelativePath());
0310:
0311: entry = root.getRawClasspathEntry();
0312: assertTrue(contains(new Path(folder.getName()), entry
0313: .getExclusionPatterns(), null));
0314:
0315: validateClasspath();
0316: }
0317:
0318: public void testAddPackageToCP() throws JavaModelException,
0319: CoreException, InvocationTargetException,
0320: InterruptedException {
0321: IPackageFragmentRoot root = addToClasspath(new Path(
0322: fNormalFolder));
0323: getFolderHandle(new Path(fNormalFolder).append(fSubFolder));
0324: IPackageFragment fragment = root.getPackageFragment(fSubFolder);
0325:
0326: IClasspathEntry entry = root.getRawClasspathEntry();
0327:
0328: int nrExclusions = entry.getExclusionPatterns().length;
0329: assertFalse(contains(new Path(fragment.getElementName()), entry
0330: .getExclusionPatterns(), null));
0331:
0332: addToClasspath(fragment);
0333:
0334: entry = root.getRawClasspathEntry();
0335: assertTrue(contains(new Path(fragment.getElementName()), entry
0336: .getExclusionPatterns(), null));
0337: assertTrue(entry.getExclusionPatterns().length - 1 == nrExclusions);
0338:
0339: validateClasspath();
0340: }
0341:
0342: public void testAddIncludedPackageToCP() throws JavaModelException,
0343: CoreException, InvocationTargetException,
0344: InterruptedException {
0345: IFolder cpFolder = includePackage();
0346:
0347: IPackageFragmentRoot root = fProject
0348: .findPackageFragmentRoot(cpFolder.getFullPath());
0349: IPackageFragment fragment = root.getPackageFragment(fSubFolder);
0350:
0351: IClasspathEntry entry = root.getRawClasspathEntry();
0352:
0353: int nrInclusions = entry.getInclusionPatterns().length;
0354: int nrExclusions = entry.getExclusionPatterns().length;
0355: assertTrue(contains(new Path(fragment.getElementName()), entry
0356: .getInclusionPatterns(), null));
0357: assertFalse(contains(new Path(fragment.getElementName()), entry
0358: .getExclusionPatterns(), null));
0359:
0360: addToClasspath(fragment);
0361:
0362: entry = root.getRawClasspathEntry();
0363: assertFalse(contains(new Path(fragment.getElementName()), entry
0364: .getInclusionPatterns(), null));
0365: assertTrue(contains(new Path(fragment.getElementName()), entry
0366: .getExclusionPatterns(), null));
0367: assertTrue(entry.getInclusionPatterns().length + 1 == nrInclusions);
0368: assertTrue(entry.getExclusionPatterns().length - 1 == nrExclusions);
0369:
0370: validateClasspath();
0371: }
0372:
0373: public void testAddExcludedPackageToCP() throws JavaModelException,
0374: CoreException, InvocationTargetException,
0375: InterruptedException {
0376: IFolder cpFolder = excludePackage();
0377: IPackageFragmentRoot root = fProject
0378: .findPackageFragmentRoot(cpFolder.getFullPath());
0379: IPackageFragment element = root.getPackageFragment(fSubFolder);
0380:
0381: IClasspathEntry entry = root.getRawClasspathEntry();
0382: assertTrue(contains(new Path(element.getElementName()), entry
0383: .getExclusionPatterns(), null));
0384:
0385: addToClasspath(element);
0386:
0387: root = fProject.findPackageFragmentRoot(element.getParent()
0388: .getPath());
0389: entry = root.getRawClasspathEntry();
0390:
0391: assertTrue(contains(new Path(element.getElementName()), entry
0392: .getExclusionPatterns(), null));
0393:
0394: validateClasspath();
0395: }
0396:
0397: public void testAddProjectToCP() throws CoreException,
0398: InvocationTargetException, InterruptedException {
0399: // Situation: Project wich one source folder and one normal folder as
0400: // direct childs --> adding the project to the CP should convert the folder into
0401: // a package and the .java file into a compilation unit
0402: IPath srcPath = new Path("src2");
0403: IPackageFragmentRoot root = addToClasspath(srcPath);
0404: IFolder normalFolder = getFolderHandle(new Path(fNormalFolder));
0405: IPackageFragmentRoot projectRoot = getProjectRoot(fProject
0406: .getCorrespondingResource());
0407: IPackageFragment fragment = projectRoot.createPackageFragment(
0408: "", false, null);
0409:
0410: assertTrue(ClasspathModifier.getClasspathEntryFor(fProject
0411: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0412: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0413: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0414: assertTrue(fProject.findPackageFragment(normalFolder
0415: .getFullPath()) == null);
0416: assertTrue(fProject.findPackageFragment(fragment.getPath()) == null);
0417:
0418: IJavaProject project = (IJavaProject) executeOperation(
0419: BuildpathModifierAction.ADD_SEL_SF_TO_BP, fProject,
0420: getOutputFolderQueryInternal(fProject
0421: .getOutputLocation()), null, null, null);
0422: assertTrue(project.equals(fProject));
0423:
0424: // project is on classpath
0425: assertFalse(ClasspathModifier.getClasspathEntryFor(fProject
0426: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0427: // root is on classpath and excluded on the project
0428: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0429: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0430: assertTrue(ClasspathModifier.isExcluded(root
0431: .getUnderlyingResource(), fProject));
0432: assertFalse(fProject.findPackageFragment(normalFolder
0433: .getFullPath()) == null);
0434: assertFalse(fProject.findPackageFragment(fragment.getPath()) == null);
0435:
0436: validateClasspath();
0437: }
0438:
0439: public void testAddJarFileToCP() throws InvocationTargetException,
0440: InterruptedException, CoreException, IOException {
0441: IPath libraryPath = fProject.getPath().append("src2").append(
0442: "archive.jar");
0443: testRemoveJarFileFromCP();
0444: IFile jarFile = fProject.getProject().getFile(
0445: libraryPath.removeFirstSegments(1));
0446: assertTrue(jarFile.getFileExtension().equals("jar"));
0447:
0448: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
0449: BuildpathModifierAction.ADD_SEL_LIB_TO_BP, jarFile,
0450: getOutputFolderQueryInternal(fProject
0451: .getOutputLocation()), null, null, null);
0452: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0453: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0454:
0455: validateClasspath();
0456: }
0457:
0458: public void testAddZipFileToCP() throws InvocationTargetException,
0459: InterruptedException, CoreException {
0460: IPath libraryPath = fProject.getPath().append("src2").append(
0461: "archive.zip");
0462: testRemoveZipFileFromCP();
0463: IFile zipFile = fProject.getProject().getFile(
0464: libraryPath.removeFirstSegments(1));
0465: assertTrue(zipFile.getFileExtension().equals("zip"));
0466:
0467: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
0468: BuildpathModifierAction.ADD_SEL_LIB_TO_BP, zipFile,
0469: getOutputFolderQueryInternal(fProject
0470: .getOutputLocation()), null, null, null);
0471: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0472: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0473:
0474: validateClasspath();
0475: }
0476:
0477: public void testAddJREToCP() throws InvocationTargetException,
0478: InterruptedException, CoreException {
0479: IClasspathEntry[] entries = fProject.getRawClasspath();
0480: IClasspathEntry entry = null;
0481: for (int i = 0; i < entries.length; i++) {
0482: if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
0483: entry = entries[i];
0484: break;
0485: }
0486: }
0487: assertTrue(entry != null);
0488: assertFalse(ClasspathModifier.getClasspathEntryFor(entry
0489: .getPath(), fProject, IClasspathEntry.CPE_CONTAINER) == null);
0490: testRemoveJREFromCP();
0491: ClassPathContainer container = (ClassPathContainer) executeOperation(
0492: BuildpathModifierAction.ADD_LIB_TO_BP, entry, null,
0493: null, null, null);
0494: assertTrue(container.getClasspathEntry().equals(entry));
0495: assertFalse(ClasspathModifier.getClasspathEntryFor(entry
0496: .getPath(), fProject, IClasspathEntry.CPE_CONTAINER) == null);
0497:
0498: validateClasspath();
0499: }
0500:
0501: public void testRemoveFromCP() throws JavaModelException,
0502: CoreException, InvocationTargetException,
0503: InterruptedException {
0504: // add folder
0505: int before = fProject.getRawClasspath().length;
0506: IPackageFragmentRoot root = addToClasspath(new Path(
0507: fNormalFolder));
0508:
0509: // and remove it
0510: IFolder folder = (IFolder) executeOperation(
0511: BuildpathModifierAction.REMOVE_FROM_BP, root, null,
0512: null, null, null);
0513:
0514: assertTrue(folder.getFullPath().equals(root.getPath()));
0515: assertFalse(contains(new Path(fNormalFolder), getPaths(), null));
0516: int after = fProject.getRawClasspath().length;
0517: assertTrue(before == after);
0518:
0519: validateClasspath();
0520: }
0521:
0522: public void removeProjectFromCP() throws CoreException,
0523: InvocationTargetException, InterruptedException {
0524: IPath srcPath = new Path("src2");
0525: IPackageFragmentRoot root = addToClasspath(srcPath);
0526: IFolder normalFolder = getFolderHandle(new Path(fNormalFolder));
0527: IPackageFragmentRoot projectRoot = getProjectRoot(fProject
0528: .getCorrespondingResource());
0529: IPackageFragment fragment = projectRoot.createPackageFragment(
0530: "", false, null);
0531:
0532: // add project to class path
0533: projectRoot = (IPackageFragmentRoot) executeOperation(
0534: BuildpathModifierAction.ADD_SEL_SF_TO_BP, fProject,
0535: getOutputFolderQueryInternal(fProject
0536: .getOutputLocation()), null, null, null);
0537: assertTrue(projectRoot.equals(getProjectRoot(fProject
0538: .getCorrespondingResource())));
0539:
0540: // project is on classpath
0541: assertFalse(ClasspathModifier.getClasspathEntryFor(fProject
0542: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0543: // root is on classpath
0544: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0545: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0546: assertFalse(fProject.findPackageFragment(normalFolder
0547: .getFullPath()) == null);
0548: assertFalse(fProject.findPackageFragment(fragment.getPath()) == null);
0549:
0550: IJavaProject jProject = (IJavaProject) executeOperation(
0551: BuildpathModifierAction.REMOVE_FROM_BP, fProject, null,
0552: null, null, null);
0553:
0554: assertTrue(jProject.equals(fProject));
0555: assertTrue(ClasspathModifier.getClasspathEntryFor(fProject
0556: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0557: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0558: .getPath(), fProject, IClasspathEntry.CPE_SOURCE) == null);
0559: assertTrue(fProject.findPackageFragment(normalFolder
0560: .getFullPath()) == null);
0561: assertTrue(fProject.findPackageFragment(fragment.getPath()) == null);
0562:
0563: projectRoot = (IPackageFragmentRoot) executeOperation(
0564: BuildpathModifierAction.ADD_SEL_SF_TO_BP, fProject,
0565: getOutputFolderQueryInternal(fProject
0566: .getOutputLocation()), null, null, null);
0567: assertTrue(projectRoot.equals(getProjectRoot(fProject
0568: .getCorrespondingResource())));
0569:
0570: validateClasspath();
0571: }
0572:
0573: // Test include, exclude, uninclude, unexclude, ...
0574: public void testIncludePackage() throws JavaModelException,
0575: CoreException, InvocationTargetException,
0576: InterruptedException {
0577: includePackage();
0578:
0579: validateClasspath();
0580: }
0581:
0582: public void testExcludePackage() throws JavaModelException,
0583: CoreException, InvocationTargetException,
0584: InterruptedException {
0585: excludePackage();
0586:
0587: validateClasspath();
0588: }
0589:
0590: public void testExcludeIncludedPackage() throws JavaModelException,
0591: CoreException, InvocationTargetException,
0592: InterruptedException {
0593: IFolder cpFolder = includePackage();
0594: IPackageFragmentRoot root = fProject
0595: .findPackageFragmentRoot(cpFolder.getFullPath());
0596: IPackageFragment fragment = root.getPackageFragment(fSubFolder);
0597:
0598: IClasspathEntry entry = root.getRawClasspathEntry();
0599: int nrIncluded = entry.getInclusionPatterns().length;
0600: int nrExcluded = entry.getExclusionPatterns().length;
0601:
0602: IFolder folder = (IFolder) executeOperation(
0603: BuildpathModifierAction.EXCLUDE, fragment, null, null,
0604: null, null);
0605:
0606: entry = root.getRawClasspathEntry();
0607: IPath[] inclusionPatterns = entry.getInclusionPatterns();
0608: IPath[] exclusionPatterns = entry.getExclusionPatterns();
0609: assertTrue(inclusionPatterns.length + 1 == nrIncluded);
0610: assertTrue(exclusionPatterns.length - 1 == nrExcluded);
0611: assertFalse(contains(new Path(fragment.getElementName()),
0612: inclusionPatterns, null));
0613: assertTrue(contains(new Path(fragment.getElementName()),
0614: exclusionPatterns, null));
0615: assertTrue(folder.getFullPath().equals(fragment.getPath()));
0616:
0617: validateClasspath();
0618: }
0619:
0620: public void testIncludeExcludedFolder() throws JavaModelException,
0621: CoreException, InvocationTargetException,
0622: InterruptedException {
0623: IFolder cpFolder = excludePackage();
0624:
0625: IPackageFragmentRoot root = fProject
0626: .findPackageFragmentRoot(cpFolder.getFullPath());
0627: IFolder folder = getFolderHandle(cpFolder
0628: .getProjectRelativePath().append(fSubFolder));
0629:
0630: IClasspathEntry entry = root.getRawClasspathEntry();
0631: int nrIncluded = entry.getInclusionPatterns().length;
0632: int nrExcluded = entry.getExclusionPatterns().length;
0633:
0634: IPackageFragment fragment = (IPackageFragment) executeOperation(
0635: BuildpathModifierAction.INCLUDE, folder, null, null,
0636: null, null);
0637:
0638: entry = root.getRawClasspathEntry();
0639: IPath[] inclusionPatterns = entry.getInclusionPatterns();
0640: IPath[] exclusionPatterns = entry.getExclusionPatterns();
0641: assertTrue(inclusionPatterns.length - 1 == nrIncluded);
0642: assertTrue(exclusionPatterns.length + 1 == nrExcluded);
0643: assertTrue(contains(new Path(folder.getName()),
0644: inclusionPatterns, null));
0645: assertFalse(contains(new Path(folder.getName()),
0646: exclusionPatterns, null));
0647: assertTrue(fragment.getPath().equals(folder.getFullPath()));
0648:
0649: validateClasspath();
0650: }
0651:
0652: public void testRemoveInclusion() throws JavaModelException,
0653: CoreException, InvocationTargetException,
0654: InterruptedException {
0655: IFolder cpFolder = includePackage();
0656:
0657: IPackageFragmentRoot root = fProject
0658: .findPackageFragmentRoot(cpFolder.getFullPath());
0659: IPackageFragment fragment = root.getPackageFragment(fSubFolder);
0660:
0661: IClasspathEntry entry = root.getRawClasspathEntry();
0662: int nrIncluded = entry.getInclusionPatterns().length;
0663: int nrExcluded = entry.getExclusionPatterns().length;
0664:
0665: IPackageFragment frag = (IPackageFragment) executeOperation(
0666: BuildpathModifierAction.UNINCLUDE, fragment, null,
0667: null, null, null);
0668:
0669: entry = root.getRawClasspathEntry();
0670: IPath[] inclusionPatterns = entry.getInclusionPatterns();
0671: IPath[] exclusionPatterns = entry.getExclusionPatterns();
0672: assertTrue(inclusionPatterns.length + 1 == nrIncluded);
0673: assertTrue(exclusionPatterns.length == nrExcluded);
0674: assertFalse(contains(new Path(fragment.getElementName()),
0675: inclusionPatterns, null));
0676: assertFalse(contains(new Path(fragment.getElementName()),
0677: exclusionPatterns, null));
0678: assertTrue(frag.getPath().equals(fragment.getPath()));
0679:
0680: validateClasspath();
0681: }
0682:
0683: public void testRemoveExclusion() throws JavaModelException,
0684: CoreException, InvocationTargetException,
0685: InterruptedException {
0686: IFolder cpFolder = excludePackage();
0687:
0688: IPackageFragmentRoot root = fProject
0689: .findPackageFragmentRoot(cpFolder.getFullPath());
0690: IFolder folder = getFolderHandle(cpFolder
0691: .getProjectRelativePath().append(fSubFolder));
0692:
0693: IClasspathEntry entry = root.getRawClasspathEntry();
0694: int nrIncluded = entry.getInclusionPatterns().length;
0695: int nrExcluded = entry.getExclusionPatterns().length;
0696:
0697: IPackageFragment fragment = (IPackageFragment) executeOperation(
0698: BuildpathModifierAction.UNEXCLUDE, folder, null, null,
0699: null, null);
0700:
0701: entry = root.getRawClasspathEntry();
0702: IPath[] inclusionPatterns = entry.getInclusionPatterns();
0703: IPath[] exclusionPatterns = entry.getExclusionPatterns();
0704: assertTrue(inclusionPatterns.length == nrIncluded);
0705: assertTrue(exclusionPatterns.length + 1 == nrExcluded);
0706: assertFalse(contains(new Path(folder.getName()),
0707: inclusionPatterns, null));
0708: assertFalse(contains(new Path(folder.getName()),
0709: exclusionPatterns, null));
0710: assertTrue(fragment.getPath().equals(folder.getFullPath()));
0711:
0712: validateClasspath();
0713: }
0714:
0715: public void testRemoveJarFileFromCP()
0716: throws InvocationTargetException, InterruptedException,
0717: CoreException, IOException {
0718: IPath srcPath = new Path("src2");
0719: IPackageFragmentRoot parentRoot = addToClasspath(srcPath);
0720: IPath libraryPath = parentRoot.getPath().append("archive.jar");
0721: IPackageFragmentRoot root = JavaProjectHelper.addLibrary(
0722: fProject, libraryPath, null, null);
0723: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0724: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0725: executeOperation(BuildpathModifierAction.REMOVE_FROM_BP, root,
0726: null, null, null, null);
0727: //assertTrue(jarFile.getFileExtension().equals("jar"));
0728: //assertTrue(ClasspathModifier.isArchive(jarFile, fProject));
0729: //assertTrue(ClasspathModifier.getClasspathEntryFor(jarFile.getFullPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0730:
0731: validateClasspath();
0732: }
0733:
0734: public void testRemoveZipFileFromCP()
0735: throws InvocationTargetException, InterruptedException,
0736: CoreException {
0737: IPath srcPath = new Path("src2");
0738: IPackageFragmentRoot parentRoot = addToClasspath(srcPath);
0739: IPath libraryPath = parentRoot.getPath().append("archive.zip");
0740: IPackageFragmentRoot root = JavaProjectHelper.addLibrary(
0741: fProject, libraryPath);
0742: assertFalse(ClasspathModifier.getClasspathEntryFor(root
0743: .getPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0744: executeOperation(BuildpathModifierAction.REMOVE_FROM_BP, root,
0745: null, null, null, null);
0746: //zipFile.create(null, false, null);
0747: // assertTrue(zipFile.getFileExtension().equals("zip"));
0748: //assertTrue(ClasspathModifier.isArchive(zipFile, fProject));
0749: //assertTrue(ClasspathModifier.getClasspathEntryFor(zipFile.getFullPath(), fProject, IClasspathEntry.CPE_LIBRARY) == null);
0750:
0751: validateClasspath();
0752: }
0753:
0754: public void testRemoveJREFromCP() throws InvocationTargetException,
0755: InterruptedException, CoreException {
0756: IClasspathEntry[] entries = fProject.getRawClasspath();
0757: IClasspathEntry entry = null;
0758: for (int i = 0; i < entries.length; i++) {
0759: if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
0760: entry = entries[i];
0761: break;
0762: }
0763: }
0764: assertTrue(entry != null);
0765: assertFalse(ClasspathModifier.getClasspathEntryFor(entry
0766: .getPath(), fProject, IClasspathEntry.CPE_CONTAINER) == null);
0767:
0768: ClassPathContainer container = new ClassPathContainer(fProject,
0769: entry);
0770: IJavaProject project = (IJavaProject) executeOperation(
0771: BuildpathModifierAction.REMOVE_FROM_BP, container,
0772: null, null, null, null);
0773: assertTrue(project.equals(fProject));
0774: assertTrue(ClasspathModifier.getClasspathEntryFor(entry
0775: .getPath(), fProject, IClasspathEntry.CPE_CONTAINER) == null);
0776:
0777: validateClasspath();
0778: }
0779:
0780: public void testEditFilters() throws CoreException,
0781: InvocationTargetException, InterruptedException {
0782: IPackageFragmentRoot root = addToClasspath(new Path(
0783: fNormalFolder));
0784: final IPackageFragment includedPackage = root
0785: .getPackageFragment(fSubFolder);
0786: final IPackageFragment excludedPackage = root
0787: .getPackageFragment(fSubFolder + "2");
0788:
0789: assertFalse(contains(new Path(includedPackage.getElementName())
0790: .addTrailingSeparator(), root.getRawClasspathEntry()
0791: .getInclusionPatterns(), null));
0792: assertFalse(contains(new Path(excludedPackage.getElementName())
0793: .addTrailingSeparator(), root.getRawClasspathEntry()
0794: .getExclusionPatterns(), null));
0795:
0796: ClasspathModifierQueries.IInclusionExclusionQuery query = new ClasspathModifierQueries.IInclusionExclusionQuery() {
0797:
0798: public boolean doQuery(CPListElement element,
0799: boolean focusOnExcluded) {
0800: return true;
0801: }
0802:
0803: public IPath[] getInclusionPattern() {
0804: return new IPath[] { new Path(includedPackage
0805: .getElementName()).addTrailingSeparator() };
0806: }
0807:
0808: public IPath[] getExclusionPattern() {
0809: return new IPath[] { new Path(excludedPackage
0810: .getElementName()).addTrailingSeparator() };
0811: }
0812:
0813: };
0814: root = (IPackageFragmentRoot) executeOperation(
0815: BuildpathModifierAction.EDIT_FILTERS, root, null, null,
0816: null, query);
0817:
0818: assertTrue(contains(new Path(includedPackage.getElementName())
0819: .addTrailingSeparator(), root.getRawClasspathEntry()
0820: .getInclusionPatterns(), null));
0821: assertTrue(contains(new Path(excludedPackage.getElementName())
0822: .addTrailingSeparator(), root.getRawClasspathEntry()
0823: .getExclusionPatterns(), null));
0824:
0825: validateClasspath();
0826: }
0827:
0828: public void testResetFilters() throws JavaModelException,
0829: CoreException, InvocationTargetException,
0830: InterruptedException {
0831: IPackageFragmentRoot root = addToClasspath(new Path(
0832: fNormalFolder));
0833: IFolder folder = getFolderHandle(new Path(fNormalFolder));
0834:
0835: IPackageFragment includedPackage = root
0836: .getPackageFragment(fSubFolder);
0837: IPackageFragment excludedPackage = root
0838: .getPackageFragment(fSubFolder + "2");
0839:
0840: IClasspathEntry entry = root.getRawClasspathEntry();
0841:
0842: executeOperation(BuildpathModifierAction.INCLUDE,
0843: includedPackage, null, null, null, null);
0844: executeOperation(BuildpathModifierAction.EXCLUDE,
0845: excludedPackage, null, null, null, null);
0846: addToClasspath(folder.getProjectRelativePath().append(
0847: fSubFolder + "3"));
0848: IFolder subSrcFolder = getFolderHandle(folder
0849: .getProjectRelativePath().append(fSubFolder + "3"));
0850: int numberOnCP = fProject.getRawClasspath().length;
0851:
0852: IPackageFragmentRoot editedRoot = (IPackageFragmentRoot) executeOperation(
0853: BuildpathModifierAction.RESET, root, null, null, null,
0854: null);
0855:
0856: entry = editedRoot.getRawClasspathEntry();
0857: assertTrue(entry.getInclusionPatterns().length == 0);
0858: // one has to be left because it is a source folder
0859: assertTrue(entry.getExclusionPatterns().length == 1);
0860: assertTrue(contains(folder.getFullPath(), getPaths(), null));
0861: assertTrue(contains(subSrcFolder.getFullPath(), getPaths(),
0862: null));
0863: assertTrue(fProject.getRawClasspath().length == numberOnCP);
0864: assertTrue(editedRoot.getPath().equals(root.getPath()));
0865:
0866: validateClasspath();
0867: }
0868:
0869: // Test output folder manipulations (create, edit, reset)
0870: public void testCreateOutputFolder() throws JavaModelException,
0871: CoreException, InvocationTargetException,
0872: InterruptedException {
0873: IPath oldOutputPath = fProject.getOutputLocation();
0874: IPath newOutputPath = new Path(oldOutputPath.toString() + "2");
0875: createOutputFolder(newOutputPath);
0876:
0877: validateClasspath();
0878: }
0879:
0880: public CPListElementAttribute createOutputFolder(IPath newOutputPath)
0881: throws JavaModelException, CoreException,
0882: InvocationTargetException, InterruptedException {
0883: IPackageFragmentRoot root = addToClasspath(new Path(
0884: fNormalFolder));
0885:
0886: IFolder outputFolder = fProject.getProject().getFolder(
0887: newOutputPath);
0888: assertFalse(outputFolder.exists());
0889:
0890: ClasspathModifierQueries.IOutputLocationQuery query = getOutputLocationQuery();
0891:
0892: CPListElementAttribute outputAttribute = (CPListElementAttribute) executeOperation(
0893: BuildpathModifierAction.CREATE_OUTPUT, root, null,
0894: query, null, null);
0895:
0896: root = fProject.findPackageFragmentRoot(root.getPath());
0897: CPListElement elem = CPListElement.createFromExisting(root
0898: .getRawClasspathEntry(), fProject);
0899:
0900: assertTrue(((IPath) outputAttribute.getValue())
0901: .equals(newOutputPath));
0902: assertTrue(((IPath) outputAttribute.getValue()).equals(elem
0903: .getAttribute(CPListElement.OUTPUT)));
0904:
0905: validateClasspath();
0906: return outputAttribute;
0907: }
0908:
0909: public void testEditOutputFolder() throws JavaModelException,
0910: CoreException, InvocationTargetException,
0911: InterruptedException {
0912: IPath oldOutputPath = fProject.getOutputLocation();
0913: IPath newOutputPath = new Path(oldOutputPath.toString() + "2");
0914: CPListElementAttribute attribute = createOutputFolder(newOutputPath);
0915:
0916: ClasspathModifierQueries.IOutputLocationQuery query = new ClasspathModifierQueries.IOutputLocationQuery() {
0917:
0918: public boolean doQuery(CPListElement element) {
0919: return true;
0920: }
0921:
0922: public IPath getOutputLocation() {
0923: IPath path = null;
0924: try {
0925: path = fProject.getOutputLocation();
0926: } catch (JavaModelException e) {
0927: }
0928: return new Path(path.toString() + "3");
0929: }
0930:
0931: public ClasspathModifierQueries.OutputFolderQuery getOutputFolderQuery(
0932: IPath path) throws JavaModelException {
0933: return getOutputFolderQueryInternal(fProject
0934: .getOutputLocation());
0935: }
0936: };
0937: CPListElementAttribute newAttribute = (CPListElementAttribute) executeOperation(
0938: BuildpathModifierAction.EDIT_OUTPUT, attribute, null,
0939: query, null, null);
0940:
0941: IPath editedOutputPath = new Path(oldOutputPath.toString()
0942: + "3");
0943:
0944: IFolder folder = getFolderHandle(new Path(fNormalFolder));
0945: IPackageFragmentRoot root = fProject
0946: .findPackageFragmentRoot(folder.getFullPath());
0947: CPListElement elem = CPListElement.createFromExisting(root
0948: .getRawClasspathEntry(), fProject);
0949:
0950: assertTrue(((IPath) newAttribute.getValue())
0951: .equals(editedOutputPath));
0952: assertTrue(((IPath) newAttribute.getValue()).equals(elem
0953: .getAttribute(CPListElement.OUTPUT)));
0954:
0955: validateClasspath();
0956: }
0957:
0958: public void testEditOutputFolderWithNullReturn()
0959: throws JavaModelException, CoreException,
0960: InvocationTargetException, InterruptedException {
0961: CPListElementAttribute attribute = createOutputFolder(getOutputLocationQuery()
0962: .getOutputLocation());
0963: IPackageFragmentRoot root = fProject
0964: .findPackageFragmentRoot(fProject.getPath().append(
0965: fNormalFolder));
0966:
0967: assertTrue(root.getRawClasspathEntry().getOutputLocation() != null);
0968:
0969: ClasspathModifierQueries.IOutputLocationQuery query = new ClasspathModifierQueries.IOutputLocationQuery() {
0970:
0971: public boolean doQuery(CPListElement element) {
0972: return true;
0973: }
0974:
0975: public IPath getOutputLocation() {
0976: return null;
0977: }
0978:
0979: public ClasspathModifierQueries.OutputFolderQuery getOutputFolderQuery(
0980: IPath path) throws JavaModelException {
0981: return NewProjectWizardTest.this
0982: .getOutputFolderQueryInternal(fProject
0983: .getOutputLocation());
0984: }
0985: };
0986: CPListElementAttribute newAttribute = (CPListElementAttribute) executeOperation(
0987: BuildpathModifierAction.EDIT_OUTPUT, attribute, null,
0988: query, null, null);
0989:
0990: assertTrue(root.getRawClasspathEntry().getOutputLocation() == null);
0991: assertTrue(newAttribute.getValue() == null);
0992:
0993: validateClasspath();
0994: }
0995:
0996: public void testResetOutputFolder() throws JavaModelException,
0997: CoreException, InvocationTargetException,
0998: InterruptedException {
0999: IPath oldOutputPath = fProject.getOutputLocation();
1000: IPath newOutputPath = new Path(oldOutputPath.toString() + "2");
1001: CPListElementAttribute attribute = createOutputFolder(newOutputPath);
1002:
1003: CPListElementAttribute newAttribute = (CPListElementAttribute) executeOperation(
1004: BuildpathModifierAction.RESET, attribute, null, null,
1005: null, null);
1006:
1007: assertTrue(((IPath) newAttribute.getValue()) == null);
1008:
1009: validateClasspath();
1010: }
1011:
1012: // Test file manipulations (include, exclude, ...)
1013: public void testIncludeCompilationUnit() throws CoreException,
1014: InvocationTargetException, InterruptedException {
1015: IPackageFragmentRoot root = addToClasspath(new Path(
1016: fNormalFolder));
1017: IPackageFragment fragment = root.createPackageFragment(
1018: fSubFolder, false, null);
1019: ICompilationUnit cu = createICompilationUnit("C", fragment);
1020:
1021: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1022:
1023: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1024: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1025: null);
1026:
1027: assertTrue(contains(newCu.getPath().removeFirstSegments(2),
1028: root.getRawClasspathEntry().getInclusionPatterns(),
1029: null));
1030: assertTrue(newCu.getPath().equals(cu.getPath()));
1031:
1032: validateClasspath();
1033: }
1034:
1035: public void testExcludeCompilationUnit() throws CoreException,
1036: InvocationTargetException, InterruptedException {
1037: IPackageFragmentRoot root = addToClasspath(new Path(
1038: fNormalFolder));
1039: IPackageFragment fragment = root.createPackageFragment(
1040: fSubFolder, false, null);
1041: ICompilationUnit cu = createICompilationUnit("C", fragment);
1042:
1043: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1044:
1045: IFile excludedFile = (IFile) executeOperation(
1046: BuildpathModifierAction.EXCLUDE, cu, null, null, null,
1047: null);
1048:
1049: assertTrue(contains(excludedFile.getFullPath()
1050: .removeFirstSegments(2), root.getRawClasspathEntry()
1051: .getExclusionPatterns(), null));
1052: assertTrue(excludedFile.getFullPath().equals(cu.getPath()));
1053:
1054: validateClasspath();
1055: }
1056:
1057: public void testIncludeExcludedFile() throws CoreException,
1058: InvocationTargetException, InterruptedException {
1059: IPackageFragmentRoot root = addToClasspath(new Path(
1060: fNormalFolder));
1061: IPackageFragment fragment = root.createPackageFragment(
1062: fSubFolder, false, null);
1063: ICompilationUnit cu = createICompilationUnit("C", fragment);
1064:
1065: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1066: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1067:
1068: IFile excludedFile = (IFile) executeOperation(
1069: BuildpathModifierAction.EXCLUDE, cu, null, null, null,
1070: null);
1071:
1072: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1073: assertTrue(contains(excludedFile.getFullPath()
1074: .removeFirstSegments(2), root.getRawClasspathEntry()
1075: .getExclusionPatterns(), null));
1076:
1077: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1078: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1079: null);
1080:
1081: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1082: assertTrue(contains(newCu.getPath().removeFirstSegments(2),
1083: root.getRawClasspathEntry().getInclusionPatterns(),
1084: null));
1085: assertTrue(excludedFile.getFullPath().equals(newCu.getPath()));
1086:
1087: validateClasspath();
1088: }
1089:
1090: public void testExcludeIncludedFile() throws CoreException,
1091: InvocationTargetException, InterruptedException {
1092: IPackageFragmentRoot root = addToClasspath(new Path(
1093: fNormalFolder));
1094: IPackageFragment fragment = root.createPackageFragment(
1095: fSubFolder, false, null);
1096: ICompilationUnit cu = createICompilationUnit("C", fragment);
1097:
1098: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1099: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1100:
1101: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1102: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1103: null);
1104:
1105: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1106: assertTrue(contains(cu.getPath().removeFirstSegments(2), root
1107: .getRawClasspathEntry().getInclusionPatterns(), null));
1108:
1109: IFile excludedFile = (IFile) executeOperation(
1110: BuildpathModifierAction.EXCLUDE, newCu, null, null,
1111: null, null);
1112:
1113: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1114: assertTrue(contains(excludedFile.getFullPath()
1115: .removeFirstSegments(2), root.getRawClasspathEntry()
1116: .getExclusionPatterns(), null));
1117: assertTrue(excludedFile.getFullPath().equals(newCu.getPath()));
1118:
1119: validateClasspath();
1120: }
1121:
1122: public void testUnincludeFile() throws CoreException,
1123: InvocationTargetException, InterruptedException {
1124: IPackageFragmentRoot root = addToClasspath(new Path(
1125: fNormalFolder));
1126: IPackageFragment fragment = root.createPackageFragment(
1127: fSubFolder, false, null);
1128: ICompilationUnit cu = createICompilationUnit("C", fragment);
1129:
1130: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 0);
1131: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1132: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1133: null);
1134: assertTrue(contains(newCu.getPath().removeFirstSegments(2),
1135: root.getRawClasspathEntry().getInclusionPatterns(),
1136: null));
1137:
1138: newCu = (ICompilationUnit) executeOperation(
1139: BuildpathModifierAction.UNINCLUDE, newCu, null, null,
1140: null, null);
1141:
1142: assertFalse(contains(newCu.getPath().removeFirstSegments(2),
1143: root.getRawClasspathEntry().getInclusionPatterns(),
1144: null));
1145: assertTrue(newCu.getPath().equals(cu.getPath()));
1146:
1147: validateClasspath();
1148: }
1149:
1150: public void testUnexcludeFile() throws CoreException,
1151: InvocationTargetException, InterruptedException {
1152: IPackageFragmentRoot root = addToClasspath(new Path(
1153: fNormalFolder));
1154: IPackageFragment fragment = root.createPackageFragment(
1155: fSubFolder, false, null);
1156: ICompilationUnit cu = createICompilationUnit("C", fragment);
1157:
1158: assertTrue(root.getRawClasspathEntry().getExclusionPatterns().length == 0);
1159: IFile excludedFile = (IFile) executeOperation(
1160: BuildpathModifierAction.EXCLUDE, cu, null, null, null,
1161: null);
1162:
1163: assertTrue(contains(excludedFile.getFullPath()
1164: .removeFirstSegments(2), root.getRawClasspathEntry()
1165: .getExclusionPatterns(), null));
1166: assertTrue(excludedFile.getFullPath().equals(cu.getPath()));
1167:
1168: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1169: BuildpathModifierAction.UNEXCLUDE, excludedFile, null,
1170: null, null, null);
1171:
1172: assertFalse(contains(excludedFile.getFullPath()
1173: .removeFirstSegments(2), root.getRawClasspathEntry()
1174: .getExclusionPatterns(), null));
1175: assertTrue(newCu.getPath().equals(cu.getPath()));
1176:
1177: validateClasspath();
1178: }
1179:
1180: public void testIncludeFileOnFolder() throws CoreException,
1181: InvocationTargetException, InterruptedException {
1182: IPackageFragmentRoot root = addToClasspath(new Path(
1183: fNormalFolder));
1184: IPackageFragment fragment2 = root.createPackageFragment(
1185: fSubFolder + "2", false, null);
1186: executeOperation(BuildpathModifierAction.INCLUDE, fragment2,
1187: null, null, null, null);
1188:
1189: IPackageFragment fragment = root.createPackageFragment(
1190: fSubFolder, false, null);
1191: ICompilationUnit cu = createICompilationUnit("C", fragment);
1192:
1193: // Note: the parent of cu is a folder which is not excluded! This is important as
1194: // 'include' on a cu which parent folder is explicitly excluded is not possible!
1195: // Therefore fragment2 had to be included to test this case!
1196: assertTrue(fProject.findElement(cu.getPath().makeRelative()) == null);
1197:
1198: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1199: BuildpathModifierAction.INCLUDE, cu
1200: .getUnderlyingResource(), null, null, null,
1201: null);
1202:
1203: assertTrue(contains(newCu.getPath().removeFirstSegments(2),
1204: root.getRawClasspathEntry().getInclusionPatterns(),
1205: null));
1206: assertTrue(newCu.getPath().equals(cu.getPath()));
1207:
1208: validateClasspath();
1209: }
1210:
1211: public void testExcludeFileOnFolder() throws CoreException,
1212: InvocationTargetException, InterruptedException {
1213: // Special case: there are 2 packages fSubFolder and fSubFolder2 where the fSubFolder2 is
1214: // included. Now we include the compilation unit from fSubFolder and then exlude it.
1215: // After inclusion, the returned object must be of type ICompilation unit,
1216: // after exclusion, the returned object must be of type IFile (because
1217: // only fSubFolder2 is included. We only test that the return type is correct because
1218: // the correctness of the filters has been tested before.
1219: IPackageFragmentRoot root = addToClasspath(new Path(
1220: fNormalFolder));
1221: IPackageFragment fragment2 = root.createPackageFragment(
1222: fSubFolder + "2", false, null);
1223: IPackageFragment fragment = root.createPackageFragment(
1224: fSubFolder, false, null);
1225: ICompilationUnit cu = createICompilationUnit("C", fragment);
1226:
1227: // include fragment2
1228: executeOperation(BuildpathModifierAction.INCLUDE, fragment2,
1229: null, null, null, null);
1230:
1231: // Check that the compilation unit cannot be found (because now its only
1232: // a normal file and not a CU).
1233: assertTrue(fProject.findElement(cu.getPath().makeRelative()) == null);
1234: // include the cu --> if cast fails, then include is broken
1235: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1236: BuildpathModifierAction.INCLUDE, cu
1237: .getUnderlyingResource(), null, null, null,
1238: null);
1239:
1240: // exclude the file --> if cast fails, then exclude is broken
1241: IFile excludedFile = (IFile) executeOperation(
1242: BuildpathModifierAction.EXCLUDE, newCu, null, null,
1243: null, null);
1244:
1245: assertTrue(excludedFile.getFullPath().equals(newCu.getPath()));
1246:
1247: validateClasspath();
1248: }
1249:
1250: public void testUnincludeFileOnFolder() throws CoreException,
1251: InvocationTargetException, InterruptedException {
1252: // Same situation as in testExcludeFileOnFolder, but this time, we use
1253: // uninclude instead of exclude
1254: IPackageFragmentRoot root = addToClasspath(new Path(
1255: fNormalFolder));
1256: IPackageFragment fragment2 = root.createPackageFragment(
1257: fSubFolder + "2", false, null);
1258: IPackageFragment fragment = root.createPackageFragment(
1259: fSubFolder, false, null);
1260: ICompilationUnit cu = createICompilationUnit("C", fragment);
1261:
1262: // include fragment2
1263: executeOperation(BuildpathModifierAction.INCLUDE, fragment2,
1264: null, null, null, null);
1265:
1266: // Check that the compilation unit cannot be found (because now its only
1267: // a normal file and not a CU).
1268: assertTrue(fProject.findElement(cu.getPath().makeRelative()) == null);
1269: // include the cu --> if cast fails, then include is broken
1270: ICompilationUnit newCu = (ICompilationUnit) executeOperation(
1271: BuildpathModifierAction.INCLUDE, cu
1272: .getUnderlyingResource(), null, null, null,
1273: null);
1274:
1275: // uninclude the file --> if cast fails, then uninclude is broken
1276: IFile file = (IFile) executeOperation(
1277: BuildpathModifierAction.UNINCLUDE, newCu, null, null,
1278: null, null);
1279:
1280: assertTrue(file.getFullPath().equals(cu.getPath()));
1281: validateClasspath();
1282: }
1283:
1284: public void testIncludeFileOnIncludedFragment()
1285: throws CoreException, InvocationTargetException,
1286: InterruptedException {
1287: IPackageFragmentRoot root = addToClasspath(new Path(
1288: fNormalFolder));
1289: IPackageFragment fragment = root.createPackageFragment(
1290: fSubFolder, false, null);
1291: ICompilationUnit cu = createICompilationUnit("C", fragment);
1292:
1293: fragment = (IPackageFragment) executeOperation(
1294: BuildpathModifierAction.INCLUDE, fragment, null, null,
1295: null, null);
1296: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 1);
1297:
1298: cu = (ICompilationUnit) executeOperation(
1299: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1300: null);
1301:
1302: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 2);
1303: assertTrue(contains(cu.getPath().removeFirstSegments(2), root
1304: .getRawClasspathEntry().getInclusionPatterns(), null));
1305:
1306: validateClasspath();
1307: }
1308:
1309: public void testExcludeFileOnIncludedFragment()
1310: throws CoreException, InvocationTargetException,
1311: InterruptedException {
1312: IPackageFragmentRoot root = addToClasspath(new Path(
1313: fNormalFolder));
1314: IPackageFragment fragment = root.createPackageFragment(
1315: fSubFolder, false, null);
1316: ICompilationUnit cu = createICompilationUnit("C", fragment);
1317:
1318: fragment = (IPackageFragment) executeOperation(
1319: BuildpathModifierAction.INCLUDE, fragment, null, null,
1320: null, null);
1321: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 1);
1322:
1323: IFile excludedFile = (IFile) executeOperation(
1324: BuildpathModifierAction.EXCLUDE, cu, null, null, null,
1325: null);
1326:
1327: assertTrue(root.getRawClasspathEntry().getInclusionPatterns().length == 1);
1328: assertFalse(contains(excludedFile.getFullPath()
1329: .removeFirstSegments(2), root.getRawClasspathEntry()
1330: .getInclusionPatterns(), null));
1331: assertTrue(contains(excludedFile.getFullPath()
1332: .removeFirstSegments(2), root.getRawClasspathEntry()
1333: .getExclusionPatterns(), null));
1334:
1335: validateClasspath();
1336: }
1337:
1338: public void testUnincludeOnIncludedFragment() throws CoreException,
1339: InvocationTargetException, InterruptedException {
1340: IPackageFragmentRoot root = addToClasspath(new Path(
1341: fNormalFolder));
1342: IPackageFragment fragment = root.createPackageFragment(
1343: fSubFolder, false, null);
1344: ICompilationUnit cu = createICompilationUnit("C", fragment);
1345:
1346: fragment = (IPackageFragment) executeOperation(
1347: BuildpathModifierAction.INCLUDE, fragment, null, null,
1348: null, null);
1349: cu = (ICompilationUnit) executeOperation(
1350: BuildpathModifierAction.INCLUDE, cu, null, null, null,
1351: null);
1352:
1353: cu = (ICompilationUnit) executeOperation(
1354: BuildpathModifierAction.UNINCLUDE, cu, null, null,
1355: null, null);
1356: assertFalse(contains(cu.getPath().removeFirstSegments(2), root
1357: .getRawClasspathEntry().getInclusionPatterns(), null));
1358:
1359: validateClasspath();
1360: }
1361:
1362: public void testUnexcludeOnIncludedFragment() throws CoreException,
1363: InvocationTargetException, InterruptedException {
1364: IPackageFragmentRoot root = addToClasspath(new Path(
1365: fNormalFolder));
1366: IPackageFragment fragment = root.createPackageFragment(
1367: fSubFolder, false, null);
1368: ICompilationUnit cu = createICompilationUnit("C", fragment);
1369:
1370: fragment = (IPackageFragment) executeOperation(
1371: BuildpathModifierAction.INCLUDE, fragment, null, null,
1372: null, null);
1373: IFile excludedFile = (IFile) executeOperation(
1374: BuildpathModifierAction.EXCLUDE, cu, null, null, null,
1375: null);
1376:
1377: cu = (ICompilationUnit) executeOperation(
1378: BuildpathModifierAction.UNEXCLUDE, excludedFile, null,
1379: null, null, null);
1380: assertFalse(contains(cu.getPath().removeFirstSegments(2), root
1381: .getRawClasspathEntry().getExclusionPatterns(), null));
1382: assertTrue(excludedFile.getFullPath().equals(cu.getPath()));
1383:
1384: validateClasspath();
1385: }
1386:
1387: // Helper methods
1388: protected Object executeOperation(
1389: int type,
1390: final Object selection,
1391: final ClasspathModifierQueries.OutputFolderQuery outputQuery,
1392: final ClasspathModifierQueries.IOutputLocationQuery locationQuery,
1393: final ClasspathModifierQueries.ICreateFolderQuery creationQuery,
1394: final ClasspathModifierQueries.IInclusionExclusionQuery inclQuery)
1395: throws InvocationTargetException, InterruptedException {
1396: return null;
1397: }
1398:
1399: protected IPackageFragmentRoot addToClasspath(IPath path)
1400: throws CoreException, InvocationTargetException,
1401: InterruptedException {
1402: IPath[] paths = getPaths();
1403: assertFalse(contains(path, paths, null));
1404:
1405: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
1406: BuildpathModifierAction.ADD_SEL_SF_TO_BP,
1407: getFolderHandle(path),
1408: getOutputFolderQueryInternal(fProject
1409: .getOutputLocation()), null, null, null);
1410:
1411: paths = getPaths();
1412: assertTrue(contains(root.getPath(), getPaths(), null));
1413: return root;
1414: }
1415:
1416: protected IPackageFragmentRoot addToClasspath(IJavaElement element)
1417: throws CoreException, InvocationTargetException,
1418: InterruptedException {
1419: IPath[] paths = getPaths();
1420: assertFalse(contains(element.getPath(), paths, null));
1421:
1422: IPackageFragmentRoot root = (IPackageFragmentRoot) executeOperation(
1423: BuildpathModifierAction.ADD_SEL_SF_TO_BP, element,
1424: getOutputFolderQueryInternal(fProject
1425: .getOutputLocation()), null, null, null);
1426:
1427: paths = getPaths();
1428: assertTrue(contains(element.getPath(), paths, null));
1429: return root;
1430: }
1431:
1432: protected IFolder includePackage() throws JavaModelException,
1433: CoreException, InvocationTargetException,
1434: InterruptedException {
1435: IPackageFragmentRoot root = addToClasspath(new Path(
1436: fNormalFolder));
1437: IFolder cpFolder = getFolderHandle(new Path(fNormalFolder));
1438:
1439: IFolder folder = getFolderHandle(cpFolder
1440: .getProjectRelativePath().append(fSubFolder));
1441:
1442: IClasspathEntry entry = root.getRawClasspathEntry();
1443: int before = entry.getInclusionPatterns().length;
1444:
1445: // include
1446: executeOperation(BuildpathModifierAction.INCLUDE, folder, null,
1447: null, null, null);
1448:
1449: entry = root.getRawClasspathEntry();
1450: IPath[] inclusionPatterns = entry.getInclusionPatterns();
1451: int after = inclusionPatterns.length;
1452: assertTrue(contains(new Path(folder.getName()),
1453: inclusionPatterns, null));
1454: assertTrue(before + 1 == after);
1455: return cpFolder;
1456: }
1457:
1458: protected IFolder excludePackage() throws JavaModelException,
1459: CoreException, InvocationTargetException,
1460: InterruptedException {
1461: IPackageFragmentRoot root = addToClasspath(new Path(
1462: fNormalFolder));
1463: IFolder cpFolder = getFolderHandle(new Path(fNormalFolder));
1464:
1465: IFolder folder = getFolderHandle(cpFolder
1466: .getProjectRelativePath().append(fSubFolder));
1467: IPackageFragment fragment = root.getPackageFragment(folder
1468: .getName());
1469:
1470: IClasspathEntry entry = root.getRawClasspathEntry();
1471: int nrExcluded = entry.getExclusionPatterns().length;
1472: executeOperation(BuildpathModifierAction.EXCLUDE, fragment,
1473: null, null, null, null);
1474:
1475: entry = root.getRawClasspathEntry();
1476: IPath[] exclusionPatterns = entry.getExclusionPatterns();
1477: assertTrue(nrExcluded + 1 == exclusionPatterns.length);
1478: assertTrue(contains(new Path(fragment.getElementName()),
1479: exclusionPatterns, null));
1480: return cpFolder;
1481: }
1482:
1483: protected IFolder getFolderHandle(IPath path) {
1484: IFolder folder = fProject.getProject().getFolder(path);
1485: try {
1486: if (!folder.exists())
1487: folder.create(true, false, null);
1488: } catch (CoreException e) {
1489: }
1490: return folder;
1491: }
1492:
1493: protected ICompilationUnit createICompilationUnit(String className,
1494: IPackageFragment fragment) throws JavaModelException {
1495: String packString = fragment.getElementName().equals("") ? fragment
1496: .getElementName()
1497: : "package " + fragment.getElementName() + ";\n";
1498: StringBuffer content = getFileContent(className, packString);
1499: return fragment.createCompilationUnit(className + ".java",
1500: content.toString(), false, null);
1501: }
1502:
1503: protected StringBuffer getFileContent(String className,
1504: String packageHeader) {
1505: StringBuffer buf = new StringBuffer();
1506: buf.append(packageHeader);
1507: buf.append("\n");
1508: buf.append("public class " + className + " {\n");
1509: buf.append(" public void foo() {\n");
1510: buf.append(" }\n");
1511: buf.append("}\n");
1512: return buf;
1513: }
1514:
1515: public ClasspathModifierQueries.OutputFolderQuery getOutputFolderQueryInternal(
1516: IPath desiredOutputLocation) throws JavaModelException {
1517: return new ClasspathModifierQueries.OutputFolderQuery(
1518: desiredOutputLocation) {
1519: public boolean doQuery(boolean b,
1520: OutputFolderValidator validator,
1521: IJavaProject project) {
1522: return true;
1523: }
1524:
1525: public IPath getOutputLocation() {
1526: IPath newOutputFolder = null;
1527: try {
1528: if (fProject.isOnClasspath(fProject
1529: .getUnderlyingResource())) {
1530: String outputFolderName = PreferenceConstants
1531: .getPreferenceStore()
1532: .getString(
1533: PreferenceConstants.SRCBIN_BINNAME);
1534: newOutputFolder = fProject.getPath().append(
1535: outputFolderName);
1536: return newOutputFolder;
1537: }
1538: } catch (JavaModelException e) {
1539: fail();
1540: }
1541: return null;
1542: }
1543:
1544: public boolean removeProjectFromClasspath() {
1545: return true;
1546: }
1547: };
1548: }
1549:
1550: public ClasspathModifierQueries.IOutputLocationQuery getOutputLocationQuery() {
1551: return new ClasspathModifierQueries.IOutputLocationQuery() {
1552:
1553: public boolean doQuery(CPListElement element) {
1554: return true;
1555: }
1556:
1557: public IPath getOutputLocation() {
1558: IPath oldOutputPath = null;
1559: try {
1560: oldOutputPath = fProject.getOutputLocation();
1561: } catch (JavaModelException e) {
1562: }
1563: return new Path(oldOutputPath.toString() + "2");
1564: }
1565:
1566: public ClasspathModifierQueries.OutputFolderQuery getOutputFolderQuery(
1567: IPath path) throws JavaModelException {
1568: return NewProjectWizardTest.this
1569: .getOutputFolderQueryInternal(fProject
1570: .getOutputLocation());
1571: }
1572: };
1573: }
1574:
1575: protected ClasspathModifierQueries.ICreateFolderQuery getSourceFolderCreationQuery() {
1576: return new ClasspathModifierQueries.ICreateFolderQuery() {
1577:
1578: public boolean doQuery() {
1579: return true;
1580: }
1581:
1582: public boolean isSourceFolder() {
1583: return true;
1584: }
1585:
1586: public IFolder getCreatedFolder() {
1587: return getFolderHandle(new Path(fSubFolder));
1588: }
1589:
1590: };
1591: }
1592:
1593: protected ClasspathModifierQueries.ICreateFolderQuery getNormalFolderCreationQuery() {
1594: return new ClasspathModifierQueries.ICreateFolderQuery() {
1595:
1596: public boolean doQuery() {
1597: return true;
1598: }
1599:
1600: public boolean isSourceFolder() {
1601: return false;
1602: }
1603:
1604: public IFolder getCreatedFolder() {
1605: return getFolderHandle(new Path(fNormalFolder));
1606: }
1607:
1608: };
1609: }
1610:
1611: protected IPath[] getPaths() throws JavaModelException {
1612: IClasspathEntry[] entries = fProject.getRawClasspath();
1613: IPath[] paths = new IPath[entries.length];
1614: for (int i = 0; i < entries.length; i++) {
1615: paths[i] = entries[i].getPath();
1616: }
1617: return paths;
1618: }
1619:
1620: protected void validateClasspath() throws JavaModelException {
1621: IJavaModelStatus status = JavaConventions.validateClasspath(
1622: fProject, fProject.getRawClasspath(), fProject
1623: .getOutputLocation());
1624: assertFalse(status.getSeverity() == IStatus.ERROR);
1625: }
1626:
1627: protected IPackageFragmentRoot getProjectRoot(IResource resource)
1628: throws JavaModelException {
1629: return ClasspathModifier.getFragmentRoot(resource, fProject,
1630: null);
1631: }
1632:
1633: protected static boolean contains(IPath path, IPath[] paths,
1634: IProgressMonitor monitor) {
1635: if (monitor == null)
1636: monitor = new NullProgressMonitor();
1637: if (path == null)
1638: return false;
1639: try {
1640: monitor
1641: .beginTask(
1642: NewWizardMessages.ClasspathModifier_Monitor_ComparePaths,
1643: paths.length);
1644: if (path.getFileExtension() == null)
1645: path = new Path(completeName(path.toString())); //$NON-NLS-1$
1646: for (int i = 0; i < paths.length; i++) {
1647: if (paths[i].equals(path))
1648: return true;
1649: monitor.worked(1);
1650: }
1651: } finally {
1652: monitor.done();
1653: }
1654: return false;
1655: }
1656:
1657: /**
1658: * Add a '/' at the end of the name if
1659: * it does not end with '.java'.
1660: *
1661: * @param name append '/' at the end if
1662: * necessary
1663: * @return modified string
1664: */
1665: private static String completeName(String name) {
1666: if (!name.endsWith(".java")) { //$NON-NLS-1$
1667: name = name + "/"; //$NON-NLS-1$
1668: name = name.replace('.', '/');
1669: return name;
1670: }
1671: return name;
1672: }
1673: }
|