Skip to content

Commit

Permalink
Add ut 1221 (#813)
Browse files Browse the repository at this point in the history
* add ut

* add tests

* update test

* update format

* update test

* update jarutils test

* update tests

* update tests

* update web server test

* update tests

* update tests

* update test

* update test

* update tests

* update tests

* update format

* update tests

* update test

---------

Co-authored-by: LiuYu <ly109106@alibaba-inc.com>
  • Loading branch information
lylingzhen and LiuYu committed Dec 22, 2023
1 parent 52910e5 commit bb17be2
Show file tree
Hide file tree
Showing 46 changed files with 1,690 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private String normalize(String file) {
.append(file.substring(0, afterLastSeparatorIndex)).append(afterSeparator).toString();
}

private String replaceParentDir(String file) {
String replaceParentDir(String file) {
int parentDirIndex;
while ((parentDirIndex = file.indexOf("/../")) >= 0) {
int precedingSlashIndex = file.lastIndexOf('/', parentDirIndex - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class JarUtils {

private static final Map<String, Optional<String>> artifactIdCacheMap = new ConcurrentHashMap<>();

private static File searchPomProperties(File dirOrFile) {
static File searchPomProperties(File dirOrFile) {
if (dirOrFile == null || !dirOrFile.exists()) {
return null;
}
Expand All @@ -84,8 +84,8 @@ private static File searchPomProperties(File dirOrFile) {
return null;
}

private static String getArtifactIdFromLocalClassPath(String fileClassPath) {
// file:/Users/youji.zzl/Documents/workspace/iexpprodbase/app/bootstrap/target/classes/spring/
static String getArtifactIdFromLocalClassPath(String fileClassPath) {

String libraryFile = fileClassPath.replace("file:", "");
// 1. search pom.properties
int classesRootIndex = libraryFile.endsWith(CLASSPATH_ROOT_IDENTITY) ? libraryFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
import com.alipay.sofa.ark.spi.archive.Archive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.MockedStatic;
Expand All @@ -35,6 +34,9 @@
import java.util.Arrays;
import java.util.List;

import static com.alipay.sofa.ark.bootstrap.ArkLauncher.main;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

/**
Expand All @@ -46,7 +48,8 @@ public class ArkLauncherTest {
static MockedStatic<ManagementFactory> managementFactoryMockedStatic;

@BeforeClass
public static void setup(){
public static void setup() {

List<String> mockArguments = new ArrayList<>();
String filePath = ClasspathLauncherTest.class.getClassLoader()
.getResource("SampleClass.class").getPath();
Expand All @@ -56,9 +59,8 @@ public static void setup(){
RuntimeMXBean runtimeMXBean = Mockito.mock(RuntimeMXBean.class);
when(runtimeMXBean.getInputArguments()).thenReturn(mockArguments);

managementFactoryMockedStatic = Mockito.mockStatic(ManagementFactory.class);
managementFactoryMockedStatic = mockStatic(ManagementFactory.class);
managementFactoryMockedStatic.when(ManagementFactory::getRuntimeMXBean).thenReturn(runtimeMXBean);

}

@AfterClass
Expand All @@ -68,9 +70,10 @@ public static void tearDown() {

@Test
public void testContainerClassLoader() throws Exception {

URL url = this.getClass().getClassLoader().getResource("sample-springboot-fat-biz.jar");
URL[] agentUrl = ClassLoaderUtils.getAgentClassPath();
Assert.assertEquals(1, agentUrl.length);
assertEquals(1, agentUrl.length);

List<URL> urls = new ArrayList<>();
JarFileArchive jarFileArchive = new JarFileArchive(new File(url.getFile()));
Expand All @@ -80,25 +83,31 @@ public void testContainerClassLoader() throws Exception {
}
urls.addAll(Arrays.asList(agentUrl));


EmbedClassPathArchive classPathArchive = new EmbedClassPathArchive(
this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));
this.getClass().getCanonicalName(), null, urls.toArray(new URL[]{}));
ArkLauncher arkLauncher = new ArkLauncher(classPathArchive);
ClassLoader classLoader = arkLauncher.createContainerClassLoader(classPathArchive.getContainerArchive());
Assert.assertNotNull(classLoader);
assertNotNull(classLoader);

try {
Class clazz = classLoader.loadClass("com.alipay.sofa.ark.bootstrap.ArkLauncher");
Assert.assertNotNull(clazz);
assertNotNull(clazz);
clazz = classLoader.loadClass("SampleClass");
Assert.assertNotNull(clazz);
} catch (Exception e){
Assert.assertTrue("loadClass class failed ",false);
assertNotNull(clazz);
} catch (Exception e) {
assertTrue("loadClass class failed ", false);
}
Assert.assertThrows(ClassNotFoundException.class, () -> classLoader.loadClass("NotExistClass"));

assertThrows(ClassNotFoundException.class, () -> classLoader.loadClass("NotExistClass"));
}

protected boolean isNestedArchive(Archive.Entry entry) {
return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName()
.startsWith("BOOT-INF/lib/");
}
}

@Test(expected = Exception.class)
public void testMain() throws Exception {
main(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
*/
package com.alipay.sofa.ark.bootstrap;

import com.alipay.sofa.ark.bootstrap.ClasspathLauncher.ClassPathArchive;
import com.alipay.sofa.ark.common.util.ClassLoaderUtils;
import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.loader.DirectoryBizArchive;
import com.alipay.sofa.ark.loader.EmbedClassPathArchive;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
import com.alipay.sofa.ark.spi.archive.Archive;
import com.alipay.sofa.ark.spi.archive.BizArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
Expand All @@ -39,29 +39,31 @@
import java.util.Arrays;
import java.util.List;

import static com.alipay.sofa.ark.common.util.ClassLoaderUtils.getAgentClassPath;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

/**
* @author qilong.zql
* @since 0.6.0
*/
public class ClasspathLauncherTest {

static MockedStatic<ManagementFactory> managementFactoryMockedStatic;

@BeforeClass
public static void setup(){
public static void setup() {

List<String> mockArguments = new ArrayList<>();
String filePath = ClasspathLauncherTest.class.getClassLoader()
.getResource("SampleClass.class").getPath();
.getResource("SampleClass.class").getPath();
String workingPath = FileUtils.file(filePath).getParent();
mockArguments.add(String.format("-javaagent:%s", workingPath));

RuntimeMXBean runtimeMXBean = Mockito.mock(RuntimeMXBean.class);
when(runtimeMXBean.getInputArguments()).thenReturn(mockArguments);

managementFactoryMockedStatic = Mockito.mockStatic(ManagementFactory.class);
managementFactoryMockedStatic.when(ManagementFactory::getRuntimeMXBean).thenReturn(runtimeMXBean);

}

@AfterClass
Expand All @@ -71,26 +73,28 @@ public static void tearDown() {

@Test
public void testFilterAgentClasspath() throws Exception {

URL url = this.getClass().getClassLoader().getResource("sample-biz.jar");
URL[] agentUrl = ClassLoaderUtils.getAgentClassPath();
Assert.assertEquals(1, agentUrl.length);
URL[] agentUrl = getAgentClassPath();
assertEquals(1, agentUrl.length);

List<URL> urls = new ArrayList<>();
urls.add(url);
urls.addAll(Arrays.asList(agentUrl));

ClasspathLauncher.ClassPathArchive classPathArchive = new ClasspathLauncher.ClassPathArchive(
ClassPathArchive classPathArchive = new ClassPathArchive(
this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));
List<BizArchive> bizArchives = classPathArchive.getBizArchives();
Assert.assertEquals(1, bizArchives.size());
Assert.assertEquals(2, urls.size());
assertEquals(1, bizArchives.size());
assertEquals(2, urls.size());
}

@Test
public void testSpringBootFatJar() throws Exception {

URL url = this.getClass().getClassLoader().getResource("sample-springboot-fat-biz.jar");
URL[] agentUrl = ClassLoaderUtils.getAgentClassPath();
Assert.assertEquals(1, agentUrl.length);
URL[] agentUrl = getAgentClassPath();
assertEquals(1, agentUrl.length);

List<URL> urls = new ArrayList<>();
JarFileArchive jarFileArchive = new JarFileArchive(FileUtils.file(url.getFile()));
Expand All @@ -100,21 +104,21 @@ public void testSpringBootFatJar() throws Exception {
}
urls.addAll(Arrays.asList(agentUrl));


EmbedClassPathArchive classPathArchive = new EmbedClassPathArchive(
this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));
this.getClass().getCanonicalName(), null, urls.toArray(new URL[]{}));
List<BizArchive> bizArchives = classPathArchive.getBizArchives();
Assert.assertEquals(0, bizArchives.size());
Assert.assertNotNull(classPathArchive.getContainerArchive());
Assert.assertEquals(1, classPathArchive.getPluginArchives().size());
Assert.assertEquals(archives.size() + 1, urls.size());
Assert.assertEquals(3, classPathArchive.getConfClasspath().size());
assertEquals(0, bizArchives.size());
assertNotNull(classPathArchive.getContainerArchive());
assertEquals(1, classPathArchive.getPluginArchives().size());
assertEquals(archives.size() + 1, urls.size());
assertEquals(3, classPathArchive.getConfClasspath().size());

URLClassLoader classLoader = new URLClassLoader(classPathArchive.getContainerArchive().getUrls());
try {
Class clazz = classLoader.loadClass("com.alipay.sofa.ark.bootstrap.ArkLauncher");
Assert.assertNotNull(clazz);
} catch (Exception e){
Assert.assertTrue("loadClass class failed ",false);
assertNotNull(clazz);
} catch (Exception e) {
assertTrue("loadClass class failed ", false);
}
}

Expand All @@ -126,16 +130,17 @@ protected boolean isNestedArchive(Archive.Entry entry) {
@Test
public void testConfClasspath() throws IOException {
ClassLoader classLoader = this.getClass().getClassLoader();
ClasspathLauncher.ClassPathArchive classPathArchive = new ClasspathLauncher.ClassPathArchive(
ClassPathArchive classPathArchive = new ClassPathArchive(
this.getClass().getCanonicalName(), null, ClassLoaderUtils.getURLs(classLoader));
List<URL> confClasspath = classPathArchive.getConfClasspath();
Assert.assertEquals(3, confClasspath.size());
assertEquals(3, confClasspath.size());
}

@Test
public void testFromSurefire() throws IOException {

ClassLoader classLoader = this.getClass().getClassLoader();
ClasspathLauncher.ClassPathArchive classPathArchive = new ClasspathLauncher.ClassPathArchive(
ClassPathArchive classPathArchive = new ClassPathArchive(
this.getClass().getCanonicalName(), null, ClassLoaderUtils.getURLs(classLoader));

URL url1 = Mockito.mock(URL.class);
Expand All @@ -145,14 +150,75 @@ public void testFromSurefire() throws IOException {
when(url1.getFile()).thenReturn("surefirebooter17233117990150815938.jar");
when(url2.getFile()).thenReturn("org.jacoco.agent-0.8.4-runtime.jar");
when(url3.getFile()).thenReturn("byte-buddy-agent-1.10.15.jar");

Assert.assertTrue(classPathArchive.fromSurefire(new URL[] { url1, url2, url3 }));
assertTrue(classPathArchive.fromSurefire(new URL[] { url1, url2, url3 }));

List<URL> urls2 = classPathArchive.getConfClasspath();
urls2.add(url2);
urls2.add(url3);
assertFalse(classPathArchive.fromSurefire(urls2.toArray(new URL[0])));
}

@Test
public void testOtherMethods() throws IOException {

URL url = this.getClass().getClassLoader().getResource("sample-biz.jar");
URL[] agentUrl = getAgentClassPath();
assertEquals(1, agentUrl.length);

Assert.assertFalse(classPathArchive.fromSurefire(urls2.toArray(new URL[0])));
List<URL> urls = new ArrayList<>();
urls.add(url);
urls.addAll(Arrays.asList(agentUrl));

ClassPathArchive classPathArchive = new ClassPathArchive(
this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));

try {
classPathArchive.getUrl();
} catch (Exception e) {
}
try {
classPathArchive.getManifest();
} catch (Exception e) {
}
try {
classPathArchive.getNestedArchives(null);
} catch (Exception e) {
}
try {
classPathArchive.getNestedArchive(null);
} catch (Exception e) {
}
try {
classPathArchive.getInputStream(null);
} catch (Exception e) {
}
try {
classPathArchive.iterator();
} catch (Exception e) {
}

assertTrue(classPathArchive.createDirectoryBizModuleArchive().getClass()
.equals(DirectoryBizArchive.class));
URL url2 = new URL("file://aa");
assertArrayEquals(new URL[] { url2 },
classPathArchive.filterBizUrls(new URL[] { agentUrl[0], url, url2 }));

URL surefireJarURL = this.getClass().getClassLoader()
.getResource("sample-biz-surefire.jar");
assertArrayEquals(new URL[] { url2, new URL("file://b") },
classPathArchive.parseClassPathFromSurefireBoot(surefireJarURL));
}

}
@Test
public void testBaseExecutableArchiveLauncher() {

BaseExecutableArchiveLauncher baseExecutableArchiveLauncher = new BaseExecutableArchiveLauncher() {
@Override
protected String getMainClass() throws Exception {
return null;
}
};

assertNotNull(baseExecutableArchiveLauncher.getExecutableArchive());
}
}

0 comments on commit bb17be2

Please sign in to comment.