package common.ecommerce.shared.testkit; import java.io.ByteArrayOutputStream; import java.io.InputStream; /** * 夹具加载:从 classpath 读一个 UTF-8 文本文件。 * *
* String json = JsonFixture.load("fixtures/lingxing/orderList/success.json");
*
*
* 路径相对 classpath 根;找不到直接抛异常,不返回空串。
*/ public final class JsonFixture { private JsonFixture() { } public static String load(String path) { String normalized = path.startsWith("/") ? path.substring(1) : path; InputStream in = JsonFixture.class.getClassLoader().getResourceAsStream(normalized); if (in == null) { throw new IllegalStateException("fixture not found: " + normalized); } try (InputStream stream = in) { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int n; while ((n = stream.read(buf)) >= 0) { out.write(buf, 0, n); } return new String(out.toByteArray(), "UTF-8"); } catch (Exception e) { throw new IllegalStateException("read fixture failed: " + normalized, e); } } }