Skip to content

Commit

Permalink
add deny class/resource test case
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang committed Apr 8, 2018
1 parent b1a2b34 commit 725e50c
Showing 1 changed file with 51 additions and 6 deletions.
Expand Up @@ -86,9 +86,9 @@ public void testImport() throws Exception {
BizModel bizModel = new BizModel();
bizModel.setBizName("biz A").setClassPath(new URL[] { classPathURL })
.setClassLoader(new BizClassLoader(bizModel.getBizName(), bizModel.getClassPath()));
bizModel.setDenyImportResources("");
bizModel.setDenyImportClasses("");
bizModel.setDenyImportPackages("");
bizModel.setDenyImportResources(StringUtils.EMPTY_STRING);
bizModel.setDenyImportClasses(StringUtils.EMPTY_STRING);
bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);

bizManagerService.registerBiz(bizModel);

Expand Down Expand Up @@ -127,13 +127,58 @@ public void testLoadClassFromAgentClassLoader() throws ClassNotFoundException {
BizModel bizModel = new BizModel();
bizModel.setBizName("MockBiz").setClassPath(new URL[] {})
.setClassLoader(new BizClassLoader(bizModel.getBizName(), bizModel.getClassPath()));
bizModel.setDenyImportResources("");
bizModel.setDenyImportClasses("");
bizModel.setDenyImportPackages("");
bizModel.setDenyImportResources(StringUtils.EMPTY_STRING);
bizModel.setDenyImportClasses(StringUtils.EMPTY_STRING);
bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);

bizManagerService.registerBiz(bizModel);
BizClassLoader bizClassLoader = (BizClassLoader) bizModel.getBizClassLoader();
Assert.assertNotNull(bizClassLoader.loadClass("SampleClass", false));

}

@Test
public void testDenyImport() throws Exception {
PluginModel pluginA = new PluginModel();
pluginA
.setPluginName("pluginA")
.setClassPath(new URL[] { classPathURL })
.setImportClasses(StringUtils.EMPTY_STRING)
.setImportPackages(StringUtils.EMPTY_STRING)
.setExportIndex(new HashSet<>(Collections.singletonList(ITest.class.getName())))
.setPluginClassLoader(
new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));

pluginManagerService.registerPlugin(pluginA);
pluginDeployService.deploy();
classloaderService.prepareExportClassCache();

BizModel bizModel = new BizModel();
bizModel.setBizName("bizA").setClassPath(new URL[] {})
.setClassLoader(new BizClassLoader(bizModel.getBizName(), bizModel.getClassPath()));
bizModel.setDenyImportResources(StringUtils.EMPTY_STRING);
bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);
bizModel.setDenyImportClasses(StringUtils.EMPTY_STRING);

bizManagerService.registerBiz(bizModel);

Assert.assertNotNull(bizModel.getBizClassLoader().getResource(
"pluginA_sofa_ark_export_resource_test1.xml"));
bizModel.setDenyImportResources("pluginA_sofa_ark_export_resource_test1.xml");
Assert.assertNull(bizModel.getBizClassLoader().getResource(
"pluginA_sofa_ark_export_resource_test1.xml"));

Assert.assertTrue(bizModel.getBizClassLoader().loadClass(ITest.class.getName())
.getClassLoader() instanceof PluginClassLoader);

bizModel.setDenyImportPackages("com.alipay.sofa.ark.container.testdata");
Assert.assertFalse(bizModel.getBizClassLoader().loadClass(ITest.class.getName())
.getClassLoader() instanceof PluginClassLoader);

bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);
bizModel.setDenyImportClasses(ITest.class.getCanonicalName());
Assert.assertFalse(bizModel.getBizClassLoader().loadClass(ITest.class.getName())
.getClassLoader() instanceof PluginClassLoader);

}
}

0 comments on commit 725e50c

Please sign in to comment.