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; 026 027import org.apache.hadoop.fs.ByteBufferReadable; 028import org.apache.hadoop.fs.CanSetDropBehind; 029import org.apache.hadoop.fs.CanSetReadahead; 030import org.apache.hadoop.fs.CanUnbuffer; 031import org.apache.hadoop.fs.FSDataInputStream; 032import org.apache.hadoop.fs.FSInputStream; 033import org.apache.hadoop.fs.HasEnhancedByteBufferAccess; 034import org.apache.hadoop.fs.ReadOption; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.testclassification.SmallTests; 037import org.apache.hadoop.io.ByteBufferPool; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042@Category(SmallTests.class) 043public class TestFSDataInputStreamWrapper { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestFSDataInputStreamWrapper.class); 048 049 @Test 050 public void testUnbuffer() throws Exception { 051 InputStream pc = new ParentClass(); 052 FSDataInputStreamWrapper fsdisw1 = 053 new FSDataInputStreamWrapper(new FSDataInputStream(pc)); 054 fsdisw1.unbuffer(); 055 // parent class should be true 056 assertTrue(((ParentClass)pc).getIsCallUnbuffer()); 057 fsdisw1.close(); 058 059 InputStream cc1 = new ChildClass1(); 060 FSDataInputStreamWrapper fsdisw2 = 061 new FSDataInputStreamWrapper(new FSDataInputStream(cc1)); 062 fsdisw2.unbuffer(); 063 // child1 class should be true 064 assertTrue(((ChildClass1)cc1).getIsCallUnbuffer()); 065 fsdisw2.close(); 066 } 067 068 private class ParentClass extends FSInputStream 069 implements ByteBufferReadable, CanSetDropBehind, CanSetReadahead, 070 HasEnhancedByteBufferAccess, CanUnbuffer { 071 072 public boolean isCallUnbuffer = false; 073 074 public boolean getIsCallUnbuffer(){ 075 return isCallUnbuffer; 076 } 077 078 @Override 079 public void unbuffer() { 080 isCallUnbuffer = true; 081 } 082 083 @Override 084 public int read() throws IOException { 085 return 0; 086 } 087 088 @Override 089 public ByteBuffer read(ByteBufferPool paramByteBufferPool, 090 int paramInt, EnumSet<ReadOption> paramEnumSet) 091 throws IOException, UnsupportedOperationException { 092 return null; 093 } 094 095 @Override 096 public void releaseBuffer(ByteBuffer paramByteBuffer) { 097 098 } 099 100 @Override 101 public void setReadahead(Long paramLong) 102 throws IOException, UnsupportedOperationException { 103 104 } 105 106 @Override 107 public void setDropBehind(Boolean paramBoolean) 108 throws IOException, UnsupportedOperationException { 109 110 } 111 112 @Override 113 public int read(ByteBuffer paramByteBuffer) throws IOException { 114 return 0; 115 } 116 117 @Override 118 public void seek(long paramLong) throws IOException { 119 120 } 121 122 @Override 123 public long getPos() throws IOException { 124 return 0; 125 } 126 127 @Override 128 public boolean seekToNewSource(long paramLong) throws IOException { 129 return false; 130 } 131 } 132 133 private class ChildClass1 extends ParentClass{ 134 @Override 135 public void unbuffer() { 136 isCallUnbuffer = true; 137 } 138 } 139}