Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e32dd12d8f042776ad346f487162fa07ab825597 (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
/*******************************************************************************
 *  Copyright (c) 2009, 2017 IBM Corporation and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.artifact.repository;

import java.io.*;
import java.net.URI;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.QueryUtil;
import org.eclipse.equinox.p2.repository.artifact.*;
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class Bug265577 extends AbstractProvisioningTest {
	IProfile profile;
	IMetadataRepository metadataRepo;
	IArtifactRepository artifactRepo;
	ProvisioningContext context;
	IEngine engine;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		profile = createProfile(Bug265577.class.getName());

		engine = (IEngine) getAgent().getService(IEngine.SERVICE_NAME);
		// Load repositories
		File repoLocation = getTestData("Repository location", "/testData/bug265577/zipRepo.zip");
		if (repoLocation == null)
			fail("unable to load test data");
		URI location = URIUtil.toJarURI(repoLocation.toURI(), null);
		initializeArtifactRepo(location);
		initializeMetadataRepo(location);
	}

	private void initializeArtifactRepo(URI location) throws ProvisionException {
		IArtifactRepositoryManager mgr = getArtifactRepositoryManager();
		artifactRepo = mgr.loadRepository(location, new NullProgressMonitor());
	}

	private void initializeMetadataRepo(URI location) throws ProvisionException {
		IMetadataRepositoryManager mgr = getMetadataRepositoryManager();
		metadataRepo = mgr.loadRepository(location, new NullProgressMonitor());
		context = new ProvisioningContext(getAgent());
		context.setMetadataRepositories(mgr.getKnownRepositories(0));
	}

	// Tests the response to a feature folder inside a jar
	public void testZippedRepoWithFolderFeature() {
		IQueryResult<IInstallableUnit> queryResult = metadataRepo.query(QueryUtil.createIUQuery("Field_Assist_Example.feature.jar"), null);
		IInstallableUnit[] ius = queryResult.toArray(IInstallableUnit.class);
		IArtifactKey key = (ius[0].getArtifacts()).iterator().next();

		IArtifactDescriptor[] descriptors = artifactRepo.getArtifactDescriptors(key);
		ArtifactDescriptor desc = (ArtifactDescriptor) descriptors[0];
		// Set folder property
		desc.setProperty("artifact.folder", String.valueOf(true));

		IStatus status = null;
		try {
			OutputStream destination = new BufferedOutputStream(new FileOutputStream(new File(getTempFolder(), "FieldAssist")));
			status = artifactRepo.getArtifact(desc, destination, new NullProgressMonitor());
		} catch (FileNotFoundException e) {
			fail(e.getMessage());
		}

		if (status.isOK())
			fail("OK Status on download");
		assertTrue(status.getMessage().equals(getJarFolderMessage(key)));
	}

	// Test to retrieve a file from a zipped metadata & artifact repository
	public void testZippedRepo() {
		IQueryResult<IInstallableUnit> queryResult = metadataRepo.query(QueryUtil.createIUQuery("valid.feature.jar"), null);
		IInstallableUnit[] ius = queryResult.toArray(IInstallableUnit.class);
		IArtifactKey key = (ius[0].getArtifacts()).iterator().next();

		IArtifactDescriptor[] descriptors = artifactRepo.getArtifactDescriptors(key);
		IArtifactDescriptor desc = descriptors[0];

		IStatus status = null;
		try {
			OutputStream destination = new BufferedOutputStream(new FileOutputStream(new File(getTempFolder(), "valid")));
			status = artifactRepo.getArtifact(desc, destination, new NullProgressMonitor());
		} catch (FileNotFoundException e) {
			fail(e.getMessage());
		}

		assertTrue(status.getMessage(), status.isOK());
	}

	// Return expected error message for the attempt to retrieve an artifact if it is a folder from inside a jar
	private String getJarFolderMessage(IArtifactKey key) {
		return "Artifact " + key.toString() + " is a folder but the repository is an archive or remote location.";
	}
}

Back to the top