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