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        @Override
63        public boolean isAborted() {
64          return false;
65        }
66        
67      };
68      ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
69        name, abortable, true);
70      ZKTable zkt = new ZKTable(zkw);
71      assertTrue(zkt.isEnabledTable(name));
72      assertFalse(zkt.isDisablingTable(name));
73      assertFalse(zkt.isDisabledTable(name));
74      assertFalse(zkt.isEnablingTable(name));
75      assertFalse(zkt.isDisablingOrDisabledTable(name));
76      assertFalse(zkt.isDisabledOrEnablingTable(name));
77      zkt.setDisablingTable(name);
78      assertTrue(zkt.isDisablingTable(name));
79      assertTrue(zkt.isDisablingOrDisabledTable(name));
80      assertFalse(zkt.getDisabledTables().contains(name));
81      zkt.setDisabledTable(name);
82      assertTrue(zkt.isDisabledTable(name));
83      assertTrue(zkt.isDisablingOrDisabledTable(name));
84      assertFalse(zkt.isDisablingTable(name));
85      assertTrue(zkt.getDisabledTables().contains(name));
86      zkt.setEnablingTable(name);
87      assertTrue(zkt.isEnablingTable(name));
88      assertTrue(zkt.isDisabledOrEnablingTable(name));
89      assertFalse(zkt.isDisabledTable(name));
90      assertFalse(zkt.getDisabledTables().contains(name));
91      zkt.setEnabledTable(name);
92      assertTrue(zkt.isEnabledTable(name));
93      assertFalse(zkt.isEnablingTable(name));
94    }
95  }