Skip to content

Commit 2fd1083

Browse files
committed
TDD: Write test code for JschServer
1 parent 8528714 commit 2fd1083

File tree

2 files changed

+106
-17
lines changed

2 files changed

+106
-17
lines changed

src/main/java/root/common/server/implement/JschServer.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import com.jcraft.jsch.JSchException;
1010
import com.jcraft.jsch.Session;
1111

12+
import lombok.ToString;
1213
import lombok.extern.slf4j.Slf4j;
1314
import root.core.domain.JschConnectionInfo;
1415

1516
@Slf4j
17+
@ToString
1618
public class JschServer {
1719
private JSch jsch;
1820
private Session session;
@@ -40,59 +42,65 @@ public void init() {
4042
log.error(e.getMessage());
4143
}
4244
}
43-
45+
4446
public Session getSession() {
45-
if(session == null) {
47+
if (session == null) {
4648
return null;
4749
}
4850
return session;
4951
}
50-
51-
public Session connect(Session session) {
52+
53+
public Session connect(Session session) throws JSchException {
54+
if(session == null) {
55+
throw new NullPointerException("Session is null");
56+
}
57+
5258
try {
5359
session.connect();
5460
} catch (JSchException e) {
5561
log.error(e.getMessage());
62+
throw e;
5663
}
64+
5765
return session;
5866
}
59-
67+
6068
public void disConnect(Session session) {
6169
session.disconnect();
6270
}
63-
71+
6472
public Channel openExecChannel(Session session, String command) {
6573
Channel channel = null;
6674
try {
6775
channel = session.openChannel("exec");
68-
//채널접속
69-
ChannelExec channelExec = (ChannelExec) channel; //명령 전송 채널사용
76+
// 채널접속
77+
ChannelExec channelExec = (ChannelExec) channel; // 명령 전송 채널사용
7078
channelExec.setPty(true);
71-
channelExec.setCommand(command);
79+
channelExec.setCommand(command);
7280
} catch (JSchException e) {
7381
log.error(e.getMessage());
7482
}
7583
return channel;
7684
}
77-
85+
7886
public InputStream connectChannel(Channel channel) {
7987
InputStream in = null;
8088
try {
8189
// CallBack
8290
in = channel.getInputStream();
83-
((ChannelExec) channel).setErrStream(System.err);
84-
91+
((ChannelExec) channel).setErrStream(System.err);
92+
8593
channel.connect();
8694
} catch (Exception e) {
8795
log.error(e.getMessage());
8896
}
8997
return in;
9098
}
91-
99+
92100
public void disConnectChannel(Channel channel) {
93-
channel.disconnect();
101+
channel.disconnect();
94102
}
95-
103+
96104
public String getServerName() {
97105
return this.jschConnectionInfo.getServerName();
98106
}
@@ -104,12 +112,12 @@ public static boolean validateConn(Session session) {
104112
}
105113

106114
try {
107-
session.connect(15);
115+
session.connect(3000);
108116
} catch (JSchException e) {
109117
log.error(e.getMessage());
110118
return false;
111119
}
112-
120+
113121
return session.isConnected();
114122
}
115123
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package root.common.server.implement;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
import static org.junit.jupiter.api.Assertions.fail;
9+
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.Test;
12+
13+
import com.jcraft.jsch.JSchException;
14+
import com.jcraft.jsch.Session;
15+
16+
import root.core.domain.JschConnectionInfo;
17+
18+
public class JschServerTest {
19+
20+
public static JschServer jsch;
21+
22+
@BeforeAll
23+
public static void setup() {
24+
String serverName = "DKY SERVER";
25+
String host = "192.168.154.1";
26+
String userName = "dky";
27+
String port = "22";
28+
String password = "ehruddbs1!";
29+
jsch = new JschServer(new JschConnectionInfo(serverName, host, port, userName, password));
30+
}
31+
32+
@Test
33+
public void testInit_ValidConnInfo() {
34+
jsch.init();
35+
Session session = jsch.getSession();
36+
assertNotNull(session);
37+
}
38+
39+
@Test
40+
public void testInit_Fail_InvalidConnInfo() {
41+
JschServer jsch = new JschServer(new JschConnectionInfo());
42+
jsch.init();
43+
Session session = jsch.getSession();
44+
assertNull(session);
45+
}
46+
47+
@Test
48+
public void testConnect_ConnectionSuccess() {
49+
jsch.init();
50+
Session session = jsch.getSession();
51+
try {
52+
session = jsch.connect(session);
53+
} catch (JSchException e) {
54+
fail();
55+
}
56+
assertTrue(session.isConnected());
57+
}
58+
59+
@Test
60+
public void testConnect_ConnectionFail_SessionIsNull() {
61+
JschServer jsch = new JschServer(new JschConnectionInfo());
62+
jsch.init();
63+
Session session = jsch.getSession();
64+
NullPointerException thrown = assertThrows(NullPointerException.class, () -> jsch.connect(session));
65+
assertEquals("Session is null", thrown.getMessage());
66+
}
67+
68+
@Test
69+
public void testConnect_ConnectionFail_JSchException() {
70+
jsch.init();
71+
Session session = jsch.getSession();
72+
try {
73+
jsch.connect(session);
74+
} catch (JSchException e) {
75+
fail();
76+
}
77+
78+
JSchException thrown = assertThrows(JSchException.class, () -> jsch.connect(session));
79+
assertEquals("session is already connected", thrown.getMessage());
80+
}
81+
}

0 commit comments

Comments
 (0)