Add test and fix type inspection for scanned functions

Fixes gh-73
This commit is contained in:
Dave Syer
2017-06-28 11:00:07 +01:00
parent e4b181d0c4
commit f94d672dc4
4 changed files with 152 additions and 2 deletions

View File

@@ -409,8 +409,18 @@ public class ContextFunctionCatalogAutoConfiguration {
}
else if (source instanceof FileSystemResource) {
try {
Type type = ClassUtils.forName(definition.getBeanClassName(), null);
param = extractType(type, paramType, index);
Class<?> beanType = ClassUtils.forName(definition.getBeanClassName(),
null);
for (Type type : beanType.getGenericInterfaces()) {
if (type.getTypeName().startsWith("java.util.function")) {
param = extractType(type, paramType, index);
break;
}
}
if (param == null) {
// Last chance
param = beanType;
}
}
catch (ClassNotFoundException e) {
throw new IllegalStateException(

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.scan.ScannedFunction;
import org.springframework.cloud.function.test.GenericFunction;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -120,6 +121,15 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
public void componentScanBeanFunction() {
create(ComponentScanBeanConfiguration.class);
assertThat(context.getBean("function")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("function")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("function")).isAssignableFrom(Map.class);
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
public void componentScanFunction() {
create(ComponentScanConfiguration.class);
@@ -285,6 +295,12 @@ public class ContextFunctionCatalogAutoConfigurationTests {
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses = GenericFunction.class)
protected static class ComponentScanBeanConfiguration {
}
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses = ScannedFunction.class)
protected static class ComponentScanConfiguration {
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.scan;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
/**
* @author Dave Syer
*
*/
@Component("function")
public class ScannedFunction
implements Function<Map<String, String>, Map<String, String>> {
@Override
public Map<String, String> apply(Map<String, String> m) {
return m.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(),
e -> e.getValue().toString().toUpperCase()));
}
}