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.client;
019
020import static org.junit.Assert.fail;
021
022import java.io.IOException;
023import java.util.Arrays;
024import java.util.List;
025import java.util.function.Consumer;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HColumnDescriptor;
028import org.apache.hadoop.hbase.HTableDescriptor;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.BuilderStyleTest;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039
040@Category({ ClientTests.class, SmallTests.class })
041public class TestImmutableHTableDescriptor {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestImmutableHTableDescriptor.class);
046
047  @Rule
048  public TestName name = new TestName();
049  private static final List<Consumer<ImmutableHTableDescriptor>> TEST_FUNCTION = Arrays.asList(
050    htd -> htd.setValue("a", "a"), htd -> htd.setValue(Bytes.toBytes("a"), Bytes.toBytes("a")),
051    htd -> htd.setValue(new Bytes(Bytes.toBytes("a")), new Bytes(Bytes.toBytes("a"))),
052    htd -> htd.setCompactionEnabled(false), htd -> htd.setConfiguration("aaa", "ccc"),
053    htd -> htd.setDurability(Durability.USE_DEFAULT), htd -> htd.setFlushPolicyClassName("class"),
054    htd -> htd.setMaxFileSize(123), htd -> htd.setMemStoreFlushSize(123123123),
055    htd -> htd.setNormalizationEnabled(false), htd -> htd.setPriority(123),
056    htd -> htd.setReadOnly(true), htd -> htd.setRegionMemstoreReplication(true),
057    htd -> htd.setRegionReplication(123), htd -> htd.setRegionSplitPolicyClassName("class"),
058    htd -> htd.addFamily(new HColumnDescriptor(Bytes.toBytes("fm"))),
059    htd -> htd.remove(new Bytes(Bytes.toBytes("aaa"))), htd -> htd.remove("aaa"),
060    htd -> htd.remove(Bytes.toBytes("aaa")), htd -> htd.removeConfiguration("xxx"),
061    htd -> htd.removeFamily(Bytes.toBytes("fm")), htd -> {
062      try {
063        htd.addCoprocessor("xxx");
064      } catch (IOException e) {
065        throw new RuntimeException(e);
066      }
067    });
068
069  @Test
070  public void testImmutable() {
071    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName()));
072    ImmutableHTableDescriptor immutableHtd = new ImmutableHTableDescriptor(htd);
073    TEST_FUNCTION.forEach(f -> {
074      try {
075        f.accept(immutableHtd);
076        fail("ImmutableHTableDescriptor can't be modified!!!");
077      } catch (UnsupportedOperationException e) {
078      }
079    });
080  }
081
082  @Test
083  public void testImmutableHColumnDescriptor() {
084    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName()));
085    htd.addFamily(new HColumnDescriptor(Bytes.toBytes("family")));
086    ImmutableHTableDescriptor immutableHtd = new ImmutableHTableDescriptor(htd);
087    for (HColumnDescriptor hcd : immutableHtd.getColumnFamilies()) {
088      assertReadOnly(hcd);
089    }
090    for (HColumnDescriptor hcd : immutableHtd.getFamilies()) {
091      assertReadOnly(hcd);
092    }
093  }
094
095  private void assertReadOnly(HColumnDescriptor hcd) {
096    try {
097      hcd.setBlocksize(10);
098      fail("ImmutableHColumnDescriptor can't be modified!!!");
099    } catch (UnsupportedOperationException e) {
100      // Expected
101    }
102  }
103
104  @Test
105  public void testClassMethodsAreBuilderStyle() {
106    /*
107     * ImmutableHTableDescriptor should have a builder style setup where setXXX/addXXX methods can
108     * be chainable together: . For example: ImmutableHTableDescriptor d = new
109     * ImmutableHTableDescriptor() .setFoo(foo) .setBar(bar) .setBuz(buz) This test ensures that all
110     * methods starting with "set" returns the declaring object
111     */
112
113    BuilderStyleTest.assertClassesAreBuilderStyle(ImmutableHTableDescriptor.class);
114  }
115}