提交 9cb0a0cd 编写于 作者: S shroman 提交者: yukon

[ROCKETMQ-5] Avoid creating directories in...

[ROCKETMQ-5] Avoid creating directories in UtilAll#getDiskPartitionSpaceUsedPercent(), closes apache/incubator-rocketmq#23
上级 c4f12497
...@@ -183,17 +183,16 @@ public class UtilAll { ...@@ -183,17 +183,16 @@ public class UtilAll {
try { try {
File file = new File(path); File file = new File(path);
if (!file.exists()) {
boolean result = file.mkdirs(); if (!file.exists())
if (!result) { return -1;
//TO DO
}
}
long totalSpace = file.getTotalSpace(); long totalSpace = file.getTotalSpace();
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
if (totalSpace > 0) { if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace; return usedSpace / (double) totalSpace;
} }
} catch (Exception e) { } catch (Exception e) {
......
...@@ -21,6 +21,8 @@ import java.net.URL; ...@@ -21,6 +21,8 @@ import java.net.URL;
import java.util.Properties; import java.util.Properties;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class UtilAllTest { public class UtilAllTest {
...@@ -78,6 +80,17 @@ public class UtilAllTest { ...@@ -78,6 +80,17 @@ public class UtilAllTest {
assertTrue(pid > 0); assertTrue(pid > 0);
} }
@Test
public void test_getDiskPartitionSpaceUsedPercent() {
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(null), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(""), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath"), 0);
String tmpDir = System.getProperty("java.io.tmpdir");
assertNotEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir), 0);
}
@Test @Test
public void test_isBlank() { public void test_isBlank() {
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册