Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3fbfdb4d10951fec1cdb6682885b9924a6b8c0b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2007 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.io.*;
import java.net.*;
import java.util.*;
import junit.framework.Assert;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.artifact.repository.ArtifactRequest;
import org.eclipse.equinox.internal.p2.artifact.repository.Transport;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.ProcessingStepHandler;
import org.eclipse.equinox.internal.provisional.p2.core.repository.RepositoryCreationException;
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
import org.eclipse.equinox.internal.provisional.spi.p2.artifact.repository.AbstractArtifactRepository;

/**
 * A simple artifact repository implementation used for testing purposes.
 * All artifacts are kept in memory.
 */
public class TestArtifactRepository extends AbstractArtifactRepository {
	private static final String SCHEME = "testartifactrepo";
	private static final String NAME = "ATestArtifactRepository"; //$NON-NLS-1$
	private static final String TYPE = "testartifactrepo"; //$NON-NLS-1$
	private static final String VERSION = "1"; //$NON-NLS-1$
	private static final String PROVIDER = "org.eclipse"; //$NON-NLS-1$
	private static final String DESCRIPTION = "A Test Artifact Repository"; //$NON-NLS-1$

	/**
	 * Map of IArtifactKey -> String (location)
	 */
	Map keysToLocations = new HashMap();
	/**
	 * Map of String (location) -> byte[] (contents)
	 */
	Map locationsToContents = new HashMap();

	/**
	 * 	Set of artifact descriptors
	 */
	Set artifactDescriptors = new HashSet();

	Transport testhandler = new Transport() {
		public IStatus download(String toDownload, OutputStream target, IProgressMonitor pm) {
			byte[] contents = (byte[]) locationsToContents.get(toDownload);
			if (contents == null)
				Assert.fail("Attempt to download missing artifact in TestArtifactRepository: " + toDownload);
			try {
				target.write(contents);
			} catch (IOException e) {
				e.printStackTrace();
				Assert.fail("Unexpected exception in TestArtifactRepository" + e.getMessage());
			}
			return Status.OK_STATUS;
		}
	};

	public TestArtifactRepository() {
		super(NAME, TYPE, VERSION, null, DESCRIPTION, PROVIDER);
	}

	public void addArtifact(IArtifactKey key, byte[] contents) {
		String location = key.toString();
		keysToLocations.put(key, location);
		locationsToContents.put(location, contents);
	}

	public URI getArtifact(IArtifactKey key) {
		String location = (String) keysToLocations.get(key);
		if (location == null)
			return null;
		try {
			return new URI(SCHEME, location, null);
		} catch (URISyntaxException e) {
			Assert.fail("Invalid URI in TestArtifactRepository: " + e.getMessage());
			return null;
		}
	}

	public IArtifactKey[] getArtifactKeys() {
		return (IArtifactKey[]) keysToLocations.keySet().toArray(new IArtifactKey[keysToLocations.keySet().size()]);
	}

	private IStatus getArtifact(ArtifactRequest request, IProgressMonitor monitor) {
		request.setSourceRepository(this);
		request.perform(monitor);
		return request.getResult();
	}

	public IStatus getArtifacts(IArtifactRequest[] requests, IProgressMonitor monitor) {
		SubMonitor subMonitor = SubMonitor.convert(monitor, requests.length);
		try {
			MultiStatus overallStatus = new MultiStatus(TestActivator.PI_PROV_TESTS, IStatus.OK, null, null);
			for (int i = 0; i < requests.length; i++) {
				overallStatus.add(getArtifact((ArtifactRequest) requests[i], subMonitor.newChild(1)));
			}
			return (monitor.isCanceled() ? Status.CANCEL_STATUS : overallStatus);
		} finally {
			subMonitor.done();
		}
	}

	public void initialize(URL repoURL, InputStream descriptorFile) throws RepositoryCreationException {
		location = repoURL;
	}

	public boolean contains(IArtifactDescriptor descriptor) {
		return keysToLocations.get(descriptor.getArtifactKey()) != null;
	}

	public boolean contains(IArtifactKey key) {
		return keysToLocations.get(key) != null;
	}

	public IStatus getArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
		ProcessingStepHandler handler = new ProcessingStepHandler();
		destination = handler.createAndLink(descriptor.getProcessingSteps(), null, destination, monitor);
		testhandler.download((String) keysToLocations.get(descriptor.getArtifactKey()), destination, monitor);
		return Status.OK_STATUS;
	}

	public IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
		if (!contains(key))
			return null;
		return new IArtifactDescriptor[] {new ArtifactDescriptor(key)};
	}

	public void addDescriptor(IArtifactDescriptor descriptor) {
		((ArtifactDescriptor) descriptor).setRepository(this);
		artifactDescriptors.add(descriptor);
		keysToLocations.put(descriptor.getArtifactKey(), null);
	}

	public void removeDescriptor(IArtifactDescriptor descriptor) {
		removeDescriptor(descriptor.getArtifactKey());
	}

	public void removeDescriptor(IArtifactKey key) {
		for (Iterator iter = artifactDescriptors.iterator(); iter.hasNext();) {
			IArtifactDescriptor nextDescriptor = (IArtifactDescriptor) iter.next();
			if (key.equals(nextDescriptor.getArtifactKey())) {
				artifactDescriptors.remove(nextDescriptor);
			}
		}
		if (keysToLocations.containsKey(key)) {
			String theLocation = (String) keysToLocations.get(key);
			locationsToContents.remove(theLocation);
			keysToLocations.remove(key);
		}
	}

	public void removeAll() {
		artifactDescriptors.clear();
		keysToLocations.clear();
		locationsToContents.clear();
	}

	public boolean isModifiable() {
		return true;
	}
}

Back to the top