1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.util.FSTableDescriptors;
30  import org.junit.*;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestFSTableDescriptorForceCreation {
35    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
36  
37    @Test
38    public void testShouldCreateNewTableDescriptorIfForcefulCreationIsFalse()
39        throws IOException {
40      final String name = "newTable2";
41      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
42      Path rootdir = new Path(UTIL.getDataTestDir(), name);
43      HTableDescriptor htd = new HTableDescriptor(name);
44  
45      assertTrue("Should create new table descriptor",
46        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false));
47    }
48  
49    @Test
50    public void testShouldNotCreateTheSameTableDescriptorIfForcefulCreationIsFalse()
51        throws IOException {
52      final String name = "testAlreadyExists";
53      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
54      // Cleanup old tests if any detrius laying around.
55      Path rootdir = new Path(UTIL.getDataTestDir(), name);
56      TableDescriptors htds = new FSTableDescriptors(fs, rootdir);
57      HTableDescriptor htd = new HTableDescriptor(name);
58      htds.add(htd);
59      assertFalse("Should not create new table descriptor",
60        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false));
61    }
62  
63    @Test
64    public void testShouldAllowForcefulCreationOfAlreadyExistingTableDescriptor()
65        throws Exception {
66      final String name = "createNewTableNew2";
67      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
68      Path rootdir = new Path(UTIL.getDataTestDir(), name);
69      HTableDescriptor htd = new HTableDescriptor(name);
70      FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false);
71      assertTrue("Should create new table descriptor",
72        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, true));
73    }
74  
75    @org.junit.Rule
76    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
77      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
78  }
79