Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4ed547764ef9ce931e56ce9bce7da6a5d888061 (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
/*******************************************************************************
 * Copyright (c) 2011, 2017 WindRiver 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:
 *     WindRiver Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.importexport;

import java.io.*;
import java.net.*;
import java.util.List;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.importexport.IUDetail;
import org.eclipse.equinox.internal.p2.importexport.P2ImportExport;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.repository.IRepositoryManager;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.p2.tests.TestActivator;
import org.eclipse.equinox.p2.tests.metadata.repository.ServerBasedTestCase;
import org.osgi.util.tracker.ServiceTracker;

public class ImportExportRemoteTests extends ServerBasedTestCase {

	private P2ImportExport importexportService;

	private URI getTestRepository() throws URISyntaxException {
		return new URI(getBaseURL() + "/importexport/");
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		ServiceTracker<P2ImportExport, P2ImportExport> tracker = new ServiceTracker<>(TestActivator.getContext(), P2ImportExport.class, null);
		tracker.open();
		importexportService = tracker.getService();
		assertNotNull("Fail to get ImportExport service", importexportService);
		tracker.close();
	}

	@Override
	public void tearDown() throws Exception {
		super.tearDown();
		importexportService = null;
		IMetadataRepositoryManager repoMan = (IMetadataRepositoryManager) getAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);
		URI[] urls = repoMan.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
		for (int i = 0; i < urls.length; i++) {
			repoMan.removeRepository(urls[i]);
		}
	}

	public void testExportFeaturesFromRemoteRepository() throws URISyntaxException, IOException, ProvisionException, OperationCanceledException {
		File testFile = File.createTempFile("test", "p2f");
		URI uri = getTestRepository();
		try {
			IMetadataRepositoryManager metaManager = (IMetadataRepositoryManager) getAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);
			IMetadataRepository repo = metaManager.loadRepository(uri, null);
			assertNotNull("Fail to load remote repo", repo);
			IInstallableUnit iu = AbstractProvisioningTest.createIU("A", Version.create("1.0.0"));
			try (OutputStream output = new FileOutputStream(testFile)) {
				IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] {iu}, false, null);
				assertTrue("Not expected return result.", status.isOK());
			}
			try (InputStream input = new FileInputStream(testFile)) {
				List<IUDetail> ius = importexportService.importP2F(input);
				assertEquals("Exported the number of features is not expected.", 1, ius.size());
				assertTrue("Exported feature is not expected.", iu.equals(ius.get(0).getIU()));
				assertEquals("Exported the number of referred repositories is not expected.", 1, ius.get(0).getReferencedRepositories().size());
				assertEquals("Exported referred repository is not expected.", uri, ius.get(0).getReferencedRepositories().get(0));
			}
		} finally {
			testFile.delete();
		}
	}

	protected File getTestData(String message, String entry) {
		if (entry == null)
			fail(message + " entry is null.");
		URL base = TestActivator.getContext().getBundle().getEntry(entry);
		if (base == null)
			fail(message + " entry not found in bundle: " + entry);
		try {
			String osPath = new Path(FileLocator.toFileURL(base).getPath()).toOSString();
			File result = new File(osPath);
			if (!result.getCanonicalPath().equals(result.getPath()))
				fail(message + " result path: " + result.getPath() + " does not match canonical path: " + result.getCanonicalFile().getPath());
			return result;
		} catch (IOException e) {
			fail(message);
		}
		// avoid compile error... should never reach this code
		return null;
	}

	public void testExportFeaturesFromBothRemoteRepositoryAndLocalRepository() throws URISyntaxException, IOException, ProvisionException, OperationCanceledException {
		File testFile = File.createTempFile("test", "p2f");
		URI uri = getTestRepository();
		try {
			IMetadataRepositoryManager metaManager = (IMetadataRepositoryManager) getAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);
			File localRepoFile = getTestData("Error load data", "testData/importexport/repo1");
			IMetadataRepository localRepo = metaManager.loadRepository(localRepoFile.toURI(), null);
			assertNotNull("Fail to load local repo", localRepo);
			IMetadataRepository repo = metaManager.loadRepository(uri, null);
			assertNotNull("Fail to load remote repo", repo);
			IInstallableUnit iu = AbstractProvisioningTest.createIU("A", Version.create("1.0.0"));
			try (OutputStream output = new FileOutputStream(testFile)) {
				IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] {iu}, false, null);
				assertTrue("Not expected return result.", status.isOK());
			}
			try (InputStream input = new FileInputStream(testFile)) {
				List<IUDetail> ius = importexportService.importP2F(input);
				assertEquals("Exported the number of features is not expected.", 1, ius.size());
				assertTrue("Exported feature is not expected.", iu.equals(ius.get(0).getIU()));
				assertEquals("Exported the number of referred repositories is not expected.", 1, ius.get(0).getReferencedRepositories().size());
				assertEquals("Exported referred repository is not expected.", uri, ius.get(0).getReferencedRepositories().get(0));
			}
		} finally {
			testFile.delete();
		}
	}
}

Back to the top