Minimize and centralize assumptions about build output
Closes gh-15471
This commit is contained in:
@@ -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"))
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user