Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a0ab65372b37ca712ab3088f42dd48ae49b6eb44 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 compeople AG 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:
 * 	compeople AG (Stefan Liebig) - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.artifact.processors;

import java.io.*;
import java.lang.reflect.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
import org.eclipse.equinox.p2.tests.TestActivator;

/**
 * Mock the getArtifact() needed to locate the predecessor.
 */
public class ArtifactRepositoryMock implements InvocationHandler {

	private String artifactResource;

	public static IArtifactRepository getMock(String artifactResource) {
		return (IArtifactRepository) Proxy.newProxyInstance(IArtifactRepository.class.getClassLoader(), new Class[] {IArtifactRepository.class}, new ArtifactRepositoryMock(artifactResource));
	}

	private ArtifactRepositoryMock(String artifactResource) {
		this.artifactResource = artifactResource;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (!method.getName().equals("getArtifact"))
			throw new RuntimeException("Unexpected usage!");

		return getArtifact((IArtifactDescriptor) args[0], (OutputStream) args[1], (IProgressMonitor) args[2]);
	}

	private IStatus getArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
		InputStream inputStream;
		try {
			inputStream = TestActivator.getContext().getBundle().getEntry(artifactResource).openStream();
			FileUtils.copyStream(inputStream, true, destination, true);
			return Status.OK_STATUS;
		} catch (IOException e) {
			return new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, ":-(", e);
		}
	}
}

Back to the top