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    *      http://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  package org.apache.commons.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  
27  import org.junit.jupiter.api.AfterEach;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test for the SwappedDataInputStream. This also
33   * effectively tests the underlying EndianUtils Stream methods.
34   */
35  public class SwappedDataInputStreamTest {
36  
37      private SwappedDataInputStream sdis;
38      private byte[] bytes;
39  
40      @BeforeEach
41      public void setUp() {
42          bytes = new byte[] {
43              0x01,
44              0x02,
45              0x03,
46              0x04,
47              0x05,
48              0x06,
49              0x07,
50              0x08
51          };
52          final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
53          this.sdis = new SwappedDataInputStream(bais);
54      }
55  
56      @AfterEach
57      public void tearDown() {
58          this.sdis = null;
59      }
60  
61      @SuppressWarnings({ "resource" })
62      @Test
63      public void testCloseHandleIOException() throws IOException {
64          ProxyInputStreamTest.testCloseHandleIOException(new SwappedDataInputStream(new BrokenInputStream((Throwable) new IOException())));
65      }
66  
67      @Test
68      public void testReadBoolean() throws IOException {
69          bytes = new byte[] { 0x00, 0x01, 0x02, };
70          try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
71                  SwappedDataInputStream sdis = new SwappedDataInputStream(bais)) {
72              assertFalse(sdis.readBoolean());
73              assertTrue(sdis.readBoolean());
74              assertTrue(sdis.readBoolean());
75          }
76      }
77  
78      @Test
79      public void testReadByte() throws IOException {
80          assertEquals(0x01, this.sdis.readByte());
81      }
82  
83      @Test
84      public void testReadChar() throws IOException {
85          assertEquals((char) 0x0201, this.sdis.readChar());
86      }
87  
88      @Test
89      public void testReadDouble() throws IOException {
90          assertEquals(Double.longBitsToDouble(0x0807060504030201L), this.sdis.readDouble(), 0);
91      }
92  
93      @Test
94      public void testReadFloat() throws IOException {
95          assertEquals(Float.intBitsToFloat(0x04030201), this.sdis.readFloat(), 0);
96      }
97  
98      @Test
99      public void testReadFully() throws IOException {
100         final byte[] bytesIn = new byte[8];
101         this.sdis.readFully(bytesIn);
102         for (int i = 0; i < 8; i++) {
103             assertEquals(bytes[i], bytesIn[i]);
104         }
105     }
106 
107     @Test
108     public void testReadInt() throws IOException {
109         assertEquals(0x04030201, this.sdis.readInt());
110     }
111 
112     @Test
113     public void testReadLine() {
114         assertThrows(UnsupportedOperationException.class, () -> this.sdis.readLine(), "readLine should be unsupported.");
115     }
116 
117     @Test
118     public void testReadLong() throws IOException {
119         assertEquals(0x0807060504030201L, this.sdis.readLong());
120     }
121 
122     @Test
123     public void testReadShort() throws IOException {
124         assertEquals((short) 0x0201, this.sdis.readShort());
125     }
126 
127     @Test
128     public void testReadUnsignedByte() throws IOException {
129         assertEquals(0x01, this.sdis.readUnsignedByte());
130     }
131 
132     @Test
133     public void testReadUnsignedShort() throws IOException {
134         assertEquals((short) 0x0201, this.sdis.readUnsignedShort());
135     }
136 
137     @Test
138     public void testReadUTF() {
139         assertThrows(UnsupportedOperationException.class, () -> this.sdis.readUTF(), "readUTF should be unsupported.");
140     }
141 
142     @Test
143     public void testSkipBytes() throws IOException {
144         this.sdis.skipBytes(4);
145         assertEquals(0x08070605, this.sdis.readInt());
146     }
147 
148 }