UUID

HTTP 请求

加密/解密

Common 常用模块

Reflection 反射

Input/Output 输入/输出

Resources

Resources 类提供静态方法来定位和检索类路径中或相对于特定类的资源。 这对于访问与应用程序捆绑在一起的配置文件、模板或其他数据文件特别有用。

方法

1. getResourcesFromClasspath(String resource)

检索位于类路径中的资源的路径。

示例:

String resourcePath = Resources.getResourcesFromClasspath("com/example/config.properties");
System.out.println(resourcePath);

2. getResourcesFromClasspath(String resource, boolean isDecode)

检索位于类路径中的资源的路径,并可选择进行 URL 解码。

示例:

String resourcePath = Resources.getResourcesFromClasspath("com/example/config.properties", true);
System.out.println(resourcePath);

3. getResourcesFromClass(Class<?> clz, String resource)

检索位于相对于给定类的资源的路径。

示例:

String resourcePath = Resources.getResourcesFromClass(MyClass.class, "my_resource.txt");
System.out.println(resourcePath);

4. getResourcesFromClass(Class<?> clz, String resource, boolean isDecode)

检索位于相对于给定类的资源的路径,并可选择进行 URL 解码。

示例:

String resourcePath = Resources.getResourcesFromClass(MyClass.class, "my_resource.txt", true);
System.out.println(resourcePath);

5. getClassName(File file, String packageName)

从 Java 类文件中提取类名。

示例:

File classFile = new File("path/to/MyClass.class");
String className = Resources.getClassName(classFile, "com.example");
System.out.println(className); // Output: com.example.MyClass

单元测试示例

package com.ajaxjs.util.io;

import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

public class TestResources {
    @Test
    public void testGetResourcesFromClasspath_FoundResource_ShouldReturnPath() {
        assertThrows(RuntimeException.class, () -> Resources.getResourcesFromClasspath("\\com\\test.txt"));
    }

    @Test
    public void testGetResourcesFromClasspath_NotFoundResource_ShouldReturnNull() {
        assertThrows(RuntimeException.class, () -> Resources.getResourcesFromClasspath("application.yml"));
    }

    @Test
    public void testGetResourcesFromClass_FoundResource_ShouldReturnPath() {
        String resourcePath = Resources.getResourcesFromClass(TestResources.class, "test.txt");
        System.out.println(resourcePath);
        assertNull(resourcePath);
    }

    @Test
    public void testGetResourcesFromClass_NotFoundResource_ShouldReturnNull() {
        String resourcePath = Resources.getResourcesFromClass(TestResources.class, "non-existent-resource.txt");
        assertNull(resourcePath, "Expected null for non-existent resource");
    }

    @Test
    public void testGetResourceText_FoundResource_ShouldReturnContent() {
        String resourceContent = Resources.getResourceText("README.md");
        //assertNotNull(resourceContent, "Resource content not found");
    }

    @Test
    public void testGetResourceText_NotFoundResource_ShouldReturnNull() {
        String resourceContent = Resources.getResourceText("non-existent-file.md");
        //assertNull(resourceContent, "Expected null for non-existent resource");
    }

    @Test
    public void testGetProperties_ValidPropertiesFile_ShouldLoadProperties() {
        Properties properties = Resources.getProperties("test-demo.properties");
        System.out.println(properties);
        //assertNotNull(properties);
    }

    @Test
    public void testGetProperties_NotFoundPropertiesFile_ShouldThrowException() {
        assertThrows(RuntimeException.class, () -> Resources.getProperties("application.properties"));
    }
}

注意: 单元测试使用 assertThrowsassertNull 来检查预期的异常和 null 返回值。 由于提供的代码中没有相关方法,因此某些测试被注释掉了。

结论

Resources 类提供了一种简单有效的方法来访问应用程序中的资源。 通过使用这些方法,您可以轻松地从类路径或相对于特定类检索配置文件、模板和其他数据文件。