|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.osgi.tests.resource; |
| 12 |
|
| 13 |
import java.util.*; |
| 14 |
import junit.framework.Test; |
| 15 |
import junit.framework.TestSuite; |
| 16 |
import org.osgi.framework.Bundle; |
| 17 |
import org.osgi.framework.ServiceRegistration; |
| 18 |
import org.osgi.framework.hooks.resolver.ResolverHook; |
| 19 |
import org.osgi.framework.hooks.resolver.ResolverHookFactory; |
| 20 |
import org.osgi.framework.wiring.*; |
| 21 |
|
| 22 |
public class ResolverHookTests extends AbstractResourceTest { |
| 23 |
|
| 24 |
public static Test suite() { |
| 25 |
return new TestSuite(ResolverHookTests.class); |
| 26 |
} |
| 27 |
|
| 28 |
public ResolverHookTests(String name) { |
| 29 |
super(name); |
| 30 |
} |
| 31 |
|
| 32 |
public void testSingletonIdentity() throws Exception { |
| 33 |
final RuntimeException error[] = {null}; |
| 34 |
final boolean called[] = {false}; |
| 35 |
ResolverHookFactory resolverHookFactory = new ResolverHookFactory() { |
| 36 |
public ResolverHook begin(Collection triggers) { |
| 37 |
return new ResolverHook() { |
| 38 |
|
| 39 |
public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) { |
| 40 |
if (error[0] != null) |
| 41 |
return; |
| 42 |
called[0] = true; |
| 43 |
try { |
| 44 |
assertEquals("Wrong namespace", ResourceConstants.IDENTITY_NAMESPACE, singleton.getNamespace()); |
| 45 |
assertEquals("Wrong singleton directive", "true", singleton.getDirectives().get(ResourceConstants.IDENTITY_SINGLETON_DIRECTIVE)); |
| 46 |
String symbolicName = (String) singleton.getAttributes().get(ResourceConstants.IDENTITY_NAMESPACE); |
| 47 |
for (Iterator iCandidates = collisionCandidates.iterator(); iCandidates.hasNext();) { |
| 48 |
BundleCapability candidate = (BundleCapability) iCandidates.next(); |
| 49 |
assertEquals("Wrong namespace", ResourceConstants.IDENTITY_NAMESPACE, candidate.getNamespace()); |
| 50 |
assertEquals("Wrong singleton directive", "true", candidate.getDirectives().get(ResourceConstants.IDENTITY_SINGLETON_DIRECTIVE)); |
| 51 |
assertEquals("Wrong symbolic name", symbolicName, (String) candidate.getAttributes().get(ResourceConstants.IDENTITY_NAMESPACE)); |
| 52 |
} |
| 53 |
} catch (RuntimeException e) { |
| 54 |
error[0] = e; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public void filterResolvable(Collection candidates) { |
| 59 |
// nothing |
| 60 |
} |
| 61 |
|
| 62 |
public void filterMatches(BundleRequirement requirement, Collection candidates) { |
| 63 |
// nothing |
| 64 |
} |
| 65 |
|
| 66 |
public void end() { |
| 67 |
// nothing |
| 68 |
} |
| 69 |
}; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
ServiceRegistration hookReg = getContext().registerService(ResolverHookFactory.class, resolverHookFactory, null); |
| 74 |
|
| 75 |
try { |
| 76 |
Bundle tb1v1 = installer.installBundle("singleton.tb1v1"); |
| 77 |
Bundle tb1v2 = installer.installBundle("singleton.tb1v2"); |
| 78 |
assertFalse(((FrameworkWiring) getContext().getBundle(0).adapt(FrameworkWiring.class)).resolveBundles(Arrays.asList(new Bundle[] {tb1v1, tb1v2}))); |
| 79 |
assertTrue("ResolverHook was not called", called[0]); |
| 80 |
if (error[0] != null) |
| 81 |
throw error[0]; |
| 82 |
} finally { |
| 83 |
hookReg.unregister(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |