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;
019
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.FSTableDescriptors;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034import org.junit.jupiter.api.TestInfo;
035
036@Tag(MiscTests.TAG)
037@Tag(SmallTests.TAG)
038public class TestFSTableDescriptorForceCreation {
039
040  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
041
042  private String name;
043
044  @BeforeEach
045  public void setUp(TestInfo testInfo) {
046    name = testInfo.getTestMethod().get().getName();
047  }
048
049  @Test
050  public void testShouldCreateNewTableDescriptorIfForcefulCreationIsFalse() throws IOException {
051    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
052    Path rootdir = new Path(UTIL.getDataTestDir(), name);
053    try (FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir)) {
054      assertTrue(
055        fstd.createTableDescriptor(
056          TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build(), false),
057        "Should create new table descriptor");
058    }
059  }
060
061  @Test
062  public void testShouldNotCreateTheSameTableDescriptorIfForcefulCreationIsFalse()
063    throws IOException {
064    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
065    // Cleanup old tests if any detritus laying around.
066    Path rootdir = new Path(UTIL.getDataTestDir(), name);
067    try (FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir)) {
068      TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
069      fstd.update(htd);
070      assertFalse(fstd.createTableDescriptor(htd, false), "Should not create new table descriptor");
071    }
072  }
073
074  @Test
075  public void testShouldAllowForcefulCreationOfAlreadyExistingTableDescriptor() throws Exception {
076    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
077    Path rootdir = new Path(UTIL.getDataTestDir(), name);
078    try (FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir)) {
079      TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
080      fstd.createTableDescriptor(htd, false);
081      assertTrue(fstd.createTableDescriptor(htd, true), "Should create new table descriptor");
082    }
083  }
084
085}