From b3c79f49a06710d962a09acb11bc348eb68c83c4 Mon Sep 17 00:00:00 2001 From: SerLiunx-ctrl <17689543@qq.com> Date: Wed, 15 Jan 2025 15:50:26 +0800 Subject: [PATCH] =?UTF-8?q?change:=20=E8=B0=83=E6=95=B4jline=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E9=80=BB=E8=BE=91,=20=E7=A7=BB=E9=99=A4ip-provider?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/serliunx/ddns/ManagerLite.java | 40 +++++++++++++----- .../ddns/test/support/ProviderTest.java | 41 +------------------ 2 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/serliunx/ddns/ManagerLite.java b/src/main/java/com/serliunx/ddns/ManagerLite.java index 1fb5d40..5bee971 100644 --- a/src/main/java/com/serliunx/ddns/ManagerLite.java +++ b/src/main/java/com/serliunx/ddns/ManagerLite.java @@ -14,6 +14,8 @@ import org.jline.reader.impl.history.DefaultHistory; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; +import java.io.IOException; + /** * 启动类 * @@ -36,7 +38,7 @@ public final class ManagerLite { */ private static SystemInitializer systemInitializer; - public static void main(String[] args) throws Exception { + public static void main(String[] args) { // 配置初始化 initConfiguration(args); @@ -47,7 +49,16 @@ public final class ManagerLite { // 系统初始化 initSystem(); - Terminal terminal = TerminalBuilder.builder().system(true).build(); + Terminal terminal; + try { + terminal = TerminalBuilder.builder() + .system(true) + .build(); + } catch (IOException e) { + // 不应该发生 + System.exit(0); + throw new RuntimeException(e); + } LineReader lineReader = LineReaderBuilder.builder() .terminal(terminal) // 如果想记录历史命令,可以配置一个 History @@ -60,19 +71,26 @@ public final class ManagerLite { while (true) { // 该方法会阻塞,直到用户敲回车 - String line = lineReader.readLine(prompt); + try { + String cmd = lineReader.readLine(prompt); - // 当用户输入 exit 或 quit,就退出循环 - if ("exit".equalsIgnoreCase(line) - || "quit".equalsIgnoreCase(line)) { - break; + // 当用户输入 exit 或 quit,就退出循环 + if ("exit".equalsIgnoreCase(cmd)) { + break; + } + // 在这里可以对用户输入做进一步处理 + terminal.flush(); + } catch (Exception e) { + System.exit(0); } - // 在这里可以对用户输入做进一步处理 - terminal.writer().println("You entered: " + line); - terminal.flush(); } - terminal.close(); + try { + System.exit(0); + terminal.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** diff --git a/src/test/java/com/serliunx/ddns/test/support/ProviderTest.java b/src/test/java/com/serliunx/ddns/test/support/ProviderTest.java index 1710e81..cee5b3c 100644 --- a/src/test/java/com/serliunx/ddns/test/support/ProviderTest.java +++ b/src/test/java/com/serliunx/ddns/test/support/ProviderTest.java @@ -1,17 +1,8 @@ package com.serliunx.ddns.test.support; -import com.serliunx.ddns.support.ipprovider.IcanhazipProvider; -import com.serliunx.ddns.support.ipprovider.IpApiProvider; -import com.serliunx.ddns.support.ipprovider.Provider; -import com.serliunx.ddns.support.ipprovider.ScheduledProvider; -import org.junit.Test; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - /** * 供应器测试 + * //TODO 暂时移除,待重写单元测试 * * @author SerLiunx * @version 1.0.3 @@ -19,34 +10,4 @@ import java.util.concurrent.TimeUnit; */ public class ProviderTest { - @Test - public void testIpApiProvider() { - Provider provider = new IpApiProvider(); - System.out.println(provider.get()); - } - - @Test - public void testScheduledProvider() throws Exception { - ScheduledProvider provider = new ScheduledProvider(new IpApiProvider(), 3); - String ip = provider.get(); - System.out.println(ip); - } - - @Test - public void testIcanhazipProvider() { - Provider provider = new IcanhazipProvider(); - System.out.println(provider.get()); - } - - @Test - public void testScheduledProviderForRunnable() throws InterruptedException { - ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); - ScheduledProvider provider = new ScheduledProvider(new IpApiProvider(), 3); - provider.close(); - - ses.scheduleAtFixedRate(provider, 0, 1000, TimeUnit.MILLISECONDS); - provider.whenUpdate(ip -> System.out.println("ip update: " + ip)); - - TimeUnit.SECONDS.sleep(120); - } }