add slash for path

This commit is contained in:
Looly
2021-04-23 10:59:44 +08:00
parent 588933a733
commit 63b93e02f9
7 changed files with 166 additions and 104 deletions

View File

@@ -1,5 +1,10 @@
package cn.hutool.system;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.StrUtil;
import java.io.PrintWriter;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
@@ -14,11 +19,6 @@ import java.lang.management.ThreadMXBean;
import java.util.List;
import java.util.Properties;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.StrUtil;
/**
* Java的System类封装工具类。<br>
* 参考http://blog.csdn.net/zhongweijian/article/details/7619383
@@ -168,7 +168,7 @@ public class SystemUtil {
* 取得系统属性如果因为Java安全的限制而失败则将错误打在Log中然后返回 {@code null}
*
* @param name 属性名
* @param quiet 安静模式,不将出错信息打在<code>System.err</code>
* @param quiet 安静模式,不将出错信息打在{@code System.err}
* @return 属性值或{@code null}
* @see System#getProperty(String)
* @see System#getenv(String)
@@ -312,9 +312,9 @@ public class SystemUtil {
/**
* 返回Java虚拟机编译系统相关属性<br>
* 如果没有编译系统,则返回<code>null</code>
* 如果没有编译系统,则返回{@code null}
*
* @return a {@link CompilationMXBean} ,如果没有编译系统,则返回<code>null</code>
* @return a {@link CompilationMXBean} ,如果没有编译系统,则返回{@code null}
* @since 4.1.4
*/
public static CompilationMXBean getCompilationMXBean() {
@@ -363,7 +363,7 @@ public class SystemUtil {
/**
* 取得Java Virtual Machine Specification的信息。
*
* @return <code>JvmSpecInfo</code>对象
* @return {@link JvmSpecInfo}对象
*/
public static JvmSpecInfo getJvmSpecInfo() {
return Singleton.get(JvmSpecInfo.class);
@@ -372,7 +372,7 @@ public class SystemUtil {
/**
* 取得Java Virtual Machine Implementation的信息。
*
* @return <code>JvmInfo</code>对象
* @return {@link JvmInfo}对象
*/
public static JvmInfo getJvmInfo() {
return Singleton.get(JvmInfo.class);
@@ -381,7 +381,7 @@ public class SystemUtil {
/**
* 取得Java Specification的信息。
*
* @return <code>JavaSpecInfo</code>对象
* @return {@link JavaSpecInfo}对象
*/
public static JavaSpecInfo getJavaSpecInfo() {
return Singleton.get(JavaSpecInfo.class);
@@ -390,7 +390,7 @@ public class SystemUtil {
/**
* 取得Java Implementation的信息。
*
* @return <code>JavaInfo</code>对象
* @return {@link JavaInfo}对象
*/
public static JavaInfo getJavaInfo() {
return Singleton.get(JavaInfo.class);
@@ -399,7 +399,7 @@ public class SystemUtil {
/**
* 取得当前运行的JRE的信息。
*
* @return <code>JreInfo</code>对象
* @return {@link JavaRuntimeInfo}对象
*/
public static JavaRuntimeInfo getJavaRuntimeInfo() {
return Singleton.get(JavaRuntimeInfo.class);
@@ -408,7 +408,7 @@ public class SystemUtil {
/**
* 取得OS的信息。
*
* @return <code>OsInfo</code>对象
* @return {@code OsInfo}对象
*/
public static OsInfo getOsInfo() {
return Singleton.get(OsInfo.class);
@@ -417,7 +417,7 @@ public class SystemUtil {
/**
* 取得User的信息。
*
* @return <code>UserInfo</code>对象
* @return {@code UserInfo}对象
*/
public static UserInfo getUserInfo() {
return Singleton.get(UserInfo.class);
@@ -426,7 +426,7 @@ public class SystemUtil {
/**
* 取得Host的信息。
*
* @return <code>HostInfo</code>对象
* @return {@link HostInfo}对象
*/
public static HostInfo getHostInfo() {
return Singleton.get(HostInfo.class);
@@ -435,7 +435,7 @@ public class SystemUtil {
/**
* 取得Runtime的信息。
*
* @return <code>RuntimeInfo</code>对象
* @return {@link RuntimeInfo}对象
*/
public static RuntimeInfo getRuntimeInfo() {
return Singleton.get(RuntimeInfo.class);
@@ -487,16 +487,16 @@ public class SystemUtil {
// ------------------------------------------------------------------ Dump
/**
* 将系统信息输出到<code>System.out</code>中。
* 将系统信息输出到{@link System#out}中。
*/
public static void dumpSystemInfo() {
dumpSystemInfo(new PrintWriter(System.out));
}
/**
* 将系统信息输出到指定<code>PrintWriter</code>中。
* 将系统信息输出到指定{@link PrintWriter}中。
*
* @param out <code>PrintWriter</code>输出流
* @param out {@link PrintWriter}输出流
*/
public static void dumpSystemInfo(PrintWriter out) {
out.println("--------------");
@@ -522,9 +522,9 @@ public class SystemUtil {
}
/**
* 输出到<code>StringBuilder</code>
* 输出到{@link StringBuilder}
*
* @param builder <code>StringBuilder</code>对象
* @param builder {@link StringBuilder}对象
* @param caption 标题
* @param value 值
*/

View File

@@ -1,5 +1,8 @@
package cn.hutool.system;
import cn.hutool.core.util.StrUtil;
import java.io.File;
import java.io.Serializable;
/**
@@ -8,22 +11,37 @@ import java.io.Serializable;
public class UserInfo implements Serializable{
private static final long serialVersionUID = 1L;
private final String USER_NAME = SystemUtil.get("user.name", false);
private final String USER_HOME = SystemUtil.get("user.home", false);
private final String USER_DIR = SystemUtil.get("user.dir", false);
private final String USER_LANGUAGE = SystemUtil.get("user.language", false);
private final String USER_COUNTRY = ((SystemUtil.get("user.country", false) == null) ? SystemUtil.get("user.region", false) : SystemUtil.get("user.country", false));
private final String JAVA_IO_TMPDIR = SystemUtil.get("java.io.tmpdir", false);
private final String USER_NAME;
private final String USER_HOME;
private final String USER_DIR;
private final String JAVA_IO_TMPDIR;
private final String USER_LANGUAGE;
private final String USER_COUNTRY;
public UserInfo(){
USER_NAME = fixPath(SystemUtil.get("user.name", false));
USER_HOME = fixPath(SystemUtil.get("user.home", false));
USER_DIR = fixPath(SystemUtil.get("user.dir", false));
JAVA_IO_TMPDIR = fixPath(SystemUtil.get("java.io.tmpdir", false));
USER_LANGUAGE = SystemUtil.get("user.language", false);
// JDK1.4 {@code user.country}JDK1.2 {@code user.region}
String userCountry = SystemUtil.get("user.country", false);
if(null == userCountry){
userCountry = SystemUtil.get("user.country", false);
}
USER_COUNTRY = userCountry;
}
/**
* 取得当前登录用户的名字(取自系统属性:<code>user.name</code>)。
*
* 取得当前登录用户的名字(取自系统属性:{@code user.name})。
*
* <p>
* 例如:<code>"admin"</code>
* 例如:{@code "admin"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
* @since Java 1.1
*/
public final String getName() {
@@ -31,14 +49,14 @@ public class UserInfo implements Serializable{
}
/**
* 取得当前登录用户的home目录取自系统属性<code>user.home</code>)。
*
* 取得当前登录用户的home目录取自系统属性{@code user.home})。
*
* <p>
* 例如:<code>"/home/admin"</code>
* 例如:{@code "/home/admin/"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
* @since Java 1.1
*/
public final String getHomeDir() {
@@ -46,14 +64,14 @@ public class UserInfo implements Serializable{
}
/**
* 取得当前目录(取自系统属性:<code>user.dir</code>)。
*
* 取得当前目录(取自系统属性:{@code user.dir})。
*
* <p>
* 例如:<code>"/home/admin/working"</code>
* 例如:{@code "/home/admin/working/"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
* @since Java 1.1
*/
public final String getCurrentDir() {
@@ -61,43 +79,43 @@ public class UserInfo implements Serializable{
}
/**
* 取得临时目录(取自系统属性:<code>java.io.tmpdir</code>)。
*
* 取得临时目录(取自系统属性:{@code java.io.tmpdir})。
*
* <p>
* 例如:<code>"/tmp"</code>
* 例如:{@code "/tmp/"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
*
*/
public final String getTempDir() {
return JAVA_IO_TMPDIR;
}
/**
* 取得当前登录用户的语言设置(取自系统属性:<code>user.language</code>)。
*
* 取得当前登录用户的语言设置(取自系统属性:{@code user.language})。
*
* <p>
* 例如:<code>"zh"</code>、<code>"en"</code>
* 例如:{@code "zh"}、{@code "en"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
*/
public final String getLanguage() {
return USER_LANGUAGE;
}
/**
* 取得当前登录用户的国家或区域设置取自系统属性JDK1.4 <code>user.country</code>或JDK1.2 <code>user.region</code>)。
*
* 取得当前登录用户的国家或区域设置取自系统属性JDK1.4 {@code user.country}或JDK1.2 {@code user.region})。
*
* <p>
* 例如:<code>"CN"</code>、<code>"US"</code>
* 例如:{@code "CN"}、{@code "US"}
* </p>
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回<code>null</code>
*
*
* @return 属性值如果不能取得因为Java安全限制或值不存在则返回{@code null}
*
*/
public final String getCountry() {
return USER_COUNTRY;
@@ -105,7 +123,7 @@ public class UserInfo implements Serializable{
/**
* 将当前用户的信息转换成字符串。
*
*
* @return 用户信息的字符串表示
*/
@Override
@@ -122,4 +140,17 @@ public class UserInfo implements Serializable{
return builder.toString();
}
/**
* 修正路径,包括:
*
* <ul>
* <li>1. 末尾补充 /</li>
* </ul>
* @param path 路径
* @return 修正后的路径
* @since 5.6.4
*/
private static String fixPath(String path){
return StrUtil.addSuffixIfNot(path, File.separator);
}
}

View File

@@ -4,26 +4,28 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
public class SystemUtilTest {
@Test
@Ignore
public void dumpTest() {
SystemUtil.dumpSystemInfo();
}
@Test
public void getCurrentPidTest() {
long pid = SystemUtil.getCurrentPID();
Assert.assertTrue(pid > 0);
}
@Test
public void getJavaInfoTest() {
JavaInfo javaInfo = SystemUtil.getJavaInfo();
Assert.assertNotNull(javaInfo);
}
@Test
public void getOsInfoTest() {
OsInfo osInfo = SystemUtil.getOsInfo();
@@ -35,5 +37,11 @@ public class SystemUtilTest {
HostInfo hostInfo = SystemUtil.getHostInfo();
Assert.assertNotNull(hostInfo);
}
@Test
public void getUserInfoTest(){
// https://gitee.com/dromara/hutool/issues/I3NM39
final UserInfo userInfo = SystemUtil.getUserInfo();
Assert.assertTrue(userInfo.getTempDir().endsWith(File.separator));
}
}