ResourceHelper { public static boolean matches(Requirement requirement, Capability capability) { if (requirement == null && capability == null) return true; else if (requirement == null || capability == null) return false; else if (!capability.getNamespace().equals(requirement.getNamespace())) return false; else { Filter filter = null; try { if (requirement instanceof AbstractRequirement) { filter = ((AbstractRequirement)requirement).getFilter(); } else { String filterStr = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE); if (filterStr != null) { filter = FrameworkUtil.createFilter(filterStr); } } } catch (InvalidSyntaxException e) { logger.debug("Requirement had invalid filter string: " + requirement, e); return false; } if (filter != null && !filter.matches(capability.getAttributes())) { return false; } } return matchMandatoryDirective(requirement, capability); }  static boolean areEqual(Resource resource1, Resource resource2); static String getContentAttribute(Resource resource); static Object getContentAttribute(Resource resource, String name); static Object getIdentityAttribute(Resource resource, String name); static String getLocation(Resource resource); static Resource getResource(Requirement requirement, org.apache.aries.subsystem.core.repository.Repository repository); static String getSymbolicNameAttribute(Resource resource); static String getTypeAttribute(Resource resource); static Version getVersionAttribute(Resource resource); static boolean matches(Requirement requirement, Capability capability);  }
@Test public void testMandatoryDirectiveAbsent() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("b", "c") .attribute("c", "d") .resource(EasyMock.createNiceMock(Resource.class)).build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .directive(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(&(osgi.wiring.package=com.foo)(a=b)(b=c))") .resource(EasyMock.createNiceMock(Resource.class)).build(); assertTrue("Capability should match requirement", ResourceHelper.matches(req, cap)); }
@Test public void testMandatoryDirectiveAndNullFilterDirective() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("b", "c") .attribute("c", "d") .directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "b") .resource(EasyMock.createNiceMock(Resource.class)).build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .resource(EasyMock.createNiceMock(Resource.class)).build(); assertFalse("Capability should not match requirement", ResourceHelper.matches(req, cap)); }
@Test public void testMandatoryDirectiveCaseSensitive() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("bAr", "c") .attribute("c", "d") .directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "bAr") .resource(EasyMock.createNiceMock(Resource.class)).build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .directive(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(&(osgi.wiring.package=com.foo)(a=b)(baR=c)(c=d))") .resource(EasyMock.createNiceMock(Resource.class)).build(); assertFalse("Capability should not match requirement", ResourceHelper.matches(req, cap)); }
@Test public void testMandatoryDirectiveExportPackageFail() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("b", "c") .attribute("c", "d") .directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "a,c") .resource(EasyMock.createNiceMock(Resource.class)) .build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .directive(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(&(osgi.wiring.package=com.foo)(a=b)(b=c))") .resource(EasyMock.createNiceMock(Resource.class)).build(); assertFalse("Capability should not match requirement", ResourceHelper.matches(req, cap)); }
@Test public void testMandatoryDirectiveExportPackagePass() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("b", "c") .attribute("c", "d") .directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "a,c") .resource(EasyMock.createNiceMock(Resource.class)) .build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .directive(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(&(osgi.wiring.package=com.foo)(a=b)(c=d))") .resource(EasyMock.createNiceMock(Resource.class)).build(); assertTrue("Capability should match requirement", ResourceHelper.matches(req, cap)); }
@Test public void testMandatoryDirectiveWithWhitespace() { Capability cap = new BasicCapability.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo") .attribute("a", "b") .attribute("b", "c") .attribute("c", "d") .directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "\ra\n, c ") .resource(EasyMock.createNiceMock(Resource.class)) .build(); Requirement req = new BasicRequirement.Builder() .namespace(PackageNamespace.PACKAGE_NAMESPACE) .directive(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(&(osgi.wiring.package=com.foo)(a=b)(c=d))") .resource(EasyMock.createNiceMock(Resource.class)).build(); assertTrue("Capability should match requirement", ResourceHelper.matches(req, cap)); }