KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > input > SwappedDataInputStreamTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.io.input;
19
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import junit.framework.TestCase;
25
26
27 /**
28  * Test for the SwappedDataInputStream. This also
29  * effectively tests the underlying EndianUtils Stream methods.
30  *
31  * @author Henri Yandell (bayard at apache dot org)
32  * @version $Revision: 1.9 $ $Date: 2004/02/29 22:03:45 $
33  */

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