Skip to content

Commit

Permalink
Add Base.isEncoded method
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Nov 26, 2023
1 parent 7bfdc5e commit d45a6fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/apicatalog/multibase/Multibase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public int length() {
return length;
}

/**
* Checks if the given value is encoded with the base.
*
* @param encoded an encoded value to test
* @return <code>true</code> is the given value is encoded with the base,
* <code>false</code> otherwise
*/
public boolean isEncoded(final String encoded) {
return encoded != null
&& encoded.trim().length() > 0
&& prefix == encoded.charAt(0);
}

/**
* Decodes the given data into byte array.
*
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/com/apicatalog/multibase/MultibaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -19,19 +22,29 @@ void testBase58BTCEncode(String expected, byte[] data) {
String output = Multibase.BASE_58_BTC.encode(data);
assertEquals(expected, output);
}

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("testData")
void testDecode(String encoded, byte[] expected) {
byte[] output = DECODER.decode(encoded);
assertArrayEquals(expected, output);
}

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("testData")
void testIsBase58BTCEncoded(String encoded, byte[] data) {
assertTrue(Multibase.BASE_58_BTC.isEncoded(encoded));
}

@Test
void testIsNotBase58BTCEncoded() {
assertFalse(Multibase.BASE_58_BTC.isEncoded("abc"));
}

static Stream<Arguments> testData() {
return Stream.of(
Arguments.of("zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe", "Decentralize everything!!".getBytes()),
Arguments.of("zStV1DL6CwTryKyV", "hello world".getBytes()),
Arguments.of("z7paNL19xttacUY", "yes mani !".getBytes())
);
Arguments.of("zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe", "Decentralize everything!!".getBytes()),
Arguments.of("zStV1DL6CwTryKyV", "hello world".getBytes()),
Arguments.of("z7paNL19xttacUY", "yes mani !".getBytes()));
}
}

0 comments on commit d45a6fc

Please sign in to comment.