blob: 64ccd35799efcd1a451741e4e3dece5ac8581261 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2008 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.director;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.internal.p2.director.DirectorActivator;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.core.location.AgentLocation;
import org.eclipse.equinox.internal.provisional.p2.director.IDirector;
import org.eclipse.equinox.internal.provisional.p2.director.ProfileChangeRequest;
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningContext;
import org.eclipse.equinox.internal.provisional.p2.metadata.*;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.p2.tests.TestActivator;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.osgi.framework.Version;
public class RollbackTest extends AbstractProvisioningTest {
IInstallableUnit a1;
IInstallableUnit b1;
IInstallableUnit c1;
IInstallableUnit d1;
private IProfile profile;
private IDirector director;
protected void setUp() throws Exception {
super.setUp();
a1 = createIU("A", DEFAULT_VERSION, true);
b1 = createIU("B", DEFAULT_VERSION, true);
c1 = createIU("C", DEFAULT_VERSION, true);
RequiredCapability[] req = new RequiredCapability[1];
req[0] = MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, "A", VersionRange.emptyRange, null, false, false, true);
d1 = createIU("D", new Version("1.0.0"), req);
createTestMetdataRepository(new IInstallableUnit[] {a1, b1, c1, d1});
profile = createProfile("TestProfile." + getName());
director = createDirector();
try {
IMetadataRepository rollbackRepo = getRollbackRepository();
if (rollbackRepo != null)
rollbackRepo.removeAll();
} catch (ProvisionException e) {
return;
}
}
private IMetadataRepository getRollbackRepository() throws ProvisionException {
try {
IMetadataRepositoryManager repoMan = (IMetadataRepositoryManager) ServiceHelper.getService(TestActivator.getContext(), IMetadataRepositoryManager.class.getName());
URL location = ((AgentLocation) ServiceHelper.getService(DirectorActivator.context, AgentLocation.class.getName())).getDataArea(DirectorActivator.PI_DIRECTOR);
return repoMan.loadRepository(new URL(location, "rollback"), null);
} catch (MalformedURLException e) {
fail("0.02", e);
return null;
}
}
private void assertEmptyRollbackRepo() {
try {
IMetadataRepository rollbackRepo = getRollbackRepository();
IInstallableUnit[] ius = (IInstallableUnit[]) rollbackRepo.query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("0.01", 0, ius.length);
} catch (ProvisionException e) {
return;
}
}
public void testRollbackProfileProperties() throws ProvisionException {
assertEmptyRollbackRepo();
ProfileChangeRequest request1 = new ProfileChangeRequest(profile);
request1.setProfileProperty("test1", "test");
request1.setProfileProperty("test2", "test");
IStatus status = director.provision(request1, null, new NullProgressMonitor());
assertEquals("1.0", IStatus.OK, status.getCode());
assertEquals("2.0", "test", profile.getProperty("test1"));
assertEquals("3.0", "test", profile.getProperty("test2"));
IInstallableUnit[] ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("4.0", 1, ius.length);
IInstallableUnit emptyProfileIU = ius[0];
ProfileChangeRequest request2 = new ProfileChangeRequest(profile);
request2.removeProfileProperty("test1");
request2.setProfileProperty("test2", "bad");
request2.setProfileProperty("test3", "test");
status = director.provision(request2, null, new NullProgressMonitor());
assertEquals("5.0", IStatus.OK, status.getCode());
assertEquals("6.0", null, profile.getProperty("test1"));
assertEquals("7.0", "bad", profile.getProperty("test2"));
assertEquals("8.0", "test", profile.getProperty("test3"));
ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("9.0", 2, ius.length);
IInstallableUnit revertProfileIU = null;
for (int i = 0; i < ius.length; i++) {
if (!ius[i].equals(emptyProfileIU))
revertProfileIU = ius[i];
}
status = director.revert(revertProfileIU, profile, new ProvisioningContext(), new NullProgressMonitor());
assertEquals("10.0", IStatus.OK, status.getCode());
assertEquals("11.0", "test", profile.getProperty("test1"));
assertEquals("12.0", "test", profile.getProperty("test2"));
assertEquals("13.0", null, profile.getProperty("test3"));
}
public void testRollbackIUs() throws ProvisionException {
assertEmptyRollbackRepo();
ProfileChangeRequest request1 = new ProfileChangeRequest(profile);
request1.addInstallableUnits(new IInstallableUnit[] {a1});
request1.addInstallableUnits(new IInstallableUnit[] {b1});
IStatus status = director.provision(request1, null, new NullProgressMonitor());
assertEquals("1.0", IStatus.OK, status.getCode());
List profileIUs = new ArrayList(profile.query(InstallableUnitQuery.ANY, new Collector(), null).toCollection());
assertTrue("2.0", profileIUs.contains(a1));
assertTrue("3.0", profileIUs.contains(b1));
IInstallableUnit[] ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("4.0", 1, ius.length);
IInstallableUnit emptyProfileIU = ius[0];
ProfileChangeRequest request2 = new ProfileChangeRequest(profile);
request2.removeInstallableUnits(new IInstallableUnit[] {a1});
request2.addInstallableUnits(new IInstallableUnit[] {c1});
status = director.provision(request2, null, new NullProgressMonitor());
assertEquals("5.0", IStatus.OK, status.getCode());
profileIUs = new ArrayList(profile.query(InstallableUnitQuery.ANY, new Collector(), null).toCollection());
assertFalse("6.0", profileIUs.contains(a1));
assertTrue("7.0", profileIUs.contains(b1));
assertTrue("8.0", profileIUs.contains(c1));
ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("9.0", 2, ius.length);
IInstallableUnit revertProfileIU = null;
for (int i = 0; i < ius.length; i++) {
if (!ius[i].equals(emptyProfileIU))
revertProfileIU = ius[i];
}
status = director.revert(revertProfileIU, profile, new ProvisioningContext(), new NullProgressMonitor());
assertEquals("10.0", IStatus.OK, status.getCode());
profileIUs = new ArrayList(profile.query(InstallableUnitQuery.ANY, new Collector(), null).toCollection());
assertTrue("11.0", profileIUs.contains(a1));
assertTrue("12.0", profileIUs.contains(b1));
assertFalse("13.0", profileIUs.contains(c1));
}
public void testRollbackIUProfileProperties() throws ProvisionException {
assertEmptyRollbackRepo();
ProfileChangeRequest request1 = new ProfileChangeRequest(profile);
request1.addInstallableUnits(new IInstallableUnit[] {a1});
request1.setInstallableUnitProfileProperty(a1, "test1", "test");
request1.setInstallableUnitProfileProperty(a1, "test2", "test");
IStatus status = director.provision(request1, null, new NullProgressMonitor());
assertEquals("1.0", IStatus.OK, status.getCode());
assertEquals("2.0", "test", profile.getInstallableUnitProperty(a1, "test1"));
assertEquals("3.0", "test", profile.getInstallableUnitProperty(a1, "test2"));
IInstallableUnit[] ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("4.0", 1, ius.length);
IInstallableUnit emptyProfileIU = ius[0];
ProfileChangeRequest request2 = new ProfileChangeRequest(profile);
request2.removeInstallableUnitProfileProperty(a1, "test1");
request2.setInstallableUnitProfileProperty(a1, "test2", "bad");
request2.setInstallableUnitProfileProperty(a1, "test3", "test");
status = director.provision(request2, null, new NullProgressMonitor());
assertEquals("5.0", IStatus.OK, status.getCode());
assertEquals("6.0", null, profile.getInstallableUnitProperty(a1, "test1"));
assertEquals("7.0", "bad", profile.getInstallableUnitProperty(a1, "test2"));
assertEquals("8.0", "test", profile.getInstallableUnitProperty(a1, "test3"));
ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("9.0", 2, ius.length);
IInstallableUnit revertProfileIU = null;
for (int i = 0; i < ius.length; i++) {
if (!ius[i].equals(emptyProfileIU))
revertProfileIU = ius[i];
}
status = director.revert(revertProfileIU, profile, new ProvisioningContext(), new NullProgressMonitor());
assertEquals("10.0", IStatus.OK, status.getCode());
assertEquals("11.0", "test", profile.getInstallableUnitProperty(a1, "test1"));
assertEquals("12.0", "test", profile.getInstallableUnitProperty(a1, "test2"));
assertEquals("13.0", null, profile.getInstallableUnitProperty(a1, "test3"));
}
public void testRollbackDependentIUProfileProperties() throws ProvisionException {
assertEmptyRollbackRepo();
ProfileChangeRequest request1 = new ProfileChangeRequest(profile);
request1.addInstallableUnits(new IInstallableUnit[] {d1});
request1.setInstallableUnitProfileProperty(d1, "test1", "test");
request1.setInstallableUnitProfileProperty(a1, "test2", "test");
IStatus status = director.provision(request1, null, new NullProgressMonitor());
assertEquals("1.0", IStatus.OK, status.getCode());
assertEquals("2.0", "test", profile.getInstallableUnitProperty(d1, "test1"));
assertEquals("3.0", "test", profile.getInstallableUnitProperty(a1, "test2"));
IInstallableUnit[] ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("4.0", 1, ius.length);
IInstallableUnit emptyProfileIU = ius[0];
ProfileChangeRequest request2 = new ProfileChangeRequest(profile);
request2.removeInstallableUnits(new IInstallableUnit[] {d1});
request2.addInstallableUnits(new IInstallableUnit[] {b1});
request2.setInstallableUnitProfileProperty(b1, "test3", "test");
status = director.provision(request2, null, new NullProgressMonitor());
assertEquals("5.0", IStatus.OK, status.getCode());
assertEquals("6.0", null, profile.getInstallableUnitProperty(d1, "test1"));
assertEquals("7.0", null, profile.getInstallableUnitProperty(a1, "test2"));
assertEquals("8.0", "test", profile.getInstallableUnitProperty(b1, "test3"));
ius = (IInstallableUnit[]) getRollbackRepository().query(InstallableUnitQuery.ANY, new Collector(), null).toArray(IInstallableUnit.class);
assertEquals("9.0", 2, ius.length);
IInstallableUnit revertProfileIU = null;
for (int i = 0; i < ius.length; i++) {
if (!ius[i].equals(emptyProfileIU))
revertProfileIU = ius[i];
}
status = director.revert(revertProfileIU, profile, new ProvisioningContext(), new NullProgressMonitor());
assertEquals("10.0", IStatus.OK, status.getCode());
assertEquals("11.0", "test", profile.getInstallableUnitProperty(d1, "test1"));
assertEquals("12.0", "test", profile.getInstallableUnitProperty(a1, "test2"));
assertEquals("13.0", null, profile.getInstallableUnitProperty(b1, "test3"));
}
}