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