Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加对 IOServer 依赖注入的测试 #21

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@
<artifactId>log4j-api</artifactId>
<version>3.0.0-alpha1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.11</version>
</dependency>

</dependencies>
<groupId>io.github.hit-refresh</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ReFreSH/JMobileSuit/IO/ColorSetting.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -32,6 +35,7 @@ public class IOServer {
/**
* Color settings for this IOServer. (default getInstance)
*/
@Autowired
public ReFreSH.JMobileSuit.IO.ColorSetting ColorSetting;
public PromptServer Prompt;
/**
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/applicationContextTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描功能-->
<context:component-scan base-package="ReFreSH.JMobileSuit"></context:component-scan>


</beans>