Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 31c4f9a4896fe598f0d85b56038a3da4845dc8de (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
package org.eclipse.osgi.tests.hooks.framework;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Map;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.launch.EquinoxFactory;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public abstract class AbstractFrameworkHookTests extends CoreTest {
	protected static class BasicURLClassLoader extends URLClassLoader {
		public BasicURLClassLoader(URL[] urls, ClassLoader parent) {
			super(urls, parent);
		}

		@Override
		public URL getResource(String name) {
			if (isLocalResource(name))
				return findResource(name);
			return super.getResource(name);
		}

		@Override
		public void addURL(URL url) {
			super.addURL(url);
		}

		@Override
		public Enumeration<URL> getResources(String name) throws IOException {
			if (isLocalResource(name))
				return findResources(name);
			return super.getResources(name);
		}

		@Override
		protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
			if (name.startsWith("org.eclipse")) {
				Class<?> result = findLoadedClass(name);
				if (result == null)
					result = findClass(name);
				return result;
			}
			return super.loadClass(name, resolve);
		}

		private boolean isLocalResource(String name) {
			return name.startsWith("org/eclipse") || name.equals(HookRegistry.HOOK_CONFIGURATORS_FILE);
		}
	}

	protected static final String BUNDLES_ROOT = "bundle_tests";

	protected BasicURLClassLoader classLoader;
	protected BundleInstaller bundleInstaller;

	public BundleContext getContext() {
		return OSGiTestsActivator.getContext();
	}

	protected void assertBundleDiscarded(String location, Framework framework) {
		assertNull("Bundle " + location + " was not discarded", framework.getBundleContext().getBundle(location));
	}

	protected void assertBundleNotDiscarded(String location, Framework framework) {
		assertNotNull("Bundle " + location + " was discarded", framework.getBundleContext().getBundle(location));
	}

	protected Framework createFramework(Map<String, String> configuration) throws Exception {
		FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(EquinoxFactory.class.getName()).newInstance();
		Framework framework = factory.newFramework(configuration);
		return framework;
	}

	protected void initAndStart(Framework framework) throws Exception {
		framework.init();
		framework.start();
	}

	protected Framework restart(Framework framework, Map<String, String> configuration) throws Exception {
		stop(framework);
		framework = createFramework(configuration);
		initAndStart(framework);
		return framework;
	}

	protected void setUp() throws Exception {
		setUpBundleInstaller();
		setUpClassLoader();
	}

	protected void stop(Framework framework) throws Exception {
		framework.stop();
		FrameworkEvent event = framework.waitForStop(5000);
		assertEquals("The framework was not stopped", FrameworkEvent.STOPPED, event.getType());
	}

	protected void stopQuietly(Framework framework) {
		if (framework == null)
			return;
		try {
			stop(framework);
		} catch (Exception e) {
			// Ignore.
		}
	}

	protected void tearDown() throws Exception {
		bundleInstaller.shutdown();
	}

	private void setUpBundleInstaller() throws Exception {
		bundleInstaller = new BundleInstaller(BUNDLES_ROOT, getContext());
		bundleInstaller.refreshPackages(null);
	}

	private void setUpClassLoader() throws Exception {
		BundleContext context = getContext();
		String osgiFramework = context.getProperty(EclipseStarter.PROP_FRAMEWORK);
		URL[] urls;
		if ("folder".equals(context.getProperty(EclipseStarter.PROP_FRAMEWORK_SHAPE)))
			urls = new URL[] {new URL(osgiFramework), new URL(osgiFramework + "bin/")};
		else
			urls = new URL[] {new URL(osgiFramework)};
		classLoader = new BasicURLClassLoader(urls, getClass().getClassLoader());
	}
}

Back to the top