Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da7c864b5b9f9606542fdd26afe3d79e85defb00 (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
/*******************************************************************************
 * Copyright (c) 2005, 2015 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.common.tests.registry.simple;

import java.io.*;
import java.net.URL;
import junit.framework.TestCase;

import org.eclipse.core.internal.runtime.MetaDataKeeper;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.spi.RegistryStrategy;
import org.osgi.framework.FrameworkUtil;

public class BaseExtensionRegistryRun extends TestCase {

	// The imaging device registry
	protected IExtensionRegistry simpleRegistry;
	protected Object masterToken = new Object();
	protected Object userToken = new Object();

	// Path to the XML files
	private final static String xmlPath = "Plugin_Testing/registry/testSimple/"; //$NON-NLS-1$

	public BaseExtensionRegistryRun() {
		super();
	}

	public BaseExtensionRegistryRun(String name) {
		super(name);
	}

	protected URL getXML(String fileName) {
		return FrameworkUtil.getBundle(getClass()).getEntry(xmlPath + fileName);
	}

	/**
	 * Create the "imaging device" registry
	 */
	@Override
	protected void setUp() throws Exception {
		// create the imaging device registry
		simpleRegistry = startRegistry();
	}

	/**
	 * Properly dispose of the extension registry
	 */
	@Override
	protected void tearDown() throws Exception {
		stopRegistry();
	}

	/**
	 * @return - open extension registry
	 */
	protected IExtensionRegistry startRegistry() {
		return startRegistry(this.getClass().getName());
	}

	/**
	 * @return - open extension registry
	 */
	protected IExtensionRegistry startRegistry(String subDir) {
		// use plugin's metadata directory to save cache data
		IPath userDataPath = getStateLocation();
		userDataPath = userDataPath.append(subDir);

		File[] registryLocations = new File[] {new File(userDataPath.toOSString())};
		boolean[] readOnly = new boolean[] {false};
		RegistryStrategy registryStrategy = new RegistryStrategy(registryLocations, readOnly);
		return RegistryFactory.createRegistry(registryStrategy, masterToken, userToken);
	}

	/**
	 * Stops the extension registry.
	 */
	protected void stopRegistry() {
		assertNotNull(simpleRegistry);
		simpleRegistry.stop(masterToken);
	}

	protected void processXMLContribution(IContributor nonBundleContributor, URL url) {
		processXMLContribution(nonBundleContributor, url, false);
	}

	protected void processXMLContribution(IContributor nonBundleContributor, URL url, boolean persist) {
		try {
			InputStream is = url.openStream();
			simpleRegistry.addContribution(is, nonBundleContributor, persist, url.getFile(), null, persist ? masterToken : userToken);
		} catch (IOException eFile) {
			fail(eFile.getMessage());
			return;
		}
	}

	protected String qualifiedName(String namespace, String simpleName) {
		return namespace + "." + simpleName; //$NON-NLS-1$
	}
	
	protected IPath getStateLocation() {
		IPath stateLocation = MetaDataKeeper.getMetaArea().getStateLocation(FrameworkUtil.getBundle(getClass()));
		stateLocation.toFile().mkdirs();
		return stateLocation;
	}

}

Back to the top