1 /******************************************************************************* 2 * Copyright (c) 2008, 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.equinox.p2.tests.touchpoint.eclipse; 15 16 import java.io.File; 17 import java.net.URI; 18 import java.util.*; 19 import org.eclipse.core.runtime.NullProgressMonitor; 20 import org.eclipse.equinox.frameworkadmin.BundleInfo; 21 import org.eclipse.equinox.internal.p2.engine.InstallableUnitOperand; 22 import org.eclipse.equinox.internal.p2.touchpoint.eclipse.EclipseTouchpoint; 23 import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Util; 24 import org.eclipse.equinox.internal.p2.touchpoint.eclipse.actions.ActionConstants; 25 import org.eclipse.equinox.internal.p2.touchpoint.eclipse.actions.MarkStartedAction; 26 import org.eclipse.equinox.internal.provisional.frameworkadmin.Manipulator; 27 import org.eclipse.equinox.p2.engine.IProfile; 28 import org.eclipse.equinox.p2.metadata.IArtifactKey; 29 import org.eclipse.equinox.p2.metadata.IInstallableUnit; 30 import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction; 31 import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; 32 import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository; 33 import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; 34 import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; 35 import org.eclipse.osgi.service.resolver.BundleDescription; 36 37 public class MarkStartedActionTest extends AbstractProvisioningTest { 38 MarkStartedActionTest(String name)39 public MarkStartedActionTest(String name) { 40 super(name); 41 } 42 MarkStartedActionTest()43 public MarkStartedActionTest() { 44 super(""); 45 } 46 testExecuteUndo()47 public void testExecuteUndo() throws Exception { 48 Map<String, String> profileProperties = new HashMap<>(); 49 File installFolder = getTempFolder(); 50 profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); 51 profileProperties.put(IProfile.PROP_CACHE, installFolder.toString()); 52 IProfile profile = createProfile("test", profileProperties); 53 54 IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); 55 File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); 56 File targetPlugins = new File(installFolder, "plugins"); 57 assertTrue(targetPlugins.mkdir()); 58 File osgiTarget = new File(targetPlugins, "org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); 59 copy("2.0", osgiSource, osgiTarget); 60 61 BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); 62 IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); 63 IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); 64 IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); 65 bundlePool.addDescriptor(descriptor, new NullProgressMonitor()); 66 67 Map<String, Object> parameters = new HashMap<>(); 68 parameters.put(ActionConstants.PARM_AGENT, getAgent()); 69 parameters.put(ActionConstants.PARM_PROFILE, profile); 70 EclipseTouchpoint touchpoint = new EclipseTouchpoint(); 71 touchpoint.initializePhase(null, profile, "test", parameters); 72 InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); 73 parameters.put("iu", operand.second()); 74 touchpoint.initializeOperand(profile, parameters); 75 76 parameters.put(ActionConstants.PARM_STARTED, Boolean.TRUE.toString()); 77 parameters = Collections.unmodifiableMap(parameters); 78 79 Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); 80 assertNotNull(manipulator); 81 82 BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); 83 manipulator.getConfigData().addBundle(bundleInfo); 84 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 85 86 MarkStartedAction action = new MarkStartedAction(); 87 action.execute(parameters); 88 assertTrue(isMarkedStarted(manipulator, osgiTarget, true)); 89 action.undo(parameters); 90 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 91 } 92 testExecuteUndoWithMissingArtifact()93 public void testExecuteUndoWithMissingArtifact() throws Exception { 94 Map<String, String> profileProperties = new HashMap<>(); 95 File installFolder = getTempFolder(); 96 profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); 97 profileProperties.put(IProfile.PROP_CACHE, installFolder.toString()); 98 IProfile profile = createProfile("test", profileProperties); 99 100 IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); 101 File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); 102 File targetPlugins = new File(installFolder, "plugins"); 103 assertTrue(targetPlugins.mkdir()); 104 File osgiTarget = new File(targetPlugins, "org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar"); 105 copy("2.0", osgiSource, osgiTarget); 106 107 BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); 108 IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); 109 IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); 110 IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); 111 bundlePool.addDescriptor(descriptor, new NullProgressMonitor()); 112 113 Map<String, Object> parameters = new HashMap<>(); 114 parameters.put(ActionConstants.PARM_AGENT, getAgent()); 115 parameters.put(ActionConstants.PARM_PROFILE, profile); 116 EclipseTouchpoint touchpoint = new EclipseTouchpoint(); 117 touchpoint.initializePhase(null, profile, "test", parameters); 118 InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); 119 parameters.put("iu", operand.second()); 120 touchpoint.initializeOperand(profile, parameters); 121 122 parameters.put(ActionConstants.PARM_STARTED, Boolean.TRUE.toString()); 123 parameters = Collections.unmodifiableMap(parameters); 124 125 Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); 126 assertNotNull(manipulator); 127 128 BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); 129 manipulator.getConfigData().addBundle(bundleInfo); 130 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 131 132 // let's remove the artifact now 133 bundlePool.removeDescriptor(descriptor, new NullProgressMonitor()); 134 135 MarkStartedAction action = new MarkStartedAction(); 136 action.execute(parameters); 137 assertTrue(isMarkedStarted(manipulator, osgiTarget, true)); 138 action.undo(parameters); 139 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 140 } 141 testExecuteOnFragmentBundleResultsInBundleNotBeingMarkedStarted()142 public void testExecuteOnFragmentBundleResultsInBundleNotBeingMarkedStarted() throws Exception { 143 Map<String, String> profileProperties = new HashMap<>(); 144 File installFolder = getTempFolder(); 145 profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); 146 profileProperties.put(IProfile.PROP_CACHE, installFolder.toString()); 147 IProfile profile = createProfile("test", profileProperties); 148 149 IFileArtifactRepository bundlePool = Util.getBundlePoolRepository(getAgent(), profile); 150 File osgiSource = getTestData("1.0", "/testData/eclipseTouchpoint/bundles/org.eclipse.osgi.fragment_1.0.0.jar"); 151 File targetPlugins = new File(installFolder, "plugins"); 152 assertTrue(targetPlugins.mkdir()); 153 File osgiTarget = new File(targetPlugins, "org.eclipse.osgi.fragment_1.0.0.jar"); 154 copy("2.0", osgiSource, osgiTarget); 155 156 BundleDescription bundleDescription = BundlesAction.createBundleDescription(osgiTarget); 157 IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), bundleDescription.getVersion().toString()); 158 IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, osgiTarget); 159 IInstallableUnit iu = createBundleIU(bundleDescription, osgiTarget.isDirectory(), key); 160 bundlePool.addDescriptor(descriptor, new NullProgressMonitor()); 161 162 Map<String, Object> parameters = new HashMap<>(); 163 parameters.put(ActionConstants.PARM_AGENT, getAgent()); 164 parameters.put(ActionConstants.PARM_PROFILE, profile); 165 EclipseTouchpoint touchpoint = new EclipseTouchpoint(); 166 touchpoint.initializePhase(null, profile, "test", parameters); 167 InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); 168 parameters.put("iu", operand.second()); 169 touchpoint.initializeOperand(profile, parameters); 170 171 parameters.put(ActionConstants.PARM_STARTED, Boolean.TRUE.toString()); 172 parameters = Collections.unmodifiableMap(parameters); 173 174 Manipulator manipulator = (Manipulator) parameters.get(EclipseTouchpoint.PARM_MANIPULATOR); 175 assertNotNull(manipulator); 176 177 BundleInfo bundleInfo = Util.createBundleInfo(osgiTarget, iu); 178 manipulator.getConfigData().addBundle(bundleInfo); 179 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 180 181 MarkStartedAction action = new MarkStartedAction(); 182 action.execute(parameters); 183 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 184 action.undo(parameters); 185 assertTrue(isMarkedStarted(manipulator, osgiTarget, false)); 186 } 187 isMarkedStarted(Manipulator manipulator, File osgiTarget, boolean started)188 private boolean isMarkedStarted(Manipulator manipulator, File osgiTarget, boolean started) { 189 URI location = osgiTarget.toURI(); 190 BundleInfo[] bundles = manipulator.getConfigData().getBundles(); 191 for (BundleInfo bundle : bundles) { 192 if (location.equals(bundle.getLocation()) && (started == bundle.isMarkedAsStarted())) { 193 return true; 194 } 195 } 196 return false; 197 } 198 }