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  
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  
23  import junit.framework.TestCase;
24  
25  
26  /**
27   * Test for the SwappedDataInputStream. This also 
28   * effectively tests the underlying EndianUtils Stream methods.
29   *
30   * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
31   */
32  
33  public class SwappedDataInputStreamTest extends TestCase {
34  
35      private SwappedDataInputStream sdis;
36      private byte[] bytes;
37  
38      public SwappedDataInputStreamTest(String name) {
39          super(name);
40      }
41  
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          ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
54          this.sdis = new SwappedDataInputStream( bais );
55      }
56  
57      public void tearDown() {
58          this.sdis = null;
59      }
60  
61      public void testReadBoolean() throws IOException {
62          assertEquals( false, this.sdis.readBoolean() );
63      }
64  
65      public void testReadByte() throws IOException {
66          assertEquals( 0x01, this.sdis.readByte() );
67      }
68  
69      public void testReadChar() throws IOException {
70          assertEquals( (char) 0x0201, this.sdis.readChar() );
71      }
72  
73      public void testReadDouble() throws IOException {
74          assertEquals( Double.longBitsToDouble(0x0807060504030201L), this.sdis.readDouble(), 0 );
75      }
76  
77      public void testReadFloat() throws IOException {
78          assertEquals( Float.intBitsToFloat(0x04030201), this.sdis.readFloat(), 0 );
79      }
80  
81      public void testReadFully() throws IOException {
82          byte[] bytesIn = new byte[8];
83          this.sdis.readFully(bytesIn);
84          for( int i=0; i<8; i++) {
85              assertEquals( bytes[i], bytesIn[i] );
86          }
87      }
88  
89      public void testReadInt() throws IOException {
90          assertEquals( (int) 0x04030201, this.sdis.readInt() );
91      }
92  
93      public void testReadLine() throws IOException {
94          try {
95              String unexpected = this.sdis.readLine();
96              fail("readLine should be unsupported. ");
97          } catch(UnsupportedOperationException uoe) {
98          }
99      }
100 
101     public void testReadLong() throws IOException {
102         assertEquals( 0x0807060504030201L, this.sdis.readLong() );
103     }
104 
105     public void testReadShort() throws IOException {
106         assertEquals( (short) 0x0201, this.sdis.readShort() );
107     }
108 
109     public void testReadUnsignedByte() throws IOException {
110         assertEquals( 0x01, this.sdis.readUnsignedByte() );
111     }
112 
113     public void testReadUnsignedShort() throws IOException {
114         assertEquals( (short) 0x0201, this.sdis.readUnsignedShort() );
115     }
116 
117     public void testReadUTF() throws IOException {
118         try {
119             String unexpected = this.sdis.readUTF();
120             fail("readUTF should be unsupported. ");
121         } catch(UnsupportedOperationException uoe) {
122         }
123     }
124 
125     public void testSkipBytes() throws IOException {
126         this.sdis.skipBytes(4);
127         assertEquals( (int)0x08070605, this.sdis.readInt() );
128     }
129 
130 }