|
18 | 18 |
|
19 | 19 | package org.jpos.iso; |
20 | 20 |
|
21 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
23 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
24 | | -import static org.junit.jupiter.api.Assertions.assertSame; |
25 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
26 | | -import static org.junit.jupiter.api.Assertions.fail; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.hamcrest.Matchers.not; |
| 25 | +import static org.hamcrest.Matchers.notNullValue; |
| 26 | +import static org.junit.jupiter.api.Assertions.*; |
27 | 27 | import static org.mockito.Mockito.*; |
28 | 28 |
|
29 | 29 | import static org.apache.commons.lang3.JavaVersion.JAVA_10; |
30 | 30 | import static org.apache.commons.lang3.JavaVersion.JAVA_14; |
31 | 31 | import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost; |
32 | 32 |
|
| 33 | +import java.io.ByteArrayInputStream; |
33 | 34 | import java.io.ByteArrayOutputStream; |
34 | 35 | import java.io.IOException; |
35 | 36 | import java.net.*; |
@@ -1223,4 +1224,61 @@ public void testStreamReceive() throws Throwable { |
1223 | 1224 | byte[] result = aSCIIChannel.streamReceive(); |
1224 | 1225 | assertEquals(0, result.length, "result.length"); |
1225 | 1226 | } |
| 1227 | + |
| 1228 | + /** |
| 1229 | + * Connect using input stream, send a message and check what is written in the output stream |
| 1230 | + * is the same as the packed message. |
| 1231 | + */ |
| 1232 | + @Test |
| 1233 | + public void testConnectWithStreamsAndSend() throws ISOException, IOException { |
| 1234 | + BaseChannel ch = new PADChannel() { |
| 1235 | + @Override |
| 1236 | + public boolean isConnected() { |
| 1237 | + return serverOut != null; |
| 1238 | + } |
| 1239 | + }; |
| 1240 | + ISOPackager packager = new ISO87APackager(); |
| 1241 | + ch.setPackager(packager); |
| 1242 | + |
| 1243 | + ISOMsg m = new ISOMsg(); |
| 1244 | + m.setMTI("0800"); |
| 1245 | + m.set(11, "000000"); |
| 1246 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 1247 | + ch.connect(null, out); |
| 1248 | + ch.send(m); |
| 1249 | + assertThat("Packed message should match output stream content", |
| 1250 | + out.toByteArray(), is(equalTo(packager.pack(m)))); |
| 1251 | + //Double check just to verify we are not comparing empty arrays. |
| 1252 | + m.set(41, "ABC"); |
| 1253 | + assertThat("Packed message should not match a different message", |
| 1254 | + out.toByteArray(), is(not(equalTo(packager.pack(m))))); |
| 1255 | + } |
| 1256 | + |
| 1257 | + /** |
| 1258 | + * Connect using output stream, receive a message and check it is the same as sent. |
| 1259 | + */ |
| 1260 | + @Test |
| 1261 | + public void testConnectWithStreamsAndReceive() throws ISOException, IOException { |
| 1262 | + BaseChannel ch = new PADChannel(){ |
| 1263 | + @Override |
| 1264 | + public boolean isConnected() { |
| 1265 | + return serverIn != null; |
| 1266 | + } |
| 1267 | + }; |
| 1268 | + ISOPackager packager = new ISO87APackager(); |
| 1269 | + ch.setPackager(packager); |
| 1270 | + |
| 1271 | + ISOMsg m = new ISOMsg(); |
| 1272 | + m.setMTI("0800"); |
| 1273 | + m.set(11, "000000"); |
| 1274 | + m.setPackager(packager); |
| 1275 | + ByteArrayInputStream in = new ByteArrayInputStream(m.pack()); |
| 1276 | + ch.connect(in, null); |
| 1277 | + ISOMsg m2 = ch.receive(); |
| 1278 | + assertThat("Received message should not be null", m2, is(notNullValue())); |
| 1279 | + assertThat("Received message should be the same as original", |
| 1280 | + m2.pack(), is(equalTo(m.pack()))); |
| 1281 | + |
| 1282 | + } |
| 1283 | + |
1226 | 1284 | } |
0 commit comments