View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link BaseNCodecOutputStream}.
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 }