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.encoding; 019 020import static org.junit.jupiter.api.Assertions.assertSame; 021import static org.junit.jupiter.api.Assertions.assertThrows; 022import static org.mockito.ArgumentMatchers.any; 023import static org.mockito.Mockito.mockStatic; 024 025import org.apache.hadoop.hbase.io.compress.Compression.Algorithm; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.hadoop.util.ReflectionUtils; 029import org.junit.jupiter.api.Tag; 030import org.junit.jupiter.api.Test; 031import org.mockito.MockedStatic; 032 033/** 034 * Test for HBASE-23342 035 */ 036@Tag(MiscTests.TAG) 037@Tag(SmallTests.TAG) 038public class TestEncodedDataBlock { 039 040 private static final byte[] INPUT_BYTES = new byte[] { 0, 1, 0, 0, 1, 2, 3, 0, 0, 1, 0, 0, 1, 2, 041 3, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 1, 0, 0, 1, 2, 3, 0 }; 042 043 @SuppressWarnings("unchecked") 044 @Test 045 public void testGetCompressedSize() throws Exception { 046 RuntimeException inject = new RuntimeException("inject error"); 047 try (MockedStatic<ReflectionUtils> mockedReflectionUtils = mockStatic(ReflectionUtils.class)) { 048 mockedReflectionUtils.when(() -> ReflectionUtils.newInstance(any(Class.class), any())) 049 .thenThrow(inject); 050 RuntimeException error = assertThrows(RuntimeException.class, 051 () -> EncodedDataBlock.getCompressedSize(Algorithm.GZ, null, INPUT_BYTES, 0, 0)); 052 // make sure we get the injected error instead of NPE 053 assertSame(inject, error); 054 } 055 } 056}