Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 128fa0fbe193205a248ef87169c6770056657d60 (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
/*******************************************************************************
 * Copyright (c) 2011, 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.osgi.tests.resource;

import java.util.*;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.hooks.resolver.ResolverHook;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.wiring.*;

public class ResolverHookTests extends AbstractResourceTest {

	public static Test suite() {
		return new TestSuite(ResolverHookTests.class);
	}

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

	public void testSingletonIdentity() throws Exception {
		final RuntimeException error[] = {null};
		final boolean called[] = {false};
		ResolverHookFactory resolverHookFactory = new ResolverHookFactory() {
			public ResolverHook begin(Collection triggers) {
				return new ResolverHook() {

					public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
						if (error[0] != null)
							return;
						called[0] = true;
						try {
							assertEquals("Wrong namespace", IdentityNamespace.IDENTITY_NAMESPACE, singleton.getNamespace());
							assertEquals("Wrong singleton directive", "true", singleton.getDirectives().get(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE));
							String symbolicName = (String) singleton.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
							for (Iterator iCandidates = collisionCandidates.iterator(); iCandidates.hasNext();) {
								BundleCapability candidate = (BundleCapability) iCandidates.next();
								assertEquals("Wrong namespace", IdentityNamespace.IDENTITY_NAMESPACE, candidate.getNamespace());
								assertEquals("Wrong singleton directive", "true", candidate.getDirectives().get(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE));
								assertEquals("Wrong symbolic name", symbolicName, (String) candidate.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE));
							}
						} catch (RuntimeException e) {
							error[0] = e;
						}
					}

					public void filterResolvable(Collection candidates) {
						// nothing
					}

					public void filterMatches(BundleRequirement requirement, Collection candidates) {
						// nothing
					}

					public void end() {
						// nothing
					}
				};
			}
		};

		ServiceRegistration hookReg = getContext().registerService(ResolverHookFactory.class, resolverHookFactory, null);

		try {
			Bundle tb1v1 = installer.installBundle("singleton.tb1v1");
			Bundle tb1v2 = installer.installBundle("singleton.tb1v2");
			assertFalse(getContext().getBundle(0).adapt(FrameworkWiring.class).resolveBundles(Arrays.asList(new Bundle[] {tb1v1, tb1v2})));
			assertTrue("ResolverHook was not called", called[0]);
			if (error[0] != null)
				throw error[0];
		} finally {
			hookReg.unregister();
		}
	}

}

Back to the top