Minimize and centralize assumptions about build output

Closes gh-15471
This commit is contained in:
Andy Wilkinson
2018-12-14 17:50:20 +00:00
parent 7d0a7a65c8
commit 61d04db0d7
57 changed files with 778 additions and 337 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 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.
@@ -16,21 +16,22 @@
package sample.integration;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class SampleCommandLineRunner implements CommandLineRunner {
public class SampleApplicationRunner implements ApplicationRunner {
private final SampleMessageGateway gateway;
public SampleCommandLineRunner(SampleMessageGateway gateway) {
public SampleApplicationRunner(SampleMessageGateway gateway) {
this.gateway = gateway;
}
@Override
public void run(String... args) throws Exception {
for (String arg : args) {
public void run(ApplicationArguments args) throws Exception {
for (String arg : args.getNonOptionArgs()) {
this.gateway.echo(arg);
}
}

View File

@@ -16,7 +16,6 @@
package sample.integration;
import java.io.File;
import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
@@ -35,10 +34,16 @@ import org.springframework.integration.file.FileWritingMessageHandler;
@EnableConfigurationProperties(ServiceProperties.class)
public class SampleIntegrationApplication {
private final ServiceProperties serviceProperties;
public SampleIntegrationApplication(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
}
@Bean
public FileReadingMessageSource fileReader() {
FileReadingMessageSource reader = new FileReadingMessageSource();
reader.setDirectory(new File("target/input"));
reader.setDirectory(this.serviceProperties.getInputDir());
return reader;
}
@@ -55,7 +60,7 @@ public class SampleIntegrationApplication {
@Bean
public FileWritingMessageHandler fileWriter() {
FileWritingMessageHandler writer = new FileWritingMessageHandler(
new File("target/output"));
this.serviceProperties.getOutputDir());
writer.setExpectReply(false);
return writer;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2018 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.
@@ -16,6 +16,8 @@
package sample.integration;
import java.io.File;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
@@ -26,6 +28,10 @@ public class ServiceProperties {
private String greeting = "Hello";
private File inputDir;
private File outputDir;
@ManagedAttribute
public String getGreeting() {
return this.greeting;
@@ -35,4 +41,20 @@ public class ServiceProperties {
this.greeting = greeting;
}
public File getInputDir() {
return this.inputDir;
}
public void setInputDir(File inputDir) {
this.inputDir = inputDir;
}
public File getOutputDir() {
return this.outputDir;
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
}

View File

@@ -1,2 +1,4 @@
logging.level.org.springframework.integration.file=DEBUG
service.greeting=Hello
service.input-dir=input
service.output-dir=output

View File

@@ -25,9 +25,11 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import sample.integration.SampleIntegrationApplication;
import sample.integration.ServiceProperties;
import sample.integration.producer.ProducerApplication;
import org.springframework.boot.SpringApplication;
@@ -35,7 +37,6 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -48,20 +49,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class SampleIntegrationApplicationTests {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();
private ConfigurableApplicationContext context;
@Before
public void deleteInputAndOutput() {
deleteIfExists(new File("target/input"));
deleteIfExists(new File("target/output"));
}
private void deleteIfExists(File directory) {
if (directory.exists()) {
assertThat(FileSystemUtils.deleteRecursively(directory)).isTrue();
}
}
@After
public void stop() {
if (this.context != null) {
@@ -71,29 +63,37 @@ public class SampleIntegrationApplicationTests {
@Test
public void testVanillaExchange() throws Exception {
this.context = SpringApplication.run(SampleIntegrationApplication.class);
SpringApplication.run(ProducerApplication.class, "World");
String output = getOutput();
File inputDir = new File(this.temp.getRoot(), "input");
File outputDir = new File(this.temp.getRoot(), "output");
this.context = SpringApplication.run(SampleIntegrationApplication.class,
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir);
SpringApplication.run(ProducerApplication.class, "World",
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir);
String output = getOutput(outputDir);
assertThat(output).contains("Hello World");
}
@Test
public void testMessageGateway() throws Exception {
File inputDir = new File(this.temp.getRoot(), "input");
File outputDir = new File(this.temp.getRoot(), "output");
this.context = SpringApplication.run(SampleIntegrationApplication.class,
"testviamg");
String output = getOutput();
"testviamg", "--service.input-dir=" + inputDir,
"--service.output-dir=" + outputDir);
String output = getOutput(
this.context.getBean(ServiceProperties.class).getOutputDir());
assertThat(output).contains("testviamg");
}
private String getOutput() throws Exception {
private String getOutput(File outputDir) throws Exception {
Future<String> future = Executors.newSingleThreadExecutor()
.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Resource[] resources = getResourcesWithContent();
Resource[] resources = getResourcesWithContent(outputDir);
while (resources.length == 0) {
Thread.sleep(200);
resources = getResourcesWithContent();
resources = getResourcesWithContent(outputDir);
}
StringBuilder builder = new StringBuilder();
for (Resource resource : resources) {
@@ -108,10 +108,10 @@ public class SampleIntegrationApplicationTests {
return future.get(30, TimeUnit.SECONDS);
}
private Resource[] getResourcesWithContent() throws IOException {
private Resource[] getResourcesWithContent(File outputDir) throws IOException {
Resource[] candidates = ResourcePatternUtils
.getResourcePatternResolver(new DefaultResourceLoader())
.getResources("file:target/output/**");
.getResources("file:" + outputDir.getAbsolutePath() + "/**");
for (Resource candidate : candidates) {
if ((candidate.getFilename() != null
&& candidate.getFilename().endsWith(".writing"))

View File

@@ -19,20 +19,32 @@ package sample.integration.producer;
import java.io.File;
import java.io.FileOutputStream;
import org.springframework.boot.CommandLineRunner;
import sample.integration.ServiceProperties;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProducerApplication implements CommandLineRunner {
@EnableConfigurationProperties(ServiceProperties.class)
public class ProducerApplication implements ApplicationRunner {
private final ServiceProperties serviceProperties;
public ProducerApplication(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
}
@Override
public void run(String... args) throws Exception {
new File("target/input").mkdirs();
if (args.length > 0) {
public void run(ApplicationArguments args) throws Exception {
this.serviceProperties.getInputDir().mkdirs();
if (args.getNonOptionArgs().size() > 0) {
FileOutputStream stream = new FileOutputStream(
"target/input/data" + System.currentTimeMillis() + ".txt");
for (String arg : args) {
new File(this.serviceProperties.getInputDir(),
"data" + System.currentTimeMillis() + ".txt"));
for (String arg : args.getNonOptionArgs()) {
stream.write(arg.getBytes());
}
stream.flush();