Skip to content

Commit 40535f2

Browse files
committed
JschServer test for window, linux server
1 parent f92f9c1 commit 40535f2

File tree

2 files changed

+100
-58
lines changed

2 files changed

+100
-58
lines changed

Common/src/test/java/root/common/server/implement/JschServerTest.java

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
65
import static org.junit.jupiter.api.Assertions.assertNotNull;
76
import static org.junit.jupiter.api.Assertions.assertNull;
87
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -21,80 +20,128 @@
2120

2221
public class JschServerTest {
2322

24-
public static JschServer jsch;
23+
public static JschServer windowJsch;
24+
public static JschServer linuxJsch;
2525

2626
@BeforeAll
2727
public static void setup() {
28-
String serverName = "DKY SERVER";
29-
ServerOS serverOS = ServerOS.WINDOW;
30-
String host = "192.168.154.1";
31-
String userName = "dky";
32-
String port = "22";
33-
String password = "ehruddbs1!";
34-
jsch = new JschServer(new JschConnectionInfo(serverName, serverOS, host, port, userName, password));
28+
String windowServerName = "DKY WINDOW SERVER";
29+
ServerOS windowServerOS = ServerOS.WINDOW;
30+
String windowHost = "192.168.154.1";
31+
String windowUserName = "dky";
32+
int windowPort = 22;
33+
String windowPassword = "ehruddbs1!";
34+
windowJsch = new JschServer(new JschConnectionInfo(windowServerName, windowServerOS, windowHost, windowPort, windowUserName, windowPassword));
35+
windowJsch.init();
36+
37+
String linuxServerName = "DKY LINUX SERVER";
38+
ServerOS linuxServerOS = ServerOS.LINUX;
39+
String linuxHost = "192.168.56.137";
40+
String linuxUserName = "dokyeongyun";
41+
int linuxPort = 22;
42+
String linuxPassword = "ehruddbs1!";
43+
linuxJsch = new JschServer(new JschConnectionInfo(linuxServerName, linuxServerOS, linuxHost, linuxPort, linuxUserName, linuxPassword));
44+
linuxJsch.init();
45+
}
46+
47+
@Test
48+
public void testGetServerName_Window() {
49+
String serverName = windowJsch.getServerName();
50+
assertEquals(serverName, "DKY WINDOW SERVER");
51+
}
52+
53+
@Test
54+
public void testGetServerName_Linux() {
55+
String serverName = linuxJsch.getServerName();
56+
assertEquals(serverName, "DKY LINUX SERVER");
3557
}
3658

3759
@Test
38-
public void testInit_ValidConnInfo() throws Exception {
39-
jsch.init();
40-
Session session = jsch.getSession();
60+
public void testInit_ValidConnInfo_Window() throws Exception {
61+
Session session = windowJsch.getSession();
62+
assertNotNull(session);
63+
}
64+
65+
@Test
66+
public void testInit_ValidConnInfo_Linux() throws Exception {
67+
Session session = linuxJsch.getSession();
4168
assertNotNull(session);
4269
}
4370

4471
@Test
4572
public void testInit_Fail_InvalidConnInfo() throws Exception {
4673
JschServer jsch = new JschServer(new JschConnectionInfo());
47-
jsch.init();
4874
Session session = jsch.getSession();
4975
assertNull(session);
5076
}
5177

5278
@Test
53-
public void testConnect_Success() throws Exception {
54-
jsch.init();
55-
Session session = jsch.getSession();
79+
public void testConnect_Success_Window() throws Exception {
80+
Session session = windowJsch.getSession();
81+
assertTrue(session.isConnected());
82+
}
83+
84+
@Test
85+
public void testConnect_Success_Linux() throws Exception {
86+
Session session = linuxJsch.getSession();
5687
assertTrue(session.isConnected());
5788
}
5889

5990
@Test
6091
public void testDisConnect_DisConnectionSuccess() throws Exception {
61-
jsch.init();
62-
Session session = jsch.getSession();
63-
92+
Session session = windowJsch.getSession();
6493
assertTrue(session.isConnected());
6594

66-
jsch.disConnect(session);
95+
windowJsch.disConnect(session);
6796
assertFalse(session.isConnected());
6897
}
6998

7099
@Test
71100
public void testDisConnect_DisConnectionFail_SessionIsNull() throws Exception {
72-
jsch.disConnect(null);
101+
Session session = null;
102+
windowJsch.disConnect(session);
103+
assertNull(session);
73104
}
74105

75106
@Test
76-
public void testOpenExecChannel_Success() throws Exception {
77-
jsch.init();
78-
Session session = jsch.getSession();
79-
80-
Channel channel = jsch.openExecChannel(session, "echo 1");
107+
public void testOpenExecChannel_Success_Window() throws Exception {
108+
Session session = windowJsch.getSession();
109+
Channel channel = windowJsch.openExecChannel(session, "echo 1");
110+
assertNotNull(channel);
111+
}
112+
113+
@Test
114+
public void testOpenExecChannel_Success_Linux() throws Exception {
115+
Session session = linuxJsch.getSession();
116+
Channel channel = linuxJsch.openExecChannel(session, "echo 1");
81117
assertNotNull(channel);
82118
}
83119

84120
@Test
85-
public void testOpenExecChannel_Success_WhenSessionIsNull() throws JSchException {
121+
public void testOpenExecChannel_Fail_WhenSessionIsNull() throws JSchException {
86122
Session session = null;
87-
JSchException thrown = assertThrows(JSchException.class, () -> jsch.openExecChannel(session, "echo 1"));
123+
JSchException thrown = assertThrows(JSchException.class, () -> windowJsch.openExecChannel(session, "echo 1"));
88124
assertEquals("session is not valid", thrown.getMessage());
89125
}
90126

91127
@Test
92-
public void testConnectChannel_Success() throws Exception {
93-
jsch.init();
94-
Session session = jsch.getSession();
128+
public void testConnectChannel_Success_Window() throws Exception {
129+
Session session = windowJsch.getSession();
130+
Channel channel = windowJsch.openExecChannel(session, "echo 1");
131+
InputStream in = windowJsch.connectChannel(channel);
95132

96-
Channel channel = jsch.openExecChannel(session, "echo 1");
97-
InputStream in = jsch.connectChannel(channel);
133+
assertNotNull(in);
134+
try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
135+
String echoLine = br.readLine();
136+
assertEquals(echoLine, "1");
137+
}
138+
}
139+
140+
@Test
141+
public void testConnectChannel_Success_Linux() throws Exception {
142+
Session session = linuxJsch.getSession();
143+
Channel channel = linuxJsch.openExecChannel(session, "echo 1");
144+
InputStream in = linuxJsch.connectChannel(channel);
98145

99146
assertNotNull(in);
100147
try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
@@ -104,50 +151,44 @@ public void testConnectChannel_Success() throws Exception {
104151
}
105152

106153
@Test
107-
public void testConnectChannel_Success_WhenSessionIsNull() throws Exception {
154+
public void testConnectChannel_Fail_WhenSessionIsNull() throws Exception {
108155
Session session = null;
109-
assertThrows(JSchException.class, () -> jsch.openExecChannel(session, "echo 1"));
156+
assertThrows(JSchException.class, () -> windowJsch.openExecChannel(session, "echo 1"));
110157
}
111158

112159
@Test
113160
public void testDisConnectChannel_Success() throws Exception {
114-
jsch.init();
115-
Session session = jsch.getSession();
161+
Session session = windowJsch.getSession();
116162

117-
Channel channel = jsch.openExecChannel(session, "echo 1");
118-
jsch.connectChannel(channel);
163+
Channel channel = windowJsch.openExecChannel(session, "echo 1");
164+
windowJsch.connectChannel(channel);
119165
assertTrue(channel.isConnected());
120166

121-
jsch.disConnectChannel(channel);
167+
windowJsch.disConnectChannel(channel);
122168
assertFalse(channel.isConnected());
123169
}
124170

125171
@Test
126-
public void testGetServerName() {
127-
String serverName = jsch.getServerName();
128-
assertEquals(serverName, "DKY SERVER");
129-
}
130-
131-
@Test
132-
public void testExecuteCommand_EchoCommand() throws Exception {
133-
jsch.init();
134-
Session session = jsch.getSession();
135-
String result = jsch.executeCommand(session, "echo 1");
172+
public void testExecuteCommand_EchoCommand_Window() throws Exception {
173+
Session session = windowJsch.getSession();
174+
String result = windowJsch.executeCommand(session, "echo 1");
175+
assertEquals("1", result.trim());
176+
177+
result = windowJsch.executeCommand("echo 1");
136178
assertEquals("1", result.trim());
137179
}
138-
180+
139181
@Test
140-
public void testExecuteCommand_TailCommand() throws Exception {
141-
jsch.init();
142-
Session session = jsch.getSession();
143-
String result = jsch.executeCommand(session, "tail -500 C://Users/aserv/Desktop/alert_DB.log");
144-
assertNotEquals(result, "");
182+
public void testExecuteCommand_EchoCommand_Linux() throws Exception {
183+
Session session = linuxJsch.getSession();
184+
String result = linuxJsch.executeCommand(session, "echo 1");
185+
assertEquals("1", result.trim());
145186
}
146187

147188
@Test
148189
public void testValidateConn_Valid() throws Exception {
149-
jsch.init();
150-
assertTrue(jsch.validateConn(jsch.getSession()));
190+
windowJsch.init();
191+
assertTrue(windowJsch.validateConn(windowJsch.getSession()));
151192
}
152193

153194
@Test

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ subprojects {
1414
dependencies {
1515
// junit
1616
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
17+
testImplementation 'org.junit.platform:junit-platform-launcher:1.8.2'
1718

1819
// mockito
1920
testImplementation 'org.mockito:mockito-core:4.3.1'

0 commit comments

Comments
 (0)