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:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user