001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.pde.ui.tests.imports;
011:
012: import junit.framework.Test;
013: import junit.framework.TestSuite;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.NullProgressMonitor;
017: import org.eclipse.core.runtime.OperationCanceledException;
018: import org.eclipse.jdt.core.JavaCore;
019: import org.eclipse.pde.internal.core.PDECore;
020: import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
021: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
022: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
023: import org.eclipse.pde.internal.core.natures.PDE;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.wizards.imports.FeatureImportOperation;
026: import org.eclipse.pde.internal.ui.wizards.imports.FeatureImportWizard.ReplaceQuery;
027: import org.eclipse.pde.ui.tests.NewProjectTestCase;
028:
029: public class ImportFeatureProjectsTestCase extends NewProjectTestCase {
030:
031: private String fProjectName;
032:
033: public static Test suite() {
034: return new TestSuite(ImportFeatureProjectsTestCase.class);
035: }
036:
037: protected void tearDown() {
038: fProjectName = null;
039: super .tearDown();
040: }
041:
042: protected String getProjectName() {
043: return fProjectName;
044: }
045:
046: private void lookingAtProject(IFeatureModel model) {
047: String name = model.getFeature().getId();
048:
049: IFeaturePlugin[] plugins = model.getFeature().getPlugins();
050: for (int i = 0; i < plugins.length; i++) {
051: if (name.equals(plugins[i].getId())) {
052: name += "-feature"; //$NON-NLS-1$
053: break;
054: }
055:
056: }
057: fProjectName = name;
058: }
059:
060: private void importFeature(IFeatureModel[] models, boolean binary) {
061: FeatureImportOperation op = new FeatureImportOperation(models,
062: binary, null, new ReplaceQuery(getShell()));
063: try {
064: PDEPlugin.getWorkspace().run(op, new NullProgressMonitor());
065: if (models.length > 0)
066: lookingAtProject(models[0]);
067: } catch (OperationCanceledException e) {
068: fail("Feature import failed...");
069: } catch (CoreException e) {
070: fail("Feature import failed...");
071: }
072: }
073:
074: private void verifyNatures() {
075: IFeatureModel[] imported = PDECore.getDefault()
076: .getFeatureModelManager().getWorkspaceModels();
077: for (int i = 0; i < imported.length; i++) {
078: lookingAtProject(imported[i]);
079: assertTrue("Verifying feature nature...",
080: hasNature(PDE.FEATURE_NATURE));
081: IFeatureInstallHandler installHandler = imported[i]
082: .getFeature().getInstallHandler();
083: boolean shouldHaveJavaNature = installHandler != null ? installHandler
084: .getLibrary() != null
085: : false;
086: assertTrue(
087: "Verifying java nature...",
088: hasNature(JavaCore.NATURE_ID) == shouldHaveJavaNature);
089: }
090: }
091:
092: private void verifyFeature(boolean isBinary) {
093: IFeatureModel[] imported = PDECore.getDefault()
094: .getFeatureModelManager().getWorkspaceModels();
095: for (int i = 0; i < imported.length; i++) {
096: lookingAtProject(imported[i]);
097: try {
098: assertTrue(
099: "Verifing feature is binary...",
100: isBinary == PDECore.BINARY_PROJECT_VALUE
101: .equals(getProject()
102: .getPersistentProperty(
103: PDECore.EXTERNAL_PROJECT_PROPERTY)));
104: } catch (CoreException e) {
105: }
106: }
107: }
108:
109: protected void verifyProjectExistence() {
110: IFeatureModel[] imported = PDECore.getDefault()
111: .getFeatureModelManager().getWorkspaceModels();
112: for (int i = 0; i < imported.length; i++) {
113: lookingAtProject(imported[i]);
114: super .verifyProjectExistence();
115: }
116: }
117:
118: public void testImportFeature() {
119: IFeatureModel[] model = PDECore.getDefault()
120: .getFeatureModelManager().getModels();
121: if (model.length == 0)
122: return;
123: boolean binary = false;
124: importFeature(new IFeatureModel[] { model[0] }, binary);
125: verifyProjectExistence();
126: verifyNatures();
127: verifyFeature(binary);
128: }
129:
130: public void testImportBinaryFeature() {
131: IFeatureModel[] model = PDECore.getDefault()
132: .getFeatureModelManager().getModels();
133: if (model.length == 0)
134: return;
135: boolean binary = true;
136: importFeature(new IFeatureModel[] { model[0] }, binary);
137: verifyProjectExistence();
138: verifyNatures();
139: verifyFeature(binary);
140: }
141:
142: public void testImportMulitpleFeatures() {
143: IFeatureModel[] models = PDECore.getDefault()
144: .getFeatureModelManager().getModels();
145: if (models.length == 0)
146: return;
147: boolean binary = false;
148: importFeature(models, binary);
149: verifyProjectExistence();
150: verifyNatures();
151: verifyFeature(binary);
152: IFeatureModel[] imported = PDECore.getDefault()
153: .getFeatureModelManager().getWorkspaceModels();
154: assertTrue("Verifing number models imported...",
155: imported.length == models.length);
156: }
157:
158: public void testFeaturePlugins() {
159: IFeatureModel[] model = PDECore.getDefault()
160: .getFeatureModelManager().getModels();
161: if (model.length == 0)
162: return;
163: boolean binary = false;
164: importFeature(new IFeatureModel[] { model[0] }, binary);
165: verifyProjectExistence();
166: verifyNatures();
167: verifyFeature(binary);
168: IFeatureModel[] imported = PDECore.getDefault()
169: .getFeatureModelManager().getWorkspaceModels();
170: assertTrue("Verifing number models imported...",
171: imported.length == 1);
172: IFeaturePlugin[] plugins = model[0].getFeature().getPlugins();
173: if (plugins != null) {
174: IFeaturePlugin[] importedFeaturePlugins = getFeaturePluginsFrom(
175: model[0].getFeature().getId(), imported);
176: assertNotNull("Verifying feature plugins exist...",
177: importedFeaturePlugins);
178: assertTrue("Verifying total equal feature plugins...",
179: plugins.length == importedFeaturePlugins.length);
180: }
181: }
182:
183: private IFeaturePlugin[] getFeaturePluginsFrom(String id,
184: IFeatureModel[] imported) {
185: for (int i = 0; i < imported.length; i++)
186: if (imported[i].getFeature().getId().equals(id))
187: return imported[0].getFeature().getPlugins();
188: return null;
189: }
190:
191: }
|