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.assertEquals;
021
022import java.lang.reflect.InvocationTargetException;
023import java.lang.reflect.Method;
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027import java.util.NoSuchElementException;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * Testcase for HBASE-21732. Make sure that all enum configurations can accept lower case value.
040 */
041@Category({ ClientTests.class, SmallTests.class })
042public class TestColumnFamilyDescriptorLowerCaseEnum {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestColumnFamilyDescriptorLowerCaseEnum.class);
047
048  private static final Logger LOG =
049    LoggerFactory.getLogger(TestColumnFamilyDescriptorLowerCaseEnum.class);
050
051  private Method getSetMethod(Method getMethod, Class<?> enumType) throws NoSuchMethodException {
052    String methodName = getMethod.getName().replaceFirst("get", "set");
053    return ColumnFamilyDescriptorBuilder.class.getMethod(methodName, enumType);
054  }
055
056  private Enum<?> getEnumValue(Class<?> enumType) {
057    for (Enum<?> enumConst : enumType.asSubclass(Enum.class).getEnumConstants()) {
058      if (!enumConst.name().equalsIgnoreCase("NONE") && !enumConst.name().equals("DEFAULT")) {
059        return enumConst;
060      }
061    }
062    throw new NoSuchElementException(enumType.getName());
063  }
064
065  private boolean contains(Collection<Enum<?>> enumConsts, String value) {
066    return enumConsts.stream().anyMatch(e -> e.name().equals(value));
067  }
068
069  @Test
070  public void test()
071    throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
072    Map<Method, Enum<?>> getMethod2Value = new HashMap<>();
073    ColumnFamilyDescriptorBuilder builder =
074      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("test"));
075    for (Method method : ColumnFamilyDescriptor.class.getMethods()) {
076      if (method.getParameterCount() == 0 && method.getReturnType().isEnum()) {
077        LOG.info("Checking " + method);
078        Class<?> enumType = method.getReturnType();
079        Method setMethod = getSetMethod(method, enumType);
080        Enum<?> enumConst = getEnumValue(enumType);
081        LOG.info("Using " + setMethod + " to set the value to " + enumConst);
082        setMethod.invoke(builder, enumConst);
083        getMethod2Value.put(method, enumConst);
084      }
085    }
086    ColumnFamilyDescriptor desc = builder.build();
087    ColumnFamilyDescriptorBuilder builder2 =
088      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("test2"));
089    desc.getValues().forEach((k, v) -> {
090      LOG.info(k.toString() + "=>" + v.toString());
091      String str = Bytes.toString(v.get(), v.getOffset(), v.getLength());
092      if (contains(getMethod2Value.values(), str)) {
093        LOG.info("Set to lower case " + str.toLowerCase());
094        builder2.setValue(k, new Bytes(Bytes.toBytes(str.toLowerCase())));
095      }
096    });
097
098    ColumnFamilyDescriptor desc2 = builder2.build();
099    for (Map.Entry<Method, Enum<?>> entry : getMethod2Value.entrySet()) {
100      assertEquals(entry.getKey() + " should return " + entry.getValue(), entry.getValue(),
101        entry.getKey().invoke(desc2));
102    }
103  }
104}