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