package org.dromara.test;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

/**
 * @author lcj
 * @date 2025/4/23 10:15
 */
@SpringBootTest
public class CmdTest {

    @Test
    public void test() {
        // 获取当前项目根目录
        String projectRoot = System.getProperty("user.dir");
        // EXE 和参数
        String exePath = projectRoot + File.separator + "file" + File.separator + "resource" + File.separator + "dxf" + File.separator + "main.exe";
        String inputDXF = "file/resource/dxf/1897160897167638529/桩点图.dxf";
        String outputJSON = "file/resource/dxf/1897160897167638529/output.json";
        String sourceEPSG = "4524";
        String targetEPSG = "4326";

        // 构造命令行
        List<String> command = Arrays.asList(
            exePath,
            inputDXF,
            outputJSON,
            sourceEPSG,
            targetEPSG
        );

        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true); // 合并标准错误和输出

        try {
            Process process = builder.start();

            // 读取输出
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream(), "GBK")
            );
            String line;
            System.out.println("程序输出：");
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int exitCode = process.waitFor();
            System.out.println("程序退出码：" + exitCode);

            reader.close();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
