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()
051      throws IOException {
052    final String name = this.name.getMethodName();
053    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
054    Path rootdir = new Path(UTIL.getDataTestDir(), name);
055    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
056
057    assertTrue("Should create new table descriptor",
058      fstd.createTableDescriptor(TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build(), false));
059  }
060
061  @Test
062  public void testShouldNotCreateTheSameTableDescriptorIfForcefulCreationIsFalse()
063      throws IOException {
064    final String name = this.name.getMethodName();
065    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
066    // Cleanup old tests if any detritus laying around.
067    Path rootdir = new Path(UTIL.getDataTestDir(), name);
068    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
069    TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
070    fstd.update(htd);
071    assertFalse("Should not create new table descriptor", fstd.createTableDescriptor(htd, false));
072  }
073
074  @Test
075  public void testShouldAllowForcefulCreationOfAlreadyExistingTableDescriptor()
076      throws Exception {
077    final String name = this.name.getMethodName();
078    FileSystem fs = FileSystem.get(UTIL.getConfiguration());
079    Path rootdir = new Path(UTIL.getDataTestDir(), name);
080    FSTableDescriptors fstd = new FSTableDescriptors(fs, rootdir);
081    TableDescriptor htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(name)).build();
082    fstd.createTableDescriptor(htd, false);
083    assertTrue("Should create new table descriptor",
084        fstd.createTableDescriptor(htd, true));
085  }
086
087}
088