diff --git a/pom.xml b/pom.xml index 5f70e9fd3..4f694db68 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.baidu.hugegraph hugegraph-common - 1.7.4 + 1.7.5 hugegraph-common https://github.com/hugegraph/hugegraph-common @@ -260,7 +260,7 @@ - 1.7.4.0 + 1.7.5.0 diff --git a/src/main/java/com/baidu/hugegraph/license/FileKeyStoreParam.java b/src/main/java/com/baidu/hugegraph/license/FileKeyStoreParam.java index 919cc2374..4065513e7 100755 --- a/src/main/java/com/baidu/hugegraph/license/FileKeyStoreParam.java +++ b/src/main/java/com/baidu/hugegraph/license/FileKeyStoreParam.java @@ -37,7 +37,7 @@ public class FileKeyStoreParam extends AbstractKeyStoreParam { private String keyPwd; private String storePwd; - public FileKeyStoreParam(Class clazz, String resource, String alias, + public FileKeyStoreParam(Class clazz, String resource, String alias, String storePwd, String keyPwd) { super(clazz, resource); this.storePath = resource; diff --git a/src/main/java/com/baidu/hugegraph/license/MachineInfo.java b/src/main/java/com/baidu/hugegraph/license/MachineInfo.java index eccc7bb7e..9b9a96ca6 100755 --- a/src/main/java/com/baidu/hugegraph/license/MachineInfo.java +++ b/src/main/java/com/baidu/hugegraph/license/MachineInfo.java @@ -77,7 +77,7 @@ public List getMacAddress() { } public List getLocalAllInetAddress() { - Enumeration interfaces; + Enumeration interfaces; try { interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { @@ -86,10 +86,10 @@ public List getLocalAllInetAddress() { List result = new ArrayList<>(); while (interfaces.hasMoreElements()) { - NetworkInterface iface = (NetworkInterface) interfaces.nextElement(); - for (Enumeration inetAddresses = iface.getInetAddresses(); + NetworkInterface nw = interfaces.nextElement(); + for (Enumeration inetAddresses = nw.getInetAddresses(); inetAddresses.hasMoreElements(); ) { - InetAddress inetAddr = (InetAddress) inetAddresses.nextElement(); + InetAddress inetAddr = inetAddresses.nextElement(); if (!inetAddr.isLoopbackAddress() && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()) { diff --git a/src/main/java/com/baidu/hugegraph/rest/RestResult.java b/src/main/java/com/baidu/hugegraph/rest/RestResult.java index e4908f8b3..b63359193 100644 --- a/src/main/java/com/baidu/hugegraph/rest/RestResult.java +++ b/src/main/java/com/baidu/hugegraph/rest/RestResult.java @@ -66,6 +66,7 @@ public T readObject(Class clazz) { } } + @SuppressWarnings("deprecation") public List readList(String key, Class clazz) { try { JsonNode root = mapper.readTree(this.content); @@ -84,6 +85,7 @@ public List readList(String key, Class clazz) { } } + @SuppressWarnings("deprecation") public List readList(Class clazz) { JavaType type = mapper.getTypeFactory() .constructParametrizedType(ArrayList.class, diff --git a/src/main/java/com/baidu/hugegraph/util/InsertionOrderUtil.java b/src/main/java/com/baidu/hugegraph/util/InsertionOrderUtil.java index 3f235a485..bfe911468 100644 --- a/src/main/java/com/baidu/hugegraph/util/InsertionOrderUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/InsertionOrderUtil.java @@ -32,6 +32,10 @@ public static Map newMap() { return new LinkedHashMap<>(); } + public static Map newMap(int initialCapacity) { + return new LinkedHashMap<>(initialCapacity); + } + public static Map newMap(Map origin) { return new LinkedHashMap<>(origin); } @@ -40,6 +44,10 @@ public static Set newSet() { return new LinkedHashSet<>(); } + public static Set newSet(int initialCapacity) { + return new LinkedHashSet<>(initialCapacity); + } + public static Set newSet(Set origin) { return new LinkedHashSet<>(origin); } @@ -48,6 +56,10 @@ public static List newList() { return new ArrayList<>(); } + public static List newList(int initialCapacity) { + return new ArrayList<>(initialCapacity); + } + public static List newList(List origin) { return new ArrayList<>(origin); } diff --git a/src/main/java/com/baidu/hugegraph/util/LongEncoding.java b/src/main/java/com/baidu/hugegraph/util/LongEncoding.java index 4456cf517..0383cd79e 100644 --- a/src/main/java/com/baidu/hugegraph/util/LongEncoding.java +++ b/src/main/java/com/baidu/hugegraph/util/LongEncoding.java @@ -23,11 +23,12 @@ */ public final class LongEncoding { - private static final String BASE_SYMBOLS = + private static final String B64_SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~"; private static final String LENGTH_SYMBOLS = "0123456789ABCDEF"; - private static final char NEG = LENGTH_SYMBOLS.charAt(0); + private static final char SORTABLE_NEG = LENGTH_SYMBOLS.charAt(0); + private static final char SIGNED_NEG = '-'; private static final long FULL_LONG = Long.MIN_VALUE; @@ -50,14 +51,14 @@ public static String encodeSortable(long num) { num += FULL_LONG; } - String encoded = encode(num, BASE_SYMBOLS); + String encoded = encode(num, B64_SYMBOLS); int length = encoded.length(); E.checkArgument(length <= LENGTH_SYMBOLS.length(), "Length symbols can't represent encoded number '%s'", encoded); StringBuilder sb = new StringBuilder(length + 2); if (negative) { - sb.append(NEG); + sb.append(SORTABLE_NEG); } char len = LENGTH_SYMBOLS.charAt(length); sb.append(len); @@ -69,45 +70,70 @@ public static String encodeSortable(long num) { public static long decodeSortable(String str) { E.checkArgument(str.length() >= 2, "Length of sortable encoded string must be >=2"); - boolean negative = str.charAt(0) == NEG; + boolean negative = str.charAt(0) == SORTABLE_NEG; int lengthPos = 0; if (negative) { lengthPos = 1; } - int length = BASE_SYMBOLS.indexOf(str.charAt(lengthPos)); + int length = B64_SYMBOLS.indexOf(str.charAt(lengthPos)); E.checkArgument(length == str.length() - lengthPos - 1, "Can't decode illegal string '%s' with wrong length", str); String encoded = str.substring(lengthPos + 1); - long value = decode(encoded); + long value = decode(encoded, B64_SYMBOLS); if (negative) { value -= FULL_LONG; } return value; } - public static boolean validSortableChar(char c) { - return BASE_SYMBOLS.indexOf(c) != -1; + public static String encodeSignedB64(long value) { + boolean negative = false; + if (value < 0L) { + negative = true; + if (value == FULL_LONG) { + return "-80000000000"; + } + value = -value; + } + assert value >= 0L : value; + String encoded = encodeB64(value); + return negative ? SIGNED_NEG + encoded : encoded; + } + + public static long decodeSignedB64(String value) { + boolean negative = false; + if (!value.isEmpty() && value.charAt(0) == SIGNED_NEG) { + negative = true; + value = value.substring(1); + } + long decoded = decodeB64(value); + return negative ? -decoded : decoded; + } + + public static boolean validB64Char(char c) { + return B64_SYMBOLS.indexOf(c) != -1; } - public static String encode(long num) { - return encode(num, BASE_SYMBOLS); + public static String encodeB64(long num) { + return encode(num, B64_SYMBOLS); } - public static long decode(String str) { - return decode(str, BASE_SYMBOLS); + public static long decodeB64(String str) { + return decode(str, B64_SYMBOLS); } - public static long decode(String str, String symbols) { + public static long decode(String encoded, String symbols) { final int B = symbols.length(); E.checkArgument(B > 0, "The symbols parameter can't be empty"); - long num = 0; - for (char ch : str.toCharArray()) { + long num = 0L; + for (char ch : encoded.toCharArray()) { num *= B; int pos = symbols.indexOf(ch); if (pos < 0) - throw new NumberFormatException( - "Symbol set does not match string"); + throw new NumberFormatException(String.format( + "Can't decode symbol '%s' in string '%s'", + ch, encoded)); num += pos; } return num; @@ -115,14 +141,14 @@ public static long decode(String str, String symbols) { public static String encode(long num, String symbols) { final int B = symbols.length(); - E.checkArgument(num >= 0, "Expected non-negative number: %s", num); + E.checkArgument(num >= 0L, "Expected non-negative number: %s", num); E.checkArgument(B > 0, "The symbols parameter can't be empty"); StringBuilder sb = new StringBuilder(); do { sb.append(symbols.charAt((int) (num % B))); num /= B; - } while (num != 0); + } while (num != 0L); return sb.reverse().toString(); } diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java index e8e2dd1fa..c409ff66b 100644 --- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java +++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java @@ -27,5 +27,5 @@ public class CommonVersion { // The second parameter of Version.of() is for all-in-one JAR public static final Version VERSION = Version.of(CommonVersion.class, - "1.7.4"); + "1.7.5"); } diff --git a/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java b/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java index 3298c5ae2..a8d9fa5d7 100644 --- a/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java +++ b/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java @@ -45,6 +45,40 @@ public void testSet() { ImmutableList.copyOf(set)); } + @Test + public void testSetWithInitialCapacity() { + Set set = InsertionOrderUtil.newSet(3); + set.add(4); + set.add(2); + set.add(5); + set.add(1); + set.add(3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(set)); + } + + @Test + public void testSetCopy() { + Set set = InsertionOrderUtil.newSet(); + set.add(4); + set.add(2); + set.add(5); + set.add(1); + set.add(3); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(set)); + + Set set2 = InsertionOrderUtil.newSet(set); + set2.add(6); + set2.add(1); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3, 6), + ImmutableList.copyOf(set2)); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(set)); + } + @Test public void testList() { List list = InsertionOrderUtil.newList(); @@ -58,6 +92,40 @@ public void testList() { ImmutableList.copyOf(list)); } + @Test + public void testListWithInitialCapacity() { + List list = InsertionOrderUtil.newList(3); + list.add(4); + list.add(2); + list.add(5); + list.add(1); + list.add(3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(list)); + } + + @Test + public void testListCopy() { + List list = InsertionOrderUtil.newList(); + list.add(4); + list.add(2); + list.add(5); + list.add(1); + list.add(3); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(list)); + + List list2 = InsertionOrderUtil.newList(list); + list2.add(6); + list2.add(1); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3, 6, 1), + ImmutableList.copyOf(list2)); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(list)); + } + @Test public void testMap() { Map map = InsertionOrderUtil.newMap(); @@ -70,4 +138,38 @@ public void testMap() { Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), ImmutableList.copyOf(map.keySet())); } + + @Test + public void testMapWithInitialCapacity() { + Map map = InsertionOrderUtil.newMap(3); + map.put(4, 4); + map.put(2, 2); + map.put(5, 5); + map.put(1, 1); + map.put(3, 3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(map.keySet())); + } + + @Test + public void testMapCopy() { + Map map = InsertionOrderUtil.newMap(3); + map.put(4, 4); + map.put(2, 2); + map.put(5, 5); + map.put(1, 1); + map.put(3, 3); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(map.keySet())); + + Map map2 = InsertionOrderUtil.newMap(map); + map2.put(6, 6); + map2.put(1, 7); + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3, 6), + ImmutableList.copyOf(map2.keySet())); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(map.keySet())); + } } diff --git a/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java b/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java index 3004d856c..5a245a397 100644 --- a/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java +++ b/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java @@ -36,194 +36,379 @@ public class LongEncodingTest extends BaseUnitTest { + @Test + public void testValidB64Char() { + Assert.assertTrue(LongEncoding.validB64Char('0')); + Assert.assertTrue(LongEncoding.validB64Char('1')); + Assert.assertTrue(LongEncoding.validB64Char('9')); + Assert.assertTrue(LongEncoding.validB64Char('A')); + Assert.assertTrue(LongEncoding.validB64Char('Z')); + Assert.assertTrue(LongEncoding.validB64Char('_')); + Assert.assertTrue(LongEncoding.validB64Char('a')); + Assert.assertTrue(LongEncoding.validB64Char('z')); + Assert.assertTrue(LongEncoding.validB64Char('~')); + + Assert.assertFalse(LongEncoding.validB64Char('`')); + Assert.assertFalse(LongEncoding.validB64Char('!')); + Assert.assertFalse(LongEncoding.validB64Char('@')); + Assert.assertFalse(LongEncoding.validB64Char('#')); + Assert.assertFalse(LongEncoding.validB64Char('$')); + Assert.assertFalse(LongEncoding.validB64Char('%')); + Assert.assertFalse(LongEncoding.validB64Char('^')); + Assert.assertFalse(LongEncoding.validB64Char('&')); + Assert.assertFalse(LongEncoding.validB64Char('*')); + Assert.assertFalse(LongEncoding.validB64Char('(')); + Assert.assertFalse(LongEncoding.validB64Char(')')); + Assert.assertFalse(LongEncoding.validB64Char('-')); + Assert.assertFalse(LongEncoding.validB64Char('+')); + Assert.assertFalse(LongEncoding.validB64Char('=')); + Assert.assertFalse(LongEncoding.validB64Char('[')); + Assert.assertFalse(LongEncoding.validB64Char(']')); + Assert.assertFalse(LongEncoding.validB64Char('{')); + Assert.assertFalse(LongEncoding.validB64Char('}')); + Assert.assertFalse(LongEncoding.validB64Char('|')); + Assert.assertFalse(LongEncoding.validB64Char('\\')); + Assert.assertFalse(LongEncoding.validB64Char(';')); + Assert.assertFalse(LongEncoding.validB64Char(':')); + Assert.assertFalse(LongEncoding.validB64Char('\'')); + Assert.assertFalse(LongEncoding.validB64Char('\"')); + Assert.assertFalse(LongEncoding.validB64Char('<')); + Assert.assertFalse(LongEncoding.validB64Char(',')); + Assert.assertFalse(LongEncoding.validB64Char('>')); + Assert.assertFalse(LongEncoding.validB64Char('.')); + Assert.assertFalse(LongEncoding.validB64Char('?')); + Assert.assertFalse(LongEncoding.validB64Char('/')); + Assert.assertFalse(LongEncoding.validB64Char('\t')); + Assert.assertFalse(LongEncoding.validB64Char('\b')); + } + @Test public void testEncode() { - String val0 = LongEncoding.encode(0); + String val0 = LongEncoding.encodeB64(0); Assert.assertEquals("0", val0); - String val1 = LongEncoding.encode(1); + String val1 = LongEncoding.encodeB64(1); Assert.assertEquals("1", val1); - String val9 = LongEncoding.encode(9); + String val9 = LongEncoding.encodeB64(9); Assert.assertEquals("9", val9); - String val10 = LongEncoding.encode(10); + String val10 = LongEncoding.encodeB64(10); Assert.assertEquals("A", val10); - String val35 = LongEncoding.encode(35); + String val35 = LongEncoding.encodeB64(35); Assert.assertEquals("Z", val35); - String val36 = LongEncoding.encode(36); + String val36 = LongEncoding.encodeB64(36); Assert.assertEquals("_", val36); - String val37 = LongEncoding.encode(37); + String val37 = LongEncoding.encodeB64(37); Assert.assertEquals("a", val37); - String val62 = LongEncoding.encode(62); + String val62 = LongEncoding.encodeB64(62); Assert.assertEquals("z", val62); - String val63 = LongEncoding.encode(63); + String val63 = LongEncoding.encodeB64(63); Assert.assertEquals("~", val63); } @Test public void testEncodeWithMultiString() { - String val64 = LongEncoding.encode(64); + Assert.assertEquals("0", LongEncoding.encode(0L, "0123456789")); + Assert.assertEquals("1", LongEncoding.encode(1L, "0123456789")); + Assert.assertEquals("123", LongEncoding.encode(123L, "0123456789")); + Assert.assertEquals("13579", LongEncoding.encode(13579L, "0123456789")); + Assert.assertEquals("24680", LongEncoding.encode(24680L, "0123456789")); + + String val64 = LongEncoding.encodeB64(64); Assert.assertEquals("10", val64); - String val65 = LongEncoding.encode(65); + String val65 = LongEncoding.encodeB64(65); Assert.assertEquals("11", val65); - String val99 = LongEncoding.encode(99); + String val99 = LongEncoding.encodeB64(99); Assert.assertEquals("1Z", val99); - String val100 = LongEncoding.encode(100); + String val100 = LongEncoding.encodeB64(100); Assert.assertEquals("1_", val100); - String val126 = LongEncoding.encode(126); + String val126 = LongEncoding.encodeB64(126); Assert.assertEquals("1z", val126); - String val127 = LongEncoding.encode(127); + String val127 = LongEncoding.encodeB64(127); Assert.assertEquals("1~", val127); - String val128 = LongEncoding.encode(128); + String val128 = LongEncoding.encodeB64(128); Assert.assertEquals("20", val128); - String val200 = LongEncoding.encode(200); + String val200 = LongEncoding.encodeB64(200); Assert.assertEquals("38", val200); - String val1000 = LongEncoding.encode(1000); + String val1000 = LongEncoding.encodeB64(1000); Assert.assertEquals("Fd", val1000); - String val1234 = LongEncoding.encode(1234); + String val1234 = LongEncoding.encodeB64(1234); Assert.assertEquals("JI", val1234); - String val10000 = LongEncoding.encode(10000); + String val10000 = LongEncoding.encodeB64(10000); Assert.assertEquals("2SG", val10000); - String val12345 = LongEncoding.encode(12345); + String val12345 = LongEncoding.encodeB64(12345); Assert.assertEquals("30u", val12345); - String val22345 = LongEncoding.encode(22345); + String val22345 = LongEncoding.encodeB64(22345); Assert.assertEquals("5T9", val22345); - String val92345 = LongEncoding.encode(92345); + String val92345 = LongEncoding.encodeB64(92345); Assert.assertEquals("MYu", val92345); - String val12345678 = LongEncoding.encode(12345678); + String val12345678 = LongEncoding.encodeB64(12345678); Assert.assertEquals("k65E", val12345678); - String val112345678 = LongEncoding.encode(112345678); + String val112345678 = LongEncoding.encodeB64(112345678); Assert.assertEquals("6h_9E", val112345678); - String valIntMax = LongEncoding.encode(Integer.MAX_VALUE); + String valIntMax = LongEncoding.encodeB64(Integer.MAX_VALUE); Assert.assertEquals("1~~~~~", valIntMax); - String valLongMax = LongEncoding.encode(Long.MAX_VALUE); + String valLongMax = LongEncoding.encodeB64(Long.MAX_VALUE); Assert.assertEquals("7~~~~~~~~~~", valLongMax); } @Test - public void testEncodeNegative() { + public void testEncodeWithError() { + Assert.assertThrows(IllegalArgumentException.class, () -> { + LongEncoding.encode(1, ""); + }, e -> { + Assert.assertEquals("The symbols parameter can't be empty", + e.getMessage()); + }); + + Assert.assertThrows(IllegalArgumentException.class, () -> { + LongEncoding.encode(-1, ""); + }, e -> { + Assert.assertContains("Expected non-negative number", + e.getMessage()); + }); + Assert.assertThrows(IllegalArgumentException.class, () -> { - LongEncoding.encode(-1); + LongEncoding.encodeB64(-1); + }, e -> { + Assert.assertContains("Expected non-negative number", + e.getMessage()); }); Assert.assertThrows(IllegalArgumentException.class, () -> { - LongEncoding.encode(Long.MIN_VALUE); + LongEncoding.encodeB64(Long.MIN_VALUE); + }, e -> { + Assert.assertContains("Expected non-negative number", + e.getMessage()); }); } @Test public void testDecode() { - long valEmpty = LongEncoding.decode(""); + long valEmpty = LongEncoding.decodeB64(""); Assert.assertEquals(0, valEmpty); - long val0 = LongEncoding.decode("0"); + long val0 = LongEncoding.decodeB64("0"); Assert.assertEquals(0, val0); - long val1 = LongEncoding.decode("1"); + long val1 = LongEncoding.decodeB64("1"); Assert.assertEquals(1, val1); - long val9 = LongEncoding.decode("9"); + long val9 = LongEncoding.decodeB64("9"); Assert.assertEquals(9, val9); - long val10 = LongEncoding.decode("A"); + long val10 = LongEncoding.decodeB64("A"); Assert.assertEquals(10, val10); - long val35 = LongEncoding.decode("Z"); + long val35 = LongEncoding.decodeB64("Z"); Assert.assertEquals(35, val35); - long val36 = LongEncoding.decode("_"); + long val36 = LongEncoding.decodeB64("_"); Assert.assertEquals(36, val36); - long val37 = LongEncoding.decode("a"); + long val37 = LongEncoding.decodeB64("a"); Assert.assertEquals(37, val37); - long val62 = LongEncoding.decode("z"); + long val62 = LongEncoding.decodeB64("z"); Assert.assertEquals(62, val62); - long val63 = LongEncoding.decode("~"); + long val63 = LongEncoding.decodeB64("~"); Assert.assertEquals(63, val63); } @Test public void testDecodeWithMultiString() { - long val64 = LongEncoding.decode("10"); + Assert.assertEquals(0L, LongEncoding.decode("0", "0123456789")); + Assert.assertEquals(1L, LongEncoding.decode("1", "0123456789")); + Assert.assertEquals(123L, LongEncoding.decode("123", "0123456789")); + Assert.assertEquals(13579L, LongEncoding.decode("13579", "0123456789")); + Assert.assertEquals(24680L, LongEncoding.decode("24680", "0123456789")); + + long val64 = LongEncoding.decodeB64("10"); Assert.assertEquals(64, val64); - long val65 = LongEncoding.decode("11"); + long val65 = LongEncoding.decodeB64("11"); Assert.assertEquals(65, val65); - long val99 = LongEncoding.decode("1Z"); + long val99 = LongEncoding.decodeB64("1Z"); Assert.assertEquals(99, val99); - long val100 = LongEncoding.decode("1_"); + long val100 = LongEncoding.decodeB64("1_"); Assert.assertEquals(100, val100); - long val126 = LongEncoding.decode("1z"); + long val126 = LongEncoding.decodeB64("1z"); Assert.assertEquals(126, val126); - long val127 = LongEncoding.decode("1~"); + long val127 = LongEncoding.decodeB64("1~"); Assert.assertEquals(127, val127); - long val128 = LongEncoding.decode("20"); + long val128 = LongEncoding.decodeB64("20"); Assert.assertEquals(128, val128); - long val200 = LongEncoding.decode("38"); + long val200 = LongEncoding.decodeB64("38"); Assert.assertEquals(200, val200); - long val1000 = LongEncoding.decode("Fd"); + long val1000 = LongEncoding.decodeB64("Fd"); Assert.assertEquals(1000, val1000); - long val1234 = LongEncoding.decode("JI"); + long val1234 = LongEncoding.decodeB64("JI"); Assert.assertEquals(1234, val1234); - long val10000 = LongEncoding.decode("2SG"); + long val10000 = LongEncoding.decodeB64("2SG"); Assert.assertEquals(10000, val10000); - long val12345 = LongEncoding.decode("30u"); + long val12345 = LongEncoding.decodeB64("30u"); Assert.assertEquals(12345, val12345); - long val22345 = LongEncoding.decode("5T9"); + long val22345 = LongEncoding.decodeB64("5T9"); Assert.assertEquals(22345, val22345); - long val92345 = LongEncoding.decode("MYu"); + long val92345 = LongEncoding.decodeB64("MYu"); Assert.assertEquals(92345, val92345); - long val12345678 = LongEncoding.decode("k65E"); + long val12345678 = LongEncoding.decodeB64("k65E"); Assert.assertEquals(12345678, val12345678); - long val112345678 = LongEncoding.decode("6h_9E"); + long val112345678 = LongEncoding.decodeB64("6h_9E"); Assert.assertEquals(112345678, val112345678); - long valIntMax = LongEncoding.decode("1~~~~~"); + long valIntMax = LongEncoding.decodeB64("1~~~~~"); Assert.assertEquals(Integer.MAX_VALUE, valIntMax); - long valLongMax = LongEncoding.decode("7~~~~~~~~~~"); + long valLongMax = LongEncoding.decodeB64("7~~~~~~~~~~"); Assert.assertEquals(Long.MAX_VALUE, valLongMax); } + @Test + public void testDecodeWithError() { + Assert.assertThrows(IllegalArgumentException.class, () -> { + LongEncoding.decode("1", ""); + }, e -> { + Assert.assertEquals("The symbols parameter can't be empty", + e.getMessage()); + }); + + Assert.assertThrows(IllegalArgumentException.class, () -> { + LongEncoding.decode("1a", "0123456789"); + }, e -> { + Assert.assertEquals("Can't decode symbol 'a' in string '1a'", + e.getMessage()); + }); + } + + @Test + public void testEncodeSignedB64() { + String val1234 = LongEncoding.encodeSignedB64(1234); + Assert.assertEquals("JI", val1234); + + String val23 = LongEncoding.encodeSignedB64(23); + Assert.assertEquals("N", val23); + + String valIntMax = LongEncoding.encodeSignedB64(Integer.MAX_VALUE); + Assert.assertEquals("1~~~~~", valIntMax); + + String valLongMax = LongEncoding.encodeSignedB64(Long.MAX_VALUE); + Assert.assertEquals("7~~~~~~~~~~", valLongMax); + + String val0 = LongEncoding.encodeSignedB64(0); + Assert.assertEquals("0", val0); + + String valNeg1 = LongEncoding.encodeSignedB64(-1); + Assert.assertEquals("-1", valNeg1); + + String valIntMinP1 = LongEncoding.encodeSignedB64(Integer.MIN_VALUE + + 1L); + Assert.assertEquals("-1~~~~~", valIntMinP1); + + String valIntMin = LongEncoding.encodeSignedB64(Integer.MIN_VALUE); + Assert.assertEquals("-200000", valIntMin); + + String valLongMinPlus1 = LongEncoding.encodeSignedB64(Long.MIN_VALUE + + 1L); + Assert.assertEquals("-7~~~~~~~~~~", valLongMinPlus1); + + String valLongMin = LongEncoding.encodeSignedB64(Long.MIN_VALUE); + Assert.assertEquals("-80000000000", valLongMin); + } + + @Test + public void testDecodeSignedB64() { + long val1234 = LongEncoding.decodeSignedB64("JI"); + Assert.assertEquals(1234, val1234); + + long val23 = LongEncoding.decodeSignedB64("N"); + Assert.assertEquals(23, val23); + + long valIntMax = LongEncoding.decodeSignedB64("1~~~~~"); + Assert.assertEquals(Integer.MAX_VALUE, valIntMax); + + long valLongMax = LongEncoding.decodeSignedB64("7~~~~~~~~~~"); + Assert.assertEquals(Long.MAX_VALUE, valLongMax); + + long val0 = LongEncoding.decodeSignedB64("0"); + Assert.assertEquals(0, val0); + + long valn1 = LongEncoding.decodeSignedB64("-1"); + Assert.assertEquals(-1, valn1); + + long valIntMinPlus1 = LongEncoding.decodeSignedB64("-1~~~~~"); + Assert.assertEquals(Integer.MIN_VALUE + 1L, valIntMinPlus1); + + long valIntMin = LongEncoding.decodeSignedB64("-200000"); + Assert.assertEquals(Integer.MIN_VALUE, valIntMin); + + long valLongMinPlus1 = LongEncoding.decodeSignedB64("-7~~~~~~~~~~"); + Assert.assertEquals(Long.MIN_VALUE + 1L, valLongMinPlus1); + + long valLongMin = LongEncoding.decodeSignedB64("-80000000000"); + Assert.assertEquals(Long.MIN_VALUE, valLongMin); + } + + @Test + public void testDecodeSignedB64Overflow() { + long valOverflow = LongEncoding.decodeSignedB64("80000000000"); + Assert.assertEquals(Long.MIN_VALUE, valOverflow); + + long valOverflow2 = LongEncoding.decodeSignedB64("80000000001"); + Assert.assertEquals(Long.MIN_VALUE + 1L, valOverflow2); + + long valOverflow3 = LongEncoding.decodeSignedB64("800000000001"); + Assert.assertEquals(1L, valOverflow3); + + long valOverflow4 = LongEncoding.decodeSignedB64("80000000000JI"); + Assert.assertEquals(1234L, valOverflow4); + + long valOverflow5 = LongEncoding.decodeSignedB64("80000000000N"); + Assert.assertEquals(23L, valOverflow5); + + long valOverflow6 = LongEncoding.decodeSignedB64("80000000000" + + "7~~~~~~~~~~"); + Assert.assertEquals(Long.MAX_VALUE, valOverflow6); + } + @Test public void testEncodeSortable() { String val1234 = LongEncoding.encodeSortable(1234);