View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.logging.impl.Log4JLogger;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.MasterNotRunningException;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
40  import org.apache.hadoop.hbase.client.HBaseAdmin;
41  import org.apache.hadoop.hbase.client.HTable;
42  import org.apache.hadoop.hbase.client.ScannerCallable;
43  import org.apache.hadoop.hbase.client.Table;
44  import org.apache.hadoop.hbase.ipc.AbstractRpcClient;
45  import org.apache.hadoop.hbase.ipc.RpcServer;
46  import org.apache.hadoop.hbase.testclassification.MediumTests;
47  import org.apache.hadoop.hbase.util.Bytes;
48  import org.apache.log4j.Level;
49  import org.junit.AfterClass;
50  import org.junit.BeforeClass;
51  import org.junit.experimental.categories.Category;
52  
53  /**
54   * By using this class as the super class of a set of tests you will have a HBase testing
55   * cluster available that is very suitable for writing tests for scanning and filtering against.
56   */
57  @Category({MediumTests.class})
58  public class FilterTestingCluster {
59    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
60    private static Configuration conf = null;
61    private static HBaseAdmin admin = null;
62    private static List<String> createdTables = new ArrayList<>();
63  
64    protected static void createTable(String tableName, String columnFamilyName) {
65      assertNotNull("HBaseAdmin is not initialized successfully.", admin);
66      HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
67      HColumnDescriptor colDef = new HColumnDescriptor(Bytes.toBytes(columnFamilyName));
68      desc.addFamily(colDef);
69  
70      try {
71        admin.createTable(desc);
72        createdTables.add(tableName);
73        assertTrue("Fail to create the table", admin.tableExists(tableName));
74      } catch (IOException e) {
75        assertNull("Exception found while creating table", e);
76      }
77    }
78  
79    protected static Table openTable(String tableName) throws IOException {
80      Table table = new HTable(conf, tableName);
81      assertTrue("Fail to create the table", admin.tableExists(tableName));
82      return table;
83    }
84  
85    private static void deleteTables() {
86      if (admin != null) {
87        for (String tableName: createdTables){
88          try {
89            if (admin.tableExists(tableName)) {
90              admin.disableTable(tableName);
91              admin.deleteTable(tableName);
92            }
93          } catch (IOException e) {
94            assertNull("Exception found deleting the table", e);
95          }
96        }
97      }
98    }
99  
100   private static void initialize(Configuration conf) {
101     FilterTestingCluster.conf = HBaseConfiguration.create(conf);
102     FilterTestingCluster.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
103     try {
104       admin = new HBaseAdmin(conf);
105     } catch (MasterNotRunningException e) {
106       assertNull("Master is not running", e);
107     } catch (ZooKeeperConnectionException e) {
108       assertNull("Cannot connect to Zookeeper", e);
109     } catch (IOException e) {
110       assertNull("IOException", e);
111     }
112   }
113 
114   @BeforeClass
115   public static void setUp() throws Exception {
116     ((Log4JLogger)RpcServer.LOG).getLogger().setLevel(Level.ALL);
117     ((Log4JLogger)AbstractRpcClient.LOG).getLogger().setLevel(Level.ALL);
118     ((Log4JLogger)ScannerCallable.LOG).getLogger().setLevel(Level.ALL);
119     TEST_UTIL.startMiniCluster(1);
120     initialize(TEST_UTIL.getConfiguration());
121   }
122 
123   @AfterClass
124   public static void tearDown() throws Exception {
125     deleteTables();
126     TEST_UTIL.shutdownMiniCluster();
127   }
128 
129 }