17
17
package org .springframework .integration .ftp .session ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
20
22
import static org .assertj .core .api .Assertions .fail ;
21
23
import static org .mockito .Mockito .doReturn ;
22
24
import static org .mockito .Mockito .mock ;
30
32
import java .util .concurrent .atomic .AtomicInteger ;
31
33
32
34
import org .apache .commons .net .ftp .FTPClient ;
33
- import org .junit .Ignore ;
34
- import org .junit .Test ;
35
+ import org .apache .commons .net .ftp .FTPFile ;
36
+ import org .junit .jupiter .api .Disabled ;
37
+ import org .junit .jupiter .api .Test ;
35
38
import org .mockito .Mockito ;
36
39
37
40
import org .springframework .integration .file .remote .session .CachingSessionFactory ;
44
47
* @author Oleg Zhurakousky
45
48
* @author Gunnar Hillert
46
49
* @author Gary Russell
50
+ * @author Artem Bilan
47
51
*
48
52
*/
49
- @ SuppressWarnings ({"rawtypes" , "unchecked" })
50
- public class SessionFactoryTests {
53
+ class SessionFactoryTests {
51
54
52
55
53
56
@ Test
54
- public void testTimeouts () throws Exception {
57
+ void testFtpClientInteraction () throws Exception {
55
58
final FTPClient client = mock (FTPClient .class );
56
59
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory () {
57
60
@@ -66,42 +69,49 @@ protected FTPClient createClientInstance() {
66
69
sessionFactory .setDataTimeout (789 );
67
70
doReturn (200 ).when (client ).getReplyCode ();
68
71
doReturn (true ).when (client ).login ("foo" , null );
69
- sessionFactory .getSession ();
72
+ FtpSession session = sessionFactory .getSession ();
70
73
verify (client ).setConnectTimeout (123 );
71
74
verify (client ).setDefaultTimeout (456 );
72
75
verify (client ).setDataTimeout (789 );
76
+
77
+ session .close ();
78
+
79
+ verify (client ).logout ();
80
+ verify (client ).disconnect ();
73
81
}
74
82
75
83
@ Test
76
- public void testWithControlEncoding () {
84
+ void testWithControlEncoding () {
77
85
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
78
86
sessionFactory .setControlEncoding ("UTF-8" );
79
87
assertThat (TestUtils .getPropertyValue (sessionFactory , "controlEncoding" ))
80
88
.as ("Expected controlEncoding value of 'UTF-8'" ).isEqualTo ("UTF-8" );
81
89
}
82
90
83
91
@ Test
84
- public void testWithoutControlEncoding () {
92
+ void testWithoutControlEncoding () {
85
93
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
86
94
assertThat (TestUtils .getPropertyValue (sessionFactory , "controlEncoding" ))
87
95
.as ("Expected controlEncoding value of 'ISO-8859-1'" ).isEqualTo ("ISO-8859-1" );
88
96
}
89
97
90
- @ Test ( expected = IllegalArgumentException . class )
91
- public void testEmptyControlEncoding () {
98
+ @ Test
99
+ void testEmptyControlEncoding () {
92
100
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
93
- sessionFactory .setControlEncoding ("" );
101
+ assertThatIllegalArgumentException ()
102
+ .isThrownBy (() -> sessionFactory .setControlEncoding ("" ));
94
103
}
95
104
96
- @ Test ( expected = IllegalArgumentException . class )
97
- public void testNullControlEncoding () {
105
+ @ Test
106
+ void testNullControlEncoding () {
98
107
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
99
- sessionFactory .setControlEncoding (null );
108
+ assertThatIllegalArgumentException ()
109
+ .isThrownBy (() -> sessionFactory .setControlEncoding (null ));
100
110
}
101
111
102
112
103
113
@ Test
104
- public void testClientModes () throws Exception {
114
+ void testClientModes () throws Exception {
105
115
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
106
116
Field [] fields = FTPClient .class .getDeclaredFields ();
107
117
for (Field field : fields ) {
@@ -110,7 +120,7 @@ public void testClientModes() throws Exception {
110
120
int clientMode = field .getInt (null );
111
121
sessionFactory .setClientMode (clientMode );
112
122
if (!(clientMode == FTPClient .ACTIVE_LOCAL_DATA_CONNECTION_MODE ||
113
- clientMode == FTPClient .PASSIVE_LOCAL_DATA_CONNECTION_MODE )) {
123
+ clientMode == FTPClient .PASSIVE_LOCAL_DATA_CONNECTION_MODE )) {
114
124
fail ("IllegalArgumentException expected" );
115
125
}
116
126
}
@@ -123,74 +133,79 @@ public void testClientModes() throws Exception {
123
133
124
134
125
135
@ Test
126
- public void testStaleConnection () throws Exception {
127
- SessionFactory sessionFactory = Mockito .mock (SessionFactory .class );
128
- Session sessionA = Mockito .mock (Session .class );
129
- Session sessionB = Mockito .mock (Session .class );
136
+ @ SuppressWarnings ("unchecked" )
137
+ void testStaleConnection () {
138
+ SessionFactory <FTPFile > sessionFactory = Mockito .mock (SessionFactory .class );
139
+ Session <FTPFile > sessionA = Mockito .mock (Session .class );
140
+ Session <FTPFile > sessionB = Mockito .mock (Session .class );
130
141
Mockito .when (sessionA .isOpen ()).thenReturn (true );
131
142
Mockito .when (sessionB .isOpen ()).thenReturn (false );
132
143
133
144
Mockito .when (sessionFactory .getSession ()).thenReturn (sessionA );
134
145
Mockito .when (sessionFactory .getSession ()).thenReturn (sessionB );
135
146
136
- CachingSessionFactory cachingFactory = new CachingSessionFactory (sessionFactory , 2 );
147
+ CachingSessionFactory < FTPFile > cachingFactory = new CachingSessionFactory <> (sessionFactory , 2 );
137
148
138
- Session firstSession = cachingFactory .getSession ();
139
- Session secondSession = cachingFactory .getSession ();
149
+ Session < FTPFile > firstSession = cachingFactory .getSession ();
150
+ Session < FTPFile > secondSession = cachingFactory .getSession ();
140
151
secondSession .close ();
141
- Session nonStaleSession = cachingFactory .getSession ();
152
+ Session < FTPFile > nonStaleSession = cachingFactory .getSession ();
142
153
assertThat (TestUtils .getPropertyValue (nonStaleSession , "targetSession" ))
143
154
.isEqualTo (TestUtils .getPropertyValue (firstSession , "targetSession" ));
144
155
}
145
156
146
157
@ Test
147
- public void testSameSessionFromThePool () throws Exception {
148
- SessionFactory sessionFactory = Mockito .mock (SessionFactory .class );
149
- Session session = Mockito .mock (Session .class );
158
+ @ SuppressWarnings ("unchecked" )
159
+ void testSameSessionFromThePool () {
160
+ SessionFactory <FTPFile > sessionFactory = Mockito .mock (SessionFactory .class );
161
+ Session <FTPFile > session = Mockito .mock (Session .class );
150
162
Mockito .when (sessionFactory .getSession ()).thenReturn (session );
151
163
152
- CachingSessionFactory cachingFactory = new CachingSessionFactory (sessionFactory , 2 );
164
+ CachingSessionFactory < FTPFile > cachingFactory = new CachingSessionFactory <> (sessionFactory , 2 );
153
165
154
- Session s1 = cachingFactory .getSession ();
166
+ Session < FTPFile > s1 = cachingFactory .getSession ();
155
167
s1 .close ();
156
- Session s2 = cachingFactory .getSession ();
168
+ Session < FTPFile > s2 = cachingFactory .getSession ();
157
169
s2 .close ();
158
170
assertThat (TestUtils .getPropertyValue (s2 , "targetSession" ))
159
171
.isEqualTo (TestUtils .getPropertyValue (s1 , "targetSession" ));
160
172
Mockito .verify (sessionFactory , Mockito .times (2 )).getSession ();
161
173
}
162
174
163
- @ Test (expected = PoolItemNotAvailableException .class ) // timeout expire
164
- public void testSessionWaitExpire () throws Exception {
165
- SessionFactory sessionFactory = Mockito .mock (SessionFactory .class );
166
- Session session = Mockito .mock (Session .class );
175
+ @ Test
176
+ @ SuppressWarnings ("unchecked" )
177
+ void testSessionWaitExpire () {
178
+ SessionFactory <FTPFile > sessionFactory = Mockito .mock (SessionFactory .class );
179
+ Session <FTPFile > session = Mockito .mock (Session .class );
167
180
Mockito .when (sessionFactory .getSession ()).thenReturn (session );
168
181
169
- CachingSessionFactory cachingFactory = new CachingSessionFactory (sessionFactory , 2 );
182
+ CachingSessionFactory < FTPFile > cachingFactory = new CachingSessionFactory <> (sessionFactory , 2 );
170
183
171
184
cachingFactory .setSessionWaitTimeout (3000 );
172
185
173
186
cachingFactory .getSession ();
174
187
cachingFactory .getSession ();
175
- cachingFactory .getSession ();
188
+
189
+ assertThatExceptionOfType (PoolItemNotAvailableException .class ) // timeout expire
190
+ .isThrownBy (cachingFactory ::getSession );
176
191
}
177
192
178
193
@ Test
179
- @ Ignore
180
- public void testConnectionLimit () throws Exception {
194
+ @ Disabled
195
+ void testConnectionLimit () throws Exception {
181
196
ExecutorService executor = Executors .newCachedThreadPool ();
182
197
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory ();
183
198
sessionFactory .setHost ("192.168.28.143" );
184
199
sessionFactory .setPassword ("password" );
185
200
sessionFactory .setUsername ("user" );
186
- final CachingSessionFactory factory = new CachingSessionFactory (sessionFactory , 2 );
201
+ final CachingSessionFactory < FTPFile > factory = new CachingSessionFactory <> (sessionFactory , 2 );
187
202
188
203
final Random random = new Random ();
189
204
final AtomicInteger failures = new AtomicInteger ();
190
205
for (int i = 0 ; i < 30 ; i ++) {
191
206
executor .execute (() -> {
192
207
try {
193
- Session session = factory .getSession ();
208
+ Session < FTPFile > session = factory .getSession ();
194
209
Thread .sleep (random .nextInt (5000 ));
195
210
session .close ();
196
211
}
@@ -205,4 +220,5 @@ public void testConnectionLimit() throws Exception {
205
220
206
221
assertThat (failures .get ()).isEqualTo (0 );
207
222
}
223
+
208
224
}
0 commit comments