Added future tolerance to Date10 and Date4 parsers; simplified the use of generics in all the FieldParseInfo subclasses. Added a test for the future tolerance of date parsers.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@85 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2011-07-13 16:33:37 +00:00
parent 725d52ec4e
commit a4a21551b3
16 changed files with 131 additions and 65 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
apply plugin:'java'
defaultTasks 'build'
version='1.5.1'
version='1.5.2'
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.'
repositories {
@@ -35,7 +35,7 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo {
super(t, len);
}
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException, UnsupportedEncodingException {
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -50,7 +50,8 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<String>(type, _v, length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(_v), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(_v), length, custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, _v, length, null);
}
@@ -35,12 +35,13 @@ public class AlphaParseInfo extends AlphaNumericFieldParseInfo {
super(IsoType.ALPHA, len);
}
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom)
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null);
}
@@ -36,7 +36,7 @@ public class AmountParseInfo extends FieldParseInfo {
super(IsoType.AMOUNT, 12);
}
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
public IsoValue<BigDecimal> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -51,7 +51,7 @@ public class AmountParseInfo extends FieldParseInfo {
}
}
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
public IsoValue<BigDecimal> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
char[] digits = new char[13];
digits[10] = '.';
int start = 0;
@@ -38,7 +38,7 @@ public class BinaryParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
@@ -51,7 +51,8 @@ public class BinaryParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos, length*2, getCharacterEncoding())), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos, length*2, getCharacterEncoding())), length, custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
}
@@ -60,14 +61,14 @@ public class BinaryParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
byte[] _v = new byte[length];
System.arraycopy(buf, pos, _v, 0, length);
if (custom == null) {
return new IsoValue<byte[]>(type, _v, length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(HexCodec.hexEncode(_v)), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(HexCodec.hexEncode(_v)), length, custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, _v, length, null);
}
@@ -32,13 +32,17 @@ import com.solab.iso8583.IsoValue;
*/
public class Date10ParseInfo extends FieldParseInfo {
private static final long FUTURE_TOLERANCE;
static {
FUTURE_TOLERANCE = Long.parseLong(System.getProperty("j8583.future.tolerance", "900000"));
}
public Date10ParseInfo() {
super(IsoType.DATE10, 10);
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom)
throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
@@ -55,15 +59,12 @@ public class Date10ParseInfo extends FieldParseInfo {
cal.set(Calendar.HOUR_OF_DAY, ((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
cal.set(Calendar.MINUTE, ((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48);
cal.set(Calendar.SECOND, ((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
adjustWithFutureTolerance(cal);
return new IsoValue<Date>(type, cal.getTime(), null);
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
int[] tens = new int[5];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
@@ -78,10 +79,17 @@ public class Date10ParseInfo extends FieldParseInfo {
cal.set(Calendar.HOUR_OF_DAY, tens[2]);
cal.set(Calendar.MINUTE, tens[3]);
cal.set(Calendar.SECOND, tens[4]);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
adjustWithFutureTolerance(cal);
return new IsoValue<Date>(type, cal.getTime(), null);
}
public static void adjustWithFutureTolerance(Calendar cal) {
//We need to handle a small tolerance into the future (a couple of minutes)
long now = System.currentTimeMillis();
long then = cal.getTimeInMillis();
if (then > now && then-now > FUTURE_TOLERANCE) {
cal.add(Calendar.YEAR, -1);
}
}
}
@@ -37,8 +37,7 @@ public class Date4ParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -52,15 +51,12 @@ public class Date4ParseInfo extends FieldParseInfo {
//Set the month in the date
cal.set(Calendar.MONTH, ((buf[pos] - 48) * 10) + buf[pos + 1] - 49);
cal.set(Calendar.DATE, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
Date10ParseInfo.adjustWithFutureTolerance(cal);
return new IsoValue<Date>(type, cal.getTime(), null);
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
int[] tens = new int[2];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
@@ -73,9 +69,7 @@ public class Date4ParseInfo extends FieldParseInfo {
//Set the month in the date
cal.set(Calendar.MONTH, tens[0] - 1);
cal.set(Calendar.DATE, tens[1]);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
Date10ParseInfo.adjustWithFutureTolerance(cal);
return new IsoValue<Date>(type, cal.getTime(), null);
}
@@ -37,8 +37,7 @@ public class DateExpParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -58,8 +57,7 @@ public class DateExpParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
int[] tens = new int[2];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
@@ -66,12 +66,12 @@ public abstract class FieldParseInfo {
/** Parses the character data from the buffer and returns the
* IsoValue with the correct data type in it. */
public abstract <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
public abstract IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException;
/** Parses binary data from the buffer, creating and returning an IsoValue of the configured
* type and length. */
public abstract <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom)
public abstract IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException;
/** Returns a new FieldParseInfo instance that can parse the specified type. */
@@ -37,8 +37,7 @@ public class LlbinParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -53,7 +52,8 @@ public class LlbinParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(
new String(buf, pos + 2, length)), binval.length, custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
@@ -63,8 +63,7 @@ public class LlbinParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f);
if (length < 0) {
throw new ParseException(String.format("Invalid LLBIN length %d pos %d", length, pos), pos);
@@ -77,7 +76,8 @@ public class LlbinParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<byte[]>(type, _v, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(HexCodec.hexEncode(_v)), custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(HexCodec.hexEncode(_v)), custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, _v, null);
}
@@ -37,8 +37,7 @@ public class LllbinParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -56,7 +55,8 @@ public class LllbinParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(
length == 0 ? "" : new String(buf, pos + 3, length)), length, custom);
if (v.getValue() == null) {
//problems decoding? return the string
@@ -67,8 +67,7 @@ public class LllbinParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
if (length < 0) {
throw new ParseException(String.format("Invalid LLLBIN length %d pos %d", length, pos), pos);
@@ -81,7 +80,8 @@ public class LllbinParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<byte[]>(type, _v, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(HexCodec.hexEncode(_v)), custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(HexCodec.hexEncode(_v)), custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, _v, null);
}
@@ -36,7 +36,7 @@ public class LllvarParseInfo extends FieldParseInfo {
super(IsoType.LLLVAR, 0);
}
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
@@ -62,7 +62,8 @@ public class LllvarParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<String>(type, _v, length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(_v), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(_v), length, custom);
if (v.getValue() == null) {
//problems decoding? return the string
return new IsoValue<String>(type, _v, length, null);
@@ -71,8 +72,8 @@ public class LllvarParseInfo extends FieldParseInfo {
}
}
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom)
throws ParseException, UnsupportedEncodingException {
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
if (length < 0) {
throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos);
@@ -83,7 +84,8 @@ public class LllvarParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length, getCharacterEncoding()), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 2, length, getCharacterEncoding())), custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos + 2, length, getCharacterEncoding())), custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length, getCharacterEncoding()), null);
}
@@ -35,8 +35,8 @@ public class LlvarParseInfo extends FieldParseInfo {
super(IsoType.LLVAR, 0);
}
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException, UnsupportedEncodingException {
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -57,7 +57,8 @@ public class LlvarParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<String>(type, _v, length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(_v), length, custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(_v), length, custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, _v, length, null);
}
@@ -65,8 +66,8 @@ public class LlvarParseInfo extends FieldParseInfo {
}
}
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom)
throws ParseException, UnsupportedEncodingException {
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f);
if (length < 0) {
@@ -78,7 +79,8 @@ public class LlvarParseInfo extends FieldParseInfo {
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 1, length, getCharacterEncoding()), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 1, length, getCharacterEncoding())), custom);
@SuppressWarnings("unchecked")
IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos + 1, length, getCharacterEncoding())), custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos + 1, length, getCharacterEncoding()), null);
}
@@ -35,7 +35,7 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo {
super(IsoType.NUMERIC, len);
}
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
public IsoValue<Number> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
//A long covers up to 18 digits
if (length < 19) {
long l = 0;
@@ -38,8 +38,7 @@ public class TimeParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
@@ -54,7 +53,7 @@ public class TimeParseInfo extends FieldParseInfo {
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
int[] tens = new int[3];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
+60
View File
@@ -0,0 +1,60 @@
package j8583;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import org.junit.*;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
import com.solab.iso8583.MessageFactory;
import com.solab.iso8583.parse.Date10ParseInfo;
import com.solab.iso8583.parse.Date4ParseInfo;
/** Test that the dates are formatted and parsed correctly.
*
* @author Enrique Zamudio
*/
public class TestDates {
private MessageFactory mf;
@Before
public void init() throws IOException {
mf = new MessageFactory();
mf.setCharacterEncoding("UTF-8");
mf.setCustomField(48, new CustomField48());
mf.setConfigPath("config.xml");
}
@Test
public void testDate4FutureTolerance() throws ParseException, IOException {
long now = System.currentTimeMillis();
long today = now - (now % 86400000l);
Date soon = new Date(now + 50000);
byte[] buf = IsoType.DATE4.format(soon).getBytes();
IsoValue<Date> comp = new Date4ParseInfo().parse(buf, 0, null);
assert comp.getValue().after(new Date(today));
//Now with the binary
ByteArrayOutputStream bout = new ByteArrayOutputStream();
comp.write(bout, true);
IsoValue<Date> bin = new Date4ParseInfo().parseBinary(bout.toByteArray(), 0, null);
Assert.assertEquals(comp.getValue().getTime(), bin.getValue().getTime());
}
@Test
public void testDate10FutureTolerance() throws ParseException, IOException {
Date soon = new Date(System.currentTimeMillis() + 50000);
byte[] buf = IsoType.DATE10.format(soon).getBytes();
IsoValue<Date> comp = new Date10ParseInfo().parse(buf, 0, null);
assert comp.getValue().after(new Date());
//Now with the binary
ByteArrayOutputStream bout = new ByteArrayOutputStream();
comp.write(bout, true);
IsoValue<Date> bin = new Date10ParseInfo().parseBinary(bout.toByteArray(), 0, null);
Assert.assertEquals(comp.getValue().getTime(), bin.getValue().getTime());
}
}