Skip to content

Commit

Permalink
Add protected method to connect channel to streams
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
alcarraz committed Nov 28, 2023
1 parent 98ae51d commit 986b144
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
14 changes: 14 additions & 0 deletions jpos/src/main/java/org/jpos/iso/BaseChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ protected void connect (Socket socket)
setChanged();
notifyObservers();
}

/**
* Allow the channel to be connected to a pair of input and output streams.
* <p>
* For instance for taking input from or outputting to a file or standard input/ouput.
*
* @param in Where to read from.
* @param out Where to write to.
*/
protected void connect(InputStream in, OutputStream out) {
usable = in != null || out != null; //at least one of them has to be not null
if (in != null) serverIn = new DataInputStream(in);
if (out != null) serverOut = new DataOutputStream(out);
}
protected void postConnectHook() throws IOException {
// do nothing
}
Expand Down
8 changes: 8 additions & 0 deletions jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import org.jpos.iso.packager.XMLPackager;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

Expand Down Expand Up @@ -117,4 +120,9 @@ public void disconnect () throws IOException {
reader.close ();
reader = null;
}

protected void connect(InputStream in, OutputStream out) {
super.connect(in, out);
reader = new BufferedReader(new InputStreamReader(in));
}
}
70 changes: 64 additions & 6 deletions jpos/src/test/java/org/jpos/iso/BaseChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@

package org.jpos.iso;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.*;
Expand Down Expand Up @@ -1223,4 +1224,61 @@ public void testStreamReceive() throws Throwable {
byte[] result = aSCIIChannel.streamReceive();
assertEquals(0, result.length, "result.length");
}

/**
* Connect using input stream, send a message and check what is written in the output stream
* is the same as the packed message.
*/
@Test
public void testConnectWithStreamsAndSend() throws ISOException, IOException {
BaseChannel ch = new PADChannel() {
@Override
public boolean isConnected() {
return serverOut != null;
}
};
ISOPackager packager = new ISO87APackager();
ch.setPackager(packager);

ISOMsg m = new ISOMsg();
m.setMTI("0800");
m.set(11, "000000");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ch.connect(null, out);
ch.send(m);
assertThat("Packed message should match output stream content",
out.toByteArray(), is(equalTo(packager.pack(m))));
//Double check just to verify we are not comparing empty arrays.
m.set(41, "ABC");
assertThat("Packed message should not match a different message",
out.toByteArray(), is(not(equalTo(packager.pack(m)))));
}

/**
* Connect using output stream, receive a message and check it is the same as sent.
*/
@Test
public void testConnectWithStreamsAndReceive() throws ISOException, IOException {
BaseChannel ch = new PADChannel(){
@Override
public boolean isConnected() {
return serverIn != null;
}
};
ISOPackager packager = new ISO87APackager();
ch.setPackager(packager);

ISOMsg m = new ISOMsg();
m.setMTI("0800");
m.set(11, "000000");
m.setPackager(packager);
ByteArrayInputStream in = new ByteArrayInputStream(m.pack());
ch.connect(in, null);
ISOMsg m2 = ch.receive();
assertThat("Received message should not be null", m2, is(notNullValue()));
assertThat("Received message should be the same as original",
m2.pack(), is(equalTo(m.pack())));

}

}

0 comments on commit 986b144

Please sign in to comment.