import bean.Model; import bean.Param; import com.mycim.framework.utils.lang.StringUtils; import com.mycim.framework.utils.lang.collections.CollectionUtils; import com.mycim.framework.utils.lang.collections.MapUtils; import com.mycim.framework.utils.msg.JsonUtils; import utils.HandleFileUtils; import utils.ThirdApiCaller; import javax.swing.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.List; import java.util.Map; import java.util.*; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; /** * @Author: yibing.liu * @Date: 2023/10/28 21:49 */ public class WindowStart extends JFrame { private JTextField fileTextFile; private JTextField urlTextFile; private JTextArea inTextArea; private JTextArea outTextArea; private JComboBox modelBox; private JComboBox coreNumBox; private JComboBox typeBox; private List paramList = new ArrayList<>(); public static final String SPACE = "\n\r"; public static void main(String[] args) { new WindowStart(); } public WindowStart(){ JFrame frame=new JFrame("MES2.0+接口测试小程序"); frame.setBounds(300, 200, 1100, 800);//设置大小和位置 JButton fileInput = new JButton("读取文件夹"); fileInput.addActionListener(e -> { String fileRootPath = fileTextFile.getText(); inTextArea.setText("开始读取文件夹内容:" + SPACE + "解析文件夹地址: " + fileRootPath + " " + SPACE); //先获取文件夹下所有的文件名称 List fileFullNames = HandleFileUtils .readDirectoryFileFullName(fileRootPath, "txt", "xlsx"); String str = StringUtils.join(fileFullNames, SPACE); inTextArea.setText(inTextArea.getText() + "解析文件名:" + SPACE + str); outTextArea.setText(""); //循环 根据名称读取对应的文件 if (CollectionUtils.isNotEmpty(fileFullNames)) { StringBuilder sb = new StringBuilder("开始解析文本请求内容:").append(SPACE); Model typeModel = (Model)typeBox.getSelectedItem(); List paramListTemp = new ArrayList<>(); if (typeModel.getModelCode() == 0){// Restfull paramListTemp= buildTxtData4RestFull(fileFullNames, sb); } else if (typeModel.getModelCode() == 1) {//WebService paramListTemp = buildTxtData4WebService(fileFullNames, sb); } if (CollectionUtils.isNotEmpty(paramListTemp)){ paramList = paramListTemp; inTextArea.setText(inTextArea.getText() + SPACE + sb.toString()); } } }); fileTextFile = new JTextField("C:\\Users\\GetWind\\Desktop\\TestCaseFileRootPath", 50); urlTextFile = new JTextField("http://localhost:9080/gateway/Service/JobManagementWebService?wsdl", 50); inTextArea = new JTextArea(10, 30); // 设置行数和列数 inTextArea.setLineWrap(true); // 设置自动换行 inTextArea.setWrapStyleWord(true); // 设置按单词换行 JScrollPane inScrollPane = new JScrollPane(inTextArea); // 添加滚动条 outTextArea = new JTextArea(10, 30); // 设置行数和列数 outTextArea.setLineWrap(true); // 设置自动换行 outTextArea.setWrapStyleWord(true); // 设置按单词换行 JScrollPane outScrollPane = new JScrollPane(outTextArea); // 添加滚动条 JButton startButton = new JButton("Start"); startButton.addActionListener(e -> { String url = urlTextFile.getText(); Model model = (Model)modelBox.getSelectedItem(); Model coreNum = (Model)coreNumBox.getSelectedItem(); outTextArea.setText("开始执行: 运行模式[ " + model.getModel() + " ],线程数[ " + coreNum.getModel() + " ]" + SPACE); Model typeModel = (Model)typeBox.getSelectedItem(); callTest(paramList, url, model.getModelCode(), coreNum, typeModel.getModelCode()); }); modelBox = new JComboBox<>(); modelBox.addItem(new Model("并行", 1)); modelBox.addItem(new Model("串行", 0)); modelBox.addActionListener(e -> { Model model = (Model)modelBox.getSelectedItem(); if (model.getModelCode() == 0){ frame.remove(coreNumBox); } else { frame.add(coreNumBox); } }); typeBox = new JComboBox<>(); typeBox.addItem(new Model("WebService", 1)); typeBox.addItem(new Model("RestFull", 0)); coreNumBox = new JComboBox<>(); coreNumBox.addItem(new Model("10", 10)); coreNumBox.addItem(new Model("9", 9)); coreNumBox.addItem(new Model("8", 8)); coreNumBox.addItem(new Model("7", 7)); coreNumBox.addItem(new Model("6", 6)); coreNumBox.addItem(new Model("5", 5)); coreNumBox.addItem(new Model("4", 4)); coreNumBox.addItem(new Model("3", 3)); coreNumBox.addItem(new Model("2", 2)); coreNumBox.setSelectedIndex(5); frame.setLayout(null);//没有布置可以用这个设计 fileTextFile.setBounds(50,50,450,30); fileInput.setBounds(860,225,100,30);//设置组件大小和位置 urlTextFile.setBounds(50,100,450,30);//设置组件大小和位置 typeBox.setBounds(860, 100, 100, 30); inScrollPane.setBounds(50, 150, 800, 200); outScrollPane.setBounds(50, 350, 800, 300); startButton.setBounds(860, 475, 100, 30); modelBox.setBounds(970, 475, 100, 30); coreNumBox.setBounds(970, 510, 100, 30); frame.add(fileTextFile); frame.add(fileInput); frame.add(urlTextFile); frame.add(outScrollPane); frame.add(inScrollPane); frame.add(startButton); frame.add(modelBox); frame.add(coreNumBox); frame.add(typeBox); frame.setVisible(true);//显示 frame.setResizable(false);//固定大小 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent we) { System.exit(0); } }); } private void callTest(List paramList, String url, int model, Model coreNum, int type) { rebuildList(paramList, url); StringBuffer str = new StringBuffer(); if (model == 1){ int coreCount = 5; if (coreNum != null){ coreCount = coreNum.getModelCode(); } ForkJoinPool pool = new ForkJoinPool(coreCount); ForkJoinTask task = pool.submit(() -> { paramList.parallelStream().forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl(), type))); }); task.join(); pool.shutdown(); } else { paramList.forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl(), type))); } outTextArea.setText(outTextArea.getText() + str.toString()); } private void rebuildList(List paramList, String url) { paramList.forEach(p -> p.setUrl(url)); } private static String call(Map header, Map bodyObject, String url, int type){ StringBuilder result = new StringBuilder(); result.append("time=[").append(System.currentTimeMillis()).append("] ").append(SPACE); try { Map argMap = new HashMap<>(); argMap.put("head", header); argMap.put("body", bodyObject); result.append("Request:").append(bodyObject).append(SPACE).append("Response:"); if (type == 0){// Restfull result.append(ThirdApiCaller.postJson(url, argMap, header, String.class)); } else if (type == 1) {//WebService result.append(ThirdApiCaller.postForWS(url, argMap)); } } catch (Exception e) { result.append("Response Error:[").append(e.getMessage()).append("]"); } return result.append(SPACE).append(SPACE).toString(); } private List buildTxtData4RestFull(List fileFullNames, StringBuilder sb) { List failFullNames = new ArrayList<>(); List dtoList = new ArrayList<>(); for (String fileFullName : fileFullNames) { try { String txtDataStr = HandleFileUtils.readTxtFile(fileFullName); Map txtMap = JsonUtils.toMap(txtDataStr); sb.append(txtMap.toString()).append(SPACE); Map head = MapUtils.getMap(txtMap, "head"); Map body = MapUtils.getMap(txtMap, "body"); dtoList.add(new Param(head, body, "")); } catch (Exception e) { sb.append("文件解析失败:").append(e.getMessage()).append(SPACE); failFullNames.add(fileFullName); } sb.append(SPACE); } if (CollectionUtils.isNotEmpty(failFullNames)) { failFullNames.forEach(f -> sb.append("解析失败的文件名:").append(f).append(SPACE)); } return dtoList; } private List buildTxtData4WebService(List fileFullNames, StringBuilder sb) { List failFullNames = new ArrayList<>(); List dtoList = new ArrayList<>(); for (String fileFullName : fileFullNames) { try { String txtDataStr = HandleFileUtils.readTxtFile(fileFullName); Map txtMap = JsonUtils.toMap(txtDataStr); sb.append(txtMap.toString()).append(SPACE); Map head = MapUtils.getMap(txtMap, "head"); Map body = MapUtils.getMap(txtMap, "body"); dtoList.add(new Param(head, body, "")); } catch (Exception e) { sb.append("文件解析失败:").append(e.getMessage()).append(SPACE); failFullNames.add(fileFullName); } sb.append(SPACE); } if (CollectionUtils.isNotEmpty(failFullNames)) { failFullNames.forEach(f -> sb.append("解析失败的文件名:").append(f).append(SPACE)); } return dtoList; } }