Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 52b210a

Browse files
committed
Changed trytecoverter to throw error, updated tests
1 parent 924a067 commit 52b210a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

jota/src/main/java/jota/utils/TrytesConverter.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jota.utils;
22

3+
import jota.error.ArgumentException;
4+
35
/**
46
* This class allows to convert between ASCII and tryte encoded strings.
57
*
@@ -67,12 +69,13 @@ public static String asciiToTrytes(String inputString) {
6769
* 2 Trytes == 1 Byte
6870
* @param inputTrytes the trytes we want to convert
6971
* @return an ASCII string or null when the inputTrytes are uneven
72+
* @throws ArgumentException When the trytes in the string are an odd number
7073
*/
71-
public static String trytesToAscii(String inputTrytes) {
74+
public static String trytesToAscii(String inputTrytes) throws ArgumentException {
7275

7376
// If input length is odd, return null
7477
if (inputTrytes.length() % 2 != 0)
75-
return null;
78+
throw new ArgumentException("Odd amount of trytes supplied");
7679

7780
StringBuilder string = new StringBuilder();
7881

jota/src/test/java/jota/TrytesConverterTest.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jota;
22

3+
import jota.error.ArgumentException;
34
import jota.utils.TrytesConverter;
45
import org.apache.commons.lang3.RandomStringUtils;
56
import org.junit.Test;
@@ -20,14 +21,24 @@ public void shouldConvertStringToTrytes() {
2021

2122
@Test
2223
public void shouldConvertTrytesToString() {
23-
assertEquals(TrytesConverter.trytesToAscii("IC"), "Z");
24-
assertEquals(TrytesConverter.trytesToAscii("TBYBCCKBEATBYBCCKB"), "JOTA JOTA");
24+
try {
25+
assertEquals(TrytesConverter.trytesToAscii("IC"), "Z");
26+
assertEquals(TrytesConverter.trytesToAscii("TBYBCCKBEATBYBCCKB"), "JOTA JOTA");
27+
} catch (ArgumentException e) {
28+
e.printStackTrace();
29+
}
2530
}
2631

2732
@Test
2833
public void shouldConvertBackAndForth() {
2934
String str = RandomStringUtils.randomAlphabetic(1000).toUpperCase();
30-
String back = TrytesConverter.trytesToAscii(TrytesConverter.asciiToTrytes(str));
31-
assertTrue(str.equals(back));
35+
String back;
36+
try {
37+
back = TrytesConverter.trytesToAscii(TrytesConverter.asciiToTrytes(str));
38+
assertTrue(str.equals(back));
39+
} catch (ArgumentException e) {
40+
e.printStackTrace();
41+
}
42+
3243
}
3344
}

0 commit comments

Comments
 (0)