This commit is contained in:
Looly 2023-06-16 23:54:06 +08:00
parent 0760e44c12
commit 13aff5c734
3 changed files with 33 additions and 11 deletions

View File

@ -23,6 +23,7 @@ import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.io.stream.BOMInputStream;
import org.dromara.hutool.core.io.unit.DataSizeUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.url.URLUtil;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.regex.ReUtil;
@ -2558,6 +2559,7 @@ public class FileUtil extends PathUtil {
*/
public static File getWebRoot() {
final String classPath = ClassUtil.getClassPath();
Console.log(classPath);
if (StrUtil.isNotBlank(classPath)) {
return getParent(file(classPath), 2);
}

View File

@ -4,6 +4,8 @@ import org.dromara.hutool.core.io.resource.ClassPathResource;
import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import java.io.IOException;
import java.util.Properties;
@ -22,14 +24,26 @@ public class ClassPathResourceTest {
Assertions.assertTrue(StrUtil.isNotEmpty(content));
}
@Test
@Test()
@EnabledForJreRange(max = JRE.JAVA_8)
public void readStringTest2() {
// JDK9+中因为模块的加入根路径读取可能为空
// 读取classpath根目录测试
final ClassPathResource resource = new ClassPathResource("/");
final String content = resource.readUtf8Str();
Assertions.assertTrue(StrUtil.isNotEmpty(content));
}
@Test()
@EnabledForJreRange(min = JRE.JAVA_9)
public void readStringTestForJdk9() {
// JDK9+中因为模块的加入根路径读取可能为空
// 读取classpath根目录测试
final ClassPathResource resource = new ClassPathResource("/");
final String content = resource.readUtf8Str();
Assertions.assertTrue(StrUtil.isEmpty(content));
}
@Test
public void readTest() throws IOException {
final ClassPathResource resource = new ClassPathResource("test.properties");

View File

@ -19,6 +19,8 @@ import org.dromara.hutool.core.util.SystemUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import java.io.File;
import java.nio.file.Path;
@ -250,7 +252,9 @@ public class FileUtilTest {
}
@Test
@EnabledForJreRange(max = JRE.JAVA_8)
public void listFileNamesTest() {
// JDK9+由于模块化问题获取的classoath路径非项目下而是junit下的
List<String> names = FileUtil.listFileNames("classpath:");
Assertions.assertTrue(names.contains("hutool.jpg"));
@ -400,7 +404,9 @@ public class FileUtilTest {
}
@Test
@EnabledForJreRange(max = JRE.JAVA_8)
public void getWebRootTest() {
// JDK9+环境中由于模块问题junit获取的classpath路径和实际不同
final File webRoot = FileUtil.getWebRoot();
Assertions.assertNotNull(webRoot);
Assertions.assertEquals("hutool-core", webRoot.getName());