2727import org .apache .commons .io .output .UnsynchronizedByteArrayOutputStream ;
2828import org .exist .xquery .XPathException ;
2929import java .io .IOException ;
30- import org .junit .Test ;
31- import static org .junit .Assert .assertArrayEquals ;
32- import static org .junit .Assert .assertEquals ;
30+
31+ import org .junit .jupiter .api .Test ;
32+ import org .junit .jupiter .params .ParameterizedTest ;
33+ import org .junit .jupiter .params .provider .ValueSource ;
34+
35+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
36+ import static org .junit .jupiter .api .Assertions .assertEquals ;
3337
3438/**
3539 *
36- * @author <a href="mailto:adam@existsolutions .com">Adam Retter</a>
40+ * @author <a href="mailto:adam@evolvedbinary .com">Adam Retter</a>
3741 */
3842public class BinaryValueFromBinaryStringTest {
3943
@@ -43,38 +47,68 @@ public void getInputStream() throws XPathException, IOException {
4347 final String testData = "test data" ;
4448 final String base64TestData = Base64 .encodeBase64String (testData .getBytes ()).trim ();
4549
46- BinaryValue binaryValue = new BinaryValueFromBinaryString (new Base64BinaryValueType (), base64TestData );
47-
4850
49- try (final InputStream is = binaryValue .getInputStream ();
51+ try (final BinaryValue binaryValue = new BinaryValueFromBinaryString (new Base64BinaryValueType (), base64TestData );
52+ final InputStream is = binaryValue .getInputStream ();
5053 final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream ()) {
5154 baos .write (is );
5255 assertArrayEquals (testData .getBytes (), baos .toByteArray ());
5356 }
5457 }
5558
5659 @ Test
57- public void cast_base64_to_hexBinary () throws XPathException {
60+ public void cast_base64_to_hexBinary () throws XPathException , IOException {
5861
5962 final String testData = "testdata" ;
6063 final String expectedResult = Hex .encodeHexString (testData .getBytes ()).trim ();
6164
62- BinaryValue binaryValue = new BinaryValueFromBinaryString (new Base64BinaryValueType (), Base64 .encodeBase64String (testData .getBytes ()));
65+ try ( final BinaryValue binaryValue = new BinaryValueFromBinaryString (new Base64BinaryValueType (), Base64 .encodeBase64String (testData .getBytes ()))) {
6366
64- final AtomicValue result = binaryValue .convertTo (new HexBinaryValueType ());
67+ final AtomicValue result = binaryValue .convertTo (new HexBinaryValueType ());
6568
66- assertEquals (expectedResult , result .getStringValue ());
69+ assertEquals (expectedResult , result .getStringValue ());
70+ }
6771 }
6872
6973 @ Test
70- public void cast_hexBinary_to_base64 () throws XPathException {
74+ public void cast_hexBinary_to_base64 () throws XPathException , IOException {
7175 final String testData = "testdata" ;
7276 final String expectedResult = Base64 .encodeBase64String (testData .getBytes ()).trim ();
7377
74- BinaryValue binaryValue = new BinaryValueFromBinaryString (new HexBinaryValueType (), Hex .encodeHexString (testData .getBytes ()));
78+ try (final BinaryValue binaryValue = new BinaryValueFromBinaryString (new HexBinaryValueType (), Hex .encodeHexString (testData .getBytes ()))) {
79+
80+ final AtomicValue result = binaryValue .convertTo (new Base64BinaryValueType ());
7581
76- final AtomicValue result = binaryValue .convertTo (new Base64BinaryValueType ());
82+ assertEquals (expectedResult , result .getStringValue ());
83+ }
84+ }
85+
86+ @ Test
87+ public void base64StreamBinaryTo () throws XPathException , IOException {
88+ try (final BinaryValue binaryValue = new BinaryValueFromBinaryString (new Base64BinaryValueType (), "yv4=" );
89+ final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream ()) {
7790
78- assertEquals (expectedResult , result .getStringValue ());
91+ binaryValue .streamBinaryTo (baos );
92+
93+ final byte [] data = baos .toByteArray ();
94+ assertEquals (2 , data .length );
95+ assertEquals (0xCA , data [0 ] & 0xFF );
96+ assertEquals (0xFE , data [1 ] & 0xFF );
97+ }
98+ }
99+
100+ @ ParameterizedTest
101+ @ ValueSource (strings = {"CAFE" , "cafe" })
102+ public void hexStreamBinaryTo (final String hexString ) throws XPathException , IOException {
103+ try (final BinaryValue binaryValue = new BinaryValueFromBinaryString (new HexBinaryValueType (), hexString );
104+ final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream ()) {
105+
106+ binaryValue .streamBinaryTo (baos );
107+
108+ final byte [] data = baos .toByteArray ();
109+ assertEquals (2 , data .length );
110+ assertEquals (0xCA , data [0 ] & 0xFF );
111+ assertEquals (0xFE , data [1 ] & 0xFF );
112+ }
79113 }
80- }
114+ }
0 commit comments