001/*
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase.filter;
021
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertNull;
024import static org.junit.Assert.assertTrue;
025
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.List;
029
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.HColumnDescriptor;
034import org.apache.hadoop.hbase.HConstants;
035import org.apache.hadoop.hbase.HTableDescriptor;
036import org.apache.hadoop.hbase.MasterNotRunningException;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.ZooKeeperConnectionException;
039import org.apache.hadoop.hbase.client.Admin;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.testclassification.FilterTests;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.junit.AfterClass;
045import org.junit.BeforeClass;
046import org.junit.experimental.categories.Category;
047
048/**
049 * By using this class as the super class of a set of tests you will have a HBase testing
050 * cluster available that is very suitable for writing tests for scanning and filtering against.
051 */
052@Category({FilterTests.class, MediumTests.class})
053public class FilterTestingCluster {
054  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
055  private static Admin admin = null;
056  private static List<TableName> createdTables = new ArrayList<>();
057
058  protected static void createTable(TableName tableName, String columnFamilyName) {
059    assertNotNull("HBaseAdmin is not initialized successfully.", admin);
060    HTableDescriptor desc = new HTableDescriptor(tableName);
061    HColumnDescriptor colDef = new HColumnDescriptor(Bytes.toBytes(columnFamilyName));
062    desc.addFamily(colDef);
063
064    try {
065      admin.createTable(desc);
066      createdTables.add(tableName);
067      assertTrue("Fail to create the table", admin.tableExists(tableName));
068    } catch (IOException e) {
069      assertNull("Exception found while creating table", e);
070    }
071  }
072
073  protected static Table openTable(TableName tableName) throws IOException {
074    Table table = TEST_UTIL.getConnection().getTable(tableName);
075    assertTrue("Fail to create the table", admin.tableExists(tableName));
076    return table;
077  }
078
079  private static void deleteTables() {
080    if (admin != null) {
081      for (TableName tableName: createdTables){
082        try {
083          if (admin.tableExists(tableName)) {
084            admin.disableTable(tableName);
085            admin.deleteTable(tableName);
086          }
087        } catch (IOException e) {
088          assertNull("Exception found deleting the table", e);
089        }
090      }
091    }
092  }
093
094  private static void initialize(Configuration conf) {
095    conf = HBaseConfiguration.create(conf);
096    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
097    try {
098      admin = TEST_UTIL.getAdmin();
099    } catch (MasterNotRunningException e) {
100      assertNull("Master is not running", e);
101    } catch (ZooKeeperConnectionException e) {
102      assertNull("Cannot connect to ZooKeeper", e);
103    } catch (IOException e) {
104      assertNull("IOException", e);
105    }
106  }
107
108  @BeforeClass
109  public static void setUp() throws Exception {
110    TEST_UTIL.startMiniCluster(1);
111    initialize(TEST_UTIL.getConfiguration());
112  }
113
114  @AfterClass
115  public static void tearDown() throws Exception {
116    deleteTables();
117    TEST_UTIL.shutdownMiniCluster();
118  }
119}