|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.mapper; |
| 10 | + |
| 11 | +import org.opensearch.test.OpenSearchTestCase; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.lang.reflect.Modifier; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +public class FilterFieldTypeTest extends OpenSearchTestCase { |
| 21 | + |
| 22 | + private static final class MethodSignature { |
| 23 | + private final String name; |
| 24 | + private final Class<?> returnType; |
| 25 | + private final Class<?>[] parameterTypes; |
| 26 | + |
| 27 | + public MethodSignature(String name, Class<?> returnType, Class<?>[] parameterTypes) { |
| 28 | + this.name = name; |
| 29 | + this.returnType = returnType; |
| 30 | + this.parameterTypes = parameterTypes; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean equals(Object o) { |
| 35 | + if (o == null || getClass() != o.getClass()) return false; |
| 36 | + MethodSignature that = (MethodSignature) o; |
| 37 | + return Objects.equals(name, that.name) |
| 38 | + && Objects.equals(returnType, that.returnType) |
| 39 | + && Objects.deepEquals(parameterTypes, that.parameterTypes); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public int hashCode() { |
| 44 | + return Objects.hash(name, returnType, Arrays.hashCode(parameterTypes)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static final Set<MethodSignature> EXCLUDED_SIGNATURES = Set.of(new MethodSignature("typeName", String.class, new Class<?>[0])); |
| 49 | + |
| 50 | + public void testAllMethodsDelegated() { |
| 51 | + Method[] mappedFieldTypeMethods = MappedFieldType.class.getMethods(); |
| 52 | + Method[] filterFieldTypeMethods = FilterFieldType.class.getMethods(); |
| 53 | + |
| 54 | + Set<MethodSignature> mappedFieldTypeMethodSignatures = new HashSet<>(); |
| 55 | + for (Method method : mappedFieldTypeMethods) { |
| 56 | + if (method.getDeclaringClass() == MappedFieldType.class |
| 57 | + && Modifier.isFinal(method.getModifiers()) == false |
| 58 | + && Modifier.isStatic(method.getModifiers()) == false) { |
| 59 | + mappedFieldTypeMethodSignatures.add( |
| 60 | + new MethodSignature(method.getName(), method.getReturnType(), method.getParameterTypes()) |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Set<MethodSignature> filterFieldTypeMethodSignatures = new HashSet<>(); |
| 66 | + for (Method method : filterFieldTypeMethods) { |
| 67 | + if (method.getDeclaringClass() == FilterFieldType.class) { |
| 68 | + filterFieldTypeMethodSignatures.add( |
| 69 | + new MethodSignature(method.getName(), method.getReturnType(), method.getParameterTypes()) |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + for (MethodSignature methodSignature : mappedFieldTypeMethodSignatures) { |
| 74 | + if (filterFieldTypeMethodSignatures.contains(methodSignature)) { |
| 75 | + assertFalse( |
| 76 | + "Method " + methodSignature.name + " should NOT be implemented in " + FilterFieldType.class.getSimpleName(), |
| 77 | + EXCLUDED_SIGNATURES.contains(methodSignature) |
| 78 | + ); |
| 79 | + } else { |
| 80 | + assertTrue( |
| 81 | + "Method " + methodSignature.name + " should be implemented in " + FilterFieldType.class.getSimpleName(), |
| 82 | + EXCLUDED_SIGNATURES.contains(methodSignature) |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments