Skip to content

Commit 986b144

Browse files
committed
Add protected method to connect channel to streams
These methods can be overridden by subclasses of existing channels to write to/read from streams, for example, to replay some captured stream, or just to do some tests based on ByteArrayInputStream. Signed-off-by: Andrés Alcarraz <alcarraz@gmail.com>
1 parent 98ae51d commit 986b144

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

jpos/src/main/java/org/jpos/iso/BaseChannel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ protected void connect (Socket socket)
291291
setChanged();
292292
notifyObservers();
293293
}
294+
295+
/**
296+
* Allow the channel to be connected to a pair of input and output streams.
297+
* <p>
298+
* For instance for taking input from or outputting to a file or standard input/ouput.
299+
*
300+
* @param in Where to read from.
301+
* @param out Where to write to.
302+
*/
303+
protected void connect(InputStream in, OutputStream out) {
304+
usable = in != null || out != null; //at least one of them has to be not null
305+
if (in != null) serverIn = new DataInputStream(in);
306+
if (out != null) serverOut = new DataOutputStream(out);
307+
}
294308
protected void postConnectHook() throws IOException {
295309
// do nothing
296310
}

jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import org.jpos.iso.packager.XMLPackager;
2323

2424
import java.io.BufferedReader;
25+
import java.io.DataInputStream;
2526
import java.io.EOFException;
2627
import java.io.IOException;
28+
import java.io.InputStream;
2729
import java.io.InputStreamReader;
30+
import java.io.OutputStream;
2831
import java.net.ServerSocket;
2932
import java.net.Socket;
3033

@@ -117,4 +120,9 @@ public void disconnect () throws IOException {
117120
reader.close ();
118121
reader = null;
119122
}
123+
124+
protected void connect(InputStream in, OutputStream out) {
125+
super.connect(in, out);
126+
reader = new BufferedReader(new InputStreamReader(in));
127+
}
120128
}

jpos/src/test/java/org/jpos/iso/BaseChannelTest.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818

1919
package org.jpos.iso;
2020

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.*;
2727
import static org.mockito.Mockito.*;
2828

2929
import static org.apache.commons.lang3.JavaVersion.JAVA_10;
3030
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
3131
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
3232

33+
import java.io.ByteArrayInputStream;
3334
import java.io.ByteArrayOutputStream;
3435
import java.io.IOException;
3536
import java.net.*;
@@ -1223,4 +1224,61 @@ public void testStreamReceive() throws Throwable {
12231224
byte[] result = aSCIIChannel.streamReceive();
12241225
assertEquals(0, result.length, "result.length");
12251226
}
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+
12261284
}

0 commit comments

Comments
 (0)