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  
32  public class TestFSTableDescriptorForceCreation {
33    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
34  
35    @Test
36    public void testShouldCreateNewTableDescriptorIfForcefulCreationIsFalse()
37        throws IOException {
38      final String name = "newTable2";
39      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
40      Path rootdir = new Path(UTIL.getDataTestDir(), name);
41      HTableDescriptor htd = new HTableDescriptor(name);
42  
43      assertTrue("Should create new table descriptor",
44        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false));
45    }
46  
47    @Test
48    public void testShouldNotCreateTheSameTableDescriptorIfForcefulCreationIsFalse()
49        throws IOException {
50      final String name = "testAlreadyExists";
51      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
52      // Cleanup old tests if any detrius laying around.
53      Path rootdir = new Path(UTIL.getDataTestDir(), name);
54      TableDescriptors htds = new FSTableDescriptors(fs, rootdir);
55      HTableDescriptor htd = new HTableDescriptor(name);
56      htds.add(htd);
57      assertFalse("Should not create new table descriptor",
58        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false));
59    }
60  
61    @Test
62    public void testShouldAllowForcefulCreationOfAlreadyExistingTableDescriptor()
63        throws Exception {
64      final String name = "createNewTableNew2";
65      FileSystem fs = FileSystem.get(UTIL.getConfiguration());
66      Path rootdir = new Path(UTIL.getDataTestDir(), name);
67      HTableDescriptor htd = new HTableDescriptor(name);
68      FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, false);
69      assertTrue("Should create new table descriptor",
70        FSTableDescriptors.createTableDescriptor(fs, rootdir, htd, true));
71    }
72  }