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.hfile; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotEquals; 022import static org.junit.Assert.assertTrue; 023 024import java.io.ByteArrayOutputStream; 025import java.io.PrintStream; 026 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.fs.FileSystem; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.regionserver.TestHRegionServerBulkLoad; 033import org.apache.hadoop.hbase.testclassification.IOTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.FSUtils; 037import org.junit.After; 038import org.junit.Before; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045@Category({IOTests.class, SmallTests.class}) 046public class TestHFilePrettyPrinter { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestHFilePrettyPrinter.class); 051 052 private static final Logger LOG = LoggerFactory.getLogger(TestHFilePrettyPrinter.class); 053 054 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility(); 055 private static FileSystem fs; 056 private static Configuration conf; 057 private static byte [] cf = Bytes.toBytes("cf"); 058 private static byte [] fam = Bytes.toBytes("fam"); 059 private static byte [] value = Bytes.toBytes("val"); 060 private static PrintStream original; 061 private static PrintStream ps; 062 private static ByteArrayOutputStream stream; 063 064 @Before 065 public void setup() throws Exception { 066 conf = UTIL.getConfiguration(); 067 fs = UTIL.getTestFileSystem(); 068 stream = new ByteArrayOutputStream(); 069 ps = new PrintStream(stream); 070 } 071 072 @After 073 public void teardown() { 074 original = System.out; 075 System.setOut(original); 076 } 077 078 @Test 079 public void testHFilePrettyPrinterNonRootDir() throws Exception { 080 Path fileNotInRootDir = UTIL.getDataTestDir("hfile"); 081 TestHRegionServerBulkLoad.createHFile(fs, fileNotInRootDir, cf, fam, value, 1000); 082 assertNotEquals("directory used is not an HBase root dir", 083 UTIL.getDefaultRootDirPath(), fileNotInRootDir); 084 085 System.setOut(ps); 086 new HFilePrettyPrinter(conf).run(new String[]{"-v", String.valueOf(fileNotInRootDir)}); 087 String result = new String(stream.toByteArray()); 088 String expectedResult = "Scanning -> " + fileNotInRootDir + "\n" + "Scanned kv count -> 1000\n"; 089 assertEquals(expectedResult, result); 090 } 091 092 @Test 093 public void testHFilePrettyPrinterRootDir() throws Exception { 094 Path rootPath = FSUtils.getRootDir(conf); 095 String rootString = rootPath + rootPath.SEPARATOR; 096 Path fileInRootDir = new Path(rootString + "hfile"); 097 TestHRegionServerBulkLoad.createHFile(fs, fileInRootDir, cf, fam, value, 1000); 098 assertTrue("directory used is a root dir", 099 fileInRootDir.toString().startsWith(rootString)); 100 101 System.setOut(ps); 102 HFilePrettyPrinter printer = new HFilePrettyPrinter(); 103 printer.setConf(conf); 104 printer.processFile(fileInRootDir, true); 105 printer.run(new String[]{"-v", String.valueOf(fileInRootDir)}); 106 String result = new String(stream.toByteArray()); 107 String expectedResult = "Scanning -> " + fileInRootDir + "\n" + "Scanned kv count -> 1000\n"; 108 assertEquals(expectedResult, result); 109 } 110}