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.Assert.assertFalse;
021import static org.junit.Assert.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.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036
037@Category({ MiscTests.class, SmallTests.class })
038public class TestFSTableDescriptorForceCreation {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestFSTableDescriptorForceCreation.class);
043
044  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
045
046  @Rule
047  public TestName name = new TestName();
048
049  @Test
050  public void testShouldCreateNewTableDescriptorIfForcefulCreationIsFalse() throws IOException {
051    final String name = this.name.getMethodName();
052    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
053    Path rootdir = new Path(UTIL.getDataTestDir(), name);
054    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
055
056    assertTrue("Should create new table descriptor", fstd.createTableDescriptor(
057      TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build(), false));
058  }
059
060  @Test
061  public void testShouldNotCreateTheSameTableDescriptorIfForcefulCreationIsFalse()
062    throws IOException {
063    final String name = this.name.getMethodName();
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    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
068    TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
069    fstd.update(htd);
070    assertFalse("Should not create new table descriptor", fstd.createTableDescriptor(htd, false));
071  }
072
073  @Test
074  public void testShouldAllowForcefulCreationOfAlreadyExistingTableDescriptor() throws Exception {
075    final String name = this.name.getMethodName();
076    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
077    Path rootdir = new Path(UTIL.getDataTestDir(), name);
078    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
079    TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
080    fstd.createTableDescriptor(htd, false);
081    assertTrue("Should create new table descriptor", fstd.createTableDescriptor(htd, true));
082  }
083
084}