This commit is contained in:
Looly
2023-12-29 20:21:40 +08:00
parent 03b8f57648
commit 91178e45a8
15 changed files with 442 additions and 198 deletions

View File

@@ -1,70 +0,0 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.io;
import org.dromara.hutool.core.io.watch.WatchMonitor;
import org.dromara.hutool.core.io.watch.WatchUtil;
import org.dromara.hutool.core.io.watch.Watcher;
import org.dromara.hutool.core.io.watch.watchers.DelayWatcher;
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
import org.dromara.hutool.core.lang.Console;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
/**
* 文件监听单元测试
*
* @author Looly
*
*/
public class WatchMonitorTest {
public static void main(final String[] args) {
final Watcher watcher = new SimpleWatcher(){
@Override
public void onCreate(final WatchEvent<?> event, final WatchKey key) {
final Object obj = event.context();
Console.log(((Path)obj).toAbsolutePath());
Console.log("创建:{}-> {}", key.watchable(), obj);
}
@Override
public void onModify(final WatchEvent<?> event, final WatchKey key) {
final Object obj = event.context();
Console.log("修改:{}-> {}", key.watchable(), obj);
}
@Override
public void onDelete(final WatchEvent<?> event, final WatchKey key) {
final Object obj = event.context();
Console.log("删除:{}-> {}", key.watchable(), obj);
}
@Override
public void onOverflow(final WatchEvent<?> event, final WatchKey key) {
final Object obj = event.context();
Console.log("Overflow{}-> {}", key.watchable(), obj);
}
};
//noinspection resource
final WatchMonitor monitor = WatchUtil.ofAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500));
monitor.setMaxDepth(0);
monitor.start();
}
}

View File

@@ -13,15 +13,35 @@
package org.dromara.hutool.core.io.file;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.lang.Console;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class PathUtilTest {
@SuppressWarnings("DuplicateExpressions")
@Test
void ofTest() {
// 绝对路径测试
Path path = PathUtil.of(Paths.get("d:/test/hutool"), Paths.get("data1"), Paths.get("data2"));
Assertions.assertEquals("d:/test/hutool/data1/data2", path.toString().replace('\\', '/'));
// 相对路径测试
path = PathUtil.of(Paths.get("hutool"), Paths.get("data1"), Paths.get("data2"));
Assertions.assertEquals("hutool/data1/data2", path.toString().replace('\\', '/'));
path = PathUtil.of(Paths.get("hutool"));
Assertions.assertEquals("hutool", path.toString().replace('\\', '/'));
path = PathUtil.of((Path) null);
Assertions.assertNull(path);
}
@Test
@Disabled
public void copyFileTest(){

View File

@@ -0,0 +1,35 @@
package org.dromara.hutool.core.io.watch;
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
import org.dromara.hutool.core.lang.Console;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
public class TestConsoleWatcher extends SimpleWatcher {
private static final long serialVersionUID = 1L;
@Override
public void onCreate(final WatchEvent<?> event, final WatchKey key) {
Console.log("创建:{}-> {}", key.watchable(), event.context());
Console.log("Resolved Path{}", WatchUtil.resolvePath(event, key));
}
@Override
public void onModify(final WatchEvent<?> event, final WatchKey key) {
Console.log("修改:{}-> {}", key.watchable(), event.context());
Console.log("Resolved Path{}", WatchUtil.resolvePath(event, key));
}
@Override
public void onDelete(final WatchEvent<?> event, final WatchKey key) {
Console.log("删除:{}-> {}", key.watchable(), event.context());
Console.log("Resolved Path{}", WatchUtil.resolvePath(event, key));
}
@Override
public void onOverflow(final WatchEvent<?> event, final WatchKey key) {
Console.log("Overflow{}-> {}", key.watchable(), event.context());
Console.log("Resolved Path{}", WatchUtil.resolvePath(event, key));
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.io.watch;
import org.dromara.hutool.core.io.file.PathUtil;
import org.dromara.hutool.core.io.watch.watchers.DelayWatcher;
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
import org.dromara.hutool.core.lang.Console;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
/**
* 文件监听单元测试
*
* @author Looly
*/
public class WatchMonitorTest {
@Test
@Disabled
void watchTest() {
final Path path = PathUtil.of("d:/test/");
Console.log("监听:{}", path);
//noinspection resource
final WatchMonitor monitor = WatchUtil.ofAll(path, new DelayWatcher(new TestConsoleWatcher(), 500));
monitor.setMaxDepth(0);
monitor.start();
}
}

View File

@@ -0,0 +1,22 @@
package org.dromara.hutool.core.io.watch;
import org.dromara.hutool.core.io.file.PathUtil;
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
import org.dromara.hutool.core.lang.Console;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
public class WatchServiceWrapperTest {
@SuppressWarnings("resource")
@Test
@Disabled
void watchTest() {
WatchServiceWrapper.of(WatchKind.ALL)
.registerPath(PathUtil.of("d:/test"), 0)
.watch(new TestConsoleWatcher(), null);
}
}