1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
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 }