Fixed consumer sample to accept bare string

This commit is contained in:
Dave Syer
2017-05-26 12:42:25 +01:00
parent 2a4c46f57b
commit 9bf3601143
12 changed files with 59 additions and 6 deletions

View File

@@ -26,7 +26,7 @@ public class ConsumerCompiler<T> extends AbstractFunctionCompiler<Consumer<T>> {
private final String inputType;
public ConsumerCompiler() {
this("Object");
this("Flux<Object>");
}
public ConsumerCompiler(String inputType) {

View File

@@ -21,6 +21,7 @@ import java.util.List;
/**
* @author Mark Fisher
*/
@SuppressWarnings("serial")
public class CompilationFailedException extends RuntimeException {
public CompilationFailedException(List<CompilationMessage> messages) {

View File

@@ -23,6 +23,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.function.compiler.CompilationResultFactory;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.compiler.java.SimpleClassLoader;
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ReflectionUtils;
@@ -30,7 +31,7 @@ import org.springframework.util.ReflectionUtils;
/**
* @author Mark Fisher
*/
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean {
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, FunctionFactoryMetadata {
private final Resource resource;
@@ -81,6 +82,7 @@ public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBea
return this.factory.getResult();
}
@Override
public Method getFactoryMethod() {
return this.method;
}

View File

@@ -69,6 +69,7 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
return this.factory.getResult();
}
@Override
public Method getFactoryMethod() {
return this.factory.getFactoryMethod();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.function.compiler.proxy;
import java.util.function.Consumer;
import org.springframework.cloud.function.support.ConsumerProxy;
import org.springframework.core.io.Resource;
/**
@@ -25,7 +26,7 @@ import org.springframework.core.io.Resource;
*
* @param <T> type
*/
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements Consumer<T> {
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements ConsumerProxy<T> {
public ByteCodeLoadingConsumer(Resource resource) {
super(resource, Consumer.class);

View File

@@ -18,7 +18,6 @@ package org.springframework.cloud.function.compiler.proxy;
import java.util.function.Function;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.function.support.FunctionProxy;
import org.springframework.core.io.Resource;
@@ -28,7 +27,7 @@ import org.springframework.core.io.Resource;
* @param <T> Function input type
* @param <R> Function result type
*/
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionProxy<T, R>, InitializingBean {
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionProxy<T, R> {
public ByteCodeLoadingFunction(Resource resource) {
super(resource, Function.class);

View File

@@ -0,0 +1,48 @@
/*
* 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.compiler;
import java.util.function.Consumer;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class ConsumerCompilerTests {
@Test
public void consumesFluxString() {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos",
"flux -> flux.subscribe(System.out::println)", "Flux<String>");
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isTrue();
}
@Test
public void consumesString() {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isFalse();
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.compiler;
import java.util.function.Function;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class FunctionCompilerTests {
@Test
public void transformsFluxString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
}
@Test
public void transformsString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isFalse();
assertThat(compiled.getResult().apply("hello")).isEqualTo("HELLO");
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.compiler;
import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class SupplierCompilerTests {
@Test
public void supppliesFluxString() {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> Flux.just(\"foo\", \"bar\")", "Flux<String>");
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
}
@Test
public void supppliesString() {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> \"foo\"", "String");
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isFalse();
assertThat(compiled.getResult().get()).isEqualTo("foo");
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.compiler.proxy;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.ConsumerCompiler;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.compiler.SupplierCompiler;
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
import org.springframework.cloud.function.support.FunctionUtils;
import org.springframework.core.io.ByteArrayResource;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
public class ByteCodeLoadingFunctionTests {
@Test
public void compileConsumer() throws Exception {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
consumer.afterPropertiesSet();
assertThat(consumer instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
consumer.accept("foo");
}
@Test
public void compileSupplier() throws Exception {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos", "() -> \"foo\"", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
supplier.afterPropertiesSet();
assertThat(supplier instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
assertThat(supplier.get()).isEqualTo("foo");
}
@Test
public void compileFunction() throws Exception {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
assertThat(function.apply("foo")).isEqualTo("FOO");
}
@Test
public void compileFluxFunction() throws Exception {
CompiledFunctionFactory<Function<Flux<String>, Flux<String>>> compiled = new FunctionCompiler<Flux<String>, Flux<String>>(
String.class.getName()).compile("foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
}
}