[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.mapred; |
---|
| 19 | |
---|
| 20 | import org.apache.hadoop.fs.ChecksumException; |
---|
| 21 | import org.apache.hadoop.io.DataInputBuffer; |
---|
| 22 | import org.apache.hadoop.io.DataOutputBuffer; |
---|
| 23 | |
---|
| 24 | import junit.framework.TestCase; |
---|
| 25 | |
---|
| 26 | public class TestIFileStreams extends TestCase { |
---|
| 27 | |
---|
| 28 | public void testIFileStream() throws Exception { |
---|
| 29 | final int DLEN = 100; |
---|
| 30 | DataOutputBuffer dob = new DataOutputBuffer(DLEN + 4); |
---|
| 31 | IFileOutputStream ifos = new IFileOutputStream(dob); |
---|
| 32 | for (int i = 0; i < DLEN; ++i) { |
---|
| 33 | ifos.write(i); |
---|
| 34 | } |
---|
| 35 | ifos.close(); |
---|
| 36 | DataInputBuffer dib = new DataInputBuffer(); |
---|
| 37 | dib.reset(dob.getData(), DLEN + 4); |
---|
| 38 | IFileInputStream ifis = new IFileInputStream(dib, 104); |
---|
| 39 | for (int i = 0; i < DLEN; ++i) { |
---|
| 40 | assertEquals(i, ifis.read()); |
---|
| 41 | } |
---|
| 42 | ifis.close(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | public void testBadIFileStream() throws Exception { |
---|
| 46 | final int DLEN = 100; |
---|
| 47 | DataOutputBuffer dob = new DataOutputBuffer(DLEN + 4); |
---|
| 48 | IFileOutputStream ifos = new IFileOutputStream(dob); |
---|
| 49 | for (int i = 0; i < DLEN; ++i) { |
---|
| 50 | ifos.write(i); |
---|
| 51 | } |
---|
| 52 | ifos.close(); |
---|
| 53 | DataInputBuffer dib = new DataInputBuffer(); |
---|
| 54 | final byte[] b = dob.getData(); |
---|
| 55 | ++b[17]; |
---|
| 56 | dib.reset(b, DLEN + 4); |
---|
| 57 | IFileInputStream ifis = new IFileInputStream(dib, 104); |
---|
| 58 | int i = 0; |
---|
| 59 | try { |
---|
| 60 | while (i < DLEN) { |
---|
| 61 | if (17 == i) { |
---|
| 62 | assertEquals(18, ifis.read()); |
---|
| 63 | } else { |
---|
| 64 | assertEquals(i, ifis.read()); |
---|
| 65 | } |
---|
| 66 | ++i; |
---|
| 67 | } |
---|
| 68 | ifis.close(); |
---|
| 69 | } catch (ChecksumException e) { |
---|
| 70 | assertEquals("Unexpected bad checksum", DLEN - 1, i); |
---|
| 71 | return; |
---|
| 72 | } |
---|
| 73 | fail("Did not detect bad data in checksum"); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public void testBadLength() throws Exception { |
---|
| 77 | final int DLEN = 100; |
---|
| 78 | DataOutputBuffer dob = new DataOutputBuffer(DLEN + 4); |
---|
| 79 | IFileOutputStream ifos = new IFileOutputStream(dob); |
---|
| 80 | for (int i = 0; i < DLEN; ++i) { |
---|
| 81 | ifos.write(i); |
---|
| 82 | } |
---|
| 83 | ifos.close(); |
---|
| 84 | DataInputBuffer dib = new DataInputBuffer(); |
---|
| 85 | dib.reset(dob.getData(), DLEN + 4); |
---|
| 86 | IFileInputStream ifis = new IFileInputStream(dib, 100); |
---|
| 87 | int i = 0; |
---|
| 88 | try { |
---|
| 89 | while (i < DLEN - 8) { |
---|
| 90 | assertEquals(i++, ifis.read()); |
---|
| 91 | } |
---|
| 92 | ifis.close(); |
---|
| 93 | } catch (ChecksumException e) { |
---|
| 94 | assertEquals("Checksum before close", i, DLEN - 8); |
---|
| 95 | return; |
---|
| 96 | } |
---|
| 97 | fail("Did not detect bad data in checksum"); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | } |
---|