From 986b1447b0b25adf10732baa40849e1a829b5302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Mon, 27 Nov 2023 17:26:31 -0500 Subject: [PATCH] Add protected method to connect channel to streams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../main/java/org/jpos/iso/BaseChannel.java | 14 ++++ .../java/org/jpos/iso/channel/XMLChannel.java | 8 +++ .../java/org/jpos/iso/BaseChannelTest.java | 70 +++++++++++++++++-- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/BaseChannel.java b/jpos/src/main/java/org/jpos/iso/BaseChannel.java index 1f4db83217..8252e1470c 100644 --- a/jpos/src/main/java/org/jpos/iso/BaseChannel.java +++ b/jpos/src/main/java/org/jpos/iso/BaseChannel.java @@ -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. + *

+ * 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 } diff --git a/jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java b/jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java index 5a7f852acc..4d0f077846 100644 --- a/jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java +++ b/jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java @@ -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; @@ -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)); + } } diff --git a/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java b/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java index 9a7cfc42ad..6e1e44d0ee 100644 --- a/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java +++ b/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java @@ -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.*; @@ -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()))); + + } + }