diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/BaseTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/BaseTest.java index b1c2d0853..cfa9ce1ba 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/BaseTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/BaseTest.java @@ -33,7 +33,6 @@ import org.junit.Before; import org.junit.BeforeClass; import org.mockito.MockedStatic; -import org.mockito.Mockito; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; @@ -43,7 +42,7 @@ import static com.alipay.sofa.ark.common.util.FileUtils.file; import static com.alipay.sofa.ark.spi.service.extension.ArkServiceLoader.setExtensionLoaderService; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; /** * @@ -82,11 +81,11 @@ public void before() { mockArguments.add(String.format("-javaagent:%s", workingPath)); mockArguments.add(String.format("-javaagent:%s=xx", workingPath)); - RuntimeMXBean runtimeMXBean = Mockito.mock(RuntimeMXBean.class); + RuntimeMXBean runtimeMXBean = mock(RuntimeMXBean.class); when(runtimeMXBean.getInputArguments()).thenReturn(mockArguments); when(runtimeMXBean.getName()).thenReturn(""); - managementFactoryMockedStatic = Mockito.mockStatic(ManagementFactory.class); + managementFactoryMockedStatic = mockStatic(ManagementFactory.class); managementFactoryMockedStatic.when(ManagementFactory::getRuntimeMXBean).thenReturn(runtimeMXBean); arkServiceContainer.start(); diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/ClassLoaderServiceTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/ClassLoaderServiceTest.java index dc318be9d..3e6875cef 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/ClassLoaderServiceTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/ClassLoaderServiceTest.java @@ -16,24 +16,30 @@ */ package com.alipay.sofa.ark.container.service.classloader; -import com.alipay.sofa.ark.common.util.AssertUtils; import com.alipay.sofa.ark.container.BaseTest; import com.alipay.sofa.ark.container.model.BizModel; import com.alipay.sofa.ark.container.model.PluginModel; import com.alipay.sofa.ark.container.service.ArkServiceContainerHolder; import com.alipay.sofa.ark.spi.model.Biz; -import com.alipay.sofa.ark.spi.model.BizState; import com.alipay.sofa.ark.spi.model.Plugin; import com.alipay.sofa.ark.spi.service.biz.BizManagerService; import com.alipay.sofa.ark.spi.service.classloader.ClassLoaderService; import com.alipay.sofa.ark.spi.service.plugin.PluginManagerService; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.lang.reflect.Field; import java.net.URL; import java.net.URLClassLoader; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import static com.alipay.sofa.ark.common.util.AssertUtils.isFalse; +import static com.alipay.sofa.ark.common.util.AssertUtils.isTrue; +import static com.alipay.sofa.ark.spi.model.BizState.RESOLVED; +import static java.util.Arrays.asList; +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; /** * @@ -59,125 +65,119 @@ public void before() { @Test public void testIsSunReflect() { - Assert.assertTrue(classloaderService - .isSunReflectClass("sun.reflect.GeneratedMethodAccessor100")); + assertTrue(classloaderService.isSunReflectClass("sun.reflect.GeneratedMethodAccessor100")); } @Test public void testIsNotSunReflect() { - Assert.assertFalse(classloaderService.isSunReflectClass("test")); + assertFalse(classloaderService.isSunReflectClass("test")); } @Test public void testIsArkSpiClass() { - Assert.assertTrue(classloaderService - .isArkSpiClass("com.alipay.sofa.ark.spi.service.ArkService")); + assertTrue(classloaderService.isArkSpiClass("com.alipay.sofa.ark.spi.service.ArkService")); } @Test public void testIsNotArkSpiClass() { - Assert.assertFalse(classloaderService.isArkSpiClass("test")); + assertFalse(classloaderService.isArkSpiClass("test")); } @Test public void testJDKClassLoader() { String sunToolClass = "sun.tools.attach.BsdVirtualMachine"; ClassLoader jdkClassLoader = classloaderService.getJDKClassLoader(); - Assert.assertNotNull(jdkClassLoader); + assertNotNull(jdkClassLoader); try { // only when this class can be loaded from system classloader, // then it should be loaded successfully from jdkClassLoader classloaderService.getSystemClassLoader().loadClass(sunToolClass); - Assert.assertNotNull(jdkClassLoader.loadClass(sunToolClass)); + assertNotNull(jdkClassLoader.loadClass(sunToolClass)); } catch (ClassNotFoundException e) { // ignore } - } @Test public void testArkClassLoader() { ClassLoader arkClassLoader = classloaderService.getArkClassLoader(); - Assert.assertNotNull(arkClassLoader); + assertNotNull(arkClassLoader); } @Test public void testSystemClassLoader() { ClassLoader systemClassLoader = classloaderService.getSystemClassLoader(); - Assert.assertNotNull(systemClassLoader); + assertNotNull(systemClassLoader); } @Test public void testAgentClassLoader() throws ClassNotFoundException { ClassLoader agentClassLoader = classloaderService.getAgentClassLoader(); - Assert.assertNotNull(agentClassLoader); - Assert.assertTrue(((URLClassLoader) agentClassLoader).getURLs().length == 2); - Assert.assertNotNull(agentClassLoader.loadClass("SampleClass")); + assertNotNull(agentClassLoader); + assertTrue(((URLClassLoader) agentClassLoader).getURLs().length == 2); + assertNotNull(agentClassLoader.loadClass("SampleClass")); } @Test public void testIsDeniedImportClass() { + Biz biz = new BizModel().setBizName("mockBiz").setBizVersion("1.0.0") .setDenyImportPackages("a.c, a.b.c.*, a.b.c").setDenyImportClasses("") - .setBizState(BizState.RESOLVED); + .setBizState(RESOLVED); bizManagerService.registerBiz(biz); - AssertUtils.isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c"), - "Exception error"); + isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c"), "Exception error"); - AssertUtils.isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c.E"), + isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c.E"), "Exception error"); - AssertUtils.isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c.e.G"), + isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.c.e.G"), "Exception error"); - - AssertUtils.isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c.E"), + isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c.E"), "Exception error"); - AssertUtils.isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c.e.G"), + isTrue(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c.e.G"), "Exception error"); - AssertUtils.isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c"), + isFalse(classloaderService.isDeniedImportClass(biz.getIdentity(), "a.b.c"), "Exception error"); } @Test public void testIsClassImport() { + Plugin plugin = new PluginModel().setPluginName("mockPlugin").setImportClasses(null) .setImportPackages("a.c,a.b.c.*,a.b.c"); pluginManagerService.registerPlugin(plugin); - Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.c.e")); - Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c")); - Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c.e.f")); - - Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.b.c")); - Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e")); - Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e.f")); + assertTrue(classloaderService.isClassInImport("mockPlugin", "a.c.e")); + assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c")); + assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c.e.f")); + assertFalse(classloaderService.isClassInImport("mockPlugin", "a.b.c")); + assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e")); + assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e.f")); } @Test public void testFindExportClass() { + PluginClassLoader pluginClassLoader = new PluginClassLoader("mockPlugin", new URL[] {}); Plugin plugin = new PluginModel().setPluginName("mockPlugin") .setExportPackages("a.b.*,a.f,a.b.f").setExportClasses("a.e.f.G") .setPluginClassLoader(pluginClassLoader).setExportResources(""); + pluginManagerService.registerPlugin(plugin); classloaderService.prepareExportClassAndResourceCache(); - Assert.assertNull(classloaderService.findExportClassLoader("a.b")); - Assert.assertTrue(pluginClassLoader.equals(classloaderService - .findExportClassLoader("a.b.e.f"))); - Assert.assertTrue(pluginClassLoader.equals(classloaderService - .findExportClassLoader("a.f.g"))); - Assert.assertTrue(pluginClassLoader.equals(classloaderService - .findExportClassLoader("a.e.f.G"))); - Assert.assertTrue(pluginClassLoader.equals(classloaderService - .findExportClassLoader("a.b.f.m"))); - Assert.assertTrue(pluginClassLoader.equals(classloaderService - .findExportClassLoader("a.b.f.m.g"))); - Assert.assertNull(classloaderService.findExportClassLoader("a.f.h.m")); - Assert.assertNull(classloaderService.findExportClassLoader("a")); + assertNull(classloaderService.findExportClassLoader("a.b")); + assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.e.f"))); + assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.f.g"))); + assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.e.f.G"))); + assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.f.m"))); + assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.f.m.g"))); + assertNull(classloaderService.findExportClassLoader("a.f.h.m")); + assertNull(classloaderService.findExportClassLoader("a")); pluginManagerService.getPluginsInOrder().remove(plugin); } @Test public void testFindExportResources() { + PluginClassLoader pluginClassLoader = new PluginClassLoader("mockPlugin", new URL[] {}); String exportResources = "spring-beans.xsd,*.xsd,com/alipay/sofa/*,xml-test.xml"; Plugin plugin = new PluginModel().setPluginName("mockPlugin").setExportPackages("") @@ -187,18 +187,34 @@ public void testFindExportResources() { classloaderService.prepareExportClassAndResourceCache(); Set exportPrefixResourceStems = plugin.getExportPrefixResourceStems(); - Assert.assertTrue(exportPrefixResourceStems.contains("com/alipay/sofa/")); + assertTrue(exportPrefixResourceStems.contains("com/alipay/sofa/")); Set exportSuffixResourceStems = plugin.getExportSuffixResourceStems(); - Assert.assertTrue(exportSuffixResourceStems.contains(".xsd")); + assertTrue(exportSuffixResourceStems.contains(".xsd")); Set resources = plugin.getExportResources(); - Assert.assertTrue(resources.contains("xml-test.xml")); - Assert.assertTrue(resources.contains("spring-beans.xsd")); + assertTrue(resources.contains("xml-test.xml")); + assertTrue(resources.contains("spring-beans.xsd")); plugin.getExportPrefixResourceStems().clear(); plugin.getExportSuffixResourceStems().clear(); plugin.getExportResources().clear(); pluginManagerService.getPluginsInOrder().remove(plugin); } -} \ No newline at end of file + + @Test + public void testFindExportResourceClassLoadersInOrder() throws Exception { + + Field field = ClassLoaderServiceImpl.class + .getDeclaredField("exportSuffixStemResourceAndClassLoaderMap"); + field.setAccessible(true); + ConcurrentHashMap exportPrefixStemResourceAndClassLoaderMap = new ConcurrentHashMap<>(); + Plugin plugin = mock(Plugin.class); + exportPrefixStemResourceAndClassLoaderMap.put("myaaa", asList(plugin)); + field.set(classloaderService, exportPrefixStemResourceAndClassLoaderMap); + assertEquals(null, classloaderService.findExportResourceClassLoadersInOrder("myaaa").get(0)); + + assertNull(classloaderService.getBizClassLoader("aaa:1.0")); + assertNull(classloaderService.getPluginClassLoader("aaa:2.0")); + } +} diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/PluginClassLoaderTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/PluginClassLoaderTest.java index 8f043a2be..bd0bafc9c 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/PluginClassLoaderTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/classloader/PluginClassLoaderTest.java @@ -17,26 +17,31 @@ package com.alipay.sofa.ark.container.service.classloader; import com.alipay.sofa.ark.api.ArkClient; -import com.alipay.sofa.ark.common.util.ClassLoaderUtils; -import com.alipay.sofa.ark.common.util.ClassUtils; -import com.alipay.sofa.ark.common.util.StringUtils; import com.alipay.sofa.ark.container.BaseTest; -import com.alipay.sofa.ark.container.testdata.ITest; import com.alipay.sofa.ark.container.model.PluginModel; import com.alipay.sofa.ark.container.service.ArkServiceContainerHolder; +import com.alipay.sofa.ark.container.testdata.ITest; +import com.alipay.sofa.ark.exception.ArkLoaderException; import com.alipay.sofa.ark.spi.service.classloader.ClassLoaderService; import com.alipay.sofa.ark.spi.service.plugin.PluginDeployService; import com.alipay.sofa.ark.spi.service.plugin.PluginManagerService; -import com.google.common.collect.Sets; import org.junit.Assert; import org.junit.Before; import org.junit.Test; + import java.io.IOException; import java.net.URL; import java.util.Collections; import java.util.Enumeration; import java.util.List; +import static com.alipay.sofa.ark.common.util.ClassLoaderUtils.getURLs; +import static com.alipay.sofa.ark.common.util.ClassUtils.getPackageName; +import static com.alipay.sofa.ark.common.util.StringUtils.EMPTY_STRING; +import static com.google.common.collect.Sets.newHashSet; +import static java.lang.ClassLoader.getSystemClassLoader; +import static org.junit.Assert.*; + /** * @author ruoshan * @since 0.1.0 @@ -69,12 +74,12 @@ public void testExportAndImport() throws Exception { pluginA .setPluginName("plugin A") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(ClassUtils.getPackageName(ITest.class.getName())) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(getPackageName(ITest.class.getName())) .setExportClasses("") - .setExportResources(StringUtils.EMPTY_STRING) - .setImportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())); @@ -84,11 +89,11 @@ public void testExportAndImport() throws Exception { .setPriority("1") .setClassPath(new URL[] { classPathURL }) .setImportClasses(ITest.class.getName()) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") - .setExportResources(StringUtils.EMPTY_STRING) - .setImportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())); @@ -97,8 +102,8 @@ public void testExportAndImport() throws Exception { classloaderService.prepareExportClassAndResourceCache(); pluginDeployService.deploy(); - Assert.assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), - pluginB.getPluginClassLoader().loadClass(ITest.class.getName())); + assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), pluginB + .getPluginClassLoader().loadClass(ITest.class.getName())); } @Test @@ -107,12 +112,12 @@ public void testExportAndNotImport() throws Exception { pluginA .setPluginName("plugin A") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("com.alipay.sofa.ark") .setExportClasses("") - .setExportResources(StringUtils.EMPTY_STRING) - .setImportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())); @@ -121,12 +126,12 @@ public void testExportAndNotImport() throws Exception { .setPluginName("plugin B") .setPriority("1") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") - .setExportResources(StringUtils.EMPTY_STRING) - .setImportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())); @@ -146,11 +151,11 @@ public void testExportResource() { pluginA .setPluginName("pluginA") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(getPackageName(ITest.class.getCanonicalName())) .setExportClasses("") - .setImportResources(StringUtils.EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setExportResources("pluginA_export_resource1.xml,pluginA_export_resource2.xml") .setPluginClassLoader( new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())); @@ -160,12 +165,12 @@ public void testExportResource() { .setPluginName("pluginB") .setPriority("1") .setClassPath(new URL[0]) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") .setImportResources("pluginA_export_resource1.xml") - .setExportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())); @@ -174,8 +179,7 @@ public void testExportResource() { classloaderService.prepareExportClassAndResourceCache(); pluginDeployService.deploy(); - Assert.assertNotNull(pluginB.getPluginClassLoader().getResource( - "pluginA_export_resource1.xml")); + assertNotNull(pluginB.getPluginClassLoader().getResource("pluginA_export_resource1.xml")); Assert.assertNull(pluginB.getPluginClassLoader() .getResource("pluginA_export_resource2.xml")); Assert.assertNull(pluginB.getPluginClassLoader().getResource( @@ -192,11 +196,11 @@ public void testMultiExportResource() throws Exception { .setPluginName("pluginA") .setPriority("100") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(getPackageName(ITest.class.getCanonicalName())) .setExportClasses("") - .setImportResources(StringUtils.EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setExportResources(resourceName) .setPluginClassLoader( new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())); @@ -206,11 +210,11 @@ public void testMultiExportResource() throws Exception { .setPluginName("pluginB") .setPriority("1") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") - .setImportResources(StringUtils.EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setExportResources(resourceName) .setPluginClassLoader( new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())); @@ -220,11 +224,11 @@ public void testMultiExportResource() throws Exception { .setPluginName("pluginC") .setPriority("1000") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") - .setImportResources(StringUtils.EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setExportResources(resourceName) .setPluginClassLoader( new PluginClassLoader(pluginC.getPluginName(), pluginC.getClassPath())); @@ -233,12 +237,12 @@ public void testMultiExportResource() throws Exception { pluginD .setPluginName("pluginD") .setClassPath(new URL[0]) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages("") .setExportClasses("") .setImportResources(resourceName) - .setExportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginD.getPluginName(), pluginD.getClassPath())); @@ -250,14 +254,14 @@ public void testMultiExportResource() throws Exception { pluginDeployService.deploy(); Enumeration urlEnumeration = pluginD.getPluginClassLoader().getResources(resourceName); - Assert.assertEquals(3, Collections.list(urlEnumeration).size()); + assertEquals(3, Collections.list(urlEnumeration).size()); List classLoaders = classloaderService .findExportResourceClassLoadersInOrder(resourceName); - Assert.assertEquals(3, classLoaders.size()); - Assert.assertEquals(pluginB.getPluginClassLoader(), classLoaders.get(0)); - Assert.assertEquals(pluginA.getPluginClassLoader(), classLoaders.get(1)); - Assert.assertEquals(pluginC.getPluginClassLoader(), classLoaders.get(2)); + assertEquals(3, classLoaders.size()); + assertEquals(pluginB.getPluginClassLoader(), classLoaders.get(0)); + assertEquals(pluginA.getPluginClassLoader(), classLoaders.get(1)); + assertEquals(pluginC.getPluginClassLoader(), classLoaders.get(2)); } @Test @@ -267,11 +271,11 @@ public void testExportResourceStems() { .setPluginName("pluginA") .setPriority("100") .setClassPath(new URL[] { classPathURL }) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(StringUtils.EMPTY_STRING) - .setExportClasses(StringUtils.EMPTY_STRING) - .setImportResources(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(EMPTY_STRING) + .setExportClasses(EMPTY_STRING) + .setImportResources(EMPTY_STRING) .setExportResources("export/folderA/*,export/folderB/*") .setPluginClassLoader( new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())); @@ -281,12 +285,12 @@ public void testExportResourceStems() { .setPluginName("pluginB") .setPriority("1") .setClassPath(new URL[0]) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(StringUtils.EMPTY_STRING) - .setExportClasses(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(EMPTY_STRING) + .setExportClasses(EMPTY_STRING) .setImportResources("export/folderA/*,export/folderB/test3.xml") - .setExportResources(StringUtils.EMPTY_STRING) + .setExportResources(EMPTY_STRING) .setPluginClassLoader( new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())); @@ -300,11 +304,11 @@ public void testExportResourceStems() { String testResource3 = "export/folderB/test3.xml"; String testResource4 = "export/folderB/test4.xml"; - Assert.assertEquals(pluginA.getPluginClassLoader().getResource(testResource1), pluginB + assertEquals(pluginA.getPluginClassLoader().getResource(testResource1), pluginB .getPluginClassLoader().getResource(testResource1)); - Assert.assertEquals(pluginA.getPluginClassLoader().getResource(testResource2), pluginB + assertEquals(pluginA.getPluginClassLoader().getResource(testResource2), pluginB .getPluginClassLoader().getResource(testResource2)); - Assert.assertEquals(pluginA.getPluginClassLoader().getResource(testResource3), pluginB + assertEquals(pluginA.getPluginClassLoader().getResource(testResource3), pluginB .getPluginClassLoader().getResource(testResource3)); // export/folderB/test4.xml not import Assert.assertNull(pluginB.getPluginClassLoader().getResource(testResource4)); @@ -316,18 +320,18 @@ public void testLoadClassFromAgentClassLoader() throws ClassNotFoundException { mockPlugin .setPluginName("Mock plugin") .setClassPath(new URL[] {}) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages(ITest.class.getCanonicalName()) .setPluginClassLoader( new PluginClassLoader(mockPlugin.getPluginName(), mockPlugin.getClassPath())); pluginManagerService.registerPlugin(mockPlugin); PluginClassLoader pluginClassLoader = (PluginClassLoader) mockPlugin.getPluginClassLoader(); - Assert.assertNotNull(pluginClassLoader.loadClass("SampleClass", false)); + assertNotNull(pluginClassLoader.loadClass("SampleClass", false)); Class clazz = pluginClassLoader.loadClass(ArkClient.class.getCanonicalName()); - Assert.assertTrue(clazz.getClassLoader().equals(classloaderService.getArkClassLoader())); + assertTrue(clazz.getClassLoader().equals(classloaderService.getArkClassLoader())); } @Test @@ -337,9 +341,9 @@ public void testGetJdkResource() throws IOException { mockPlugin .setPluginName("Mock plugin") .setClassPath(new URL[] {}) - .setImportResources(StringUtils.EMPTY_STRING) - .setImportClasses(StringUtils.EMPTY_STRING) - .setImportPackages(StringUtils.EMPTY_STRING) + .setImportResources(EMPTY_STRING) + .setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) .setExportPackages(ITest.class.getCanonicalName()) .setPluginClassLoader( new PluginClassLoader(mockPlugin.getPluginName(), mockPlugin.getClassPath())); @@ -349,37 +353,64 @@ public void testGetJdkResource() throws IOException { // String name = "META-INF/services/javax.script.ScriptEngineFactory"; String name = "javax/lang/model/element/Modifier.class"; - URL res1 = cl.getResource(name); - Assert.assertNotNull(res1); + assertNotNull(res1); - URL res2 = ClassLoader.getSystemClassLoader().getResource(name); - Assert.assertNotNull(res2); - Assert.assertEquals(res2, res1); + URL res2 = getSystemClassLoader().getResource(name); + assertNotNull(res2); + assertEquals(res2, res1); Enumeration enu1 = cl.getResources(name); - Assert.assertTrue(enu1.hasMoreElements()); + assertTrue(enu1.hasMoreElements()); - Enumeration enu2 = ClassLoader.getSystemClassLoader().getResources(name); - Assert.assertEquals(Sets.newHashSet(Collections.list(enu2)), - Sets.newHashSet(Collections.list(enu1))); + Enumeration enu2 = getSystemClassLoader().getResources(name); + assertEquals(newHashSet(Collections.list(enu2)), newHashSet(Collections.list(enu1))); } @Test public void testSlashResource() throws Throwable { ClassLoader classLoader = this.getClass().getClassLoader(); PluginClassLoader pluginClassLoader = new PluginClassLoader("pluginName", - ClassLoaderUtils.getURLs(classLoader)); + getURLs(classLoader)); PluginModel mockPlugin = new PluginModel(); mockPlugin.setPluginName("pluginName").setClassPath(new URL[] {}) - .setImportResources(StringUtils.EMPTY_STRING) - .setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING) - .setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())) - .setExportClasses(StringUtils.EMPTY_STRING).setPluginClassLoader(pluginClassLoader); + .setImportResources(EMPTY_STRING).setImportClasses(EMPTY_STRING) + .setImportPackages(EMPTY_STRING) + .setExportPackages(getPackageName(ITest.class.getCanonicalName())) + .setExportClasses(EMPTY_STRING).setPluginClassLoader(pluginClassLoader); pluginManagerService.registerPlugin(mockPlugin); URL url = pluginClassLoader.getResource(""); - Assert.assertNotNull(url); - Assert.assertEquals(url, this.getClass().getResource("/")); + assertNotNull(url); + assertEquals(url, this.getClass().getResource("/")); } + @Test(expected = ArkLoaderException.class) + public void testLoadClassInternalWithSunClass() throws Exception { + PluginClassLoader pluginClassLoader = new PluginClassLoader("a", new URL[] { this + .getClass().getResource("/") }); + assertEquals("a", pluginClassLoader.getPluginName()); + pluginClassLoader.loadClassInternal("sun.reflect.GeneratedMethodAccessor", true); + } + + @Test(expected = ArkLoaderException.class) + public void testLoadClassInternalWithResolve() throws Exception { + PluginClassLoader pluginClassLoader = new PluginClassLoader("a", new URL[] { this + .getClass().getResource("/") }); + assertEquals("a", pluginClassLoader.getPluginName()); + pluginClassLoader.loadClassInternal("java.lang.a", true); + } + + @Test(expected = ArkLoaderException.class) + public void testPreLoadClassWithException() throws Exception { + PluginClassLoader pluginClassLoader = new PluginClassLoader("a", new URL[] { this + .getClass().getResource("/") }); + pluginClassLoader.preLoadClass("a"); + } + + @Test(expected = ArkLoaderException.class) + public void testPostLoadClassWithException() throws Exception { + PluginClassLoader pluginClassLoader = new PluginClassLoader("a", new URL[] { this + .getClass().getResource("/") }); + pluginClassLoader.postLoadClass("a"); + } } \ No newline at end of file diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginCommandProviderTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginCommandProviderTest.java index e9cbecf11..8d052cb46 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginCommandProviderTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginCommandProviderTest.java @@ -18,18 +18,21 @@ import com.alipay.sofa.ark.common.util.ClassLoaderUtils; import com.alipay.sofa.ark.container.model.PluginModel; +import com.alipay.sofa.ark.container.service.plugin.PluginCommandProvider.PluginCommand; import com.alipay.sofa.ark.spi.model.Plugin; import com.alipay.sofa.ark.spi.service.plugin.PluginManagerService; -import org.junit.Assert; import org.junit.Test; -import org.mockito.Mockito; -import static org.mockito.Mockito.when; import java.lang.reflect.Field; import java.net.URL; import java.util.HashSet; import java.util.Set; +import static com.google.common.collect.Sets.newHashSet; +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * @author qilong.zql * @since 0.6.0 @@ -38,21 +41,22 @@ public class PluginCommandProviderTest { @Test public void testPluginCommandFormat() { + PluginCommandProvider pluginCommandProvider = new PluginCommandProvider(); - Assert.assertFalse(pluginCommandProvider.validate(" plugin ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -h ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -m pluginA ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -s pluginB pluginA ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -d plugin* ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -m -d -s plugin* ")); - Assert.assertTrue(pluginCommandProvider.validate(" plugin -msd pluginA ")); - - Assert.assertFalse(pluginCommandProvider.validate(" plu")); - Assert.assertFalse(pluginCommandProvider.validate(" plugin -h pluginA ")); - Assert.assertFalse(pluginCommandProvider.validate(" plugin -hm pluginA ")); - Assert.assertFalse(pluginCommandProvider.validate(" plugin -mb pluginA ")); - Assert.assertFalse(pluginCommandProvider.validate(" plugin -m -b pluginA ")); - Assert.assertFalse(pluginCommandProvider.validate(" plugin -m ")); + assertFalse(pluginCommandProvider.validate(" plugin ")); + assertTrue(pluginCommandProvider.validate(" plugin -h ")); + assertTrue(pluginCommandProvider.validate(" plugin -m pluginA ")); + assertTrue(pluginCommandProvider.validate(" plugin -s pluginB pluginA ")); + assertTrue(pluginCommandProvider.validate(" plugin -d plugin* ")); + assertTrue(pluginCommandProvider.validate(" plugin -m -d -s plugin* ")); + assertTrue(pluginCommandProvider.validate(" plugin -msd pluginA ")); + + assertFalse(pluginCommandProvider.validate(" plu")); + assertFalse(pluginCommandProvider.validate(" plugin -h pluginA ")); + assertFalse(pluginCommandProvider.validate(" plugin -hm pluginA ")); + assertFalse(pluginCommandProvider.validate(" plugin -mb pluginA ")); + assertFalse(pluginCommandProvider.validate(" plugin -m -b pluginA ")); + assertFalse(pluginCommandProvider.validate(" plugin -m ")); } @Test @@ -79,7 +83,7 @@ public void testPluginCommandProcess() { set.add("pluginA"); set.add("pluginB"); - PluginManagerService pluginManagerService = Mockito.mock(PluginManagerService.class); + PluginManagerService pluginManagerService = mock(PluginManagerService.class); when(pluginManagerService.getAllPluginNames()).thenReturn(set); when(pluginManagerService.getPluginByName("pluginA")).thenReturn(pluginA); when(pluginManagerService.getPluginByName("pluginB")).thenReturn(pluginB); @@ -95,31 +99,52 @@ public void testPluginCommandProcess() { String errorMessage = "Error command format. Pls type 'plugin -h' to get help message\n"; - Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu"))); - Assert.assertTrue(errorMessage.equals(pluginCommandProvider - .handleCommand("plugin -h pluginA"))); - Assert.assertTrue(errorMessage.equals(pluginCommandProvider - .handleCommand("plugin -b pluginA"))); - Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu"))); + assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu"))); + assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -h pluginA"))); + assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -b pluginA"))); + assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu"))); - Assert.assertTrue(pluginCommandProvider.getHelp().equals( + assertTrue(pluginCommandProvider.getHelp().equals( pluginCommandProvider.handleCommand("plugin -h"))); - Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin "))); + assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin "))); String details = pluginCommandProvider.handleCommand("plugin -m -d -s pluginA"); - Assert.assertTrue(details.contains("Activator")); - Assert.assertTrue(details.contains("GroupId")); + assertTrue(details.contains("Activator")); + assertTrue(details.contains("GroupId")); details = pluginCommandProvider.handleCommand("plugin -d pluginB"); - Assert.assertTrue(details.contains("GroupId")); - Assert.assertFalse(details.contains("Activator")); + assertTrue(details.contains("GroupId")); + assertFalse(details.contains("Activator")); details = pluginCommandProvider.handleCommand("plugin -m plugin."); - Assert.assertTrue(details.contains("pluginA")); - Assert.assertTrue(details.contains("pluginB")); + assertTrue(details.contains("pluginA")); + assertTrue(details.contains("pluginB")); details = pluginCommandProvider.handleCommand("plugin -m pluginC"); - Assert.assertTrue(details.contains("no matched plugin candidates.")); + assertTrue(details.contains("no matched plugin candidates.")); } + @Test + public void testPluginList() { + + PluginCommandProvider pluginCommandProvider = new PluginCommandProvider(); + PluginCommand pluginCommand = pluginCommandProvider.new PluginCommand(null); + assertFalse(pluginCommand.isValidate()); + pluginCommand = pluginCommandProvider.new PluginCommand("plugin -"); + assertFalse(pluginCommand.isValidate()); + pluginCommand = pluginCommandProvider.new PluginCommand("plugin -a"); + + PluginManagerService pluginManagerService = mock(PluginManagerService.class); + try { + Field field = PluginCommandProvider.class.getDeclaredField("pluginManagerService"); + field.setAccessible(true); + field.set(pluginCommandProvider, pluginManagerService); + } catch (Throwable throwable) { + // ignore + } + + assertEquals("no plugins.\nplugin count = 0\n", pluginCommand.process()); + when(pluginManagerService.getAllPluginNames()).thenReturn(newHashSet("a")); + assertEquals("a\nplugin count = 1\n", pluginCommand.process()); + } } \ No newline at end of file diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginFactoryServiceTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginFactoryServiceTest.java index 24f68e816..508fb57c9 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginFactoryServiceTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginFactoryServiceTest.java @@ -16,21 +16,20 @@ */ package com.alipay.sofa.ark.container.service.plugin; -import com.alipay.sofa.ark.api.ArkConfigs; import com.alipay.sofa.ark.container.BaseTest; +import com.alipay.sofa.ark.exception.ArkRuntimeException; import com.alipay.sofa.ark.loader.JarPluginArchive; import com.alipay.sofa.ark.loader.archive.JarFileArchive; import com.alipay.sofa.ark.loader.jar.JarFile; import com.alipay.sofa.ark.spi.archive.PluginArchive; import com.alipay.sofa.ark.spi.model.Plugin; import com.alipay.sofa.ark.spi.service.plugin.PluginFactoryService; -import org.junit.Assert; +import com.alipay.sofa.ark.spi.service.plugin.PluginManagerService; import org.junit.Test; import java.io.File; import java.io.IOException; import java.net.URL; -import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -39,6 +38,7 @@ import static java.lang.String.format; import static java.util.Arrays.asList; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; /** * @author qilong.zql @@ -93,4 +93,26 @@ public void testCreateEmbedPlugin() throws IOException { .getClassLoader()); assertNotNull(plugin); } -} \ No newline at end of file + + @Test(expected = ArkRuntimeException.class) + public void testDeploy() { + PluginDeployServiceImpl pluginDeployServiceImpl = new PluginDeployServiceImpl(); + PluginManagerService pluginManagerService = mock(PluginManagerService.class); + Plugin plugin = mock(Plugin.class); + doThrow(new ArkRuntimeException("test")).when(plugin).start(); + when(pluginManagerService.getPluginsInOrder()).thenReturn(asList(plugin)); + pluginDeployServiceImpl.pluginManagerService = pluginManagerService; + pluginDeployServiceImpl.deploy(); + } + + @Test(expected = ArkRuntimeException.class) + public void testUndeploy() { + PluginDeployServiceImpl pluginDeployServiceImpl = new PluginDeployServiceImpl(); + PluginManagerService pluginManagerService = mock(PluginManagerService.class); + Plugin plugin = mock(Plugin.class); + doThrow(new ArkRuntimeException("test")).when(plugin).stop(); + when(pluginManagerService.getPluginsInOrder()).thenReturn(asList(plugin)); + pluginDeployServiceImpl.pluginManagerService = pluginManagerService; + pluginDeployServiceImpl.unDeploy(); + } +} diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginManagerServiceTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginManagerServiceTest.java index 43a56ac8e..337b70cf4 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginManagerServiceTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/plugin/PluginManagerServiceTest.java @@ -22,6 +22,8 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.*; + /** * * @author ruoshan @@ -36,9 +38,8 @@ public void testGetPluginByName() { PluginModel plugin = new PluginModel(); plugin.setPluginName("plugin A"); pluginManagerService.registerPlugin(plugin); - Assert.assertNotNull(pluginManagerService.getPluginByName(plugin.getPluginName())); - Assert.assertNull(pluginManagerService.getPluginByName("test")); - + assertNotNull(pluginManagerService.getPluginByName(plugin.getPluginName())); + assertNull(pluginManagerService.getPluginByName("test")); } @Test @@ -51,15 +52,14 @@ public void testGetAllPluginNames() { pluginB.setPluginName("plugin B"); pluginManagerService.registerPlugin(pluginB); - Assert.assertTrue(pluginManagerService.getAllPluginNames() - .contains(pluginA.getPluginName())); - Assert.assertTrue(pluginManagerService.getAllPluginNames() - .contains(pluginB.getPluginName())); - Assert.assertEquals(2, pluginManagerService.getAllPluginNames().size()); + assertTrue(pluginManagerService.getAllPluginNames().contains(pluginA.getPluginName())); + assertTrue(pluginManagerService.getAllPluginNames().contains(pluginB.getPluginName())); + assertEquals(2, pluginManagerService.getAllPluginNames().size()); } @Test public void testGetPluginsInOrder() { + PluginModel pluginA = new PluginModel(); pluginA.setPluginName("plugin A").setPriority("100"); pluginManagerService.registerPlugin(pluginA); @@ -72,9 +72,9 @@ public void testGetPluginsInOrder() { pluginC.setPluginName("plugin C").setPriority("1000"); pluginManagerService.registerPlugin(pluginC); - Assert.assertEquals(3, pluginManagerService.getPluginsInOrder().size()); - Assert.assertEquals(pluginB, pluginManagerService.getPluginsInOrder().get(0)); - Assert.assertEquals(pluginA, pluginManagerService.getPluginsInOrder().get(1)); - Assert.assertEquals(pluginC, pluginManagerService.getPluginsInOrder().get(2)); + assertEquals(3, pluginManagerService.getPluginsInOrder().size()); + assertEquals(pluginB, pluginManagerService.getPluginsInOrder().get(0)); + assertEquals(pluginA, pluginManagerService.getPluginsInOrder().get(1)); + assertEquals(pluginC, pluginManagerService.getPluginsInOrder().get(2)); } -} \ No newline at end of file +} diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/registry/ServiceRegistrationTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/registry/ServiceRegistrationTest.java index 7cae09f05..6b4d38424 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/registry/ServiceRegistrationTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/registry/ServiceRegistrationTest.java @@ -52,8 +52,8 @@ import java.net.URL; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static com.google.common.collect.Sets.newHashSet; +import static org.junit.Assert.*; import static org.mockito.Mockito.mock; /** @@ -106,7 +106,7 @@ public void testPublishService() { int c = registryService.unPublishServices(new DefaultServiceFilter().setServiceInterface( ITest.class).setProviderType(ServiceProviderType.ARK_CONTAINER)); - Assert.assertTrue(c == 1); + assertTrue(c == 1); iTestServiceReference = registryService.referenceService(ITest.class); Assert.assertNull(iTestServiceReference); @@ -155,18 +155,20 @@ public void testPublishDuplicateService() { .getService().test()); int c = registryService.unPublishServices(new DefaultServiceFilter().setUniqueId("testA")); - Assert.assertTrue(c == 1); + assertTrue(c == 1); c = registryService.unPublishServices(new DefaultServiceFilter().setProviderType( ServiceProviderType.ARK_CONTAINER).setServiceInterface(ITest.class)); - Assert.assertTrue(c == 1); + assertTrue(c == 1); assertEquals(0, registryService.referenceServices(ITest.class).size()); } @Test public void testPublishDuplicateServiceInPlugin() throws Exception { + PluginModel pluginA = new PluginModel(); + PluginContextImpl pluginContext = new PluginContextImpl(pluginA); pluginA .setPluginName("plugin A") .setPriority("10") @@ -193,7 +195,12 @@ public void testPublishDuplicateServiceInPlugin() throws Exception { int c = registryService.unPublishServices(new DefaultServiceFilter() .setProviderType(ServiceProviderType.ARK_PLUGIN)); - Assert.assertTrue(c == 1); + assertEquals(1, c); + + assertEquals(pluginA, pluginContext.getPlugin()); + assertEquals(null, pluginContext.getPlugin("notexists")); + assertEquals(PluginClassLoader.class, pluginContext.getClassLoader().getClass()); + assertEquals(newHashSet("plugin A"), pluginContext.getPluginNames()); } @Test @@ -303,7 +310,7 @@ public void testFilter() { List references = registryService .referenceServices(new DefaultServiceFilter().setServiceInterface(ITest.class) .setProviderType(ServiceProviderType.ARK_PLUGIN)); - Assert.assertTrue(2 == references.size()); + assertTrue(2 == references.size()); PluginServiceProvider provider = (PluginServiceProvider) references.get(0) .getServiceMetadata().getServiceProvider(); @@ -323,18 +330,18 @@ public boolean match(ServiceReference serviceReference) { return false; } }); - Assert.assertTrue(1 == references.size()); + assertTrue(1 == references.size()); provider = (PluginServiceProvider) references.get(0).getServiceMetadata() .getServiceProvider(); assertEquals(pluginA.getPluginName(), provider.getPluginName()); references = registryService.referenceServices(new DefaultServiceFilter() .setServiceInterface(ITest.class)); - Assert.assertTrue(3 == references.size()); + assertTrue(3 == references.size()); references = registryService.referenceServices(new DefaultServiceFilter().setProviderType( ServiceProviderType.ARK_CONTAINER).setServiceInterface(ITest.class)); - Assert.assertTrue(1 == references.size()); + assertTrue(1 == references.size()); assertEquals("TestObject C", ((TestObjectC) references.get(0).getService()).test()); }