From ff73250190927e71b22326df45c3b9b55cbe84bb Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Wed, 24 May 2023 12:34:27 -0400 Subject: [PATCH 1/7] add test and method --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 63 +++++++++++++++++++ .../test/java/org/jpos/iso/ISOUtilTest.java | 25 +++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index d92f9264c7..038228ebc3 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -32,6 +32,10 @@ import java.util.StringTokenizer; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.regex.Pattern; +import java.math.BigInteger; +import java.util.regex.Pattern; +import java.util.regex.Matcher; /** * various functions needed to pack/unpack ISO-8583 fields @@ -178,6 +182,65 @@ public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); } + + + /** metodo para comprobar si mi parametro es o no un hexadecimal */ + + /* public static boolean isHexadecimal(byte[] bytes) { + for (byte b : bytes) { + int unsignedByte = b & 0xFF; + if (!((unsignedByte >= 0x30 && unsignedByte <= 0x39) || // Dígitos 0-9 + (unsignedByte >= 0x41 && unsignedByte <= 0x46) || // Letras A-F + (unsignedByte >= 0x61 && unsignedByte <= 0x66))) { // Letras a-f + return false; + } + } + return true; + }*/ + + public static boolean isHexadecimal(byte[] bytes) { + + + String patronHexadecimal = "^(0x)?[0-9A-Fa-f]+$"; + Pattern patron = Pattern.compile(patronHexadecimal); + + + //String cadena = "0xABCD"; // La cadena que deseas verificar + for(byte b:bytes ) + { + Matcher matcher = patron.matcher(String.valueOf(b)); + System.out.println(String.valueOf(b)); + + + if (matcher.matches()) { + System.out.println("La cadena es hexadecimal."); + return true; + } else { + System.out.println("La cadena no es hexadecimal."); + return false; + } + } + return false; + } + /* fin del metodo */ +/* metodo 2 */ + + public static void main (String[] args) { + byte[] valorByte = {(byte) 0xAB, (byte) 0xCD}; // El valor byte que deseas verificar + + isHexadecimal(valorByte); + if (isHexadecimal(valorByte)) + { + System.out.println("La cadena es hexadecimal."); + } + else + { + System.out.println("La cadena no es hexadecimal."); + } + } + + + /** * zeropads a long without throwing an ISOException (performs modulus operation) * diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index 921d624fe0..c8da06d764 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -43,13 +43,36 @@ import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; +import java.util.regex.Pattern; + -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class ISOUtilTest { final String lineSep = System.getProperty("line.separator"); + + @Test + public void TestHexadecimalValidHexadec () { + byte[] validHexBytes = {(byte) 0x1F}; + assertTrue(ISOUtil.isHexadecimal(validHexBytes)); + } + + @Test + public void testHex1IsDecimal2() { + byte[] validHexBytes = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF}; + assertFalse(ISOUtil.isHexadecimal(validHexBytes)); + } + + @Test + public void testHex1IsDecimal3() { + byte[] validHexBytes = null; + assertFalse(ISOUtil.isHexadecimal(validHexBytes)); + } + + + + @Test public void testAsciiToEbcdic() throws Throwable { byte[] a = new byte[0]; From 766ea7cf88b6a854028869995677478a921261eb Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Thu, 25 May 2023 14:01:44 -0400 Subject: [PATCH 2/7] add test and method --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index 038228ebc3..f196cfda40 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -186,7 +186,7 @@ public static String zeropad(String s, int len) throws ISOException { /** metodo para comprobar si mi parametro es o no un hexadecimal */ - /* public static boolean isHexadecimal(byte[] bytes) { + public static boolean isHexadecimal(byte[] bytes) { for (byte b : bytes) { int unsignedByte = b & 0xFF; if (!((unsignedByte >= 0x30 && unsignedByte <= 0x39) || // Dígitos 0-9 @@ -196,9 +196,9 @@ public static String zeropad(String s, int len) throws ISOException { } } return true; - }*/ + } - public static boolean isHexadecimal(byte[] bytes) { + /*public static boolean isHexadecimal(byte[] bytes) { String patronHexadecimal = "^(0x)?[0-9A-Fa-f]+$"; @@ -226,9 +226,11 @@ public static boolean isHexadecimal(byte[] bytes) { /* metodo 2 */ public static void main (String[] args) { - byte[] valorByte = {(byte) 0xAB, (byte) 0xCD}; // El valor byte que deseas verificar + byte[] valorByte = {(byte) 0x1F}; + //byte[] valorByte = {(byte) 0xAB, (byte) 0xCD}; // El valor byte que deseas verificar isHexadecimal(valorByte); + System.out.println(valorByte); if (isHexadecimal(valorByte)) { System.out.println("La cadena es hexadecimal."); From b3d18f071d617a0a7511c9005cc2e60c9e50c285 Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Fri, 26 May 2023 16:43:56 -0400 Subject: [PATCH 3/7] agg methods isHexadecimal2 and isDigitHexadecimal --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 71 +++++++------------ .../test/java/org/jpos/iso/ISOUtilTest.java | 15 ++++ 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index f196cfda40..67ff1dbf44 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -181,10 +181,6 @@ public static String trim (String s) { public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); } - - - - /** metodo para comprobar si mi parametro es o no un hexadecimal */ public static boolean isHexadecimal(byte[] bytes) { for (byte b : bytes) { @@ -197,52 +193,29 @@ public static boolean isHexadecimal(byte[] bytes) { } return true; } - - /*public static boolean isHexadecimal(byte[] bytes) { - - - String patronHexadecimal = "^(0x)?[0-9A-Fa-f]+$"; - Pattern patron = Pattern.compile(patronHexadecimal); - - //String cadena = "0xABCD"; // La cadena que deseas verificar - for(byte b:bytes ) - { - Matcher matcher = patron.matcher(String.valueOf(b)); - System.out.println(String.valueOf(b)); - - - if (matcher.matches()) { - System.out.println("La cadena es hexadecimal."); - return true; - } else { - System.out.println("La cadena no es hexadecimal."); - return false; - } + public static boolean isHexadecimal2(String texto) { + // Verificar si el texto está vacío o tiene una longitud impar + if (texto.isEmpty() || texto.length() % 2 != 0) { + return false; } - return false; - } - /* fin del metodo */ -/* metodo 2 */ - - public static void main (String[] args) { - byte[] valorByte = {(byte) 0x1F}; - //byte[] valorByte = {(byte) 0xAB, (byte) 0xCD}; // El valor byte que deseas verificar - - isHexadecimal(valorByte); - System.out.println(valorByte); - if (isHexadecimal(valorByte)) - { - System.out.println("La cadena es hexadecimal."); - } - else - { - System.out.println("La cadena no es hexadecimal."); + + // Verificar si cada carácter es un dígito hexadecimal válido + for (int i = 0; i < texto.length(); i++) { + char c = texto.charAt(i); + if (!isDigitHexadecimal(c)) { + throw new IllegalArgumentException("The string is not hexadecimal"); } } + + return true; + + } + public static boolean isDigitHexadecimal(char c) { + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); + } - /** * zeropads a long without throwing an ISOException (performs modulus operation) * @@ -745,7 +718,15 @@ public static byte[] hex2byte (byte[] b, int offset, int len) { * @return byte array */ public static byte[] hex2byte (String s) { - return hex2byte (s, CHARSET); + if (isHexadecimal2(s)) + { + return hex2byte (s, CHARSET); + } + else + { + return hex2byte (s, CHARSET);; + } + } /** * Converts a hex string into a byte array diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index c8da06d764..e9a225da5e 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -70,8 +70,23 @@ public void testHex1IsDecimal3() { assertFalse(ISOUtil.isHexadecimal(validHexBytes)); } + @Test + public void TestsHex1() { + String p = "1A2B3C"; + assertTrue(ISOUtil.isHexadecimal2(p)); + } + @Test + public void TestsHex2() { + String p = "1"; + assertFalse(ISOUtil.isHexadecimal2(p)); + } + @Test + public void TestsHex3() { + String p = null; + assertFalse(ISOUtil.isHexadecimal2(p)); + } @Test public void testAsciiToEbcdic() throws Throwable { From 67c059191d14672661b647401d30692d556da8b8 Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Mon, 29 May 2023 19:06:57 -0400 Subject: [PATCH 4/7] modify method hex2byte --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 46 +++++++------------ .../test/java/org/jpos/iso/ISOUtilTest.java | 8 ++-- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index 67ff1dbf44..8a7cc2f1cc 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -193,28 +193,6 @@ public static boolean isHexadecimal(byte[] bytes) { } return true; } - - public static boolean isHexadecimal2(String texto) { - // Verificar si el texto está vacío o tiene una longitud impar - if (texto.isEmpty() || texto.length() % 2 != 0) { - return false; - } - - // Verificar si cada carácter es un dígito hexadecimal válido - for (int i = 0; i < texto.length(); i++) { - char c = texto.charAt(i); - if (!isDigitHexadecimal(c)) { - throw new IllegalArgumentException("The string is not hexadecimal"); - } - } - - return true; - - } - - public static boolean isDigitHexadecimal(char c) { - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); - } /** * zeropads a long without throwing an ISOException (performs modulus operation) @@ -718,16 +696,26 @@ public static byte[] hex2byte (byte[] b, int offset, int len) { * @return byte array */ public static byte[] hex2byte (String s) { - if (isHexadecimal2(s)) + + if (s.isEmpty() || s.length() % 2 != 0) //verificar si el string es vacio { - return hex2byte (s, CHARSET); + throw new IllegalArgumentException("The string is empty or null"); } - else - { - return hex2byte (s, CHARSET);; - } - + // Verificar si cada carácter es un dígito hexadecimal válido + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!isDigitHexadecimal(c)) { + throw new IllegalArgumentException("The string is not hexadecimal"); + } + } + return hex2byte(s, CHARSET); + } + + public static boolean isDigitHexadecimal(char c) { + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); + } + /** * Converts a hex string into a byte array * @param s source string (with Hex representation) diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index e9a225da5e..0a119cec11 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -73,19 +73,19 @@ public void testHex1IsDecimal3() { @Test public void TestsHex1() { String p = "1A2B3C"; - assertTrue(ISOUtil.isHexadecimal2(p)); + ISOUtil.hex2byte(p); } @Test public void TestsHex2() { String p = "1"; - assertFalse(ISOUtil.isHexadecimal2(p)); + ISOUtil.hex2byte(p); } @Test public void TestsHex3() { String p = null; - assertFalse(ISOUtil.isHexadecimal2(p)); + ISOUtil.hex2byte(p); } @Test @@ -98,7 +98,7 @@ public void testAsciiToEbcdic() throws Throwable { @Test public void testAsciiToEbcdic1() throws Throwable { byte[] a = new byte[1]; - byte[] result = ISOUtil.asciiToEbcdic(a); + byte[] result = c.asciiToEbcdic(a); assertEquals(1, result.length, "result.length"); assertEquals((byte) 0, result[0], "result[0]"); } From dab762f6acd17e8b188c1be7a927b0afd07d0153 Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Tue, 30 May 2023 10:37:13 -0400 Subject: [PATCH 5/7] agg test --- .../src/test/java/org/jpos/iso/ISOUtilTest.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index 0a119cec11..d6b00a26bc 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -52,23 +52,6 @@ public class ISOUtilTest { final String lineSep = System.getProperty("line.separator"); - @Test - public void TestHexadecimalValidHexadec () { - byte[] validHexBytes = {(byte) 0x1F}; - assertTrue(ISOUtil.isHexadecimal(validHexBytes)); - } - - @Test - public void testHex1IsDecimal2() { - byte[] validHexBytes = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF}; - assertFalse(ISOUtil.isHexadecimal(validHexBytes)); - } - - @Test - public void testHex1IsDecimal3() { - byte[] validHexBytes = null; - assertFalse(ISOUtil.isHexadecimal(validHexBytes)); - } @Test public void TestsHex1() { From d245d6b8ff82e1cdcec15494e78f8f950a3abc34 Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Wed, 31 May 2023 19:25:58 -0400 Subject: [PATCH 6/7] agg test ValidInput and InvalidInput --- .../test/java/org/jpos/iso/ISOUtilTest.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index d6b00a26bc..eb6e7d19c1 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -54,23 +54,43 @@ public class ISOUtilTest { @Test - public void TestsHex1() { + public void TestConvertToHexValidInput() { String p = "1A2B3C"; ISOUtil.hex2byte(p); + + Exception exception = assertThrows(RuntimeException.class, () -> { + Integer.parseInt("1a"); + }); + + String expectedMessage = "For input string"; + String actualMessage = exception.getMessage(); + + assertFalse(actualMessage.contains(expectedMessage)); + } @Test - public void TestsHex2() { - String p = "1"; - ISOUtil.hex2byte(p); + public void testIsHexadecimal_ValidInput_ThrowsExceptionWithMessage() { + + Exception exception = assertThrows(RuntimeException.class, () -> { + ISOUtil.hex2byte("1A2B3C"); + }, "Not hex"); + + assertEquals("Not hex", exception.getMessage()); } @Test - public void TestsHex3() { - String p = null; - ISOUtil.hex2byte(p); + public void testIsHexadecimal_InvalidInput_ThrowsExceptionWithMessage() { + + Exception exception = assertThrows(RuntimeException.class, () -> { + ISOUtil.hex2byte("1111"); + }, "Not hex"); + //ISOUtil.hex2byte("1"); + + assertEquals("Not hex", exception.getMessage()); } + @Test public void testAsciiToEbcdic() throws Throwable { byte[] a = new byte[0]; From 95a23dc0cdb9f760ecdb04b8c078852e0365c189 Mon Sep 17 00:00:00 2001 From: JuliaRecalde Date: Wed, 31 May 2023 20:07:06 -0400 Subject: [PATCH 7/7] modify test valid and invalid modify test ValidInput delete isHexadecimal modify test validinput modify test modify test --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 14 +------ .../test/java/org/jpos/iso/ISOUtilTest.java | 41 ++++++------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index 8a7cc2f1cc..52349c7c25 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -182,18 +182,6 @@ public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); } - public static boolean isHexadecimal(byte[] bytes) { - for (byte b : bytes) { - int unsignedByte = b & 0xFF; - if (!((unsignedByte >= 0x30 && unsignedByte <= 0x39) || // Dígitos 0-9 - (unsignedByte >= 0x41 && unsignedByte <= 0x46) || // Letras A-F - (unsignedByte >= 0x61 && unsignedByte <= 0x66))) { // Letras a-f - return false; - } - } - return true; - } - /** * zeropads a long without throwing an ISOException (performs modulus operation) * @@ -699,7 +687,7 @@ public static byte[] hex2byte (String s) { if (s.isEmpty() || s.length() % 2 != 0) //verificar si el string es vacio { - throw new IllegalArgumentException("The string is empty or null"); + throw new IllegalArgumentException("The string is empty or null, invalid hexadecimal"); } // Verificar si cada carácter es un dígito hexadecimal válido for (int i = 0; i < s.length(); i++) { diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index eb6e7d19c1..e1b124344f 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -34,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertArrayEquals; import java.math.BigDecimal; import java.math.MathContext; @@ -45,47 +46,29 @@ import java.util.TimeZone; import java.util.regex.Pattern; - +import org.fest.assertions.Assert; import org.junit.jupiter.api.Test; +import org.junit.Assert; +import java.util.Arrays; public class ISOUtilTest { final String lineSep = System.getProperty("line.separator"); - - @Test - public void TestConvertToHexValidInput() { - String p = "1A2B3C"; - ISOUtil.hex2byte(p); - - Exception exception = assertThrows(RuntimeException.class, () -> { - Integer.parseInt("1a"); - }); - - String expectedMessage = "For input string"; - String actualMessage = exception.getMessage(); - assertFalse(actualMessage.contains(expectedMessage)); - - } - @Test - public void testIsHexadecimal_ValidInput_ThrowsExceptionWithMessage() { - - Exception exception = assertThrows(RuntimeException.class, () -> { - ISOUtil.hex2byte("1A2B3C"); - }, "Not hex"); - - assertEquals("Not hex", exception.getMessage()); + public void testHex2byte_ValidInput() { + byte[] result = ISOUtil.hex2byte("1A2B3C"); + byte [] expected = {0x1A, 0x2B, 0x3C}; + assertArrayEquals(expected, result); } @Test - public void testIsHexadecimal_InvalidInput_ThrowsExceptionWithMessage() { + public void testHex2byte_InvalidInput_ThrowsExceptionWithMessage() { - Exception exception = assertThrows(RuntimeException.class, () -> { - ISOUtil.hex2byte("1111"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + ISOUtil.hex2byte("1A2B3C"); }, "Not hex"); - //ISOUtil.hex2byte("1"); assertEquals("Not hex", exception.getMessage()); } @@ -101,7 +84,7 @@ public void testAsciiToEbcdic() throws Throwable { @Test public void testAsciiToEbcdic1() throws Throwable { byte[] a = new byte[1]; - byte[] result = c.asciiToEbcdic(a); + byte[] result = ISOUtil.asciiToEbcdic(a); assertEquals(1, result.length, "result.length"); assertEquals((byte) 0, result[0], "result[0]"); }