Skip to content

Commit

Permalink
Add IMetadataRepository.removeReferences()
Browse files Browse the repository at this point in the history
+ Clean-up the affected classes at a few occasions
+ Make LocalMetadataRepository.repositories a LinkedHashSet to ensure a
stable order of repository references, which simplifies manual
inspection of created content.xml files.
  • Loading branch information
HannesWell committed Oct 8, 2023
1 parent 3bac97b commit ff24c14
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,7 @@ private static File getActualLocation(URI location, String extension) {
// return new File(spec + extension);
return spec;
}
if (path.endsWith("/")) //$NON-NLS-1$
path += CompositeMetadataRepositoryFactory.CONTENT_FILENAME;
else
path += "/" + CompositeMetadataRepositoryFactory.CONTENT_FILENAME; //$NON-NLS-1$
return new File(path + extension);
return new File(path, CompositeMetadataRepositoryFactory.CONTENT_FILENAME + extension);
}

public static File getActualLocation(URI location) {
Expand All @@ -262,11 +258,17 @@ public synchronized void addReferences(Collection<? extends IRepositoryReference
throw new UnsupportedOperationException("Cannot add References to a composite repository"); //$NON-NLS-1$
}

@Override
public synchronized boolean removeReferences(Collection<? extends IRepositoryReference> references) {
throw new UnsupportedOperationException("Cannot remove References to a composite repository"); //$NON-NLS-1$
}

@Override
public Collection<IRepositoryReference> getReferences() {
HashSet<IRepositoryReference> allRefs = new HashSet<>();
for (IMetadataRepository child : loadedRepos)
Set<IRepositoryReference> allRefs = new HashSet<>();
for (IMetadataRepository child : loadedRepos) {
allRefs.addAll(child.getReferences());
}
return allRefs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
*/
public class LocalMetadataRepository extends AbstractMetadataRepository implements IIndexProvider<IInstallableUnit> {

static final private String CONTENT_FILENAME = "content"; //$NON-NLS-1$
static final private String REPOSITORY_TYPE = LocalMetadataRepository.class.getName();
static final private Integer REPOSITORY_VERSION = 1;
static final private String JAR_EXTENSION = ".jar"; //$NON-NLS-1$
static final private String XML_EXTENSION = ".xml"; //$NON-NLS-1$
private static final String CONTENT_FILENAME = "content"; //$NON-NLS-1$
private static final String REPOSITORY_TYPE = LocalMetadataRepository.class.getName();
private static final Integer REPOSITORY_VERSION = 1;
private static final String JAR_EXTENSION = ".jar"; //$NON-NLS-1$
private static final String XML_EXTENSION = ".xml"; //$NON-NLS-1$

protected IUMap units = new IUMap();
protected HashSet<IRepositoryReference> repositories = new HashSet<>();
protected final Set<IRepositoryReference> repositories = new LinkedHashSet<>();
private IIndex<IInstallableUnit> idIndex;
private IIndex<IInstallableUnit> capabilityIndex;
private TranslationSupport translationSupport;
Expand Down Expand Up @@ -114,14 +114,24 @@ public synchronized void addInstallableUnits(Collection<IInstallableUnit> instal
@Override
public void addReferences(Collection<? extends IRepositoryReference> references) {
assertModifiable();
// only write out the repository if we made changes
if (repositories.addAll(references))
save();
if (repositories.addAll(references)) {
save(); // only write out the repository if we made changes
}
}

@Override
public boolean removeReferences(Collection<? extends IRepositoryReference> references) {
assertModifiable();
if (repositories.removeAll(references)) {
save(); // only write out the repository if we made changes
return true;
}
return false;
}

@Override
public Collection<IRepositoryReference> getReferences() {
return Collections.unmodifiableCollection(repositories);
return Collections.unmodifiableSet(repositories);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public interface IMetadataRepository extends IRepository<IInstallableUnit> {
*/
Collection<IRepositoryReference> getReferences();

/**
* Removes from this repository the given references to other repositories.
*
* @since 2.8
*/
boolean removeReferences(Collection<? extends IRepositoryReference> references);

/**
* Removes all installable units in the given collection from this repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public void addReferences(Collection<? extends IRepositoryReference> references)
assertModifiable();
}

@Override
public boolean removeReferences(Collection<? extends IRepositoryReference> references) {
assertModifiable();
return false;
}


@Override
public void removeAll() {
assertModifiable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ public synchronized void addReferences(Collection<? extends IRepositoryReference
repositories.addAll(references);
}

@Override
public synchronized boolean removeReferences(Collection<? extends IRepositoryReference> references) {
assertModifiable();
return repositories.removeAll(references);
}

/**
* Returns a collection of {@link RepositoryReference}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
*******************************************************************************/
package org.eclipse.equinox.p2.tests.metadata.repository;

import static org.junit.Assert.assertThrows;

import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus;
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.SynchronousProvisioningListener;
Expand All @@ -35,6 +36,7 @@
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.QueryUtil;
import org.eclipse.equinox.p2.repository.IRepository;
import org.eclipse.equinox.p2.repository.IRepositoryReference;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.equinox.p2.repository.spi.RepositoryReference;
Expand Down Expand Up @@ -64,11 +66,19 @@ protected void tearDown() throws Exception {
super.tearDown();
}

public void testCompressedRepository() throws ProvisionException {
private IMetadataRepository createTestRepository(IMetadataRepositoryManager manager, Map<String, String> properties)
throws ProvisionException {
return manager.createRepository(repoLocation.toURI(), "TestRepo",
IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties);
}

private IMetadataRepository createTestRepository(Map<String, String> properties) throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
Map<String, String> properties = new HashMap<>();
properties.put(IRepository.PROP_COMPRESSED, "true");
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties);
return createTestRepository(manager, properties);
}

public void testCompressedRepository() throws ProvisionException {
IMetadataRepository repo = createTestRepository(Map.of(IRepository.PROP_COMPRESSED, "true"));

InstallableUnitDescription descriptor = new MetadataFactory.InstallableUnitDescription();
descriptor.setId("testIuId");
Expand All @@ -88,30 +98,20 @@ public void testCompressedRepository() throws ProvisionException {
xmlFilePresent = true;
}
}
if (!jarFilePresent) {
fail("Repository did not create JAR for content.xml");
}
if (xmlFilePresent) {
fail("Repository should not create content.xml");
}
assertTrue("Repository did not create JAR for content.xml", jarFilePresent);
assertFalse("Repository should not create content.xml", xmlFilePresent);
}

public void testGetProperties() throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
IMetadataRepository repo = createTestRepository(null);
Map<String, String> properties = repo.getProperties();
//attempting to modify the properties should fail
try {
properties.put(TEST_KEY, TEST_VALUE);
fail("Should not allow setting property");
} catch (RuntimeException e) {
//expected
}
assertThrows(RuntimeException.class, () -> properties.put(TEST_KEY, TEST_VALUE));
}

public void testSetProperty() throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
IMetadataRepository repo = createTestRepository(manager, null);
Map<String, String> properties = repo.getProperties();
assertTrue("1.0", !properties.containsKey(TEST_KEY));
repo.setProperty(TEST_KEY, TEST_VALUE);
Expand All @@ -133,8 +133,7 @@ public void testSetProperty() throws ProvisionException {
}

public void testAddRemoveIUs() throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
IMetadataRepository repo = createTestRepository(null);
IInstallableUnit iu = createIU("foo");
repo.addInstallableUnits(Arrays.asList(iu));
IQueryResult<IInstallableUnit> result = repo.query(QueryUtil.createIUQuery((String) null), getMonitor());
Expand All @@ -145,8 +144,7 @@ public void testAddRemoveIUs() throws ProvisionException {
}

public void testRemoveByQuery() throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
IMetadataRepository repo = createTestRepository(null);
IInstallableUnit iu = createIU("foo");
IInstallableUnit iu2 = createIU("bar");
repo.addInstallableUnits(Arrays.asList(iu, iu2));
Expand All @@ -162,10 +160,7 @@ public void testRemoveByQuery() throws ProvisionException {
}

public void testUncompressedRepository() throws ProvisionException {
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
Map<String, String> properties = new HashMap<>();
properties.put(IRepository.PROP_COMPRESSED, "false");
IMetadataRepository repo = manager.createRepository(repoLocation.toURI(), "TestRepo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties);
IMetadataRepository repo = createTestRepository(Map.of(IRepository.PROP_COMPRESSED, "false"));

InstallableUnitDescription descriptor = new MetadataFactory.InstallableUnitDescription();
descriptor.setId("testIuId");
Expand All @@ -174,16 +169,12 @@ public void testUncompressedRepository() throws ProvisionException {
repo.addInstallableUnits(Arrays.asList(iu));

File[] files = repoLocation.listFiles();
boolean jarFilePresent = false;
// none of the files in the repository should be the content.xml.jar
for (File file : files) {
if ("content.jar".equalsIgnoreCase(file.getName())) {
jarFilePresent = true;
fail("Repository should not create JAR for content.xml");
}
}
if (jarFilePresent) {
fail("Repository should not create JAR for content.xml");
}
}

/**
Expand All @@ -194,11 +185,10 @@ public void testUncompressedRepository() throws ProvisionException {
public void testLoadSelfReference() throws ProvisionException {
//setup a repository that has a reference to itself in disabled state
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
Map<String, String> properties = new HashMap<>();
properties.put(IRepository.PROP_COMPRESSED, "false");
final URI repoURI = repoLocation.toURI();
IMetadataRepository repo = manager.createRepository(repoURI, "testLoadSelfReference", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties);
repo.addReferences(Collections.singletonList(new RepositoryReference(repoURI, "testNick", IRepository.TYPE_METADATA, IRepository.NONE)));
IMetadataRepository repo = createTestRepository(manager, Map.of(IRepository.PROP_COMPRESSED, "false"));
repo.addReferences(
List.of(new RepositoryReference(repoURI, "testNick", IRepository.TYPE_METADATA, IRepository.NONE)));

final int[] callCount = new int[] {0};
final boolean[] wasEnabled = new boolean[] {false};
Expand Down Expand Up @@ -231,11 +221,10 @@ public void testLoadSelfReference() throws ProvisionException {
public void testRefreshSelfReference() throws ProvisionException {
//setup a repository that has a reference to itself in disabled state
IMetadataRepositoryManager manager = getMetadataRepositoryManager();
Map<String, String> properties = new HashMap<>();
properties.put(IRepository.PROP_COMPRESSED, "false");
final URI repoURL = repoLocation.toURI();
IMetadataRepository repo = manager.createRepository(repoURL, "testRefreshSelfReference", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties);
repo.addReferences(Collections.singletonList(new RepositoryReference(repoURL, "testNick", IRepository.TYPE_METADATA, IRepository.NONE)));
IMetadataRepository repo = createTestRepository(manager, Map.of(IRepository.PROP_COMPRESSED, "false"));
repo.addReferences(
List.of(new RepositoryReference(repoURL, "testNick", IRepository.TYPE_METADATA, IRepository.NONE)));

if (!manager.isEnabled(repoURL)) {
// Enable the repo if it's not enabled.
Expand Down Expand Up @@ -267,14 +256,32 @@ public void testRefreshSelfReference() throws ProvisionException {
}
}

public void testAddReference() throws ProvisionException {
IMetadataRepository repo = createTestRepository(null);
IRepositoryReference reference = new RepositoryReference(URI.create("https://foo.bar.org"), "aName",
IRepository.TYPE_METADATA, IRepository.NONE);
repo.addReferences(List.of(reference));
assertEquals(Set.of(reference), repo.getReferences());
}

public void testRemoveReference() throws ProvisionException {
IMetadataRepository repo = createTestRepository(null);
IRepositoryReference ref1 = new RepositoryReference(URI.create("https://foo.bar1.org"), "aName1",
IRepository.TYPE_METADATA, IRepository.NONE);
IRepositoryReference ref2 = new RepositoryReference(URI.create("https://foo.bar2.org"), "aName2",
IRepository.TYPE_METADATA, IRepository.NONE);

repo.addReferences(List.of(ref1, ref2));
assertEquals(Set.of(ref1, ref2), repo.getReferences());
repo.removeReferences(List.of(ref1));
assertEquals(Set.of(ref2), repo.getReferences());
}

public void testUniqueURIs() throws ProvisionException, OperationCanceledException {
// The test data bug 278668 has multiple installable units with the same license uri
IMetadataRepository repo = getMetadataRepositoryManager().loadRepository(getTestData("test data bug 278668", "testData/bug278668").toURI(), null);
IQueryResult<IInstallableUnit> units = repo.query(QueryUtil.ALL_UNITS, null);
URI last = null;
Iterator<IInstallableUnit> it = units.iterator();
while (it.hasNext()) {
IInstallableUnit iu = it.next();
for (IInstallableUnit iu : repo.query(QueryUtil.ALL_UNITS, null)) {
Collection<ILicense> licenses = iu.getLicenses();
for (ILicense license : licenses) {
URI uri = license.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void addReferences(Collection<? extends IRepositoryReference> references)
throw new UnsupportedOperationException("Repository not modifiable: " + location); //$NON-NLS-1$
}

@Override
public boolean removeReferences(Collection<? extends IRepositoryReference> references) {
throw new UnsupportedOperationException("Repository not modifiable: " + location); //$NON-NLS-1$
}

@Override
public Collection<IRepositoryReference> getReferences() {
return delegate.getReferences();
Expand Down

0 comments on commit ff24c14

Please sign in to comment.