Added unit tests for binaries (in progress). Added BINARY, LLBIN and LLLBIN to the DTD. Added a message template and a parsing template to the test config.
Fixed some problems with the stored values in IsoValues. git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@64 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
@@ -76,7 +76,11 @@ public class IsoValue<T> implements Cloneable {
|
||||
}
|
||||
} else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) {
|
||||
if (custom == null) {
|
||||
length = ((byte[])value).length;
|
||||
if (value instanceof byte[]) {
|
||||
length = ((byte[])value).length;
|
||||
} else {
|
||||
length = value.toString().length() / 2 + (value.toString().length() % 2);
|
||||
}
|
||||
} else {
|
||||
//TODO special encoder, NO strings
|
||||
String enc = custom.encodeField(value);
|
||||
@@ -177,9 +181,17 @@ public class IsoValue<T> implements Cloneable {
|
||||
} else if (value instanceof Date) {
|
||||
return type.format((Date)value);
|
||||
} else if (type == IsoType.BINARY) {
|
||||
return type.format(encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value), length * 2);
|
||||
if (value instanceof byte[]) {
|
||||
return type.format(encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value), length * 2);
|
||||
} else {
|
||||
return type.format(encoder == null ? value.toString() : encoder.encodeField(value), length);
|
||||
}
|
||||
} else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) {
|
||||
return encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value);
|
||||
if (value instanceof byte[]) {
|
||||
return encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value);
|
||||
} else {
|
||||
return encoder == null ? value.toString() : encoder.encodeField(value);
|
||||
}
|
||||
}
|
||||
return encoder == null ? value.toString() : encoder.encodeField(value);
|
||||
}
|
||||
@@ -273,7 +285,11 @@ public class IsoValue<T> implements Cloneable {
|
||||
}
|
||||
}
|
||||
if (binary && (type == IsoType.BINARY || type == IsoType.LLBIN || type == IsoType.LLLBIN)) {
|
||||
outs.write((byte[])value);
|
||||
if (value instanceof byte[]) {
|
||||
outs.write((byte[])value);
|
||||
} else {
|
||||
outs.write(HexCodec.hexDecode(value.toString()));
|
||||
}
|
||||
} else {
|
||||
//Just write the value as text
|
||||
outs.write(toString().getBytes());
|
||||
|
||||
@@ -12,4 +12,4 @@
|
||||
<!ELEMENT field ( #PCDATA ) >
|
||||
<!ATTLIST field num NMTOKEN #REQUIRED >
|
||||
<!ATTLIST field length NMTOKEN #IMPLIED >
|
||||
<!ATTLIST field type ( ALPHA | NUMERIC | AMOUNT | DATE10 | DATE4 | DATE_EXP | TIME | LLVAR | LLLVAR ) #REQUIRED >
|
||||
<!ATTLIST field type ( ALPHA | NUMERIC | AMOUNT | DATE10 | DATE4 | DATE_EXP | TIME | LLVAR | LLLVAR | BINARY | LLBIN | LLLBIN ) #REQUIRED >
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package j8583;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.solab.iso8583.IsoMessage;
|
||||
import com.solab.iso8583.MessageFactory;
|
||||
import com.solab.iso8583.util.HexCodec;
|
||||
|
||||
/** Test binary message encoding and binary fields. */
|
||||
public class TestBinaries {
|
||||
|
||||
private MessageFactory mfactAscii = new MessageFactory();
|
||||
private MessageFactory mfactBin = new MessageFactory();
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
mfactAscii.setConfigPath("config.xml");
|
||||
mfactBin.setConfigPath("config.xml");
|
||||
mfactAscii.setAssignDate(true);
|
||||
mfactBin.setAssignDate(true);
|
||||
mfactBin.setUseBinaryMessages(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessages() {
|
||||
//Create a message with both factories
|
||||
IsoMessage ascii = mfactAscii.newMessage(0x600);
|
||||
IsoMessage bin = mfactBin.newMessage(0x600);
|
||||
//HEXencode the binary message, headers should be similar to the ASCII version
|
||||
String hexBin = HexCodec.hexEncode(bin.writeData());
|
||||
String hexAscii = new String(ascii.writeData());
|
||||
System.out.printf("bin: %s%nascii: %s%n", hexBin, hexAscii);
|
||||
Assert.assertEquals("0600", hexBin.substring(0, 4));
|
||||
//Should be the same up to the trace (first 64 chars)
|
||||
Assert.assertEquals(hexAscii.substring(0, 64), hexBin.substring(0, 64));
|
||||
//Parse both messages
|
||||
//Compare values, should be the same
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,15 @@
|
||||
<field num="126" type="LLLVAR">...and yet another fixed field.</field>
|
||||
</template>
|
||||
|
||||
<template type="0600">
|
||||
<field num="4" type="AMOUNT">000000123400</field>
|
||||
<field num="11" type="NUMERIC" length="6">123</field>
|
||||
<field num="41" type="BINARY" length="8">abcdef</field>
|
||||
<field num="43" type="ALPHA" length="40">Field of length 40</field>
|
||||
<field num="62" type="LLBIN">0123456789abcdef</field>
|
||||
<field num="102" type="LLLVAR">Field of type LLLVAR</field>
|
||||
</template>
|
||||
|
||||
<!-- The server example uses this to read the requests -->
|
||||
<parse type="0200">
|
||||
<field num="3" type="NUMERIC" length="6" />
|
||||
@@ -88,4 +97,15 @@
|
||||
<field num="126" type="LLLVAR" />
|
||||
</parse>
|
||||
|
||||
<!-- this is for binary tests (message encoding AND fields) -->
|
||||
<parse type="0600">
|
||||
<field num="4" type="AMOUNT" />
|
||||
<field num="7" type="DATE10" />
|
||||
<field num="11" type="NUMERIC" length="6" />
|
||||
<field num="41" type="BINARY" length="8" />
|
||||
<field num="43" type="ALPHA" length="40" />
|
||||
<field num="62" type="LLBIN" />
|
||||
<field num="102" type="LLLVAR" />
|
||||
</parse>
|
||||
|
||||
</j8583-config>
|
||||
|
||||
Reference in New Issue
Block a user