/******************************************************************************* * Copyright (c) 2008, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.equinox.p2.tests.touchpoint.eclipse; import java.io.File; import java.net.URI; import java.util.*; import org.eclipse.equinox.frameworkadmin.BundleInfo; import org.eclipse.equinox.internal.p2.engine.InstallableUnitOperand; import org.eclipse.equinox.internal.p2.touchpoint.eclipse.EclipseTouchpoint; import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Util; import org.eclipse.equinox.internal.p2.touchpoint.eclipse.actions.ActionConstants; import org.eclipse.equinox.internal.p2.touchpoint.eclipse.actions.SetStartLevelAction; import org.eclipse.equinox.internal.provisional.frameworkadmin.Manipulator; import org.eclipse.equinox.p2.engine.IProfile; import org.eclipse.equinox.p2.metadata.IArtifactKey; import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction; import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository; import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.service.resolver.BundleDescription; public class SetStartLevelActionTest extends AbstractProvisioningTest { public SetStartLevelActionTest(String name) { super(name); } public SetStartLevelActionTest() { super(""); } public void testExecuteUndo() { Properties profileProperties = new Properties(); File installFolder = getTempFolder(); profileProperties.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); profileProperties.setProperty(IProfile.PROP_CACHE, installFolder.toString()); IProfile profile = createProfile("test", profileProperties); IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); File targetPlugins = new File(installFolder, "plugins"); assertTrue(targetPlugins.mkdir()); File osgiTarget = new File(targetPlugins, "org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); copy("2.0", osgiSource, osgiTarget); BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); bundlePool.addDescriptor(descriptor); Map parameters = new HashMap(); parameters.put(ActionConstants.PARM_AGENT, getAgent()); parameters.put(ActionConstants.PARM_PROFILE, profile); EclipseTouchpoint touchpoint = new EclipseTouchpoint(); touchpoint.initializePhase(null, profile, "test", parameters); InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); parameters.put("iu", operand.second()); touchpoint.initializeOperand(profile, parameters); parameters.put(ActionConstants.PARM_START_LEVEL, "0"); parameters = Collections.unmodifiableMap(parameters); Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); assertNotNull(manipulator); BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); manipulator.getConfigData().addBundle(bundleInfo); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); SetStartLevelAction action = new SetStartLevelAction(); action.execute(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, 0)); action.undo(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); } public void testExecuteUndoWithMissingArtifact() { Properties profileProperties = new Properties(); File installFolder = getTempFolder(); profileProperties.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); profileProperties.setProperty(IProfile.PROP_CACHE, installFolder.toString()); IProfile profile = createProfile("test", profileProperties); IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); File targetPlugins = new File(installFolder, "plugins"); assertTrue(targetPlugins.mkdir()); File osgiTarget = new File(targetPlugins, "org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); copy("2.0", osgiSource, osgiTarget); BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); bundlePool.addDescriptor(descriptor); Map parameters = new HashMap(); parameters.put(ActionConstants.PARM_AGENT, getAgent()); parameters.put(ActionConstants.PARM_PROFILE, profile); EclipseTouchpoint touchpoint = new EclipseTouchpoint(); touchpoint.initializePhase(null, profile, "test", parameters); InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); parameters.put("iu", operand.second()); touchpoint.initializeOperand(profile, parameters); parameters.put(ActionConstants.PARM_START_LEVEL, "0"); parameters = Collections.unmodifiableMap(parameters); Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); assertNotNull(manipulator); BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); manipulator.getConfigData().addBundle(bundleInfo); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); // let's remove the artifact now bundlePool.removeDescriptor(descriptor); SetStartLevelAction action = new SetStartLevelAction(); action.execute(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, 0)); action.undo(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); } public void testExecuteOnFragmentBundleResultsInBundleNotBeingMarkedStarted() { Properties profileProperties = new Properties(); File installFolder = getTempFolder(); profileProperties.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); profileProperties.setProperty(IProfile.PROP_CACHE, installFolder.toString()); IProfile profile = createProfile("test", profileProperties); IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi.fragment_1.0.0.jar"); File targetPlugins = new File(installFolder, "plugins"); assertTrue(targetPlugins.mkdir()); File osgiTarget = new File(targetPlugins, "org.eclipse.osgi.fragment_1.0.0.jar"); copy("2.0", osgiSource, osgiTarget); BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); bundlePool.addDescriptor(descriptor); Map parameters = new HashMap(); parameters.put(ActionConstants.PARM_AGENT, getAgent()); parameters.put(ActionConstants.PARM_PROFILE, profile); EclipseTouchpoint touchpoint = new EclipseTouchpoint(); touchpoint.initializePhase(null, profile, "test", parameters); InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); parameters.put("iu", operand.second()); touchpoint.initializeOperand(profile, parameters); parameters.put(ActionConstants.PARM_START_LEVEL, "0"); parameters = Collections.unmodifiableMap(parameters); Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); assertNotNull(manipulator); BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); manipulator.getConfigData().addBundle(bundleInfo); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); SetStartLevelAction action = new SetStartLevelAction(); action.execute(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); action.undo(parameters); assertTrue(isStartLevel(manipulator, osgiTarget, -1)); } private boolean isStartLevel(Manipulator manipulator, File osgiTarget, int startLevel) { URI location = osgiTarget.toURI(); BundleInfo[] bundles = manipulator.getConfigData().getBundles(); for (int i = 0; i < bundles.length; i++) { if (location.equals(bundles[i].getLocation()) && (startLevel == bundles[i].getStartLevel())) return true; } return false; } }