Skip to content

Commit

Permalink
Add ut 1221 (#817)
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

* update tests

* update test

* Delete sofa-ark-parent/core/common/C:\temp dir\b\c directory

* update test

* update test

---------

Co-authored-by: LiuYu <ly109106@alibaba-inc.com>
  • Loading branch information
lylingzhen and LiuYu committed Jan 2, 2024
1 parent 7ec6184 commit dfef361
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ sofa-ark-parent/support/ark-plugin-maven-plugin/com/alipay/sofa/ark/plugin/mark
sofa-ark-parent/support/ark-plugin-maven-plugin/null.ark.plugin
sofa-ark-parent/support/ark-plugin-maven-plugin/null.ark.plugin.bak
sofa-ark-parent/support/ark-plugin-maven-plugin/xxx.ark.plugin
*/temp dir/*
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

import org.junit.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.PriorityBlockingQueue;

import static com.alipay.sofa.ark.common.thread.ThreadPoolManager.*;
import static com.alipay.sofa.ark.common.util.ThreadPoolUtils.buildQueue;
import static java.lang.Integer.MAX_VALUE;
import static org.apache.commons.beanutils.BeanUtils.copyProperties;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;

public class CommonThreadPoolTest {

Expand All @@ -40,4 +45,24 @@ public void testCommonThreadPool() throws Exception {
unRegisterUserThread("a");
assertNull(getThreadPool("a"));
}

@Test
public void testBuildQueue() {

BlockingQueue<Runnable> queue = buildQueue(5, true);
assertEquals(PriorityBlockingQueue.class, queue.getClass());
assertEquals(MAX_VALUE, queue.remainingCapacity());

queue = buildQueue(-1, true);
assertEquals(PriorityBlockingQueue.class, queue.getClass());
assertEquals(0, queue.size());

queue = buildQueue(5, false);
assertEquals(LinkedBlockingDeque.class, queue.getClass());
assertEquals(5, queue.remainingCapacity());

queue = buildQueue(-1, false);
assertEquals(LinkedBlockingDeque.class, queue.getClass());
assertEquals(0, queue.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,66 @@
*/
package com.alipay.sofa.ark.common.util;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;

import static com.alipay.sofa.ark.common.util.AssertUtils.*;
import static com.alipay.sofa.ark.common.util.FileUtils.file;
import static java.lang.System.getProperty;
import static org.junit.Assert.assertTrue;

/**
*
* @author ruoshan
* @since 0.1.0
*/
public class AssertUtilsTest {

public static File getTmpDir() {
String tmpPath = getProperty("java.io.tmpdir");
return file(tmpPath);
}

@Test(expected = IllegalArgumentException.class)
public void testAssertNotNullNull() {
String msg = "object is null";
try {
AssertUtils.assertNotNull(null, msg);
assertNotNull(null, msg);
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
Assert.assertTrue(e.getMessage().contains(msg));
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains(msg));
throw e;
}
}

@Test
public void testAssertNotNullNotNull() {
AssertUtils.assertNotNull(new Object(), "object is null");
assertNotNull(new Object(), "object is null");
}

@Test
public void testAssertIsTrue() {
AssertUtils.isTrue(true, "Exception %s", "error");
isTrue(true, "Exception %s", "error");
try {
AssertUtils.isTrue(false, "Exception %s", "error");
isTrue(false, "Exception %s", "error");
} catch (IllegalArgumentException ex) {
Assert.assertTrue("Exception error".equals(ex.getMessage()));
assertTrue("Exception error".equals(ex.getMessage()));
}
}

@Test
public void testAssertIsFalse() {
AssertUtils.isFalse(false, "Exception %s", "error");
isFalse(false, "Exception %s", "error");
try {
AssertUtils.isFalse(true, "Exception %s", "error");
isFalse(true, "Exception %s", "error");
} catch (IllegalArgumentException ex) {
Assert.assertTrue("Exception error".equals(ex.getMessage()));
assertTrue("Exception error".equals(ex.getMessage()));
}
}

}
@Test(expected = IllegalArgumentException.class)
public void assertNull() {
AssertUtils.assertNull(new Object(), "should be nul!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.MalformedURLException;
Expand All @@ -32,6 +31,9 @@
import java.util.Collections;
import java.util.List;

import static com.alipay.sofa.ark.common.util.EnvironmentUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -151,4 +153,13 @@ public void testParseSkyWalkingAgentPath() {

managementFactoryMockedStatic.close();
}
}

@Test
public void testEnvironmentUtils() {
assertNull(getProperty("not_exists_prop"));
setSystemProperty("not_exists_prop", "aaa");
assertEquals("aaa", getProperty("not_exists_prop"));
clearSystemProperty("not_exists_prop");
assertNull(getProperty("not_exists_prop"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
*/
package com.alipay.sofa.ark.common.util;

import com.alipay.sofa.ark.spi.constant.Constants;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.alipay.sofa.ark.common.util.ClassUtils.*;
import static com.alipay.sofa.ark.spi.constant.Constants.DEFAULT_PACKAGE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author qilong.zql
* @since 0.3.0
Expand All @@ -32,36 +35,36 @@ public class ClassUtilsTest {

@Test
public void testGetPackageName() {
Assert.assertEquals("a.b", ClassUtils.getPackageName("a.b.C"));
Assert.assertEquals(Constants.DEFAULT_PACKAGE, ClassUtils.getPackageName("C"));
assertEquals("a.b", getPackageName("a.b.C"));
assertEquals(DEFAULT_PACKAGE, getPackageName("C"));
}

@Test
public void testFindCommonPackage() {
Assert.assertEquals(ClassUtils.findCommonPackage(null).size(), 0);
assertEquals(findCommonPackage(null).size(), 0);
List<String> classNames = new ArrayList<>();
classNames.add("com.example.project.subpackage1.classE");
classNames.add("com.example.project.classA");
classNames.add("com.example.project.classB");
classNames.add("com.example.project.subpackage.classC");
classNames.add("com.example.project.subpackage.classD");
Assert.assertEquals(ClassUtils.findCommonPackage(classNames).size(), 3);
assertEquals(findCommonPackage(classNames).size(), 3);
classNames.add("org.apache.util.ClassF");
Assert.assertEquals(ClassUtils.findCommonPackage(classNames).size(), 4);
assertEquals(findCommonPackage(classNames).size(), 4);
}

@Test
public void testCollectClasses() throws Exception {

File dir = new File("target/classes");
// fix mvn test fail issues
File dir2 = new File(dir.getAbsolutePath());
if (!dir2.exists()) {
return;
}

List<String> classNames = ClassUtils.collectClasses(dir2);
Assert.assertTrue(classNames.contains("com.alipay.sofa.ark.common.util.ClassUtils"));
Assert.assertTrue(ClassUtils.findCommonPackage(classNames).contains(
"com.alipay.sofa.ark.common.util"));
List<String> classNames = collectClasses(dir2);
assertTrue(classNames.contains("com.alipay.sofa.ark.common.util.ClassUtils"));
assertTrue(findCommonPackage(classNames).contains("com.alipay.sofa.ark.common.util"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,85 +17,91 @@
package com.alipay.sofa.ark.common.util;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static com.alipay.sofa.ark.common.util.FileUtils.*;
import static java.lang.System.getProperty;
import static java.lang.System.setProperty;
import static org.apache.commons.io.FileUtils.deleteQuietly;
import static org.apache.commons.io.FileUtils.touch;
import static org.junit.Assert.*;

/**
* @author guolei.sgl (guolei.sgl@antfin.com) 2019/7/28 11:24 PM
* @since
**/
public class FileUtilsTest {

private static final String ORIGIN = System.getProperty("os.name");
private static final String ORIGIN = getProperty("os.name");

@Before
public void before() {
System.setProperty("os.name", "windows");
setProperty("os.name", "windows");
}

@After
public void after() {
System.setProperty("os.name", ORIGIN);
setProperty("os.name", ORIGIN);
}

@Test
public void testGetCompatiblePath() {
String winPath = FileUtils.getCompatiblePath("C:\\a\\b\\c");
Assert.assertTrue(winPath.contains("/"));
String macPath = FileUtils.getCompatiblePath("/a/b/c");
Assert.assertTrue(winPath.contains(macPath));
String winPath = getCompatiblePath("C:\\a\\b\\c");
assertTrue(winPath.contains("/"));
String macPath = getCompatiblePath("/a/b/c");
assertTrue(winPath.contains(macPath));
}

@Test
public void testSHA1Hash() throws IOException {
URL url = this.getClass().getResource(this.getClass().getSimpleName() + ".class");
Assert.assertNotNull(FileUtils.sha1Hash(FileUtils.file(url.getFile())));
assertNotNull(sha1Hash(file(url.getFile())));
}

@Test
public void testUnzip() throws IOException {
URL sampleBiz = this.getClass().getClassLoader().getResource("sample-biz.jar");
File file = FileUtils.file(sampleBiz.getFile());
Assert.assertNotNull(FileUtils.unzip(file, file.getAbsolutePath() + "-unpack"));
File file = file(sampleBiz.getFile());
assertNotNull(unzip(file, file.getAbsolutePath() + "-unpack"));
}

@Test
public void testMkdir() {
Assert.assertNull(FileUtils.mkdir(""));
assertNull(mkdir(""));
// test recursive creation
File newDir = FileUtils.mkdir("C:\\a\\b\\c");
Assert.assertNotNull(newDir);
File newDir = mkdir("C:\\a\\b\\c");
assertNotNull(newDir);
// test for exist path
Assert.assertNotNull(FileUtils.mkdir("C:\\a\\b\\c"));
assertNotNull(mkdir("C:\\a\\b\\c"));
// del the dir
org.apache.commons.io.FileUtils.deleteQuietly(newDir);
deleteQuietly(newDir);
}

@Test
public void testDecodePath() {
String path = "C:\\temp dir\\b\\c";
String encodedPath = "C:\\temp%20dir\\b\\c";
Assert.assertEquals(path, FileUtils.decodePath(path));
Assert.assertEquals(path, FileUtils.decodePath(encodedPath));
assertEquals(path, decodePath(path));
assertEquals(path, decodePath(encodedPath));
}

@Test
public void testNewFile() throws IOException {

String dir = "C:\\temp dir\\b\\c";
String encodedPath = "C:\\temp%20dir\\b\\c";
FileUtils.mkdir(dir);
org.apache.commons.io.FileUtils.touch(new File(dir, "test.txt"));
File file = FileUtils.file(encodedPath, "test.txt");
Assert.assertNotNull(file);
Assert.assertTrue(file.exists());
mkdir(dir);
touch(new File(dir, "test.txt"));
File file = file(encodedPath, "test.txt");
assertNotNull(file);
assertTrue(file.exists());

file = new File(encodedPath, "test.txt");
Assert.assertFalse(file.exists());
assertFalse(file.exists());
}

}

0 comments on commit dfef361

Please sign in to comment.