OsgiClassPrefixer { public Optional<String> stripMatchingPrefix(Bundle bundle, String type) { String symbolicName = bundle.getSymbolicName(); if (symbolicName != null && type.startsWith(symbolicName + DELIMITER)) { return Optional.of(type.substring(type.lastIndexOf(DELIMITER) + 1)); } return Optional.absent(); } OsgiClassPrefixer(); @VisibleForTesting protected  OsgiClassPrefixer(@Nullable Function<Class<?>, Optional<Bundle>> bundleRetriever); Optional<String> getPrefix(Class<?> type); Optional<String> stripMatchingPrefix(Bundle bundle, String type); boolean hasPrefix(String type); static final String DELIMITER; }
@Test public void testStringPrefix() throws Exception { Bundle bundle = Mockito.mock(Bundle.class); Mockito.when(bundle.getSymbolicName()).thenReturn("my.symbolic.name"); Mockito.when(bundle.getVersion()).thenReturn(Version.valueOf("1.2.3")); OsgiClassPrefixer prefixer = new OsgiClassPrefixer(); assertAbsent(prefixer.stripMatchingPrefix(bundle, "my.package.MyClass")); assertAbsent(prefixer.stripMatchingPrefix(bundle, "different.symbolic.name:my.package.MyClass")); assertAbsent(prefixer.stripMatchingPrefix(bundle, "different.symbolic.name:1.2.3:my.package.MyClass")); assertPresent(prefixer.stripMatchingPrefix(bundle, "my.symbolic.name:my.package.MyClass"), "my.package.MyClass"); assertPresent(prefixer.stripMatchingPrefix(bundle, "my.symbolic.name:1.2.3:my.package.MyClass"), "my.package.MyClass"); assertPresent(prefixer.stripMatchingPrefix(bundle, "my.symbolic.name:1.0.0:my.package.MyClass"), "my.package.MyClass"); }