@Test public void testLoadClassNotInOsgi() throws Exception { ClassLoaderUtils clu = new ClassLoaderUtils(getClass()); assertLoadSucceeds(clu, getClass().getName(), getClass()); assertLoadSucceeds(clu, Entity.class.getName(), Entity.class); assertLoadSucceeds(clu, AbstractEntity.class.getName(), AbstractEntity.class); assertLoadFails(clu, "org.apache.brooklyn.this.name.does.not.Exist"); }
@Test public void testLoadClassInOsgi() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); @SuppressWarnings("unchecked") Class<? extends Entity> clazz = (Class<? extends Entity>) bundle.loadClass(classname); Entity entity = createSimpleEntity(bundleUrl, clazz); System.setProperty(ClassLoaderUtils.WHITE_LIST_KEY, "do.not.match.whitelist:1.0.0"); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadFails(classname, cluMgmt); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion()+":" + classname, clazz, cluMgmt, cluClass, cluEntity); }
@Test public void testVariousLoadersLoadClassInOsgiWhiteList() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); Class<?> clazz = bundle.loadClass(classname); Entity entity = createSimpleEntity(bundleUrl, clazz); String whiteList = bundle.getSymbolicName()+":"+bundle.getVersion(); System.setProperty(ClassLoaderUtils.WHITE_LIST_KEY, whiteList); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluEntity); }
@Test public void testLoadClassInOsgiCore() throws Exception { Class<?> clazz = BasicEntity.class; String classname = clazz.getName(); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = getBundle(mgmt, "org.apache.brooklyn.core"); Entity entity = createSimpleEntity(bundle.getLocation(), clazz); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluNone = new ClassLoaderUtils(getClass()); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion() + ":" + classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); }
@Test public void testLoadClassInOsgiApi() throws Exception { Class<?> clazz = Entity.class; String classname = clazz.getName(); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = getBundle(mgmt, "org.apache.brooklyn.api"); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluNone = new ClassLoaderUtils(getClass()); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion() + ":" + classname, clazz, cluMgmt, cluClass, cluNone); }
/** * When two guava versions installed, want us to load from the *brooklyn* version rather than * a newer version that happens to be in Karaf. */ @Test(groups={"Integration"}) public void testLoadsFromRightGuavaVersion() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); ClassLoaderUtils clu = new ClassLoaderUtils(getClass(), mgmt); String bundleUrl = MavenRetriever.localUrl(MavenArtifact.fromCoordinate("com.google.guava:guava:jar:18.0")); Bundle bundle = installBundle(mgmt, bundleUrl); String bundleName = bundle.getSymbolicName(); String classname = bundleName + ":" + ImmutableList.class.getName(); assertLoadSucceeds(clu, classname, ImmutableList.class); }
@Test public void testLoadJustOneClassInOsgiWhiteList() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); Class<?> clazz = bundle.loadClass(classname); Entity entity = createSimpleEntity(bundleUrl, clazz); String whiteList = bundle.getSymbolicName()+":"+bundle.getVersion(); System.setProperty(ClassLoaderUtils.WHITE_LIST_KEY, whiteList); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); BundledName resource = new BundledName(classname).toResource(); BundledName bn = new BundledName(resource.bundle, resource.version, "/" + resource.name); Asserts.assertSize(cluEntity.getResources(bn.toString()), 1); }
private void assertLoadFails(String bundledClassName, ClassLoaderUtils... clua) { for (ClassLoaderUtils clu : clua) { assertLoadFails(clu, bundledClassName); } }
private void assertLoadSucceeds(String bundledClassName, Class<?> expectedClass, ClassLoaderUtils... clua) throws ClassNotFoundException { for (ClassLoaderUtils clu : clua) { try { assertLoadSucceeds(clu, bundledClassName, expectedClass); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw new IllegalStateException("Load failed for " + clu, e); } } }
@Test public void testIsBundleWhiteListed() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); ClassLoaderUtils clu = new ClassLoaderUtils(getClass(), mgmt); assertTrue(clu.isBundleWhiteListed(getBundle(mgmt, "org.apache.brooklyn.core"))); assertTrue(clu.isBundleWhiteListed(getBundle(mgmt, "org.apache.brooklyn.api"))); assertFalse(clu.isBundleWhiteListed(getBundle(mgmt, "com.google.guava"))); }
Bundle bundle = installBundle(mgmt, bundleUrl); Entity entity = createSimpleEntity(bundleUrl, clazz); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluEntity);