Fixed missing stuff from CustomField48 (comparison, encoding). TestIsoMessage runs fine now.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@55 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2010-11-26 16:47:38 +00:00
parent cd300d0b28
commit 2a7d4063e2
2 changed files with 51 additions and 5 deletions
+26 -4
View File
@@ -3,6 +3,7 @@ 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.
* It's used to test the encoding and decoding of custom fields by the message factory.
*
* @author Enrique Zamudio
*/
@@ -20,7 +21,7 @@ public class CustomField48 implements CustomField<CustomField48> {
public void setValue2(int value) {
v2 = value;
}
public int setValue2() {
public int getValue2() {
return v2;
}
@@ -46,12 +47,33 @@ public class CustomField48 implements CustomField<CustomField48> {
@Override
public String encodeField(CustomField48 value) {
StringBuilder sb = new StringBuilder();
if (v1 != null) {
sb.append(v1);
if (value.getValue1() != null) {
sb.append(value.getValue1());
}
sb.append('|');
sb.append(v2);
sb.append(value.getValue2());
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof CustomField48)) {
return false;
}
CustomField48 other = (CustomField48)obj;
if (other.getValue2() == v2) {
if (other.getValue1() == null) {
return v1 == null;
} else {
return other.getValue1().equals(v1);
}
}
return false;
}
@Override
public int hashCode() {
return (v1 == null ? 0 : v1.hashCode()) | v2;
}
}
+25 -1
View File
@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -31,7 +32,7 @@ public class TestIsoMessage {
public void testCreation() {
IsoMessage iso = mf.newMessage(0x200);
assert iso.getType() == 0x200;
assert iso.getField(3).getType() == IsoType.NUMERIC && "650000".equals((String)iso.getObjectValue(3));
assert iso.getField(3).getType() == IsoType.NUMERIC && "650000".equals(iso.getObjectValue(3));
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;
@@ -58,6 +59,23 @@ public class TestIsoMessage {
}
}
@Test
public void testEncoding() throws ParseException {
IsoMessage m1 = mf.newMessage(0x200);
byte[] buf = m1.writeData();
IsoMessage m2 = mf.parseMessage(buf, mf.getIsoHeader(0x200).length());
assert m2.getType() == m1.getType();
for (int i = 2; i < 128; i++) {
//Either both have the field or neither have it
if (m1.hasField(i) && m2.hasField(i)) {
Assert.assertEquals(m1.getField(i).getType(), m2.getField(i).getType());
Assert.assertEquals(m1.getObjectValue(i), m2.getObjectValue(i));
} else {
assert !m1.hasField(i) && !m2.hasField(i);
}
}
}
/** Parses a message from a file and checks the fields. */
@Test
public void testParsing() throws IOException, ParseException {
@@ -70,6 +88,12 @@ public class TestIsoMessage {
ins.close();
IsoMessage iso = mf.parseMessage(buf, mf.getIsoHeader(0x210).length());
assert iso.getType() == 0x210;
byte[] b2 = iso.writeData();
//Remove leftover newline and stuff from the original buffer
byte[] b3 = new byte[b2.length];
System.arraycopy(buf, 0, b3, 0, b3.length);
Assert.assertArrayEquals(b3, b2);
}
}