Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protected method to connect channel to streams #572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())));

}

}
Loading