From 925f37209810076fc59262ddb777d5bc97f042fc Mon Sep 17 00:00:00 2001 From: rpete Date: Sun, 3 Dec 2023 22:53:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=20IOServer=20?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 30 +++++++++++++++++++ .../ReFreSH/JMobileSuit/IO/ColorSetting.java | 3 ++ .../java/ReFreSH/JMobileSuit/IO/IOServer.java | 6 +++- .../ReFreSH/JMobileSuit/IO/IOServerTests.java | 9 ++++++ src/test/resources/applicationContextTest.xml | 11 +++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/applicationContextTest.xml diff --git a/pom.xml b/pom.xml index 4529de2..dad07cc 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,36 @@ log4j-api 3.0.0-alpha1 + + + org.springframework + spring-core + 6.0.11 + + + + org.springframework + spring-context + 6.0.11 + + + + org.springframework + spring-beans + 6.0.11 + + + + org.springframework + spring-expression + 6.0.11 + + + + org.springframework + spring-aop + 6.0.11 + io.github.hit-refresh diff --git a/src/main/java/ReFreSH/JMobileSuit/IO/ColorSetting.java b/src/main/java/ReFreSH/JMobileSuit/IO/ColorSetting.java index 64e2748..54aeb7e 100644 --- a/src/main/java/ReFreSH/JMobileSuit/IO/ColorSetting.java +++ b/src/main/java/ReFreSH/JMobileSuit/IO/ColorSetting.java @@ -1,8 +1,11 @@ package ReFreSH.JMobileSuit.IO; +import org.springframework.stereotype.Component; + /** * Color settings of a IOServer. */ +@Component("colorSetting") public class ColorSetting { /** * Default color. For OutputType.Default diff --git a/src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java b/src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java index 9936e25..14bd213 100644 --- a/src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java +++ b/src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java @@ -5,6 +5,8 @@ import ReFreSH.Jarvis.ObjectModel.Tuple; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import java.io.IOException; import java.io.InputStream; @@ -19,6 +21,7 @@ * An entity, which serves the input/output of a mobile suit. */ @SuppressWarnings({"BooleanMethodIsAlwaysInverted", "unused"}) +@Component("IOServer") public class IOServer { /** * Default IOServer, using stdin, stdout, stderr. @@ -32,6 +35,7 @@ public class IOServer { /** * Color settings for this IOServer. (default getInstance) */ + @Autowired public ReFreSH.JMobileSuit.IO.ColorSetting ColorSetting; public PromptServer Prompt; /** @@ -53,7 +57,7 @@ public class IOServer { */ public IOServer() { Prompt = PromptServer.getInstance(); - ColorSetting = ReFreSH.JMobileSuit.IO.ColorSetting.getInstance(); +// ColorSetting = ReFreSH.JMobileSuit.IO.ColorSetting.getInstance(); _input = System.in; Output = System.out; Error = System.err; diff --git a/src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java b/src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java index ab3b818..dc3e5b1 100644 --- a/src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java +++ b/src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java @@ -8,6 +8,8 @@ import org.junit.Test; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.*; import java.util.Arrays; @@ -51,6 +53,13 @@ public void after() throws Exception { public void IOServerTest() { IOServer instance = getInstance(); assertNotNull(instance); + // 测试 IOServer 和 ColorSetting 依赖注入 + ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextTest.xml"); + ColorSetting colorSetting = context.getBean("colorSetting", ColorSetting.class); + IOServer ioServer = context.getBean("IOServer", IOServer.class); + assertNotNull(colorSetting); + assertNotNull(ioServer); + assertNotNull(ioServer.ColorSetting); } @Test diff --git a/src/test/resources/applicationContextTest.xml b/src/test/resources/applicationContextTest.xml new file mode 100644 index 0000000..47365e6 --- /dev/null +++ b/src/test/resources/applicationContextTest.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file