1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 public class BaseNCodecOutputStreamTest extends AbstractBaseNOutputStreamTest {
35
36 @Override
37 OutputStream newOutputStream() {
38 return new BaseNCodecOutputStream<>(new ByteArrayOutputStream(), new NoOpBaseNCodec(), true);
39 }
40
41 @Test
42 void testCloseAfterInvalidBase58() throws IOException {
43 final boolean[] closed = { false };
44 final ByteArrayOutputStream sink = new ByteArrayOutputStream() {
45 @Override
46 public void close() {
47 closed[0] = true;
48 }
49 };
50 final Base58OutputStream stream = Base58OutputStream.builder().setOutputStream(sink).setEncode(false).get();
51 stream.write('0');
52 final IOException failure = assertThrows(IOException.class, stream::close);
53 assertTrue(failure.getCause() instanceof IllegalArgumentException);
54 assertTrue(closed[0]);
55 assertEquals(0, sink.size());
56 }
57
58 @Test
59 void testCloseFailureAfterSuccessfulConversion() throws IOException {
60 final IOException closeFailure = new IOException("close");
61 final ByteArrayOutputStream sink = new ByteArrayOutputStream() {
62 @Override
63 public void close() throws IOException {
64 throw closeFailure;
65 }
66 };
67 final Base58OutputStream stream = new Base58OutputStream(sink);
68 stream.write(1);
69 assertSame(closeFailure, assertThrows(IOException.class, stream::close));
70 assertEquals("2", sink.toString("US-ASCII"));
71 }
72
73 @Test
74 void testCloseFailureSuppressedAfterFlushFailure() {
75 final IOException flushFailure = new IOException("flush");
76 final IOException closeFailure = new IOException("close");
77 final ByteArrayOutputStream sink = new ByteArrayOutputStream() {
78 @Override
79 public void close() throws IOException {
80 throw closeFailure;
81 }
82
83 @Override
84 public void flush() throws IOException {
85 throw flushFailure;
86 }
87 };
88 final Base64OutputStream stream = new Base64OutputStream(sink);
89 assertSame(flushFailure, assertThrows(IOException.class, stream::close));
90 assertEquals(1, flushFailure.getSuppressed().length);
91 assertSame(closeFailure, flushFailure.getSuppressed()[0]);
92 }
93
94 @Test
95 void testCloseFailureSuppressedAfterInvalidBase58() throws IOException {
96 final IOException closeFailure = new IOException("close");
97 final ByteArrayOutputStream sink = new ByteArrayOutputStream() {
98 @Override
99 public void close() throws IOException {
100 throw closeFailure;
101 }
102 };
103 final Base58OutputStream stream = Base58OutputStream.builder().setOutputStream(sink).setEncode(false).get();
104 stream.write('0');
105 final IOException failure = assertThrows(IOException.class, stream::close);
106 assertTrue(failure.getCause() instanceof IllegalArgumentException);
107 assertEquals(1, failure.getSuppressed().length);
108 assertSame(closeFailure, failure.getSuppressed()[0]);
109 }
110 }