001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.ByteBuffer;
025import java.util.EnumSet;
026import org.apache.hadoop.fs.ByteBufferReadable;
027import org.apache.hadoop.fs.CanSetDropBehind;
028import org.apache.hadoop.fs.CanSetReadahead;
029import org.apache.hadoop.fs.CanUnbuffer;
030import org.apache.hadoop.fs.FSDataInputStream;
031import org.apache.hadoop.fs.FSInputStream;
032import org.apache.hadoop.fs.HasEnhancedByteBufferAccess;
033import org.apache.hadoop.fs.ReadOption;
034import org.apache.hadoop.hbase.HBaseClassTestRule;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.io.ByteBufferPool;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041@Category(SmallTests.class)
042public class TestFSDataInputStreamWrapper {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestFSDataInputStreamWrapper.class);
047
048  @Test
049  public void testUnbuffer() throws Exception {
050    InputStream pc = new ParentClass();
051    FSDataInputStreamWrapper fsdisw1 = new FSDataInputStreamWrapper(new FSDataInputStream(pc));
052    fsdisw1.unbuffer();
053    // parent class should be true
054    assertTrue(((ParentClass) pc).getIsCallUnbuffer());
055    fsdisw1.close();
056
057    InputStream cc1 = new ChildClass1();
058    FSDataInputStreamWrapper fsdisw2 = new FSDataInputStreamWrapper(new FSDataInputStream(cc1));
059    fsdisw2.unbuffer();
060    // child1 class should be true
061    assertTrue(((ChildClass1) cc1).getIsCallUnbuffer());
062    fsdisw2.close();
063  }
064
065  private class ParentClass extends FSInputStream implements ByteBufferReadable, CanSetDropBehind,
066    CanSetReadahead, HasEnhancedByteBufferAccess, CanUnbuffer {
067
068    public boolean isCallUnbuffer = false;
069
070    public boolean getIsCallUnbuffer() {
071      return isCallUnbuffer;
072    }
073
074    @Override
075    public void unbuffer() {
076      isCallUnbuffer = true;
077    }
078
079    @Override
080    public int read() throws IOException {
081      return 0;
082    }
083
084    @Override
085    public ByteBuffer read(ByteBufferPool paramByteBufferPool, int paramInt,
086      EnumSet<ReadOption> paramEnumSet) throws IOException, UnsupportedOperationException {
087      return null;
088    }
089
090    @Override
091    public void releaseBuffer(ByteBuffer paramByteBuffer) {
092
093    }
094
095    @Override
096    public void setReadahead(Long paramLong) throws IOException, UnsupportedOperationException {
097
098    }
099
100    @Override
101    public void setDropBehind(Boolean paramBoolean)
102      throws IOException, UnsupportedOperationException {
103
104    }
105
106    @Override
107    public int read(ByteBuffer paramByteBuffer) throws IOException {
108      return 0;
109    }
110
111    @Override
112    public void seek(long paramLong) throws IOException {
113
114    }
115
116    @Override
117    public long getPos() throws IOException {
118      return 0;
119    }
120
121    @Override
122    public boolean seekToNewSource(long paramLong) throws IOException {
123      return false;
124    }
125  }
126
127  private class ChildClass1 extends ParentClass {
128    @Override
129    public void unbuffer() {
130      isCallUnbuffer = true;
131    }
132  }
133}