001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.input;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Test for the SwappedDataInputStream. This also
026: * effectively tests the underlying EndianUtils Stream methods.
027: *
028: * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
029: */
030:
031: public class SwappedDataInputStreamTest extends TestCase {
032:
033: private SwappedDataInputStream sdis;
034: private byte[] bytes;
035:
036: public SwappedDataInputStreamTest(String name) {
037: super (name);
038: }
039:
040: public void setUp() {
041: bytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
042: 0x08 };
043: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
044: this .sdis = new SwappedDataInputStream(bais);
045: }
046:
047: public void tearDown() {
048: this .sdis = null;
049: }
050:
051: public void testReadBoolean() throws IOException {
052: assertEquals(false, this .sdis.readBoolean());
053: }
054:
055: public void testReadByte() throws IOException {
056: assertEquals(0x01, this .sdis.readByte());
057: }
058:
059: public void testReadChar() throws IOException {
060: assertEquals((char) 0x0201, this .sdis.readChar());
061: }
062:
063: public void testReadDouble() throws IOException {
064: assertEquals(Double.longBitsToDouble(0x0807060504030201L),
065: this .sdis.readDouble(), 0);
066: }
067:
068: public void testReadFloat() throws IOException {
069: assertEquals(Float.intBitsToFloat(0x04030201), this .sdis
070: .readFloat(), 0);
071: }
072:
073: public void testReadFully() throws IOException {
074: byte[] bytesIn = new byte[8];
075: this .sdis.readFully(bytesIn);
076: for (int i = 0; i < 8; i++) {
077: assertEquals(bytes[i], bytesIn[i]);
078: }
079: }
080:
081: public void testReadInt() throws IOException {
082: assertEquals((int) 0x04030201, this .sdis.readInt());
083: }
084:
085: public void testReadLine() throws IOException {
086: try {
087: String unexpected = this .sdis.readLine();
088: fail("readLine should be unsupported. ");
089: } catch (UnsupportedOperationException uoe) {
090: }
091: }
092:
093: public void testReadLong() throws IOException {
094: assertEquals(0x0807060504030201L, this .sdis.readLong());
095: }
096:
097: public void testReadShort() throws IOException {
098: assertEquals((short) 0x0201, this .sdis.readShort());
099: }
100:
101: public void testReadUnsignedByte() throws IOException {
102: assertEquals(0x01, this .sdis.readUnsignedByte());
103: }
104:
105: public void testReadUnsignedShort() throws IOException {
106: assertEquals((short) 0x0201, this .sdis.readUnsignedShort());
107: }
108:
109: public void testReadUTF() throws IOException {
110: try {
111: String unexpected = this .sdis.readUTF();
112: fail("readUTF should be unsupported. ");
113: } catch (UnsupportedOperationException uoe) {
114: }
115: }
116:
117: public void testSkipBytes() throws IOException {
118: this .sdis.skipBytes(4);
119: assertEquals((int) 0x08070605, this.sdis.readInt());
120: }
121:
122: }
|