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