|
| 1 | +/* |
| 2 | + * Copyright 2021-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.function.Supplier; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 32 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 33 | +import org.springframework.nativex.domain.reflect.ClassDescriptor; |
| 34 | +import org.springframework.nativex.hint.TypeAccess; |
| 35 | + |
| 36 | +/** |
| 37 | + * |
| 38 | + * @author Oleg Zhurakousky |
| 39 | + * |
| 40 | + */ |
| 41 | +class FunctionTypeProcessorTests { |
| 42 | + |
| 43 | + private static Set<TypeAccess> ALL_MEMBERS; |
| 44 | + { |
| 45 | + ALL_MEMBERS = new HashSet<>(Arrays.asList(TypeAccess.PUBLIC_FIELDS, TypeAccess.DECLARED_FIELDS, TypeAccess.DECLARED_CONSTRUCTORS, TypeAccess.PUBLIC_CONSTRUCTORS, TypeAccess.DECLARED_METHODS, TypeAccess.PUBLIC_METHODS)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testFunctionTypes() { |
| 50 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 51 | + beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition()); |
| 52 | + beanFactory.registerBeanDefinition("sampleFunction", BeanDefinitionBuilder.rootBeanDefinition(MyFunction.class).getBeanDefinition()); |
| 53 | + NativeConfigurationRegistry registry = process(beanFactory); |
| 54 | + List<ClassDescriptor> classDescriptors = registry.reflection().toClassDescriptors(); |
| 55 | + assertThat(classDescriptors).hasSize(1); |
| 56 | + ClassDescriptor cd = classDescriptors.get(0); |
| 57 | + assertThat(cd.getName()).isEqualTo(Person.class.getName()); |
| 58 | + assertThat(cd.getAccess()).containsAll(ALL_MEMBERS); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testConsumerTypes() { |
| 63 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 64 | + beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition()); |
| 65 | + beanFactory.registerBeanDefinition("sampleConsumer", BeanDefinitionBuilder.rootBeanDefinition(MyConsumer.class).getBeanDefinition()); |
| 66 | + NativeConfigurationRegistry registry = process(beanFactory); |
| 67 | + List<ClassDescriptor> classDescriptors = registry.reflection().toClassDescriptors(); |
| 68 | + assertThat(classDescriptors).hasSize(1); |
| 69 | + ClassDescriptor cd = classDescriptors.get(0); |
| 70 | + assertThat(cd.getName()).isEqualTo(Person.class.getName()); |
| 71 | + assertThat(cd.getAccess()).containsAll(ALL_MEMBERS); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testSupplierTypes() { |
| 76 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 77 | + beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition()); |
| 78 | + beanFactory.registerBeanDefinition("sampleSupplier", BeanDefinitionBuilder.rootBeanDefinition(MySupplier.class).getBeanDefinition()); |
| 79 | + NativeConfigurationRegistry registry = process(beanFactory); |
| 80 | + List<ClassDescriptor> classDescriptors = registry.reflection().toClassDescriptors(); |
| 81 | + assertThat(classDescriptors).hasSize(0); |
| 82 | + } |
| 83 | + |
| 84 | + private NativeConfigurationRegistry process(DefaultListableBeanFactory beanFactory) { |
| 85 | + NativeConfigurationRegistry registry = new NativeConfigurationRegistry(); |
| 86 | + new FunctionTypeProcessor().process(beanFactory, registry); |
| 87 | + return registry; |
| 88 | + } |
| 89 | + |
| 90 | + public static class MyFunction implements Function<Person, String> { |
| 91 | + @Override |
| 92 | + public String apply(Person t) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static class MyConsumer implements Consumer<Person> { |
| 98 | + @Override |
| 99 | + public void accept(Person t) { |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static class MySupplier implements Supplier<Person> { |
| 104 | + @Override |
| 105 | + public Person get() { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static class Person { |
| 111 | + private String name; |
| 112 | + |
| 113 | + public String getName() { |
| 114 | + return name; |
| 115 | + } |
| 116 | + |
| 117 | + public void setName(String name) { |
| 118 | + this.name = name; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments