Skip to content

Commit

Permalink
Merge pull request #29 from 2364058719/master
Browse files Browse the repository at this point in the history
添加了IOServerTest中的用例
  • Loading branch information
FerdinandSu authored Oct 12, 2024
2 parents dc728e8 + 664aa08 commit 0dbeb89
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
61 changes: 61 additions & 0 deletions src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.springframework.context.ApplicationContext;
Expand All @@ -22,6 +23,66 @@

public class IOServerTests {

private IOServer ioServer;
private Logger logger;
private ByteArrayOutputStream outputStream;
private ByteArrayOutputStream errorStream;
private InputStream inputStream;

@BeforeEach
public void setUp() {
// Mock the logger
logger = mock(Logger.class);

// Initialize IOServer with mock dependencies
ioServer = new IOServer(null, logger, null);

// Set up custom output/error streams
outputStream = new ByteArrayOutputStream();
errorStream = new ByteArrayOutputStream();
inputStream = new ByteArrayInputStream("test input\n".getBytes());

ioServer.Output = new PrintStream(outputStream);
ioServer.Error = new PrintStream(errorStream);
ioServer.SetInput(inputStream);
}

@Test
public void testWriteLine() {
ioServer.WriteLine("Hello World");
assertEquals("Hello World\n", outputStream.toString());
}

@Test
public void testReadLine2() {
String input = ioServer.ReadLine();
assertEquals("test input", input);
}

@Test
public void testWriteDebug2() {
ioServer.WriteDebug("Debug message");
verify(logger).debug("Debug message");
}

@Test
public void testWriteException2() {
Exception e = new Exception("Test Exception");
ioServer.WriteException(e);
verify(logger).error(e);
}

@Test
public void testWriteWithColor() {
ioServer.Write("Colored Text", ConsoleColor.Red);
// Assuming ConsoleColor.Red changes the output, we verify the text without the color code
assertTrue(outputStream.toString().contains("Colored Text"));
}

@Test
public void testIsInputRedirected2() {
assertTrue(ioServer.IsInputRedirected());
}
// private final Logger Logger;
//
// public IOServerTests(Logger logger) {
Expand Down

0 comments on commit 0dbeb89

Please sign in to comment.