Added some simple unit tests.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@53 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2010-11-26 01:12:39 +00:00
parent 1b07cf9a32
commit 2a5feee7a1
6 changed files with 226 additions and 1 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.solab</groupId>
<artifactId>j8583</artifactId>
<version>1.3.4</version>
<version>1.3.3</version>
<packaging>jar</packaging>
<name>j8583</name>
<description>Java implementation of the ISO 8583 protocol, focused on making the creation, edition and reading of ISO8583 messages as simple and flexible as possible.</description>
+57
View File
@@ -0,0 +1,57 @@
package j8583;
import com.solab.iso8583.CustomField;
/** This is an example of a custom field codec, which converts between strings and instances of this same class.
*
* @author Enrique Zamudio
*/
public class CustomField48 implements CustomField<CustomField48> {
private String v1;
private int v2;
public void setValue1(String value) {
v1 = value;
}
public String getValue1() {
return v1;
}
public void setValue2(int value) {
v2 = value;
}
public int setValue2() {
return v2;
}
@Override
public CustomField48 decodeField(String value) {
CustomField48 cf = null;
if (value != null) {
if (value.length() == 1 && value.charAt(0) == '|') {
cf = new CustomField48();
} else {
int idx = value.lastIndexOf('|');
if (idx < 0 || idx == value.length() - 1) {
throw new IllegalArgumentException(String.format("Invalid data '%s' for field 48", value));
}
cf = new CustomField48();
cf.setValue1(value.substring(0, idx));
cf.setValue2(Integer.parseInt(value.substring(idx + 1)));
}
}
return cf;
}
@Override
public String encodeField(CustomField48 value) {
StringBuilder sb = new StringBuilder();
if (v1 != null) {
sb.append(v1);
}
sb.append('|');
sb.append(v2);
return sb.toString();
}
}
@@ -0,0 +1,75 @@
package j8583;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import org.junit.Before;
import org.junit.Test;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.MessageFactory;
/** These are very simple tests for creating and manipulating messages.
*
* @author Enrique Zamudio
*/
public class SimpleMessageTests {
private MessageFactory mf;
@Before
public void init() throws IOException {
mf = new MessageFactory();
mf.setCustomField(48, new CustomField48());
mf.setConfigPath("config.xml");
}
/** Creates a new message and checks that it has all the fields included in the config. */
@Test
public void testCreation() {
IsoMessage iso = mf.newMessage(0x200);
assert iso.getType() == 0x200;
assert iso.getField(3).getType() == IsoType.NUMERIC && ((Number)iso.getObjectValue(3)).intValue() == 650000;
assert iso.getField(32).getType() == IsoType.LLVAR;
assert iso.getField(35).getType() == IsoType.LLVAR;
assert iso.getField(43).getType() == IsoType.ALPHA && ((String)iso.getObjectValue(43)).length() == 40;
assert iso.getField(48).getType() == IsoType.LLLVAR && iso.getObjectValue(48) instanceof CustomField48;
assert iso.getField(49).getType() == IsoType.ALPHA;
assert iso.getField(60).getType() == IsoType.LLLVAR;
assert iso.getField(61).getType() == IsoType.LLLVAR;
assert iso.getField(100).getType() == IsoType.LLVAR;
assert iso.getField(102).getType() == IsoType.LLVAR;
for (int i = 4; i < 32; i++) {
assert !iso.hasField(i);
}
for (int i = 36; i < 48; i++) {
assert !iso.hasField(i);
}
for (int i = 50; i < 60; i++) {
assert !iso.hasField(i);
}
for (int i = 62; i < 100; i++) {
assert !iso.hasField(i);
}
for (int i = 103; i < 128; i++) {
assert !iso.hasField(i);
}
}
/** Parses a message from a file and checks the fields. */
@Test
public void testParsing() throws IOException, ParseException {
InputStream ins = getClass().getResourceAsStream("example.txt");
byte[] buf = new byte[400];
int pos = 0;
while (ins.available() > 0) {
buf[pos++] = (byte)ins.read();
}
ins.close();
IsoMessage iso = mf.parseMessage(buf, mf.getIsoHeader(0x210).length());
assert iso.getType() == 0x210;
}
}
View File
+91
View File
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE j8583-config PUBLIC "-//J8583//DTD CONFIG 1.0//EN"
"http://j8583.sourceforge.net/j8583.dtd">
<j8583-config>
<!-- This is a test config file -->
<!-- These are the ISO headers to be prepended to the message types specified -->
<header type="0200">ISO015000050</header>
<header type="0210">ISO015000055</header>
<header type="0400">ISO015000050</header>
<header type="0410">ISO015000055</header>
<header type="0800">ISO015000015</header>
<header type="0810">ISO015000015</header>
<!-- The client example uses this to create requests -->
<template type="0200">
<field num="3" type="NUMERIC" length="6">650000</field>
<field num="32" type="LLVAR">456</field>
<field num="35" type="LLVAR">4591700012340000=</field>
<field num="43" type="ALPHA" length="40">SOLABTEST TEST-3 DF MX</field>
<field num="48" type="LLLVAR">Life, the Universe, and Everything|42</field>
<field num="49" type="ALPHA" length="3">484</field>
<field num="60" type="LLLVAR">B456PRO1+000</field>
<field num="61" type="LLLVAR"> 1234P vamos a meter mas de 90 caracteres en este campo para comprobar si hay algun error en el parseo del mismo. Esta definido como un LLLVAR aqui por lo tanto esto debe caber sin problemas; las guias de parseo de 200 y 210 tienen LLLVAR en campo 61 tambien.</field>
<field num="100" type="LLVAR">999</field>
<field num="102" type="LLVAR">ABCD</field>
</template>
<!-- The server example uses this to create the responses
we only need a few fields because the rest are copied from the request -->
<template type="0210">
<field num="60" type="LLLVAR">Fixed data from template</field>
<field num="70" type="ALPHA" length="3">ABC</field>
<field num="90" type="ALPHA" length="42">Field of length 42</field>
<field num="100" type="LLVAR">Fixed field</field>
<field num="102" type="LLVAR">Another fixed field</field>
<field num="126" type="LLLVAR">...and yet another fixed field.</field>
</template>
<!-- The server example uses this to read the requests -->
<parse type="0200">
<field num="3" type="NUMERIC" length="6" />
<field num="4" type="AMOUNT" />
<field num="7" type="DATE10" />
<field num="11" type="NUMERIC" length="6" />
<field num="12" type="TIME" />
<field num="13" type="DATE4" />
<field num="15" type="DATE4" />
<field num="17" type="DATE_EXP" />
<field num="32" type="LLVAR" />
<field num="35" type="LLVAR" />
<field num="37" type="NUMERIC" length="12" />
<field num="41" type="ALPHA" length="16" />
<field num="43" type="ALPHA" length="40" />
<field num="48" type="LLLVAR" />
<field num="49" type="ALPHA" length="3" />
<field num="60" type="LLLVAR" />
<field num="61" type="LLLVAR" />
<field num="100" type="LLVAR" />
<field num="102" type="LLVAR" />
</parse>
<!-- The client example uses this to read the responses -->
<parse type="0210">
<field num="3" type="NUMERIC" length="6" />
<field num="4" type="AMOUNT" />
<field num="7" type="DATE10" />
<field num="11" type="NUMERIC" length="6" />
<field num="12" type="TIME" />
<field num="13" type="DATE4" />
<field num="15" type="DATE4" />
<field num="17" type="DATE_EXP" />
<field num="32" type="LLVAR" />
<field num="35" type="LLVAR" />
<field num="37" type="NUMERIC" length="12" />
<field num="38" type="NUMERIC" length="6" />
<field num="39" type="NUMERIC" length="2" />
<field num="41" type="ALPHA" length="16" />
<field num="43" type="ALPHA" length="40" />
<field num="48" type="LLLVAR" />
<field num="49" type="ALPHA" length="3" />
<field num="60" type="LLLVAR" />
<field num="61" type="LLLVAR" />
<field num="70" type="ALPHA" length="3" />
<field num="90" type="ALPHA" length="42" />
<field num="100" type="LLVAR" />
<field num="102" type="LLVAR" />
<field num="126" type="LLLVAR" />
</parse>
</j8583-config>
+2
View File
@@ -0,0 +1,2 @@
ISO0250000550210B23A80012EA080180000000014000004650000000000003000042813054746877112594604280428081103123173766123456123456=00123442579114472300614209027600TESTSOLAB TEST-3 DF MX484012B123PRO1+000013 0000P0312304ABCD040ABCD8123477547