From a4a21551b389d3f0338c61ded34d0c4f40b01b9d Mon Sep 17 00:00:00 2001 From: chochos Date: Wed, 13 Jul 2011 16:33:37 +0000 Subject: [PATCH] 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 --- build.gradle | 2 +- .../parse/AlphaNumericFieldParseInfo.java | 5 +- .../solab/iso8583/parse/AlphaParseInfo.java | 5 +- .../solab/iso8583/parse/AmountParseInfo.java | 4 +- .../solab/iso8583/parse/BinaryParseInfo.java | 11 ++-- .../solab/iso8583/parse/Date10ParseInfo.java | 28 +++++---- .../solab/iso8583/parse/Date4ParseInfo.java | 14 ++--- .../solab/iso8583/parse/DateExpParseInfo.java | 6 +- .../solab/iso8583/parse/FieldParseInfo.java | 4 +- .../solab/iso8583/parse/LlbinParseInfo.java | 12 ++-- .../solab/iso8583/parse/LllbinParseInfo.java | 12 ++-- .../solab/iso8583/parse/LllvarParseInfo.java | 12 ++-- .../solab/iso8583/parse/LlvarParseInfo.java | 14 +++-- .../solab/iso8583/parse/NumericParseInfo.java | 2 +- .../solab/iso8583/parse/TimeParseInfo.java | 5 +- src/test/java/j8583/TestDates.java | 60 +++++++++++++++++++ 16 files changed, 131 insertions(+), 65 deletions(-) create mode 100644 src/test/java/j8583/TestDates.java diff --git a/build.gradle b/build.gradle index 523bd6a..a5ddffd 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java b/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java index 8fc92b5..c58d968 100644 --- a/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java @@ -35,7 +35,7 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo { super(t, len); } - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, _v, length, null); } else { - IsoValue v = new IsoValue(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(type, _v, length, null); } diff --git a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java index 1887cb5..6f2d1c1 100644 --- a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java @@ -35,12 +35,13 @@ public class AlphaParseInfo extends AlphaNumericFieldParseInfo { super(IsoType.ALPHA, len); } - public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) + public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { if (custom == null) { return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); } else { - IsoValue v = new IsoValue(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(type, new String(buf, pos, length, getCharacterEncoding()), length, null); } diff --git a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java index a4a40f8..77c4e71 100644 --- a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java @@ -36,7 +36,7 @@ public class AmountParseInfo extends FieldParseInfo { super(IsoType.AMOUNT, 12); } - public IsoValue parse(byte[] buf, int pos, CustomField 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); } @@ -51,7 +51,7 @@ public class AmountParseInfo extends FieldParseInfo { } } - public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { char[] digits = new char[13]; digits[10] = '.'; int start = 0; diff --git a/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java b/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java index 71590f6..06b60d3 100644 --- a/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java @@ -38,7 +38,7 @@ public class BinaryParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, binval, binval.length, null); } else { - IsoValue v = new IsoValue(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(type, binval, binval.length, null); } @@ -60,14 +61,14 @@ public class BinaryParseInfo extends FieldParseInfo { } @Override - public IsoValue parseBinary(byte[] buf, int pos, - CustomField 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(type, _v, length, null); } else { - IsoValue v = new IsoValue(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(type, _v, length, null); } diff --git a/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java b/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java index f07eb5f..6e6e0a1 100644 --- a/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java @@ -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 IsoValue parse(byte[] buf, int pos, CustomField custom) + public IsoValue 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(type, cal.getTime(), null); } @Override - public IsoValue parseBinary(byte[] buf, int pos, - CustomField custom) throws ParseException { + public IsoValue 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(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); + } + } + } diff --git a/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java b/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java index 5326083..3429d38 100644 --- a/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java @@ -37,8 +37,7 @@ public class Date4ParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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); } @@ -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(type, cal.getTime(), null); } @Override - public IsoValue parseBinary(byte[] buf, int pos, - CustomField custom) throws ParseException { + public IsoValue 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(type, cal.getTime(), null); } diff --git a/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java b/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java index 81591e0..e329ed8 100644 --- a/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java @@ -37,8 +37,7 @@ public class DateExpParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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); } @@ -58,8 +57,7 @@ public class DateExpParseInfo extends FieldParseInfo { } @Override - public IsoValue parseBinary(byte[] buf, int pos, - CustomField custom) throws ParseException { + public IsoValue 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++) { diff --git a/src/main/java/com/solab/iso8583/parse/FieldParseInfo.java b/src/main/java/com/solab/iso8583/parse/FieldParseInfo.java index 68e078e..8843b30 100644 --- a/src/main/java/com/solab/iso8583/parse/FieldParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/FieldParseInfo.java @@ -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 IsoValue parse(byte[] buf, int pos, CustomField 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 IsoValue parseBinary(byte[] buf, int pos, CustomField 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. */ diff --git a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java index 5eab1be..0990e26 100644 --- a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java @@ -37,8 +37,7 @@ public class LlbinParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, binval, binval.length, null); } else { - IsoValue v = new IsoValue(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(type, binval, binval.length, null); @@ -63,8 +63,7 @@ public class LlbinParseInfo extends FieldParseInfo { } @Override - public IsoValue parseBinary(byte[] buf, int pos, - CustomField 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(type, _v, null); } else { - IsoValue v = new IsoValue(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(type, _v, null); } diff --git a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java index 525364d..07a60d5 100644 --- a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java @@ -37,8 +37,7 @@ public class LllbinParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, binval, binval.length, null); } else { - IsoValue v = new IsoValue(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 IsoValue parseBinary(byte[] buf, int pos, - CustomField 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(type, _v, null); } else { - IsoValue v = new IsoValue(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(type, _v, null); } diff --git a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java index 2026fcf..45b3ccd 100644 --- a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java @@ -36,7 +36,7 @@ public class LllvarParseInfo extends FieldParseInfo { super(IsoType.LLLVAR, 0); } - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, _v, length, null); } else { - IsoValue v = new IsoValue(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(type, _v, length, null); @@ -71,8 +72,8 @@ public class LllvarParseInfo extends FieldParseInfo { } } - public IsoValue parseBinary(byte[] buf, int pos, CustomField 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(type, new String(buf, pos + 2, length, getCharacterEncoding()), null); } else { - IsoValue v = new IsoValue(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(type, new String(buf, pos + 2, length, getCharacterEncoding()), null); } diff --git a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java index 157863c..93d3b9d 100644 --- a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java @@ -35,8 +35,8 @@ public class LlvarParseInfo extends FieldParseInfo { super(IsoType.LLVAR, 0); } - public IsoValue parse(byte[] buf, int pos, CustomField 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(type, _v, length, null); } else { - IsoValue v = new IsoValue(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(type, _v, length, null); } @@ -65,8 +66,8 @@ public class LlvarParseInfo extends FieldParseInfo { } } - public IsoValue parseBinary(byte[] buf, int pos, CustomField 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(type, new String(buf, pos + 1, length, getCharacterEncoding()), null); } else { - IsoValue v = new IsoValue(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(type, new String(buf, pos + 1, length, getCharacterEncoding()), null); } diff --git a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java index 8eb6805..8824b2f 100644 --- a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java @@ -35,7 +35,7 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo { super(IsoType.NUMERIC, len); } - public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { //A long covers up to 18 digits if (length < 19) { long l = 0; diff --git a/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java b/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java index 726ca2c..d1c7ae7 100644 --- a/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java @@ -38,8 +38,7 @@ public class TimeParseInfo extends FieldParseInfo { } @Override - public IsoValue parse(byte[] buf, int pos, CustomField 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); } @@ -54,7 +53,7 @@ public class TimeParseInfo extends FieldParseInfo { } @Override - public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + public IsoValue 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++) { diff --git a/src/test/java/j8583/TestDates.java b/src/test/java/j8583/TestDates.java new file mode 100644 index 0000000..10aef99 --- /dev/null +++ b/src/test/java/j8583/TestDates.java @@ -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 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 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 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 bin = new Date10ParseInfo().parseBinary(bout.toByteArray(), 0, null); + Assert.assertEquals(comp.getValue().getTime(), bin.getValue().getTime()); + } + +}