1   /**
2    * Copyright 2010 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.zookeeper;
21  
22  
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.Abortable;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
33  import org.apache.zookeeper.KeeperException;
34  import org.junit.AfterClass;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  
38  public class TestZKTable {
39    private static final Log LOG = LogFactory.getLog(TestZooKeeperNodeTracker.class);
40    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
41  
42    @BeforeClass
43    public static void setUpBeforeClass() throws Exception {
44      TEST_UTIL.startMiniZKCluster();
45    }
46  
47    @AfterClass
48    public static void tearDownAfterClass() throws Exception {
49      TEST_UTIL.shutdownMiniZKCluster();
50    }
51  
52    @Test
53    public void testTableStates()
54    throws ZooKeeperConnectionException, IOException, KeeperException {
55      final String name = "testDisabled";
56      Abortable abortable = new Abortable() {
57        @Override
58        public void abort(String why, Throwable e) {
59          LOG.info(why, e);
60        }
61      };
62      ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
63        name, abortable);
64      ZKTable zkt = new ZKTable(zkw);
65      assertTrue(zkt.isEnabledTable(name));
66      assertFalse(zkt.isDisablingTable(name));
67      assertFalse(zkt.isDisabledTable(name));
68      assertFalse(zkt.isEnablingTable(name));
69      assertFalse(zkt.isDisablingOrDisabledTable(name));
70      assertFalse(zkt.isDisabledOrEnablingTable(name));
71      zkt.setDisablingTable(name);
72      assertTrue(zkt.isDisablingTable(name));
73      assertTrue(zkt.isDisablingOrDisabledTable(name));
74      assertFalse(zkt.getDisabledTables().contains(name));
75      zkt.setDisabledTable(name);
76      assertTrue(zkt.isDisabledTable(name));
77      assertTrue(zkt.isDisablingOrDisabledTable(name));
78      assertFalse(zkt.isDisablingTable(name));
79      assertTrue(zkt.getDisabledTables().contains(name));
80      zkt.setEnablingTable(name);
81      assertTrue(zkt.isEnablingTable(name));
82      assertTrue(zkt.isDisabledOrEnablingTable(name));
83      assertFalse(zkt.isDisabledTable(name));
84      assertFalse(zkt.getDisabledTables().contains(name));
85      zkt.setEnabledTable(name);
86      assertTrue(zkt.isEnabledTable(name));
87      assertFalse(zkt.isEnablingTable(name));
88    }
89  }