From a64579dbdbd9773f0b74ad16e2d8d620043c11ee Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 18 May 2018 12:48:36 +0100 Subject: [PATCH] More tests for auto detect of start class --- .../FunctionCreatorConfiguration.java | 21 +++++++++++++------ .../FunctionCreatorConfigurationTests.java | 15 +++++++++++++ .../test/resources/app/META-INF/MANIFEST.MF | 14 +++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 spring-cloud-function-deployer/src/test/resources/app/META-INF/MANIFEST.MF diff --git a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionCreatorConfiguration.java b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionCreatorConfiguration.java index 2f13c7bef..52494cf37 100644 --- a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionCreatorConfiguration.java +++ b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionCreatorConfiguration.java @@ -47,6 +47,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.SpringApplication; import org.springframework.boot.loader.JarLauncher; import org.springframework.boot.loader.archive.Archive; +import org.springframework.boot.loader.archive.ExplodedArchive; import org.springframework.boot.loader.archive.JarFileArchive; import org.springframework.cloud.deployer.resource.support.DelegatingResourceLoader; import org.springframework.cloud.function.context.FunctionRegistration; @@ -160,7 +161,7 @@ class FunctionCreatorConfiguration { private class ComputeLauncher extends JarLauncher { - public ComputeLauncher(JarFileArchive archive) { + public ComputeLauncher(Archive archive) { super(archive); } @@ -168,7 +169,8 @@ class FunctionCreatorConfiguration { public String getMainClass() throws Exception { try { return super.getMainClass(); - } catch (Exception e) { + } + catch (Exception e) { return null; } } @@ -252,9 +254,11 @@ class FunctionCreatorConfiguration { try { File file = new File(url.toURI()); if (file.exists()) { - JarFileArchive archive = new JarFileArchive(file); + Archive archive = file.getName().endsWith(".jar") + ? new JarFileArchive(file) + : new ExplodedArchive(file); String main = new ComputeLauncher(archive).getMainClass(); - if (main !=null) { + if (main != null) { return main; } } @@ -282,8 +286,13 @@ class FunctionCreatorConfiguration { return Collections.singletonList(url); } try { - JarFileArchive archive = new JarFileArchive(new File(url.toURI())); - return Arrays.asList(new ComputeLauncher(archive).getClassLoaderUrls()); + File file = new File(url.toURI()); + if (file.exists()) { + JarFileArchive archive = new JarFileArchive(file); + return Arrays + .asList(new ComputeLauncher(archive).getClassLoaderUrls()); + } + return Collections.singletonList(url); } catch (Exception e) { throw new IllegalStateException("Cannot create class loader for " + url, diff --git a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionCreatorConfigurationTests.java b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionCreatorConfigurationTests.java index f9bebf7f6..8f9bba92e 100644 --- a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionCreatorConfigurationTests.java +++ b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionCreatorConfigurationTests.java @@ -59,6 +59,21 @@ public abstract class FunctionCreatorConfigurationTests { } } + @EnableAutoConfiguration + @TestPropertySource(properties = { + "function.location=app:classpath,file:target/test-classes,file:target/test-classes/app", + "function.bean=myDoubler" }) + public static class SingleFunctionWithAutoMainTests + extends FunctionCreatorConfigurationTests { + + @Test + public void testDouble() { + Function, Flux> function = catalog + .lookup(Function.class, "function0"); + assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo(4); + } + } + @EnableAutoConfiguration @TestPropertySource(properties = { "function.location=app:classpath", "function.bean=org.springframework.cloud.function.test.SpringDoubler" }) diff --git a/spring-cloud-function-deployer/src/test/resources/app/META-INF/MANIFEST.MF b/spring-cloud-function-deployer/src/test/resources/app/META-INF/MANIFEST.MF new file mode 100644 index 000000000..d1f4a9c13 --- /dev/null +++ b/spring-cloud-function-deployer/src/test/resources/app/META-INF/MANIFEST.MF @@ -0,0 +1,14 @@ +Manifest-Version: 1.0 +Implementation-Title: function-file-sample +Implementation-Version: 1.0.0.M1 +Archiver-Version: Plexus Archiver +Built-By: dsyer +Implementation-Vendor-Id: com.example +Spring-Boot-Version: 1.5.12.RELEASE +Implementation-Vendor: Pivotal Software, Inc. +Main-Class: org.springframework.boot.loader.JarLauncher +Start-Class: org.springframework.cloud.function.test.FunctionApp +Created-By: Apache Maven 3.5.0 +Build-Jdk: 1.8.0_131 +Implementation-URL: http://projects.spring.io/spring-boot/function-sam + ple/ \ No newline at end of file