mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-21 19:10:00 +08:00
Add support for ApplicationContextRunner in ProjectAssetTester
Closes gh-1004
This commit is contained in:
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package io.spring.initializr.generator.spring.build.gradle;
|
package io.spring.initializr.generator.spring.build.gradle;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import io.spring.initializr.generator.project.MutableProjectDescription;
|
import io.spring.initializr.generator.project.MutableProjectDescription;
|
||||||
import io.spring.initializr.generator.test.project.ProjectAssetTester;
|
import io.spring.initializr.generator.test.project.ProjectAssetTester;
|
||||||
import io.spring.initializr.generator.version.Version;
|
import io.spring.initializr.generator.version.Version;
|
||||||
@@ -42,79 +40,61 @@ public class ConditionalOnGradleVersionTests {
|
|||||||
void outcomeWithSpringBoot15() {
|
void outcomeWithSpringBoot15() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("1.5.18.RELEASE"));
|
description.setPlatformVersion(Version.parse("1.5.18.RELEASE"));
|
||||||
String bean = outcomeFor(description);
|
this.projectTester.configure(description, (context) -> assertThat(context).hasSingleBean(String.class)
|
||||||
assertThat(bean).isEqualTo("testGradle3");
|
.getBean(String.class).isEqualTo("testGradle3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithSpringBoot20() {
|
void outcomeWithSpringBoot20() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("2.0.9.RELEASE"));
|
description.setPlatformVersion(Version.parse("2.0.9.RELEASE"));
|
||||||
String bean = outcomeFor(description);
|
this.projectTester.configure(description, (context) -> assertThat(context).hasSingleBean(String.class)
|
||||||
assertThat(bean).isEqualTo("testGradle4");
|
.getBean(String.class).isEqualTo("testGradle4"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithSpringBoot21() {
|
void outcomeWithSpringBoot21() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("2.1.3.RELEASE"));
|
description.setPlatformVersion(Version.parse("2.1.3.RELEASE"));
|
||||||
String bean = outcomeFor(description);
|
this.projectTester.configure(description, (context) -> assertThat(context).hasSingleBean(String.class)
|
||||||
assertThat(bean).isEqualTo("testGradle5");
|
.getBean(String.class).isEqualTo("testGradle5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithNoMatch() {
|
void outcomeWithNoMatch() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("1.0.0.RELEASE"));
|
description.setPlatformVersion(Version.parse("1.0.0.RELEASE"));
|
||||||
this.projectTester.generate(description, (projectGenerationContext) -> {
|
this.projectTester.configure(description, (context) -> assertThat(context).doesNotHaveBean(String.class));
|
||||||
assertThat(projectGenerationContext.getBeansOfType(String.class)).isEmpty();
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithNoAvailableSpringBootVersion() {
|
void outcomeWithNoAvailableSpringBootVersion() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
this.projectTester.generate(description, (projectGenerationContext) -> {
|
this.projectTester.configure(description, (context) -> assertThat(context).doesNotHaveBean(String.class));
|
||||||
assertThat(projectGenerationContext.getBeansOfType(String.class)).isEmpty();
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithSpringBoot15AndMultipleGenerations() {
|
void outcomeWithSpringBoot15AndMultipleGenerations() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("1.5.18.RELEASE"));
|
description.setPlatformVersion(Version.parse("1.5.18.RELEASE"));
|
||||||
Map<String, String> candidates = candidatesFor(description, Gradle3Or4TestConfiguration.class);
|
this.projectTester.withConfiguration(Gradle3Or4TestConfiguration.class).configure(description,
|
||||||
assertThat(candidates).containsOnlyKeys("gradle3", "gradle3AndLater");
|
(context) -> assertThat(context).getBeanNames(String.class).containsOnly("gradle3", "gradle3AndLater"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithSpringBoot20AndMultipleGenerations() {
|
void outcomeWithSpringBoot20AndMultipleGenerations() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("2.0.9.RELEASE"));
|
description.setPlatformVersion(Version.parse("2.0.9.RELEASE"));
|
||||||
Map<String, String> candidates = candidatesFor(description, Gradle3Or4TestConfiguration.class);
|
this.projectTester.withConfiguration(Gradle3Or4TestConfiguration.class).configure(description,
|
||||||
assertThat(candidates).containsOnlyKeys("gradle4", "gradle3AndLater");
|
(context) -> assertThat(context).getBeanNames(String.class).containsOnly("gradle4", "gradle3AndLater"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void outcomeWithSpringBoot21AndMultipleNonMatchingGenerations() {
|
void outcomeWithSpringBoot21AndMultipleNonMatchingGenerations() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("2.1.3.RELEASE"));
|
description.setPlatformVersion(Version.parse("2.1.3.RELEASE"));
|
||||||
Map<String, String> candidates = candidatesFor(description, Gradle3Or4TestConfiguration.class);
|
this.projectTester.withConfiguration(Gradle3Or4TestConfiguration.class).configure(description,
|
||||||
assertThat(candidates).containsOnlyKeys("gradle5");
|
(context) -> assertThat(context).getBeanNames(String.class).containsOnly("gradle5"));
|
||||||
}
|
|
||||||
|
|
||||||
private String outcomeFor(MutableProjectDescription description) {
|
|
||||||
return this.projectTester.generate(description, (projectGenerationContext) -> {
|
|
||||||
assertThat(projectGenerationContext.getBeansOfType(String.class)).hasSize(1);
|
|
||||||
return projectGenerationContext.getBean(String.class);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, String> candidatesFor(MutableProjectDescription description, Class<?>... extraConfigurations) {
|
|
||||||
return this.projectTester.withConfiguration(extraConfigurations).generate(description,
|
|
||||||
(context) -> context.getBeansOfType(String.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|||||||
@@ -76,11 +76,12 @@ class GradleKtsProjectGenerationConfigurationTests {
|
|||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse(platformVersion));
|
description.setPlatformVersion(Version.parse(platformVersion));
|
||||||
description.setLanguage(new JavaLanguage());
|
description.setLanguage(new JavaLanguage());
|
||||||
BuildWriter buildWriter = this.projectTester.generate(description,
|
this.projectTester.configure(description, (context) -> {
|
||||||
(context) -> context.getBean(BuildWriter.class));
|
assertThat(context).hasSingleBean(BuildWriter.class).getBean(BuildWriter.class)
|
||||||
assertThat(buildWriter).isInstanceOf(GradleBuildProjectContributor.class);
|
.isInstanceOf(GradleBuildProjectContributor.class);
|
||||||
assertThat(ReflectionTestUtils.getField(buildWriter, "buildWriter"))
|
assertThat(ReflectionTestUtils.getField(context.getBean(BuildWriter.class), "buildWriter"))
|
||||||
.isInstanceOf(KotlinDslGradleBuildWriter.class);
|
.isInstanceOf(KotlinDslGradleBuildWriter.class);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static Stream<Arguments> gradleWrapperParameters() {
|
static Stream<Arguments> gradleWrapperParameters() {
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package io.spring.initializr.generator.spring.build.gradle;
|
package io.spring.initializr.generator.spring.build.gradle;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import io.spring.initializr.generator.buildsystem.BuildWriter;
|
import io.spring.initializr.generator.buildsystem.BuildWriter;
|
||||||
@@ -76,11 +75,12 @@ class GradleProjectGenerationConfigurationTests {
|
|||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse(platformVersion));
|
description.setPlatformVersion(Version.parse(platformVersion));
|
||||||
description.setLanguage(new JavaLanguage());
|
description.setLanguage(new JavaLanguage());
|
||||||
BuildWriter buildWriter = this.projectTester.generate(description,
|
this.projectTester.configure(description, (context) -> {
|
||||||
(context) -> context.getBean(BuildWriter.class));
|
assertThat(context).hasSingleBean(BuildWriter.class).getBean(BuildWriter.class)
|
||||||
assertThat(buildWriter).isInstanceOf(GradleBuildProjectContributor.class);
|
.isInstanceOf(GradleBuildProjectContributor.class);
|
||||||
assertThat(ReflectionTestUtils.getField(buildWriter, "buildWriter"))
|
assertThat(ReflectionTestUtils.getField(context.getBean(BuildWriter.class), "buildWriter"))
|
||||||
.isInstanceOf(GroovyDslGradleBuildWriter.class);
|
.isInstanceOf(GroovyDslGradleBuildWriter.class);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static Stream<Arguments> gradleWrapperParameters() {
|
static Stream<Arguments> gradleWrapperParameters() {
|
||||||
@@ -193,9 +193,8 @@ class GradleProjectGenerationConfigurationTests {
|
|||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse(platformVersion));
|
description.setPlatformVersion(Version.parse(platformVersion));
|
||||||
description.setLanguage(new JavaLanguage());
|
description.setLanguage(new JavaLanguage());
|
||||||
Map<String, GradleAnnotationProcessorScopeBuildCustomizer> generate = this.projectTester.generate(description,
|
this.projectTester.configure(description, (context) -> assertThat(context)
|
||||||
(context) -> context.getBeansOfType(GradleAnnotationProcessorScopeBuildCustomizer.class));
|
.getBeans(GradleAnnotationProcessorScopeBuildCustomizer.class).hasSize((contributorExpected) ? 1 : 0));
|
||||||
assertThat(generate).hasSize((contributorExpected) ? 1 : 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,9 +59,8 @@ class MavenProjectGenerationConfigurationTests {
|
|||||||
void buildWriterIsContributed() {
|
void buildWriterIsContributed() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
|
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
|
||||||
BuildWriter buildWriter = this.projectTester.generate(description,
|
this.projectTester.configure(description, (context) -> assertThat(context).hasSingleBean(BuildWriter.class)
|
||||||
(context) -> context.getBean(BuildWriter.class));
|
.getBean(BuildWriter.class).isInstanceOf(MavenBuildProjectContributor.class));
|
||||||
assertThat(buildWriter).isInstanceOf(MavenBuildProjectContributor.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ class SourceCodeProjectGenerationConfigurationTests {
|
|||||||
description.setPackaging(Packaging.forId("war"));
|
description.setPackaging(Packaging.forId("war"));
|
||||||
description.setPackageName("com.foo");
|
description.setPackageName("com.foo");
|
||||||
description.setPlatformVersion(Version.parse(version));
|
description.setPlatformVersion(Version.parse(version));
|
||||||
this.projectTester.generate(description, (context) -> {
|
this.projectTester.configure(description, (context) -> {
|
||||||
ServletInitializerContributor bean = context.getBean(ServletInitializerContributor.class);
|
ServletInitializerContributor bean = context.getBean(ServletInitializerContributor.class);
|
||||||
SourceCode<TypeDeclaration, CompilationUnit<TypeDeclaration>> sourceCode = mock(SourceCode.class);
|
SourceCode<TypeDeclaration, CompilationUnit<TypeDeclaration>> sourceCode = mock(SourceCode.class);
|
||||||
CompilationUnit<TypeDeclaration> compilationUnit = mock(CompilationUnit.class);
|
CompilationUnit<TypeDeclaration> compilationUnit = mock(CompilationUnit.class);
|
||||||
@@ -112,7 +112,6 @@ class SourceCodeProjectGenerationConfigurationTests {
|
|||||||
bean.customize(sourceCode);
|
bean.customize(sourceCode);
|
||||||
verify(sourceCode).createCompilationUnit("com.foo", "ServletInitializer");
|
verify(sourceCode).createCompilationUnit("com.foo", "ServletInitializer");
|
||||||
verify(typeDeclaration).extend(className);
|
verify(typeDeclaration).extend(className);
|
||||||
return null;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -69,17 +69,16 @@ class KotlinProjectGenerationConfigurationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void kotlinVersionFallbacksToMetadataIfNotPresent() {
|
void kotlinVersionFallbacksToMetadataIfNotPresent() {
|
||||||
KotlinProjectSettings settings = this.projectTester.generate(new MutableProjectDescription(),
|
this.projectTester.configure(new MutableProjectDescription(),
|
||||||
(context) -> context.getBean(KotlinProjectSettings.class));
|
(context) -> assertThat(context.getBean(KotlinProjectSettings.class).getVersion()).isEqualTo("1.1.1"));
|
||||||
assertThat(settings.getVersion()).isEqualTo("1.1.1");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void kotlinVersionResolverIsUsedIfPresent() {
|
void kotlinVersionResolverIsUsedIfPresent() {
|
||||||
KotlinProjectSettings settings = this.projectTester
|
this.projectTester.withBean(KotlinProjectSettings.class, () -> new SimpleKotlinProjectSettings("0.9.12"))
|
||||||
.withBean(KotlinProjectSettings.class, () -> new SimpleKotlinProjectSettings("0.9.12"))
|
.configure(new MutableProjectDescription(),
|
||||||
.generate(new MutableProjectDescription(), (context) -> context.getBean(KotlinProjectSettings.class));
|
(context) -> assertThat(context.getBean(KotlinProjectSettings.class).getVersion())
|
||||||
assertThat(settings.getVersion()).isEqualTo("0.9.12");
|
.isEqualTo("0.9.12"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -73,9 +73,9 @@ class HelpDocumentProjectGenerationConfigurationTests {
|
|||||||
@Test
|
@Test
|
||||||
void helpDocumentIsAddedToGitIgnore() {
|
void helpDocumentIsAddedToGitIgnore() {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
GitIgnoreCustomizer gitIgnoreCustomizer = this.projectTester.generate(description,
|
this.projectTester.configure(description,
|
||||||
(context) -> context.getBean(GitIgnoreCustomizer.class));
|
(context) -> assertThat(context).hasSingleBean(GitIgnoreCustomizer.class)
|
||||||
assertThat(gitIgnoreCustomizer).isInstanceOf(HelpDocumentGitIgnoreCustomizer.class);
|
.getBean(GitIgnoreCustomizer.class).isInstanceOf(HelpDocumentGitIgnoreCustomizer.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,10 @@
|
|||||||
<artifactId>initializr-metadata</artifactId>
|
<artifactId>initializr-metadata</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-test</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
|
|||||||
@@ -33,12 +33,20 @@ import io.spring.initializr.generator.project.ProjectDirectoryFactory;
|
|||||||
import io.spring.initializr.generator.project.ProjectGenerationContext;
|
import io.spring.initializr.generator.project.ProjectGenerationContext;
|
||||||
import io.spring.initializr.generator.project.contributor.ProjectContributor;
|
import io.spring.initializr.generator.project.contributor.ProjectContributor;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A tester for project asset that does not detect available {@link ProjectContributor
|
* A tester for project asset that does not detect available {@link ProjectContributor
|
||||||
* contributors} and does not register any bean to the context. Contributors can be added
|
* contributors} and does not register any bean to the context. Contributors can be added
|
||||||
* using a {@link #withConfiguration(Class[]) configuration class} or a
|
* using a {@link #withConfiguration(Class[]) configuration class} or a
|
||||||
* {@link #withContextInitializer(Consumer) customization of the project generation
|
* {@link #withContextInitializer(Consumer) customization of the project generation
|
||||||
* context}.
|
* context}.
|
||||||
|
* <p>
|
||||||
|
* Alternatively, the context can be queried the same way {@link ApplicationContextRunner}
|
||||||
|
* works by {@link #configure(MutableProjectDescription, ContextConsumer) configuring} the
|
||||||
|
* context.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
@@ -70,6 +78,25 @@ public class ProjectAssetTester extends AbstractProjectGenerationTester<ProjectA
|
|||||||
return withContextInitializer((context) -> context.register(configurationClasses));
|
return withContextInitializer((context) -> context.register(configurationClasses));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure a {@link ProjectGenerationContext} using the specified
|
||||||
|
* {@code description} and use the {@link ContextConsumer} to assert the context.
|
||||||
|
* @param description the description of the project to configure
|
||||||
|
* @param consumer the consumer of the created {@link ProjectGenerationContext}
|
||||||
|
* @see ApplicationContextRunner#run(ContextConsumer)
|
||||||
|
*/
|
||||||
|
public void configure(MutableProjectDescription description,
|
||||||
|
ContextConsumer<AssertableApplicationContext> consumer) {
|
||||||
|
invokeProjectGeneration(description, (contextInitializer) -> {
|
||||||
|
new ApplicationContextRunner(ProjectGenerationContext::new).withInitializer((ctx) -> {
|
||||||
|
ProjectGenerationContext projectGenerationContext = (ProjectGenerationContext) ctx;
|
||||||
|
projectGenerationContext.registerBean(ProjectDescription.class, () -> description);
|
||||||
|
contextInitializer.accept(projectGenerationContext);
|
||||||
|
}).run(consumer);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a project asset using the specified {@link ProjectAssetGenerator}.
|
* Generate a project asset using the specified {@link ProjectAssetGenerator}.
|
||||||
* @param description the description of the project to generate
|
* @param description the description of the project to generate
|
||||||
|
|||||||
@@ -18,18 +18,21 @@ package io.spring.initializr.generator.test.project;
|
|||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||||
|
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
|
||||||
import io.spring.initializr.generator.project.MutableProjectDescription;
|
import io.spring.initializr.generator.project.MutableProjectDescription;
|
||||||
|
import io.spring.initializr.generator.project.ProjectDescription;
|
||||||
import io.spring.initializr.generator.project.contributor.ProjectContributor;
|
import io.spring.initializr.generator.project.contributor.ProjectContributor;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link ProjectAssetTester}.
|
* Tests for {@link ProjectAssetTester}.
|
||||||
@@ -40,30 +43,48 @@ class ProjectAssetTesterTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testerHasNoRegisteredIndentingWriterFactoryByDefault() {
|
void testerHasNoRegisteredIndentingWriterFactoryByDefault() {
|
||||||
Map<String, IndentingWriterFactory> contributors = new ProjectAssetTester().generate(
|
new ProjectAssetTester().configure(new MutableProjectDescription(),
|
||||||
new MutableProjectDescription(), (context) -> context.getBeansOfType(IndentingWriterFactory.class));
|
(context) -> assertThat(context).doesNotHaveBean(IndentingWriterFactory.class));
|
||||||
assertThat(contributors).isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testerWithIndentingWriterFactory() {
|
void testerWithIndentingWriterFactory() {
|
||||||
new ProjectAssetTester().withIndentingWriterFactory().generate(new MutableProjectDescription(),
|
new ProjectAssetTester().withIndentingWriterFactory().configure(new MutableProjectDescription(),
|
||||||
(context) -> assertThat(context.getBeansOfType(IndentingWriterFactory.class)).hasSize(1));
|
(context) -> assertThat(context).hasSingleBean(IndentingWriterFactory.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testerWithExplicitProjectContributors(@TempDir Path directory) {
|
void testerWithExplicitProjectContributors(@TempDir Path directory) {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setName("test.text");
|
||||||
ProjectStructure project = new ProjectAssetTester().withDirectory(directory)
|
ProjectStructure project = new ProjectAssetTester().withDirectory(directory)
|
||||||
.withConfiguration(ContributorsConfiguration.class).generate(new MutableProjectDescription());
|
.withConfiguration(ContributorsConfiguration.class).generate(description);
|
||||||
assertThat(project).filePaths().containsOnly("test.text", "test2.text");
|
assertThat(project).filePaths().containsOnly("test.text", "test2.text");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testerWithContextFailureIsProperlyReported() {
|
||||||
|
new ProjectAssetTester().withConfiguration(ContributorFailureConfiguration.class)
|
||||||
|
.configure(new MutableProjectDescription(), (context) -> {
|
||||||
|
assertThat(context).hasFailed();
|
||||||
|
assertThat(context.getStartupFailure()).isInstanceOf(UnsatisfiedDependencyException.class);
|
||||||
|
assertThat(context.getStartupFailure().getMessage()).doesNotContain("Should not be invoked");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testerWithContextSuccessFailToAssertFailure() {
|
||||||
|
assertThatExceptionOfType(AssertionError.class)
|
||||||
|
.isThrownBy(() -> new ProjectAssetTester().withConfiguration(ContributorsConfiguration.class)
|
||||||
|
.configure(new MutableProjectDescription(), (context) -> assertThat(context).hasFailed()));
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class ContributorsConfiguration {
|
static class ContributorsConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
ProjectContributor contributor1() {
|
ProjectContributor contributor1(ProjectDescription description) {
|
||||||
return (projectDirectory) -> Files.createFile(projectDirectory.resolve("test.text"));
|
return (projectDirectory) -> Files.createFile(projectDirectory.resolve(description.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -73,4 +94,14 @@ class ProjectAssetTesterTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class ContributorFailureConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ProjectContributor failContributor(MustacheTemplateRenderer templateRenderer) {
|
||||||
|
throw new IllegalStateException("Should not be invoked");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user