将 isNameValid 方法移动至 FileUtils (#4491)

This commit is contained in:
Glavo
2025-09-16 16:11:38 +08:00
committed by GitHub
parent 5005343d00
commit 5a8d567bd7
8 changed files with 154 additions and 147 deletions

View File

@@ -0,0 +1,70 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.io;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import static org.junit.jupiter.api.Assertions.*;
/// @author Glavo
public class FileUtilsTest {
@ParameterizedTest
@EnumSource(OperatingSystem.class)
public void testIsNameValid(OperatingSystem os) {
assertTrue(FileUtils.isNameValid(os, "example"));
assertTrue(FileUtils.isNameValid(os, "example.zip"));
assertTrue(FileUtils.isNameValid(os, "example.tar.gz"));
assertTrue(FileUtils.isNameValid(os, "\uD83D\uDE00"));
assertTrue(FileUtils.isNameValid(os, "a\uD83D\uDE00b"));
assertFalse(FileUtils.isNameValid(os, ""));
assertFalse(FileUtils.isNameValid(os, "."));
assertFalse(FileUtils.isNameValid(os, ".."));
assertFalse(FileUtils.isNameValid(os, "exam\0ple"));
assertFalse(FileUtils.isNameValid(os, "example/0"));
// Test for invalid surrogate pair
assertFalse(FileUtils.isNameValid(os, "\uD83D"));
assertFalse(FileUtils.isNameValid(os, "\uDE00"));
assertFalse(FileUtils.isNameValid(os, "\uDE00\uD83D"));
assertFalse(FileUtils.isNameValid(os, "\uD83D\uD83D"));
assertFalse(FileUtils.isNameValid(os, "a\uD83D"));
assertFalse(FileUtils.isNameValid(os, "a\uDE00"));
assertFalse(FileUtils.isNameValid(os, "a\uDE00\uD83D"));
assertFalse(FileUtils.isNameValid(os, "a\uD83Db"));
assertFalse(FileUtils.isNameValid(os, "a\uDE00b"));
assertFalse(FileUtils.isNameValid(os, "a\uDE00\uD83Db"));
// Platform-specific tests
boolean isWindows = os == OperatingSystem.WINDOWS;
boolean isNotWindows = !isWindows;
assertEquals(isNotWindows, FileUtils.isNameValid(os, "com1"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "com1.txt"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "foo."));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "foo "));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f<oo"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f>oo"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f:oo"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f?oo"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f*oo"));
assertEquals(isNotWindows, FileUtils.isNameValid(os, "f\\oo"));
}
}

View File

@@ -1,55 +0,0 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.platform;
import org.junit.jupiter.api.Test;
import static org.jackhuang.hmcl.util.platform.OperatingSystem.isNameValid;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Glavo
*/
public final class OperatingSystemTest {
@Test
public void testIsNameValid() {
assertTrue(isNameValid("example"));
assertTrue(isNameValid("example.zip"));
assertTrue(isNameValid("example.tar.gz"));
assertTrue(isNameValid("\uD83D\uDE00"));
assertTrue(isNameValid("a\uD83D\uDE00b"));
assertFalse(isNameValid("."));
assertFalse(isNameValid(".."));
assertFalse(isNameValid("exam\0ple"));
assertFalse(isNameValid("example/0"));
// Test for invalid surrogate pair
assertFalse(isNameValid("\uD83D"));
assertFalse(isNameValid("\uDE00"));
assertFalse(isNameValid("\uDE00\uD83D"));
assertFalse(isNameValid("\uD83D\uD83D"));
assertFalse(isNameValid("a\uD83D"));
assertFalse(isNameValid("a\uDE00"));
assertFalse(isNameValid("a\uDE00\uD83D"));
assertFalse(isNameValid("a\uD83Db"));
assertFalse(isNameValid("a\uDE00b"));
assertFalse(isNameValid("a\uDE00\uD83Db"));
}
}