package com.battery.business.runtime; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.InputStream; import java.nio.charset.StandardCharsets; import javax.xml.parsers.DocumentBuilderFactory; import org.junit.jupiter.api.Test; /** 测试数据列表必须通过测试级持久化查询,不得回读模拟器历史。 */ class TestPersistedDataQueryContractTest { @Test void exposesTestScopedPersistedDataQueries() { String mapper = resource("mapper/battery/TTestMapper.xml"); assertTrue(mapper.contains("id=\"selectPersistedFinalRealtimeRecords\"")); assertTrue(mapper.contains("id=\"selectPersistedFinalStepRecords\"")); assertTrue(mapper.contains("id=\"selectPersistedFinalCycleRecords\"")); assertTrue(mapper.contains("where r.test_id = #{testId}")); } @Test void stepEndVoltageUsesPersistedDeviceStatistic() { String mapper = resource("mapper/battery/TTestMapper.xml"); assertTrue(mapper.contains("r.voltage_v endVoltage")); assertTrue(mapper.contains("from t_test_step_record r")); } @Test void mapperXmlIsWellFormed() { try (InputStream stream = TestPersistedDataQueryContractTest.class.getClassLoader() .getResourceAsStream("mapper/battery/TTestMapper.xml")) { if (stream == null) throw new AssertionError("缺少资源 mapper/battery/TTestMapper.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); factory.newDocumentBuilder().parse(stream); } catch (Exception exception) { throw new AssertionError("TTestMapper.xml 必须是可解析的 XML", exception); } } private static String resource(String path) { try (InputStream stream = TestPersistedDataQueryContractTest.class.getClassLoader().getResourceAsStream(path)) { if (stream == null) throw new AssertionError("缺少资源 " + path); return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } catch (Exception exception) { throw new AssertionError("无法读取资源 " + path, exception); } } }