diff --git a/src/main/java/run/halo/app/infra/utils/PathUtils.java b/src/main/java/run/halo/app/infra/utils/PathUtils.java index 5ccc728aef..4ac3fb3d72 100644 --- a/src/main/java/run/halo/app/infra/utils/PathUtils.java +++ b/src/main/java/run/halo/app/infra/utils/PathUtils.java @@ -55,6 +55,7 @@ public static boolean isAbsoluteUri(final String uriString) { * * @param pathSegments Path segments to be combined * @return the combined path + * @apiNote This method doesn't work for Windows system currently. */ public static String combinePath(String... pathSegments) { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/run/halo/app/plugin/YamlPluginFinder.java b/src/main/java/run/halo/app/plugin/YamlPluginFinder.java index f4cf7ef064..1e462149be 100644 --- a/src/main/java/run/halo/app/plugin/YamlPluginFinder.java +++ b/src/main/java/run/halo/app/plugin/YamlPluginFinder.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.pf4j.DevelopmentPluginClasspath; @@ -14,7 +13,6 @@ import org.springframework.core.io.Resource; import run.halo.app.core.extension.Plugin; import run.halo.app.extension.Unstructured; -import run.halo.app.infra.utils.PathUtils; import run.halo.app.infra.utils.YamlUnstructuredLoader; /** @@ -102,9 +100,7 @@ protected Plugin unstructuredToPlugin(Resource propertyResource) { protected Path getManifestPath(Path pluginPath, String propertiesFileName) { if (Files.isDirectory(pluginPath)) { for (String location : PLUGIN_CLASSPATH.getClassesDirectories()) { - String s = PathUtils.combinePath(pluginPath.toString(), - location, propertiesFileName); - Path path = Paths.get(s); + var path = pluginPath.resolve(location).resolve(propertiesFileName); Resource propertyResource = new FileSystemResource(path); if (propertyResource.exists()) { return path; diff --git a/src/test/java/run/halo/app/core/extension/SettingTest.java b/src/test/java/run/halo/app/core/extension/SettingTest.java index 86910098bf..1d3e8300d0 100644 --- a/src/test/java/run/halo/app/core/extension/SettingTest.java +++ b/src/test/java/run/halo/app/core/extension/SettingTest.java @@ -1,8 +1,8 @@ package run.halo.app.core.extension; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; -import java.util.List; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; @@ -25,7 +25,7 @@ void setting() throws JSONException { apiVersion: v1alpha1 kind: Setting metadata: - name: setting-name + name: setting-name spec: forms: - group: basic @@ -47,8 +47,9 @@ void setting() throws JSONException { name: color validation: required """; - List unstructureds = - new YamlUnstructuredLoader(new InMemoryResource(settingYaml)).load(); + var unstructureds = new YamlUnstructuredLoader( + new InMemoryResource(settingYaml.getBytes(UTF_8), "In-memory setting YAML")) + .load(); assertThat(unstructureds).hasSize(1); Unstructured unstructured = unstructureds.get(0); diff --git a/src/test/java/run/halo/app/plugin/PluginStartedListenerTest.java b/src/test/java/run/halo/app/plugin/PluginStartedListenerTest.java index c22ded3dbe..328da7049e 100644 --- a/src/test/java/run/halo/app/plugin/PluginStartedListenerTest.java +++ b/src/test/java/run/halo/app/plugin/PluginStartedListenerTest.java @@ -34,7 +34,8 @@ void lookupFromClasses() throws IOException { Set extensionResources = PluginStartedListener.PluginExtensionLoaderUtils.lookupFromClasses(tempPluginPath); - assertThat(extensionResources).containsAll(Set.of("extensions/roles.yaml")); + assertThat(extensionResources) + .containsAll(Set.of(Path.of("extensions/roles.yaml").toString())); } @Test diff --git a/src/test/java/run/halo/app/plugin/YamlPluginFinderTest.java b/src/test/java/run/halo/app/plugin/YamlPluginFinderTest.java index 7edd148235..fab7c31dc4 100644 --- a/src/test/java/run/halo/app/plugin/YamlPluginFinderTest.java +++ b/src/test/java/run/halo/app/plugin/YamlPluginFinderTest.java @@ -105,10 +105,9 @@ void unstructuredToPluginTest() throws JSONException { @Test void findFailedWhenFileNotFound() { - Path test = Paths.get("/tmp"); - assertThatThrownBy(() -> { - pluginFinder.find(test); - }).isInstanceOf(PluginRuntimeException.class) + var test = Paths.get(""); + assertThatThrownBy(() -> pluginFinder.find(test)) + .isInstanceOf(PluginRuntimeException.class) .hasMessage("Unable to find plugin descriptor file: plugin.yaml"); } diff --git a/src/test/java/run/halo/app/theme/ThemeContextTest.java b/src/test/java/run/halo/app/theme/ThemeContextTest.java index dc6f87a7e8..1a0733d339 100644 --- a/src/test/java/run/halo/app/theme/ThemeContextTest.java +++ b/src/test/java/run/halo/app/theme/ThemeContextTest.java @@ -1,6 +1,6 @@ package run.halo.app.theme; -import java.nio.file.Paths; +import java.nio.file.Path; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; @@ -16,20 +16,20 @@ class ThemeContextTest { @Test void constructorBuilderTest() throws JSONException { - ThemeContext testTheme = ThemeContext.builder() + var path = Path.of("/tmp/themes/testTheme"); + var testTheme = ThemeContext.builder() .name("testTheme") - .path(Paths.get("/tmp/themes/testTheme")) + .path(path) .active(true) .build(); - String s = JsonUtils.objectToJson(testTheme); - JSONAssert.assertEquals(""" + var got = JsonUtils.objectToJson(testTheme); + var expect = String.format(""" { "name": "testTheme", - "path": "file:///tmp/themes/testTheme", + "path": "%s", "active": true } - """, - s, - false); + """, path.toUri()); + JSONAssert.assertEquals(expect, got, false); } } \ No newline at end of file diff --git a/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java b/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java index 08eae220ce..924f1edcff 100644 --- a/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java +++ b/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java @@ -3,8 +3,9 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.FileNotFoundException; +import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.Paths; +import java.nio.file.Path; import java.util.Locale; import java.util.Map; import org.junit.jupiter.api.BeforeEach; @@ -25,7 +26,7 @@ void setUp() throws FileNotFoundException { } @Test - void resolveMessagesForTemplateForDefault() { + void resolveMessagesForTemplateForDefault() throws URISyntaxException { Map properties = ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.CHINESE, getTheme()); assertThat(properties).hasSize(1); @@ -33,7 +34,7 @@ void resolveMessagesForTemplateForDefault() { } @Test - void resolveMessagesForTemplateForEnglish() { + void resolveMessagesForTemplateForEnglish() throws URISyntaxException { Map properties = ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.ENGLISH, getTheme()); assertThat(properties).hasSize(1); @@ -48,10 +49,10 @@ void messageFormat() { assertThat(s).isEqualTo("Welcome Halo to the index"); } - ThemeContext getTheme() { + ThemeContext getTheme() throws URISyntaxException { return ThemeContext.builder() .name("default") - .path(Paths.get(defaultThemeUrl.getPath())) + .path(Path.of(defaultThemeUrl.toURI())) .active(true) .build(); } diff --git a/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java b/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java index ce4d3572eb..48a445fb47 100644 --- a/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java +++ b/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java @@ -1,8 +1,9 @@ package run.halo.app.theme.message; import java.io.FileNotFoundException; +import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.Paths; +import java.nio.file.Path; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -45,7 +46,7 @@ public class ThemeMessageResolverIntegrationTest { private WebTestClient webTestClient; @BeforeEach - void setUp() throws FileNotFoundException { + void setUp() throws FileNotFoundException, URISyntaxException { defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default"); otherThemeUrl = ResourceUtils.getURL("classpath:themes/other"); @@ -126,7 +127,7 @@ void shouldUseDefaultWhenLanguageNotSupport() { } @Test - void switchTheme() { + void switchTheme() throws URISyntaxException { webTestClient.get() .uri("/index?language=zh") .exchange() @@ -185,18 +186,18 @@ void switchTheme() { """); } - ThemeContext createDefaultContext() { + ThemeContext createDefaultContext() throws URISyntaxException { return ThemeContext.builder() .name("default") - .path(Paths.get(defaultThemeUrl.getPath())) + .path(Path.of(defaultThemeUrl.toURI())) .active(true) .build(); } - ThemeContext createOtherContext() { + ThemeContext createOtherContext() throws URISyntaxException { return ThemeContext.builder() .name("other") - .path(Paths.get(otherThemeUrl.getPath())) + .path(Path.of(otherThemeUrl.toURI())) .active(false) .build(); }