blob: 0a90349d2b7ca01ada39e4f4aeca7e81a927815c [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;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import junit.framework.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.equinox.internal.provisional.p2.core.repository.IRepository;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
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.query.Collector;
import org.eclipse.equinox.internal.provisional.p2.query.Query;
import org.eclipse.equinox.internal.provisional.spi.p2.metadata.repository.AbstractMetadataRepository;
import org.osgi.framework.Version;
/**
* A simple metadata repository used for testing purposes. All metadata
* is kept in memory.
*/
public class TestMetadataRepository extends AbstractMetadataRepository {
private static final String DESCRIPTION = "A Test Metadata Repository"; //$NON-NLS-1$
private static final String NAME = "ATestMetadataRepository"; //$NON-NLS-1$
private static final String PROVIDER = "org.eclipse"; //$NON-NLS-1$
private static final String TYPE = "testmetadatarepo"; //$NON-NLS-1$
private static final String VERSION = "1"; //$NON-NLS-1$
private final List units = new ArrayList();
private static URL createLocation() {
try {
//Just need a unique URL - we don't need to read/write this location
return new URL("http://TestMetadataRepository.com/" + Long.toString(System.currentTimeMillis()));
} catch (MalformedURLException e) {
Assert.fail(e.getMessage());
}
return null;
}
public TestMetadataRepository(IInstallableUnit[] ius) {
super(NAME, TYPE, VERSION, createLocation(), DESCRIPTION, PROVIDER);
units.addAll(Arrays.asList(ius));
}
public void addInstallableUnits(IInstallableUnit[] toAdd) {
units.addAll(Arrays.asList(toAdd));
}
public IInstallableUnit find(String id, String versionString) {
Iterator result = query(new InstallableUnitQuery(id, new Version(versionString)), new Collector(), null).iterator();
return (IInstallableUnit) (result.hasNext() ? result.next() : null);
}
public Object getAdapter(Class adapter) {
if (adapter == TestMetadataRepository.class || adapter == IMetadataRepository.class || adapter == IRepository.class) {
return this;
}
return null;
}
public Collector query(Query query, Collector collector, IProgressMonitor monitor) {
return query.perform(units.iterator(), collector);
}
public void removeAll() {
units.clear();
}
public boolean removeInstallableUnits(IInstallableUnit[] toRemove) {
boolean modified = false;
for (int i = 0; i < toRemove.length; i++) {
modified |= units.remove(toRemove[i]);
}
return modified;
}
public void initialize(RepositoryState state) {
this.name = state.Name;
this.type = state.Type;
this.version = state.Version.toString();
this.provider = state.Provider;
this.description = state.Description;
this.location = state.Location;
this.properties = state.Properties;
this.units.addAll(Arrays.asList(state.Units));
}
}