Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ut 1221 #824

Merged
merged 40 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0465098
add ut
Dec 8, 2023
6c4c0e3
add tests
Dec 20, 2023
09fb098
update test
Dec 21, 2023
021d5ed
Merge branch 'master' into add_ut_1221
lylingzhen Dec 21, 2023
fae8af6
update format
Dec 21, 2023
ce3e29c
update test
Dec 21, 2023
41da3a2
update jarutils test
Dec 21, 2023
60feb22
update tests
Dec 21, 2023
d1e9fdb
update tests
Dec 21, 2023
a9876e7
update web server test
Dec 21, 2023
5cd158e
update tests
Dec 21, 2023
5dad709
update tests
Dec 21, 2023
c7e6c05
update test
Dec 21, 2023
47d7539
update test
Dec 21, 2023
9c6652c
update tests
Dec 21, 2023
4c8d28d
update tests
Dec 22, 2023
91ceb80
update format
Dec 22, 2023
4859e29
update tests
Dec 22, 2023
562b000
update test
Dec 22, 2023
2caceaf
update tests
Jan 2, 2024
cf41880
Merge remote-tracking branch 'origin/master' into add_ut_1221
Jan 2, 2024
2d01b9e
update test
Jan 2, 2024
1fdcf98
Delete sofa-ark-parent/core/common/C:\temp dir\b\c directory
lylingzhen Jan 2, 2024
e03c109
update test
Jan 2, 2024
42da47f
Merge remote-tracking branch 'origin/add_ut_1221' into add_ut_1221
Jan 2, 2024
491cbfd
update test
Jan 2, 2024
d561950
update tesst
Jan 3, 2024
89ee098
Merge remote-tracking branch 'origin/master' into add_ut_1221
Jan 3, 2024
d456d5e
update test
Jan 3, 2024
725e0ad
update test
Jan 3, 2024
84fe504
update test
Jan 3, 2024
13f71b7
update test
Jan 3, 2024
b2cb335
update test
Jan 3, 2024
875b118
update test
Jan 4, 2024
be9ecd5
update test
Jan 4, 2024
2b8fa3f
update test
Jan 4, 2024
06b260d
update test
Jan 4, 2024
460af63
update test
Jan 8, 2024
424351e
update test
Jan 8, 2024
29b9ae2
Merge remote-tracking branch 'origin/master' into add_ut_1221
Jan 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;

/**
*
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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("")
Expand All @@ -187,18 +187,34 @@ public void testFindExportResources() {
classloaderService.prepareExportClassAndResourceCache();

Set<String> exportPrefixResourceStems = plugin.getExportPrefixResourceStems();
Assert.assertTrue(exportPrefixResourceStems.contains("com/alipay/sofa/"));
assertTrue(exportPrefixResourceStems.contains("com/alipay/sofa/"));

Set<String> exportSuffixResourceStems = plugin.getExportSuffixResourceStems();
Assert.assertTrue(exportSuffixResourceStems.contains(".xsd"));
assertTrue(exportSuffixResourceStems.contains(".xsd"));

Set<String> 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);
}
}

@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"));
}
}
Loading
Loading