From 5594f54f14e2a79df498f3e0fef73e64dcc69e09 Mon Sep 17 00:00:00 2001 From: yagol2020 Date: Wed, 17 Nov 2021 21:01:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=93=E5=8C=85=E4=B8=BAjar=E5=8C=85?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 15 ++ src/main/java/config/MyConfig.java | 57 +++++++ .../config/property/ChromeProperties.java | 13 ++ src/main/java/gui/designer/HelpGui.java | 81 +++++++++ src/main/java/gui/designer/MainGui.form | 36 +++- src/main/java/gui/designer/MainGui.java | 154 ++++++++++++++++-- src/main/java/gui/designer/PaperInfoGui.java | 52 ++++++ src/main/java/param/NormalParam.java | 1 + src/main/java/util/ChromeUtil.java | 5 +- src/main/java/util/JarUtil.java | 10 ++ src/main/resources/config/base.yaml | 2 + src/test/java/BastTest.java | 48 +----- src/test/java/gui/GuiTest.form | 9 +- src/test/java/gui/GuiTest.java | 32 +++- src/test/java/other/ChangeGlobalTester.java | 12 ++ src/test/java/other/GlobalParamClass.java | 14 ++ src/test/java/other/JarExecutePathTester.java | 13 ++ src/test/resources/config/base.yaml | 2 + 18 files changed, 493 insertions(+), 63 deletions(-) create mode 100644 src/main/java/config/MyConfig.java create mode 100644 src/main/java/config/property/ChromeProperties.java create mode 100644 src/main/java/util/JarUtil.java create mode 100644 src/main/resources/config/base.yaml create mode 100644 src/test/java/other/ChangeGlobalTester.java create mode 100644 src/test/java/other/GlobalParamClass.java create mode 100644 src/test/java/other/JarExecutePathTester.java create mode 100644 src/test/resources/config/base.yaml diff --git a/pom.xml b/pom.xml index 40b8e32..bee1ac9 100644 --- a/pom.xml +++ b/pom.xml @@ -175,6 +175,21 @@ **/* + + + config/base.yaml + + + + + + src/test/resources + + + + config/base.yaml + + diff --git a/src/main/java/config/MyConfig.java b/src/main/java/config/MyConfig.java new file mode 100644 index 0000000..b202699 --- /dev/null +++ b/src/main/java/config/MyConfig.java @@ -0,0 +1,57 @@ +package config; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.setting.yaml.YamlUtil; +import config.property.ChromeProperties; +import lombok.Data; +import param.NormalParam; + +/** + * @author yagol + * @TIME 2021/11/17 - 7:16 下午 + * @Description 配置类获取类 + **/ +@Data +public class MyConfig { + ChromeProperties chrome; + + /** + * 初始化config,并获得jar外部的config + * + * @param configSavePath + * @return + */ + public static MyConfig initConfig(String configSavePath) { + MyConfig config = YamlUtil.loadByPath("config/" + NormalParam.CONFIG_FILE_NAME, MyConfig.class); + return createOrUpdateConfig(config, configSavePath); + } + + /** + * 在不修改的情况下,获得config + * + * @param configPath + * @return + */ + public static MyConfig getConfig(String configPath) { + return YamlUtil.loadByPath(configPath + NormalParam.CONFIG_FILE_NAME, MyConfig.class); + } + + /** + * 将传进来的config保存到制定文件中 + * + * @param config + * @param configPath + * @return + */ + public static MyConfig createOrUpdateConfig(MyConfig config, String configPath) { + YamlUtil.dump(config, + FileUtil.getWriter + (configPath + NormalParam.CONFIG_FILE_NAME, + CharsetUtil.CHARSET_UTF_8, + false + ) + ); + return getConfig(configPath); + } +} diff --git a/src/main/java/config/property/ChromeProperties.java b/src/main/java/config/property/ChromeProperties.java new file mode 100644 index 0000000..91833ef --- /dev/null +++ b/src/main/java/config/property/ChromeProperties.java @@ -0,0 +1,13 @@ +package config.property; + +import lombok.Data; + +/** + * @author yagol + * @TIME 2021/11/17 - 7:21 下午 + * @Description + **/ +@Data +public class ChromeProperties { + String driverPath; +} diff --git a/src/main/java/gui/designer/HelpGui.java b/src/main/java/gui/designer/HelpGui.java index 78e2d58..d0892b7 100644 --- a/src/main/java/gui/designer/HelpGui.java +++ b/src/main/java/gui/designer/HelpGui.java @@ -74,4 +74,85 @@ public void show() { frame.setVisible(true); } + { +// GUI initializer generated by IntelliJ IDEA GUI Designer +// >>> IMPORTANT!! <<< +// DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + mainPanel = new JPanel(); + mainPanel.setLayout(new GridBagLayout()); + mainPanel.setPreferredSize(new Dimension(500, 300)); + helpPanel = new JPanel(); + helpPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + GridBagConstraints gbc; + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 1; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(helpPanel, gbc); + helpTextArea = new JTextArea(); + helpPanel.add(helpTextArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false)); + final JPanel panel1 = new JPanel(); + panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 2; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(panel1, gbc); + final JLabel label1 = new JLabel(); + label1.setText("关于"); + panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + aboutPanel = new JPanel(); + aboutPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 3; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(aboutPanel, gbc); + aboutTextArea = new JTextArea(); + aboutPanel.add(aboutTextArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false)); + final JPanel panel2 = new JPanel(); + panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(panel2, gbc); + final JLabel label2 = new JLabel(); + label2.setText("帮助"); + panel2.add(label2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + closeButton = new JButton(); + closeButton.setText("关闭"); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 4; + gbc.weightx = 1.0; + gbc.fill = GridBagConstraints.HORIZONTAL; + mainPanel.add(closeButton, gbc); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return mainPanel; + } } diff --git a/src/main/java/gui/designer/MainGui.form b/src/main/java/gui/designer/MainGui.form index 353188f..62e12ea 100644 --- a/src/main/java/gui/designer/MainGui.form +++ b/src/main/java/gui/designer/MainGui.form @@ -79,7 +79,7 @@ - + @@ -95,6 +95,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/gui/designer/MainGui.java b/src/main/java/gui/designer/MainGui.java index eb9e76f..cbb5e77 100644 --- a/src/main/java/gui/designer/MainGui.java +++ b/src/main/java/gui/designer/MainGui.java @@ -4,8 +4,10 @@ import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; +import config.MyConfig; import core.impl.IeeeResultProcessor; import core.impl.LoveScienceDetector; import gui.param.GuiParam; @@ -13,6 +15,7 @@ import log.MySwingTextAreaLog; import result.BaseResult; import result.IeeeResult; +import util.JarUtil; import javax.swing.*; import java.awt.*; @@ -28,6 +31,8 @@ * @Description 工具界面启动类 **/ public class MainGui { + private final MyConfig config = MyConfig.initConfig(JarUtil.PWC_JAR_PATH); + private final MySwingTextAreaLog log = MyLogFactory.get(); private JTextField searchQueryInput; private JTextArea logArea; @@ -38,7 +43,15 @@ public class MainGui { public MainGui() { log.setLogTextArea(logArea); + initComponent(); initButtonFunctions(); + log.info(JarUtil.PWC_JAR_PATH); + } + + private void initComponent() { + if (StrUtil.isNotEmpty(config.getChrome().getDriverPath())) { + driverFilePath.setText(config.getChrome().getDriverPath()); + } } @@ -50,18 +63,22 @@ private void initButtonFunctions() { searchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - log.info("执行爬虫任务,请稍后。请勿再次点击\"开始\"按钮"); - ThreadUtil.execute(new Runnable() { - @Override - public void run() { - BaseResult result = loadPaperInfoData2Table(); - if (saveResult2Csv.isSelected() && ObjectUtil.isNotNull(result)) { - assert result != null; - log.info("存储到csv中,地址为{}", result.getCsvResultPath()); - saveResult2Csv(result); + if (StrUtil.isNotEmpty(config.getChrome().getDriverPath())) { + log.info("执行爬虫任务,请稍后。请勿再次点击\"开始\"按钮"); + ThreadUtil.execute(new Runnable() { + @Override + public void run() { + BaseResult result = loadPaperInfoData2Table(); + if (saveResult2Csv.isSelected() && ObjectUtil.isNotNull(result)) { + assert result != null; + log.info("存储到csv中,地址为{}", result.getCsvResultPath()); + saveResult2Csv(result); + } } - } - }); + }); + } else { + log.info("驱动未选定,请选定驱动地址!"); + } } private BaseResult loadPaperInfoData2Table() { @@ -99,6 +116,20 @@ public void actionPerformed(ActionEvent e) { helpGui.show(); } }); + driverChoseButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser driverChooser = new JFileChooser(); + driverChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + if (driverChooser.showOpenDialog(content) == JFileChooser.APPROVE_OPTION) { + driverFilePath.setText(driverChooser.getSelectedFile().getAbsolutePath()); + config.getChrome().setDriverPath(driverFilePath.getText()); + log.info("设置驱动地址为:{}", driverFilePath.getText()); + MyConfig.createOrUpdateConfig(config, JarUtil.PWC_JAR_PATH); + } + } + }); + } public static void main(String[] args) { @@ -117,5 +148,106 @@ public static void main(String[] args) { private JLabel authorLabel; private JCheckBox saveResult2Csv; private JScrollPane logScrollPane; + private JTextField driverFilePath; + private JButton driverChoseButton; + private JLabel driverChoseLabel; + + { +// GUI initializer generated by IntelliJ IDEA GUI Designer +// >>> IMPORTANT!! <<< +// DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + main = new JPanel(); + main.setLayout(new GridBagLayout()); + main.setPreferredSize(new Dimension(900, 500)); + titleLabel = new JLabel(); + titleLabel.setText("Paper Web Crawler 文献网站爬虫工具"); + GridBagConstraints gbc; + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 1.0; + main.add(titleLabel, gbc); + authorLabel = new JLabel(); + authorLabel.setText("作者:@yagol, @vencerk"); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 1; + gbc.weightx = 1.0; + main.add(authorLabel, gbc); + content = new JPanel(); + content.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1)); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 2; + gbc.weightx = 1.0; + gbc.fill = GridBagConstraints.BOTH; + main.add(content, gbc); + searchQueryInput = new JTextField(); + content.add(searchQueryInput, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false)); + final JPanel panel1 = new JPanel(); + panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); + content.add(panel1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + final JPanel panel2 = new JPanel(); + panel2.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1)); + panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + searchButton = new JButton(); + searchButton.setText("开始"); + panel2.add(searchButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + saveResult2Csv = new JCheckBox(); + saveResult2Csv.setSelected(true); + saveResult2Csv.setText("是否保存到csv"); + panel2.add(saveResult2Csv, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + final JPanel panel3 = new JPanel(); + panel3.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1)); + panel1.add(panel3, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + helpButton = new JButton(); + helpButton.setText("关于帮助"); + helpButton.setMnemonic('帮'); + helpButton.setDisplayedMnemonicIndex(2); + panel3.add(helpButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + final JPanel panel4 = new JPanel(); + panel4.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1)); + panel3.add(panel4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + driverFilePath = new JTextField(); + panel4.add(driverFilePath, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false)); + driverChoseButton = new JButton(); + driverChoseButton.setText("选择驱动"); + panel4.add(driverChoseButton, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + driverChoseLabel = new JLabel(); + driverChoseLabel.setText("浏览器驱动(目前仅支持chrome)"); + panel4.add(driverChoseLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + searchQueryLabel = new JLabel(); + searchQueryLabel.setText("关键字"); + content.add(searchQueryLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + logScrollPane = new JScrollPane(); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 3; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + main.add(logScrollPane, gbc); + logArea = new JTextArea(); + logArea.setEditable(false); + logArea.setLineWrap(true); + logScrollPane.setViewportView(logArea); + } + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return main; + } } diff --git a/src/main/java/gui/designer/PaperInfoGui.java b/src/main/java/gui/designer/PaperInfoGui.java index 47b1998..fa871e4 100644 --- a/src/main/java/gui/designer/PaperInfoGui.java +++ b/src/main/java/gui/designer/PaperInfoGui.java @@ -80,4 +80,56 @@ private void createUIComponents() { paperInfoTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } + { +// GUI initializer generated by IntelliJ IDEA GUI Designer +// >>> IMPORTANT!! <<< +// DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + createUIComponents(); + paperInfo = new JPanel(); + paperInfo.setLayout(new GridBagLayout()); + paperInfo.setPreferredSize(new Dimension(900, 800)); + paperInfo.setVisible(true); + upperPanel = new JPanel(); + upperPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); + GridBagConstraints gbc; + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 1.0; + gbc.fill = GridBagConstraints.BOTH; + paperInfo.add(upperPanel, gbc); + searchLabel = new JLabel(); + searchLabel.setText("查询的关键字是:"); + upperPanel.add(searchLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + searchInputLabel = new JLabel(); + searchInputLabel.setText("Label"); + upperPanel.add(searchInputLabel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + jScrollPane = new JScrollPane(); + gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 2; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + gbc.fill = GridBagConstraints.BOTH; + paperInfo.add(jScrollPane, gbc); + jScrollPane.setViewportView(paperInfoTable); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return paperInfo; + } } diff --git a/src/main/java/param/NormalParam.java b/src/main/java/param/NormalParam.java index 00244cc..45a7d14 100644 --- a/src/main/java/param/NormalParam.java +++ b/src/main/java/param/NormalParam.java @@ -17,6 +17,7 @@ public class NormalParam { public final static String TRANSACTIONS = "Transaction"; public final static Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+"); public final static String HELP_FILE_PATH = "/gui_text_file/help.txt"; + public final static String CONFIG_FILE_NAME = "base.yaml"; /** * 小、中、大括号内容匹配,注意需要循环遍历 */ diff --git a/src/main/java/util/ChromeUtil.java b/src/main/java/util/ChromeUtil.java index 0584580..0f332ed 100644 --- a/src/main/java/util/ChromeUtil.java +++ b/src/main/java/util/ChromeUtil.java @@ -1,5 +1,6 @@ package util; +import config.MyConfig; import org.openqa.selenium.PageLoadStrategy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; @@ -11,12 +12,12 @@ * @Description **/ public class ChromeUtil { + MyConfig config = MyConfig.getConfig(JarUtil.PWC_JAR_PATH); static final String CHROME_DRIVER_PROPERTY = "webdriver.chrome.driver"; - static final String CHROME_DRIVER_PATH = "chromedriver"; static ChromeOptions CHROME_OPTIONS; public ChromeUtil() { - System.setProperty(CHROME_DRIVER_PROPERTY, CHROME_DRIVER_PATH); + System.setProperty(CHROME_DRIVER_PROPERTY, config.getChrome().getDriverPath()); CHROME_OPTIONS = new ChromeOptions(); CHROME_OPTIONS.setPageLoadStrategy(PageLoadStrategy.EAGER); //隐藏浏览器不显示 diff --git a/src/main/java/util/JarUtil.java b/src/main/java/util/JarUtil.java new file mode 100644 index 0000000..9b09eb8 --- /dev/null +++ b/src/main/java/util/JarUtil.java @@ -0,0 +1,10 @@ +package util; + +/** + * @author yagol + * @TIME 2021/11/17 - 8:36 下午 + * @Description 因为要打包成jar,因此出现了这个不该出现的类 + **/ +public class JarUtil { + public static String PWC_JAR_PATH = JarUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath(); +} diff --git a/src/main/resources/config/base.yaml b/src/main/resources/config/base.yaml new file mode 100644 index 0000000..acf639d --- /dev/null +++ b/src/main/resources/config/base.yaml @@ -0,0 +1,2 @@ +chrome: + driverPath: null # null就是java中的空,null字符串是'null' diff --git a/src/test/java/BastTest.java b/src/test/java/BastTest.java index 30de4ad..b434dfa 100644 --- a/src/test/java/BastTest.java +++ b/src/test/java/BastTest.java @@ -1,17 +1,8 @@ -import core.impl.LoveScienceDetector; +import cn.hutool.setting.yaml.YamlUtil; +import config.MyConfig; import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; -import param.IeeeParam; import param.NormalParam; -import result.BaseResult; -import result.IeeeResult; -import util.ChromeUtil; -import java.time.Duration; -import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,22 +12,6 @@ * @Description **/ public class BastTest { - public void test() throws Exception { - WebDriver webDriver = new ChromeUtil().initChrome(); - webDriver.get("https://ieeexplore.ieee.org/search/searchresult.jsp?newsearch=true&queryText=smart%20contract%20bug&returnFacets=ALL&returnType=SEARCH&matchPubs=true&rowsPerPage=75&pageNumber=1"); - WebDriverWait webDriverWait = new WebDriverWait(webDriver, Duration.ofSeconds(30)); - webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(IeeeParam.UNTIL_CONDITION_XPATH))); - webDriver.quit(); - } - - @Test - public void testLoveScience() { - IeeeResult ieeeResult = new IeeeResult(); - ieeeResult.addPaperInfo("test", Collections.singletonList("yagol"), "IEEE Transactions on Software Engineering", "2021", "Journal", "https://ieeexplore.ieee.org/document/9247056"); - LoveScienceDetector detector = new LoveScienceDetector(); - BaseResult result = detector.detector(ieeeResult); - System.out.println(result); - } @Test public void testRegexBracket() { @@ -63,21 +38,14 @@ public void testRegexNumber() { System.out.println(r.matcher(input).matches()); } - public void testIeeePaperIdXpath() { - WebDriver webDriver = new ChromeUtil().initChrome(); - webDriver.get("https://ieeexplore.ieee.org/search/searchresult.jsp?queryText=smart%20contract%20bug&returnFacets=ALL&returnType=SEARCH&matchPubs=true&rowsPerPage=10&pageNumber=1"); - WebDriverWait webDriverWait = new WebDriverWait(webDriver, Duration.ofSeconds(30)); - webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(IeeeParam.UNTIL_CONDITION_XPATH))); - webDriver.findElements(By.xpath("//xpl-search-results//xpl-results-list//div[@id>0]")).forEach(webElement -> { - System.out.println(webElement.getText()); - System.out.println(webElement.getAttribute("id")); - System.out.println("=========="); - }); - webDriver.quit(); - } - @Test public void testResourcesPath() { System.out.println(NormalParam.BASE_RESOURCES_PATH); } + + @Test + public void testConfig() { + MyConfig properties = YamlUtil.loadByPath("config/base.yaml", MyConfig.class); + System.out.println(properties.getChrome().getDriverPath()); + } } diff --git a/src/test/java/gui/GuiTest.form b/src/test/java/gui/GuiTest.form index 1c174f1..a1991aa 100644 --- a/src/test/java/gui/GuiTest.form +++ b/src/test/java/gui/GuiTest.form @@ -1,6 +1,6 @@
- + @@ -8,11 +8,6 @@ - - - - - @@ -23,7 +18,7 @@ - + diff --git a/src/test/java/gui/GuiTest.java b/src/test/java/gui/GuiTest.java index 3e28cf1..cc92d8d 100644 --- a/src/test/java/gui/GuiTest.java +++ b/src/test/java/gui/GuiTest.java @@ -2,7 +2,6 @@ import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; -import com.intellij.uiDesigner.core.Spacer; import javax.swing.*; import java.awt.*; @@ -19,7 +18,6 @@ public class GuiTest { private JButton testLog; private JTextArea logArea; private final GuiLogCreator guiLogCreator; - private JFileChooser jFileChooser; public static void main(String[] args) { JFrame frame = new JFrame("GuiTest"); @@ -40,4 +38,34 @@ public void actionPerformed(ActionEvent e) { }); } + { +// GUI initializer generated by IntelliJ IDEA GUI Designer +// >>> IMPORTANT!! <<< +// DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + logTestPanel = new JPanel(); + logTestPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); + testLog = new JButton(); + testLog.setText("Button"); + logTestPanel.add(testLog, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + logArea = new JTextArea(); + logTestPanel.add(logArea, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false)); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return logTestPanel; + } } diff --git a/src/test/java/other/ChangeGlobalTester.java b/src/test/java/other/ChangeGlobalTester.java new file mode 100644 index 0000000..ca819f9 --- /dev/null +++ b/src/test/java/other/ChangeGlobalTester.java @@ -0,0 +1,12 @@ +package other; + +/** + * @author yagol + * @TIME 2021/11/17 - 7:10 下午 + * @Description + **/ +public class ChangeGlobalTester { + public static void main(String[] args) { + GlobalParamClass globalParamClass=new GlobalParamClass(); + } +} diff --git a/src/test/java/other/GlobalParamClass.java b/src/test/java/other/GlobalParamClass.java new file mode 100644 index 0000000..9738d45 --- /dev/null +++ b/src/test/java/other/GlobalParamClass.java @@ -0,0 +1,14 @@ +package other; + +import lombok.Data; + +/** + * @author yagol + * @TIME 2021/11/17 - 7:09 下午 + * @Description + **/ +@Data +public class GlobalParamClass { + static String PARAM = "1"; + +} diff --git a/src/test/java/other/JarExecutePathTester.java b/src/test/java/other/JarExecutePathTester.java new file mode 100644 index 0000000..093f2e7 --- /dev/null +++ b/src/test/java/other/JarExecutePathTester.java @@ -0,0 +1,13 @@ +package other; + +/** + * @author yagol + * @TIME 2021/11/17 - 8:18 下午 + * @Description + **/ +public class JarExecutePathTester { + public static void main(String[] args) { + System.out.println(JarExecutePathTester.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + } + +} diff --git a/src/test/resources/config/base.yaml b/src/test/resources/config/base.yaml new file mode 100644 index 0000000..76a56dd --- /dev/null +++ b/src/test/resources/config/base.yaml @@ -0,0 +1,2 @@ +chrome: + driverPath: src/main/resources/chromedriver