001: /*******************************************************************************
002: * Copyright (c) 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.pde.ui.tests.wizards;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.jdt.core.JavaCore;
022: import org.eclipse.jface.operation.IRunnableWithProgress;
023: import org.eclipse.pde.core.plugin.IPluginBase;
024: import org.eclipse.pde.internal.core.FeatureModelManager;
025: import org.eclipse.pde.internal.core.PDECore;
026: import org.eclipse.pde.internal.core.ifeature.IFeature;
027: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
028: import org.eclipse.pde.internal.core.natures.PDE;
029: import org.eclipse.pde.internal.ui.wizards.feature.CreateFeaturePatchOperation;
030: import org.eclipse.pde.internal.ui.wizards.feature.CreateFeatureProjectOperation;
031: import org.eclipse.pde.internal.ui.wizards.feature.FeatureData;
032: import org.eclipse.pde.ui.tests.NewProjectTestCase;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.progress.IProgressService;
035:
036: public class NewFeatureProjectTestCase extends NewProjectTestCase {
037:
038: private static final String PROJECT_NAME = "com.junitTest.feature";
039: private static final FeatureData DEFAULT_FEATURE_DATA = new FeatureData();
040: static {
041: DEFAULT_FEATURE_DATA.id = PROJECT_NAME;
042: DEFAULT_FEATURE_DATA.name = PROJECT_NAME;
043: DEFAULT_FEATURE_DATA.version = "1.0.0";
044: }
045:
046: private static FeatureData createDefaultFeatureData() {
047: FeatureData fd = new FeatureData();
048: fd.id = DEFAULT_FEATURE_DATA.id;
049: fd.name = DEFAULT_FEATURE_DATA.name;
050: fd.version = DEFAULT_FEATURE_DATA.version;
051: return fd;
052: }
053:
054: public static Test suite() {
055: return new TestSuite(NewFeatureProjectTestCase.class);
056: }
057:
058: protected String getProjectName() {
059: return PROJECT_NAME;
060: }
061:
062: private void createFeature(FeatureData fd, boolean patch,
063: Object modelObject) {
064: if (fd == null)
065: fd = DEFAULT_FEATURE_DATA;
066: IProject project = ResourcesPlugin.getWorkspace().getRoot()
067: .getProject(PROJECT_NAME);
068: IPath path = Platform.getLocation();
069: IRunnableWithProgress op;
070: if ((patch && !(modelObject instanceof IFeatureModel))
071: || (!patch && modelObject != null && !(modelObject instanceof IPluginBase[])))
072: fail("Unaccepted model object passed...");
073:
074: if (patch)
075: op = new CreateFeaturePatchOperation(project, path, fd,
076: (IFeatureModel) modelObject, getShell());
077: else
078: op = new CreateFeatureProjectOperation(project, path, fd,
079: (IPluginBase[]) modelObject, getShell());
080: IProgressService progressService = PlatformUI.getWorkbench()
081: .getProgressService();
082: try {
083: progressService.runInUI(progressService, op, null);
084: } catch (InvocationTargetException e) {
085: fail("Feature creation failed...");
086: } catch (InterruptedException e) {
087: fail("Feature creation failed...");
088: }
089: }
090:
091: private void verifyFeatureNature() {
092: assertTrue("Verifying feature nature...",
093: hasNature(PDE.FEATURE_NATURE));
094: }
095:
096: public void testCreationFeatureProject() {
097: createFeature(DEFAULT_FEATURE_DATA, false, null);
098: verifyProjectExistence();
099: verifyFeatureNature();
100: }
101:
102: public void testCreationFeaturePatch() {
103: IFeatureModel[] models = PDECore.getDefault()
104: .getFeatureModelManager().getModels();
105: if (models.length == 0)
106: // cant test patches if no feature models exist
107: return;
108: createFeature(DEFAULT_FEATURE_DATA, true, models[0]);
109: verifyProjectExistence();
110: verifyFeatureNature();
111: }
112:
113: public void testFeatureProjectData() {
114: /*FeatureData fd = createDefaultFeatureData();
115: String library = "testLibrary";
116: fd.library = library;
117: String provider = "testProvider";
118: fd.provider = provider;
119: createFeature(fd, false, null);
120: IFeatureModel model = PDECore.getDefault().getFeatureModelManager().findFeatureModel(DEFAULT_FEATURE_DATA.id);
121: IFeature feature = model.getFeature();
122: assertTrue(feature.getVersion().equals(DEFAULT_FEATURE_DATA.version));
123: assertTrue(feature.getLabel().equals(DEFAULT_FEATURE_DATA.name));
124: assertTrue(feature.getId().equals(DEFAULT_FEATURE_DATA.id));
125: assertTrue(feature.getProviderName().equals(provider));
126: assertTrue(feature.getInstallHandler().getLibrary().equals(library));*/
127: }
128:
129: public void testSimpleFeature() {
130: createFeature(DEFAULT_FEATURE_DATA, false, null);
131: verifyProjectExistence();
132: assertFalse("Testing simple project for no java nature...",
133: hasNature(JavaCore.NATURE_ID));
134: }
135:
136: public void testJavaFeature() {
137: FeatureData fd = createDefaultFeatureData();
138: String library = "testLibrary";
139: fd.library = library;
140: createFeature(fd, false, null);
141: verifyProjectExistence();
142: assertTrue("Testing for existing java nature...",
143: hasNature(JavaCore.NATURE_ID));
144: }
145:
146: public void testPluginFeature() {
147: /*PluginModelManager manager = PDECore.getDefault().getModelManager();
148: IPluginModelBase model = manager.findModel("org.eclipse.pde.ui");
149: IPluginBase[] plugins = new IPluginBase[] { model.getPluginBase() };
150: createFeature(DEFAULT_FEATURE_DATA, false, plugins);
151: verifyProjectExistence();
152: FeatureModelManager featureManager = PDECore.getDefault().getFeatureModelManager();
153: IFeature pdeFeature = featureManager.findFeatureModel(DEFAULT_FEATURE_DATA.id).getFeature();
154: IFeaturePlugin[] fplugins = pdeFeature.getPlugins();
155: assertTrue("Testing number of feature plugins...", fplugins.length == 1);
156: assertTrue("Testing feature plugin id...", fplugins[0].getId().equals("org.eclipse.pde.ui"));*/
157: }
158:
159: public void testModelCount() {
160: /*FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager();
161: int numModels = manager.getModels().length;
162: createFeature(DEFAULT_FEATURE_DATA, false, null);
163: verifyProjectExistence();
164: int numModelsNew = manager.getModels().length;
165: assertTrue(numModels + 1 == numModelsNew);*/
166: }
167:
168: public void testMaskingFeature() {
169: FeatureModelManager manager = PDECore.getDefault()
170: .getFeatureModelManager();
171: int numModels = manager.getModels().length;
172: FeatureData fd = createDefaultFeatureData();
173: IFeature pdeFeature = manager.findFeatureModel(
174: "org.eclipse.pde").getFeature();
175: fd.id = pdeFeature.getId();
176: fd.version = pdeFeature.getVersion();
177: createFeature(fd, false, null);
178: verifyProjectExistence();
179: int numNewModels = manager.getModels().length;
180: int numInWorkspace = manager.getWorkspaceModels().length;
181: assertTrue(numModels == numNewModels);
182: assertTrue(numInWorkspace == 1);
183: }
184: }
|