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.master; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import org.apache.hadoop.hbase.HBaseTestingUtil; 023import org.apache.hadoop.hbase.TableName; 024import org.apache.hadoop.hbase.client.Admin; 025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 026import org.apache.hadoop.hbase.client.TableDescriptor; 027import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 028import org.apache.hadoop.hbase.testclassification.MasterTests; 029import org.apache.hadoop.hbase.testclassification.MediumTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.junit.jupiter.api.AfterAll; 032import org.junit.jupiter.api.BeforeAll; 033import org.junit.jupiter.api.BeforeEach; 034import org.junit.jupiter.api.Tag; 035import org.junit.jupiter.api.Test; 036 037@Tag(MasterTests.TAG) 038@Tag(MediumTests.TAG) 039public class TestListTablesByState { 040 041 private static HBaseTestingUtil UTIL; 042 private static Admin ADMIN; 043 private static final int SLAVES = 1; 044 private static final byte[] COLUMN = Bytes.toBytes("cf"); 045 private static final TableName TABLE = TableName.valueOf("test"); 046 private static final TableDescriptor TABLE_DESC = TableDescriptorBuilder.newBuilder(TABLE) 047 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN).setMaxVersions(3).build()) 048 .build(); 049 050 @BeforeAll 051 public static void setUpBeforeClass() throws Exception { 052 UTIL = new HBaseTestingUtil(); 053 UTIL.startMiniCluster(SLAVES); 054 ADMIN = UTIL.getAdmin(); 055 } 056 057 @BeforeEach 058 public void before() throws Exception { 059 if (ADMIN.tableExists(TABLE)) { 060 if (ADMIN.isTableEnabled(TABLE)) { 061 ADMIN.disableTable(TABLE); 062 } 063 064 ADMIN.deleteTable(TABLE); 065 } 066 } 067 068 @Test 069 public void testListTableNamesByState() throws Exception { 070 ADMIN.createTable(TABLE_DESC); 071 ADMIN.disableTable(TABLE); 072 assertEquals(ADMIN.listTableNamesByState(false).get(0), TABLE); 073 ADMIN.enableTable(TABLE); 074 assertEquals(ADMIN.listTableNamesByState(true).get(0), TABLE); 075 } 076 077 @Test 078 public void testListTableDescriptorByState() throws Exception { 079 ADMIN.createTable(TABLE_DESC); 080 ADMIN.disableTable(TABLE); 081 assertEquals(ADMIN.listTableDescriptorsByState(false).get(0).getTableName(), TABLE); 082 ADMIN.enableTable(TABLE); 083 assertEquals(ADMIN.listTableDescriptorsByState(true).get(0).getTableName(), TABLE); 084 } 085 086 @AfterAll 087 public static void tearDownAfterClass() throws Exception { 088 if (ADMIN != null) { 089 ADMIN.close(); 090 } 091 if (UTIL != null) { 092 UTIL.shutdownMiniCluster(); 093 } 094 } 095}