You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
38 lines
1.2 KiB
package common.ecommerce.shared.testkit; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
import java.io.InputStream; |
|
|
|
/** |
|
* 夹具加载:从 classpath 读一个 UTF-8 文本文件。 |
|
* |
|
* <pre> |
|
* String json = JsonFixture.load("fixtures/lingxing/orderList/success.json"); |
|
* </pre> |
|
* |
|
* <p>路径相对 classpath 根;找不到直接抛异常,不返回空串。</p> |
|
*/ |
|
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); |
|
} |
|
} |
|
}
|
|
|