package com.example; import com.aspose.cad.Image; import com.aspose.cad.fileformats.cad.CadImage; import com.aspose.cad.imageoptions.CadDrawTypeMode; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.io.File; public class LayoutExporterTest { private static final String TEST_FILES_PATH = "C:\\Users\\83500\\久翌\\CAD编辑同步excel\\测试文件区\\04_Test_Files\\"; private static final long MIN_PDF_SIZE_BYTES = 100 * 1024; // 100KB private String taqiDxfPath = TEST_FILES_PATH + "塔器.dxf"; private String rongqiDxfPath = TEST_FILES_PATH + "容器.dxf"; private String taqiDwgPath = TEST_FILES_PATH + "塔器.dwg"; private String taqiDxfLayoutName; private String rongqiDxfLayoutName; private String taqiDwgLayoutName; private String taqiDxfOutputPath; private String rongqiDxfOutputPath; private String taqiDwgOutputPath; @Before public void setup() throws Exception { System.out.println("--- 正在查找布局名称 ---"); taqiDxfLayoutName = findFirstValidLayoutName(taqiDxfPath); rongqiDxfLayoutName = findFirstValidLayoutName(rongqiDxfPath); taqiDwgLayoutName = findFirstValidLayoutName(taqiDwgPath); Assert.assertNotNull("未能找到 '塔器.dxf' 中的任何有效布局", taqiDxfLayoutName); Assert.assertNotNull("未能找到 '容器.dxf' 中的任何有效布局", rongqiDxfLayoutName); Assert.assertNotNull("未能找到 '塔器.dwg' 中的任何有效布局", taqiDwgLayoutName); System.out.println("'塔器.dxf' 的布局: " + taqiDxfLayoutName); System.out.println("'容器.dxf' 的布局: " + rongqiDxfLayoutName); System.out.println("'塔器.dwg' 的布局: " + taqiDwgLayoutName); System.out.println("--- 查找结束 ---"); } private String findFirstValidLayoutName(String filePath) throws Exception { try (CadImage cadImage = (CadImage) Image.load(filePath)) { // 直接从 keySet 获取第一个布局名称 return cadImage.getLayouts().keySet().stream().findFirst().orElse(null); } } @Test public void testExportTaqiDxfLayout() throws Exception { System.out.println("正在测试文件: " + taqiDxfPath + " 使用布局: " + taqiDxfLayoutName); taqiDxfOutputPath = LayoutExporter.exportLayout(taqiDxfPath, taqiDxfLayoutName, CadDrawTypeMode.UseObjectColor); File outputFile = new File(taqiDxfOutputPath); Assert.assertTrue("PDF file was not created for 塔器.dxf", outputFile.exists()); Assert.assertTrue("PDF file size for 塔器.dxf is less than 100KB. Size: " + outputFile.length() / 1024 + "KB", outputFile.length() > MIN_PDF_SIZE_BYTES); System.out.println("塔器.dxf 测试成功. PDF大小: " + outputFile.length() / 1024 + " KB"); } @Test public void testExportRongqiDxfLayout() throws Exception { System.out.println("正在测试文件: " + rongqiDxfPath + " 使用布局: " + rongqiDxfLayoutName); rongqiDxfOutputPath = LayoutExporter.exportLayout(rongqiDxfPath, rongqiDxfLayoutName, CadDrawTypeMode.UseObjectColor); File outputFile = new File(rongqiDxfOutputPath); Assert.assertTrue("PDF file was not created for 容器.dxf", outputFile.exists()); Assert.assertTrue("PDF file size for 容器.dxf is less than 100KB. Size: " + outputFile.length() / 1024 + "KB", outputFile.length() > MIN_PDF_SIZE_BYTES); System.out.println("容器.dxf 测试成功. PDF大小: " + outputFile.length() / 1024 + " KB"); } @Test public void testExportTaqiDwgLayout() throws Exception { System.out.println("正在测试文件: " + taqiDwgPath + " 使用布局: " + taqiDwgLayoutName); taqiDwgOutputPath = LayoutExporter.exportLayout(taqiDwgPath, taqiDwgLayoutName, CadDrawTypeMode.UseObjectColor); File outputFile = new File(taqiDwgOutputPath); Assert.assertTrue("PDF file was not created for 塔器.dwg", outputFile.exists()); Assert.assertTrue("PDF file size for 塔器.dwg is less than 100KB. Size: " + outputFile.length() / 1024 + "KB", outputFile.length() > MIN_PDF_SIZE_BYTES); System.out.println("塔器.dwg 测试成功. PDF大小: " + outputFile.length() / 1024 + " KB"); } @Test(expected = IllegalArgumentException.class) public void testNonExistentLayout() throws Exception { System.out.println("测试一个不存在的布局..."); LayoutExporter.exportLayout(taqiDxfPath, "non_existent_layout_name", CadDrawTypeMode.UseObjectColor); } @After public void cleanup() { deleteFile(taqiDxfOutputPath); deleteFile(rongqiDxfOutputPath); deleteFile(taqiDwgOutputPath); } private void deleteFile(String path) { if (path != null && !path.isEmpty()) { File file = new File(path); if (file.exists()) { System.out.println("清理文件: " + path); file.delete(); } } } }