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.filter; 019 020import static org.junit.Assert.assertNotNull; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.List; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseConfiguration; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.MasterNotRunningException; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.ZooKeeperConnectionException; 034import org.apache.hadoop.hbase.client.Admin; 035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 036import org.apache.hadoop.hbase.client.Table; 037import org.apache.hadoop.hbase.client.TableDescriptor; 038import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 039import org.apache.hadoop.hbase.testclassification.FilterTests; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.junit.AfterClass; 043import org.junit.BeforeClass; 044import org.junit.experimental.categories.Category; 045 046/** 047 * By using this class as the super class of a set of tests you will have a HBase testing cluster 048 * available that is very suitable for writing tests for scanning and filtering against. 049 */ 050@Category({ FilterTests.class, MediumTests.class }) 051public class FilterTestingCluster { 052 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 053 private static Admin admin = null; 054 private static List<TableName> createdTables = new ArrayList<>(); 055 056 protected static void createTable(TableName tableName, String columnFamilyName) { 057 assertNotNull("HBaseAdmin is not initialized successfully.", admin); 058 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 059 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes(columnFamilyName))).build(); 060 061 try { 062 admin.createTable(tableDescriptor); 063 createdTables.add(tableName); 064 assertTrue("Fail to create the table", admin.tableExists(tableName)); 065 } catch (IOException e) { 066 assertNull("Exception found while creating table", e); 067 } 068 } 069 070 protected static Table openTable(TableName tableName) throws IOException { 071 Table table = TEST_UTIL.getConnection().getTable(tableName); 072 assertTrue("Fail to create the table", admin.tableExists(tableName)); 073 return table; 074 } 075 076 private static void deleteTables() { 077 if (admin != null) { 078 for (TableName tableName : createdTables) { 079 try { 080 if (admin.tableExists(tableName)) { 081 admin.disableTable(tableName); 082 admin.deleteTable(tableName); 083 } 084 } catch (IOException e) { 085 assertNull("Exception found deleting the table", e); 086 } 087 } 088 } 089 } 090 091 private static void initialize(Configuration conf) { 092 conf = HBaseConfiguration.create(conf); 093 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 094 try { 095 admin = TEST_UTIL.getAdmin(); 096 } catch (MasterNotRunningException e) { 097 assertNull("Master is not running", e); 098 } catch (ZooKeeperConnectionException e) { 099 assertNull("Cannot connect to ZooKeeper", e); 100 } catch (IOException e) { 101 assertNull("IOException", e); 102 } 103 } 104 105 @BeforeClass 106 public static void setUp() throws Exception { 107 TEST_UTIL.startMiniCluster(1); 108 initialize(TEST_UTIL.getConfiguration()); 109 } 110 111 @AfterClass 112 public static void tearDown() throws Exception { 113 deleteTables(); 114 TEST_UTIL.shutdownMiniCluster(); 115 } 116}