Minimize and centralize assumptions about build output
Closes gh-15471
This commit is contained in:
@@ -16,11 +16,12 @@
|
||||
|
||||
package sample.parent.consumer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import sample.parent.SampleParentContextApplication;
|
||||
import sample.parent.producer.ProducerApplication;
|
||||
|
||||
@@ -41,34 +42,41 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public class SampleIntegrationParentApplicationTests {
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
@BeforeClass
|
||||
public static void start() {
|
||||
context = SpringApplication.run(SampleParentContextApplication.class);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stop() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
@Rule
|
||||
public final TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void testVanillaExchange() throws Exception {
|
||||
SpringApplication.run(ProducerApplication.class, "World");
|
||||
awaitOutputContaining("Hello World");
|
||||
File inputDir = new File(this.temp.getRoot(), "input");
|
||||
File outputDir = new File(this.temp.getRoot(), "output");
|
||||
ConfigurableApplicationContext app = SpringApplication.run(
|
||||
SampleParentContextApplication.class, "--service.input-dir=" + inputDir,
|
||||
"--service.output-dir=" + outputDir);
|
||||
try {
|
||||
ConfigurableApplicationContext producer = SpringApplication.run(
|
||||
ProducerApplication.class, "--service.input-dir=" + inputDir,
|
||||
"--service.output-dir=" + outputDir, "World");
|
||||
try {
|
||||
awaitOutputContaining(outputDir, "Hello World");
|
||||
}
|
||||
finally {
|
||||
producer.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
app.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void awaitOutputContaining(String requiredContents) throws Exception {
|
||||
private void awaitOutputContaining(File outputDir, String requiredContents)
|
||||
throws Exception {
|
||||
long endTime = System.currentTimeMillis() + 30000;
|
||||
String output = null;
|
||||
while (System.currentTimeMillis() < endTime) {
|
||||
Resource[] resources = findResources();
|
||||
Resource[] resources = findResources(outputDir);
|
||||
if (resources.length == 0) {
|
||||
Thread.sleep(200);
|
||||
resources = findResources();
|
||||
resources = findResources(outputDir);
|
||||
}
|
||||
else {
|
||||
output = readResources(resources);
|
||||
@@ -85,10 +93,10 @@ public class SampleIntegrationParentApplicationTests {
|
||||
+ "'. Output was '" + output + "'");
|
||||
}
|
||||
|
||||
private Resource[] findResources() throws IOException {
|
||||
private Resource[] findResources(File outputDir) throws IOException {
|
||||
return ResourcePatternUtils
|
||||
.getResourcePatternResolver(new DefaultResourceLoader())
|
||||
.getResources("file:target/output/*.txt");
|
||||
.getResources("file:" + outputDir.getAbsolutePath() + "/*.txt");
|
||||
}
|
||||
|
||||
private String readResources(Resource[] resources) throws IOException {
|
||||
|
||||
@@ -19,20 +19,32 @@ package sample.parent.producer;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import sample.parent.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